diff options
Diffstat (limited to 'comm/mailnews/addrbook/test/unit/test_search.js')
-rw-r--r-- | comm/mailnews/addrbook/test/unit/test_search.js | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/comm/mailnews/addrbook/test/unit/test_search.js b/comm/mailnews/addrbook/test/unit/test_search.js new file mode 100644 index 0000000000..c25ba17b96 --- /dev/null +++ b/comm/mailnews/addrbook/test/unit/test_search.js @@ -0,0 +1,65 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +const { getModelQuery, generateQueryURI } = ChromeUtils.import( + "resource:///modules/ABQueryUtils.jsm" +); + +const jsonFile = do_get_file("data/ldap_contacts.json"); + +add_task(async () => { + let contacts = await IOUtils.readJSON(jsonFile.path); + + let dirPrefId = MailServices.ab.newAddressBook( + "new book", + "", + Ci.nsIAbManager.JS_DIRECTORY_TYPE + ); + let book = MailServices.ab.getDirectoryFromId(dirPrefId); + + for (let [name, { attributes }] of Object.entries(contacts)) { + let card = Cc["@mozilla.org/addressbook/cardproperty;1"].createInstance( + Ci.nsIAbCard + ); + card.displayName = attributes.cn; + card.firstName = attributes.givenName; + card.lastName = attributes.sn; + card.primaryEmail = attributes.mail; + contacts[name] = book.addCard(card); + } + + let doSearch = async function (searchString, ...expectedContacts) { + let foundCards = await new Promise(resolve => { + let listener = { + cards: [], + onSearchFoundCard(card) { + this.cards.push(card); + }, + onSearchFinished(status, complete, secInfo, location) { + resolve(this.cards); + }, + }; + book.search(searchString, "", listener); + }); + + Assert.equal(foundCards.length, expectedContacts.length); + for (let name of expectedContacts) { + Assert.ok(foundCards.find(c => c.equals(contacts[name]))); + } + }; + + await doSearch("(DisplayName,c,watson)", "john", "mary"); + + let modelQuery = getModelQuery("mail.addr_book.autocompletequery.format"); + await doSearch( + generateQueryURI(modelQuery, ["holmes"]), + "eurus", + "mycroft", + "sherlock" + ); + await doSearch(generateQueryURI(modelQuery, ["adler"]), "irene"); + await doSearch(generateQueryURI(modelQuery, ["redbeard"])); +}); |