summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/shared-modules/ContentTabHelpers.jsm
blob: 3d7ba3ee2c567c078dee8145da4b1e80a1757f6c (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* 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 EXPORTED_SYMBOLS = [
  "open_content_tab_with_url",
  "open_content_tab_with_click",
  "plan_for_content_tab_load",
  "wait_for_content_tab_load",
  "assert_content_tab_has_favicon",
  "content_tab_e",
  "get_content_tab_element_display",
  "assert_content_tab_element_hidden",
  "assert_content_tab_element_visible",
  "wait_for_content_tab_element_display",
  "get_element_by_text",
  "assert_content_tab_text_present",
  "assert_content_tab_text_absent",
  "NotificationWatcher",
  "get_notification_bar_for_tab",
  "updateBlocklist",
  "setAndUpdateBlocklist",
  "resetBlocklist",
  "gMockExtProtSvcReg",
  "gMockExtProtSvc",
];

var utils = ChromeUtils.import("resource://testing-common/mozmill/utils.jsm");

var folderDisplayHelper = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);
var { MockObjectReplacer } = ChromeUtils.import(
  "resource://testing-common/mozmill/MockObjectHelpers.jsm"
);
var wh = ChromeUtils.import(
  "resource://testing-common/mozmill/WindowHelpers.jsm"
);

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

var FAST_TIMEOUT = 1000;
var FAST_INTERVAL = 100;
var EXT_PROTOCOL_SVC_CID = "@mozilla.org/uriloader/external-protocol-service;1";

var mc = folderDisplayHelper.mc;

var _originalBlocklistURL = null;

var gMockExtProtSvcReg = new MockObjectReplacer(
  EXT_PROTOCOL_SVC_CID,
  MockExtProtConstructor
);

/**
 * gMockExtProtocolSvc allows us to capture (most if not all) attempts to
 * open links in the default browser.
 */
var gMockExtProtSvc = {
  _loadedURLs: [],
  QueryInterface: ChromeUtils.generateQI(["nsIExternalProtocolService"]),

  externalProtocolHandlerExists(aProtocolScheme) {},

  getApplicationDescription(aScheme) {},

  getProtocolHandlerInfo(aProtocolScheme) {},

  getProtocolHandlerInfoFromOS(aProtocolScheme, aFound) {},

  isExposedProtocol(aProtocolScheme) {},

  loadURI(aURI, aWindowContext) {
    this._loadedURLs.push(aURI.spec);
  },

  setProtocolHandlerDefaults(aHandlerInfo, aOSHandlerExists) {},

  urlLoaded(aURL) {
    return this._loadedURLs.includes(aURL);
  },
};

function MockExtProtConstructor() {
  return gMockExtProtSvc;
}

/* Allows for planning / capture of notification events within
 * content tabs, for example: plugin crash notifications, theme
 * install notifications.
 */
var ALERT_TIMEOUT = 10000;

var NotificationWatcher = {
  planForNotification(aController) {
    this.alerted = false;
    aController.window.document.addEventListener(
      "AlertActive",
      this.alertActive
    );
  },
  waitForNotification(aController) {
    if (!this.alerted) {
      utils.waitFor(
        () => this.alerted,
        "Timeout waiting for alert",
        ALERT_TIMEOUT,
        100
      );
    }
    // Double check the notification box has finished animating.
    let notificationBox = mc.window.document
      .getElementById("tabmail")
      .selectedTab.panel.querySelector("notificationbox");
    if (notificationBox && notificationBox._animating) {
      utils.waitFor(
        () => !notificationBox._animating,
        "Timeout waiting for notification box animation to finish",
        ALERT_TIMEOUT,
        100
      );
    }

    aController.window.document.removeEventListener(
      "AlertActive",
      this.alertActive
    );
  },
  alerted: false,
  alertActive() {
    NotificationWatcher.alerted = true;
  },
};

/**
 * Opens a content tab with the given URL.
 *
 * @param {string} aURL - The URL to load.
 * @param {string} [aLinkHandler=null] - See specialTabs.contentTabType.openTab.
 * @param {boolean} [aBackground=false] Whether the tab is opened in the background.
 *
 * @returns {object} The newly-opened tab.
 */
function open_content_tab_with_url(
  aURL,
  aLinkHandler = null,
  aBackground = false
) {
  let tabmail = mc.window.document.getElementById("tabmail");
  let preCount = tabmail.tabContainer.allTabs.length;
  tabmail.openTab("contentTab", {
    url: aURL,
    background: aBackground,
    linkHandler: aLinkHandler,
  });
  utils.waitFor(
    () => tabmail.tabContainer.allTabs.length == preCount + 1,
    "Timeout waiting for the content tab to open with URL: " + aURL,
    FAST_TIMEOUT,
    FAST_INTERVAL
  );

  // We append new tabs at the end, so check the last one.
  let expectedNewTab = tabmail.tabInfo[preCount];
  folderDisplayHelper.assert_selected_tab(expectedNewTab);
  wait_for_content_tab_load(expectedNewTab, aURL);
  return expectedNewTab;
}

/**
 * Opens a content tab with a click on the given element. The tab is expected to
 * be opened in the foreground. The element is expected to be associated with
 * the given controller.
 *
 * @param aElem         The element to click or a function that causes the tab to open.
 * @param aExpectedURL  The URL that is expected to be opened (string).
 * @param [aController] The controller the element is associated with. Defaults
 *                      to |mc|.
 * @param [aTabType]    Optional tab type to expect (string).
 * @returns The newly-opened tab.
 */
function open_content_tab_with_click(
  aElem,
  aExpectedURL,
  aController,
  aTabType = "contentTab"
) {
  if (aController === undefined) {
    aController = mc;
  }

  let preCount =
    aController.window.document.getElementById("tabmail").tabContainer.allTabs
      .length;
  if (typeof aElem != "function") {
    EventUtils.synthesizeMouseAtCenter(aElem, {}, aElem.ownerGlobal);
  } else {
    aElem();
  }

  utils.waitFor(
    () =>
      aController.window.document.getElementById("tabmail").tabContainer.allTabs
        .length ==
      preCount + 1,
    "Timeout waiting for the content tab to open",
    FAST_TIMEOUT,
    FAST_INTERVAL
  );

  // We append new tabs at the end, so check the last one.
  let expectedNewTab =
    aController.window.document.getElementById("tabmail").tabInfo[preCount];
  folderDisplayHelper.assert_selected_tab(expectedNewTab);
  folderDisplayHelper.assert_tab_mode_name(expectedNewTab, aTabType);
  wait_for_content_tab_load(expectedNewTab, aExpectedURL);
  return expectedNewTab;
}

/**
 * Call this before triggering a page load that you are going to wait for using
 * |wait_for_content_tab_load|. This ensures that if a page is already displayed
 * in the given tab that state is sufficiently cleaned up so it doesn't trick us
 * into thinking that there is no need to wait.
 *
 * @param [aTab] optional tab, defaulting to the current tab.
 */
function plan_for_content_tab_load(aTab) {
  if (aTab === undefined) {
    aTab = mc.window.document.getElementById("tabmail").currentTabInfo;
  }
  aTab.pageLoaded = false;
}

/**
 * Waits for the given content tab to load completely with the given URL. This
 * is expected to be accompanied by a |plan_for_content_tab_load| right before
 * the action triggering the page load takes place.
 *
 * Note that you cannot call |plan_for_content_tab_load| if you're opening a new
 * tab. That is fine, because pageLoaded is initially false.
 *
 * @param [aTab]      Optional tab, defaulting to the current tab.
 * @param aURL        The URL being loaded in the tab.
 * @param [aTimeout]  Optional time to wait for the load.
 */
function wait_for_content_tab_load(aTab, aURL, aTimeout) {
  if (aTab === undefined) {
    aTab = mc.window.document.getElementById("tabmail").currentTabInfo;
  }

  function isLoadedChecker() {
    // Require that the progress listener think that the page is loaded.
    if (!aTab.pageLoaded) {
      return false;
    }
    // Also require that our tab infrastructure thinks that the page is loaded.
    return !aTab.busy;
  }

  utils.waitFor(
    isLoadedChecker,
    "Timeout waiting for the content tab page to load.",
    aTimeout
  );
  // The above may return immediately, meaning the event queue might not get a
  // chance. Give it a chance now.
  utils.sleep(0);
  // Finally, require that the tab's browser thinks that no page is being loaded.
  wh.wait_for_browser_load(aTab.browser, aURL);
}

/**
 * Gets the element with the given ID from the content tab's displayed page.
 */
function content_tab_e(aTab, aId) {
  return aTab.browser.contentDocument.getElementById(aId);
}

/**
 * Assert that the given content tab has the given URL loaded as a favicon.
 */
function assert_content_tab_has_favicon(aTab, aURL) {
  Assert.equal(aTab.favIconUrl, aURL, "Checking tab favicon");
}

/**
 * Returns the current "display" style property of an element.
 */
function get_content_tab_element_display(aTab, aElem) {
  let style = aTab.browser.contentWindow.getComputedStyle(aElem);
  return style.getPropertyValue("display");
}

/**
 * Asserts that the given element is hidden from view on the page.
 */
function assert_content_tab_element_hidden(aTab, aElem) {
  let display = get_content_tab_element_display(aTab, aElem);
  Assert.equal(display, "none", "Element should be hidden");
}

/**
 * Asserts that the given element is visible on the page.
 */
function assert_content_tab_element_visible(aTab, aElem) {
  let display = get_content_tab_element_display(aTab, aElem);
  Assert.notEqual(display, "none", "Element should be visible");
}

/**
 * Waits for the element's display property indicate it is visible.
 */
function wait_for_content_tab_element_display(aTab, aElem) {
  function isValue() {
    return get_content_tab_element_display(aTab, aElem) != "none";
  }
  try {
    utils.waitFor(isValue);
  } catch (e) {
    if (e instanceof utils.TimeoutError) {
      Assert.report(
        true,
        undefined,
        undefined,
        "Timeout waiting for element to become visible"
      );
    } else {
      throw e;
    }
  }
}

/**
 * Finds element in document fragment, containing only the specified text
 * as its textContent value.
 *
 * @param aRootNode  Root node of the node tree where search should start.
 * @param aText      The string to search.
 */
function get_element_by_text(aRootNode, aText) {
  // Check every node existing.
  let nodes = aRootNode.querySelectorAll("*");
  for (let node of nodes) {
    // We ignore surrounding whitespace.
    if (node.textContent.trim() == aText) {
      return node;
    }
  }

  return null;
}

/**
 * Finds element containing only the specified text in the content tab's page.
 */
function get_content_tab_element_by_text(aTab, aText) {
  let doc = aTab.browser.contentDocument.documentElement;
  return get_element_by_text(doc, aText);
}

/**
 * Asserts that the given text is present on the content tab's page.
 */
function assert_content_tab_text_present(aTab, aText) {
  Assert.ok(
    get_content_tab_element_by_text(aTab, aText),
    `String "${aText}" should be on the content tab's page`
  );
}

/**
 * Asserts that the given text is absent on the content tab's page.
 */
function assert_content_tab_text_absent(aTab, aText) {
  Assert.ok(
    !get_content_tab_element_by_text(aTab, aText),
    `String "${aText}" should not be on the content tab's page`
  );
}

/**
 * Returns the notification bar for a tab if one is currently visible,
 * null if otherwise.
 */
function get_notification_bar_for_tab(aTab) {
  let notificationBoxEls = mc.window.document
    .getElementById("tabmail")
    .selectedTab.panel.querySelector("notificationbox");
  if (!notificationBoxEls) {
    return null;
  }

  return notificationBoxEls;
}

function updateBlocklist(aController, aCallback) {
  let observer = function () {
    Services.obs.removeObserver(observer, "blocklist-updated");
    aController.window.setTimeout(aCallback, 0);
  };
  Services.obs.addObserver(observer, "blocklist-updated");
  Services.blocklist.QueryInterface(Ci.nsITimerCallback).notify(null);
}

function setAndUpdateBlocklist(aController, aURL, aCallback) {
  if (!_originalBlocklistURL) {
    _originalBlocklistURL = Services.prefs.getCharPref(
      "extensions.blocklist.url"
    );
  }
  Services.prefs.setCharPref("extensions.blocklist.url", aURL);
  updateBlocklist(aController, aCallback);
}

function resetBlocklist(aController, aCallback) {
  Services.prefs.setCharPref("extensions.blocklist.url", _originalBlocklistURL);
  updateBlocklist(aController, aCallback);
}