summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/addrbook/test/browser/browser_ldap_search.js
blob: 6eb7322bb4d9a8dbd9381feb539051a9a87f5a64 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* 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/. */

const { LDAPServer } = ChromeUtils.import(
  "resource://testing-common/LDAPServer.jsm"
);

const jsonFile =
  "http://mochi.test:8888/browser/comm/mail/components/addrbook/test/browser/ldap_contacts.json";

add_task(async () => {
  function waitForCountChange(expectedCount) {
    return new Promise(resolve => {
      cardsList.addEventListener("rowcountchange", function onRowCountChange() {
        console.log(cardsList.view.rowCount, expectedCount);
        if (cardsList.view.rowCount == expectedCount) {
          cardsList.removeEventListener("rowcountchange", onRowCountChange);
          resolve();
        }
      });
    });
  }

  // Set up some local people.

  let cardsToRemove = [];
  for (let name of ["daniel", "jonathan", "nathan"]) {
    let card = Cc["@mozilla.org/addressbook/cardproperty;1"].createInstance(
      Ci.nsIAbCard
    );
    card.displayName = name;

    card = personalBook.addCard(card);
    cardsToRemove.push(card);
  }

  // Set up the LDAP server.

  LDAPServer.open();
  let response = await fetch(jsonFile);
  let ldapContacts = await response.json();

  let bookPref = MailServices.ab.newAddressBook(
    "Mochitest",
    `ldap://localhost:${LDAPServer.port}/`,
    0
  );
  let book = MailServices.ab.getDirectoryFromId(bookPref);

  let abWindow = await openAddressBookWindow();
  let abDocument = abWindow.document;

  let searchBox = abDocument.getElementById("searchInput");
  let cardsList = abWindow.cardsPane.cardsList;
  let noSearchResults = abDocument.getElementById("placeholderNoSearchResults");
  let detailsPane = abDocument.getElementById("detailsPane");

  // Search for some people in the LDAP directory.

  openDirectory(book);
  checkPlaceholders(["placeholderSearchOnly"]);

  EventUtils.synthesizeMouseAtCenter(searchBox, {}, abWindow);
  EventUtils.sendString("holmes", abWindow);

  await LDAPServer.read(LDAPServer.BindRequest);
  LDAPServer.writeBindResponse();
  checkNamesListed();
  checkPlaceholders(["placeholderSearching"]);

  await LDAPServer.read(LDAPServer.SearchRequest);
  LDAPServer.writeSearchResultEntry(ldapContacts.mycroft);
  LDAPServer.writeSearchResultEntry(ldapContacts.sherlock);
  LDAPServer.writeSearchResultDone();

  Assert.ok(BrowserTestUtils.is_hidden(detailsPane));
  await waitForCountChange(2);
  checkNamesListed("Mycroft Holmes", "Sherlock Holmes");
  checkPlaceholders();

  // Check that displaying an LDAP card works without error.
  EventUtils.synthesizeMouseAtCenter(cardsList.getRowAtIndex(0), {}, abWindow);
  await TestUtils.waitForCondition(() =>
    BrowserTestUtils.is_visible(detailsPane)
  );

  EventUtils.synthesizeMouseAtCenter(searchBox, {}, abWindow);
  EventUtils.synthesizeKey("a", { accelKey: true }, abWindow);
  EventUtils.sendString("john", abWindow);

  await LDAPServer.read(LDAPServer.BindRequest);
  LDAPServer.writeBindResponse();
  checkNamesListed();
  checkPlaceholders(["placeholderSearching"]);

  await LDAPServer.read(LDAPServer.SearchRequest);
  LDAPServer.writeSearchResultEntry(ldapContacts.john);
  LDAPServer.writeSearchResultDone();

  await waitForCountChange(1);
  checkNamesListed("John Watson");
  checkPlaceholders();

  // Now move back to the "All Address Books" view and search again.
  // The search string is retained when switching books.

  openAllAddressBooks();
  checkNamesListed();
  Assert.equal(searchBox.value, "john");

  await LDAPServer.read(LDAPServer.BindRequest);
  LDAPServer.writeBindResponse();
  checkNamesListed();
  checkPlaceholders(["placeholderSearching"]);

  await LDAPServer.read(LDAPServer.SearchRequest);
  LDAPServer.writeSearchResultEntry(ldapContacts.john);
  LDAPServer.writeSearchResultDone();

  await waitForCountChange(1);
  checkNamesListed("John Watson");
  checkPlaceholders();

  EventUtils.synthesizeMouseAtCenter(searchBox, {}, abWindow);
  EventUtils.synthesizeKey("a", { accelKey: true }, abWindow);
  EventUtils.sendString("irene", abWindow);

  await LDAPServer.read(LDAPServer.BindRequest);
  LDAPServer.writeBindResponse();
  checkNamesListed();
  checkPlaceholders(["placeholderSearching"]);

  await LDAPServer.read(LDAPServer.SearchRequest);
  LDAPServer.writeSearchResultEntry(ldapContacts.irene);
  LDAPServer.writeSearchResultDone();

  await waitForCountChange(1);
  checkNamesListed("Irene Adler");
  checkPlaceholders();

  EventUtils.synthesizeMouseAtCenter(searchBox, {}, abWindow);
  EventUtils.synthesizeKey("a", { accelKey: true }, abWindow);
  EventUtils.sendString("jo", abWindow);

  await LDAPServer.read(LDAPServer.BindRequest);
  LDAPServer.writeBindResponse();
  checkNamesListed("jonathan");
  checkPlaceholders();

  await LDAPServer.read(LDAPServer.SearchRequest);
  LDAPServer.writeSearchResultEntry(ldapContacts.john);
  LDAPServer.writeSearchResultDone();

  await waitForCountChange(2);
  checkNamesListed("John Watson", "jonathan");
  checkPlaceholders();

  EventUtils.synthesizeMouseAtCenter(searchBox, {}, abWindow);
  EventUtils.synthesizeKey("a", { accelKey: true }, abWindow);
  EventUtils.sendString("mark", abWindow);

  await LDAPServer.read(LDAPServer.BindRequest);
  LDAPServer.writeBindResponse();
  checkNamesListed();
  checkPlaceholders(["placeholderSearching"]);

  await LDAPServer.read(LDAPServer.SearchRequest);
  LDAPServer.writeSearchResultDone();
  await TestUtils.waitForCondition(() =>
    BrowserTestUtils.is_visible(noSearchResults)
  );
  checkNamesListed();
  checkPlaceholders(["placeholderNoSearchResults"]);

  await closeAddressBookWindow();
  personalBook.deleteCards(cardsToRemove);
  await promiseDirectoryRemoved(book.URI);
  LDAPServer.close();
});