summaryrefslogtreecommitdiffstats
path: root/services/fxaccounts/tests/browser/head.js
blob: e9fb8ad0dcd6194414f18fd418cc025ab362f869 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Waits for the next load to complete in any browser or the given browser.
 * If a <tabbrowser> is given it waits for a load in any of its browsers.
 *
 * @return promise
 */
function waitForDocLoadComplete(aBrowser = gBrowser) {
  return new Promise(resolve => {
    let listener = {
      onStateChange(webProgress, req, flags, status) {
        let docStop =
          Ci.nsIWebProgressListener.STATE_IS_NETWORK |
          Ci.nsIWebProgressListener.STATE_STOP;
        info(
          "Saw state " +
            flags.toString(16) +
            " and status " +
            status.toString(16)
        );

        // When a load needs to be retargetted to a new process it is cancelled
        // with NS_BINDING_ABORTED so ignore that case
        if ((flags & docStop) == docStop && status != Cr.NS_BINDING_ABORTED) {
          aBrowser.removeProgressListener(this);
          waitForDocLoadComplete.listeners.delete(this);

          let chan = req.QueryInterface(Ci.nsIChannel);
          info("Browser loaded " + chan.originalURI.spec);
          resolve();
        }
      },
      QueryInterface: ChromeUtils.generateQI([
        "nsIWebProgressListener",
        "nsISupportsWeakReference",
      ]),
    };
    aBrowser.addProgressListener(listener);
    waitForDocLoadComplete.listeners.add(listener);
    info("Waiting for browser load");
  });
}

function setupMockAlertsService() {
  const alertsService = {
    showAlertNotification: (
      image,
      title,
      text,
      clickable,
      cookie,
      clickCallback
    ) => {
      // We are invoking the event handler ourselves directly.
      clickCallback(null, "alertclickcallback", null);
    },
  };
  const gBrowserGlue = Cc["@mozilla.org/browser/browserglue;1"].getService(
    Ci.nsIObserver
  );
  gBrowserGlue.observe(
    { wrappedJSObject: alertsService },
    "browser-glue-test",
    "mock-alerts-service"
  );
}

// Keep a set of progress listeners for waitForDocLoadComplete() to make sure
// they're not GC'ed before we saw the page load.
waitForDocLoadComplete.listeners = new Set();
registerCleanupFunction(() => waitForDocLoadComplete.listeners.clear());