summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/test/unit/test_search.js
blob: c25ba17b968c0c2e81dcf6d7bb5813e2138fa543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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"]));
});