summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/extensions/test/xpcshell/test_ext_addressBook_provider.js
blob: a09540dcbe16459ea528dd06b4392f2f6fe85579 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* 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/. */

var { ExtensionTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/ExtensionXPCShellUtils.sys.mjs"
);

add_task(async function () {
  let extension = ExtensionTestUtils.loadExtension({
    background: async () => {
      let id = "9b9074ff-8fa4-4c58-9c3b-bc9ea2e17db1";
      let dummy = async (node, searchString, query) => {
        await browser.test.assertTrue(
          false,
          "Should have removed this address book"
        );
      };
      await browser.addressBooks.provider.onSearchRequest.addListener(dummy, {
        addressBookName: "dummy",
        isSecure: false,
        id,
      });
      await browser.addressBooks.provider.onSearchRequest.removeListener(dummy);
      id = "00e1d9af-a846-4ef5-a6ac-15e8926bf6d3";
      await browser.addressBooks.provider.onSearchRequest.addListener(
        async (node, searchString, query) => {
          await browser.test.assertEq(
            id,
            node.id,
            "Addressbook should have the id we requested"
          );
          return {
            results: [
              {
                DisplayName: searchString,
                PrimaryEmail: searchString + "@example.com",
              },
            ],
            isCompleteResult: true,
          };
        },
        {
          addressBookName: "xpcshell",
          isSecure: false,
          id,
        }
      );
      await browser.addressBooks.provider.onSearchRequest.addListener(
        async (node, searchString, query) => {
          await browser.test.assertTrue(
            false,
            "Should not have created a duplicate address book"
          );
        },
        {
          addressBookName: "xpcshell",
          isSecure: false,
          id,
        }
      );
    },
    manifest: { permissions: ["addressBooks"] },
  });

  await extension.startup();

  const dummyUID = "9b9074ff-8fa4-4c58-9c3b-bc9ea2e17db1";
  let searchBook = MailServices.ab.getDirectoryFromUID(dummyUID);
  ok(searchBook == null, "Dummy directory was removed by extension");

  const UID = "00e1d9af-a846-4ef5-a6ac-15e8926bf6d3";
  searchBook = MailServices.ab.getDirectoryFromUID(UID);
  ok(searchBook != null, "Extension registered an async directory");

  let foundCards = 0;
  await new Promise(resolve => {
    searchBook.search(null, "test", {
      onSearchFoundCard(card) {
        ok(card != null, "A card was found.");
        equal(card.directoryUID, UID, "The card comes from the directory.");
        equal(
          card.primaryEmail,
          "test@example.com",
          "The card has the correct email address."
        );
        equal(
          card.displayName,
          "test",
          "The card has the correct display name."
        );
        foundCards++;
      },
      onSearchFinished(status, isCompleteResult) {
        ok(Components.isSuccessCode(status), "Search finished successfully.");
        equal(foundCards, 1, "One card was found.");
        ok(isCompleteResult, "A full result set was received.");
        resolve();
      },
    });
  });

  let autoCompleteSearch = Cc[
    "@mozilla.org/autocomplete/search;1?name=addrbook"
  ].createInstance(Ci.nsIAutoCompleteSearch);
  await new Promise(resolve => {
    autoCompleteSearch.startSearch("test", null, null, {
      onSearchResult(aSearch, aResult) {
        equal(aSearch, autoCompleteSearch, "This is our search.");
        if (aResult.searchResult == Ci.nsIAutoCompleteResult.RESULT_SUCCESS) {
          equal(aResult.matchCount, 1, "One match was found.");
          equal(
            aResult.getValueAt(0),
            "test <test@example.com>",
            "The match had the expected value."
          );
          resolve();
        } else {
          equal(
            aResult.searchResult,
            Ci.nsIAutoCompleteResult.RESULT_NOMATCH_ONGOING,
            "We should be waiting for the extension's results."
          );
        }
      },
    });
  });

  await extension.unload();
  searchBook = MailServices.ab.getDirectoryFromUID(UID);
  ok(searchBook == null, "Extension directory removed after unload");
});

registerCleanupFunction(() => {
  // Make sure any open database is given a chance to close.
  Services.startup.advanceShutdownPhase(
    Services.startup.SHUTDOWN_PHASE_APPSHUTDOWNCONFIRMED
  );
});