summaryrefslogtreecommitdiffstats
path: root/dom/base/test/browser_state_notifications.js
blob: 3f3b4271acd0eb1c08b3f8af0e1313402304fb90 (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
/* globals Components: true, Promise: true, gBrowser: true, Test: true,
           info: true, is: true, window: true, waitForExplicitFinish: true,
           finish: true, ok: true*/

"use strict";

const { addObserver, removeObserver } = Cc[
  "@mozilla.org/observer-service;1"
].getService(Ci.nsIObserverService);
const { openWindow } = Cc["@mozilla.org/embedcomp/window-watcher;1"].getService(
  Ci.nsIWindowWatcher
);

const Test = routine => () => {
  waitForExplicitFinish();
  routine().then(finish, error => {
    ok(false, error);
    finish();
  });
};

// Returns promise for the observer notification subject for
// the given topic. If `receive("foo")` is called `n` times
// nth promise is resolved on an `nth` "foo" notification.
const receive = (topic, p, syncCallback) => {
  return new Promise((resolve, reject) => {
    const { queue } = receive;
    const timeout = () => {
      queue.splice(queue.indexOf(resolve) - 1, 2);
      reject(new Error("Timeout"));
    };

    const observer = {
      observe: subject => {
        // Browser loads bunch of other documents that we don't care
        // about so we let allow filtering notifications via `p` function.
        if (p && !p(subject)) {
          return;
        }
        // If observer is a first one with a given `topic`
        // in a queue resolve promise and take it off the queue
        // otherwise keep waiting.
        const index = queue.indexOf(topic);
        if (queue.indexOf(resolve) === index + 1) {
          removeObserver(observer, topic);
          clearTimeout(id, reject);
          queue.splice(index, 2);
          // Some tests need to be executed synchronously when the event is fired.
          if (syncCallback) {
            syncCallback(subject);
          }
          resolve(subject);
        }
      },
    };
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    const id = setTimeout(timeout, 90000);
    addObserver(observer, topic, false);
    queue.push(topic, resolve);
  });
};
receive.queue = [];

const openTab = uri =>
  (gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, uri));

// eslint-disable-next-line mozilla/no-arbitrary-setTimeout
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

const isData = document => document.URL.startsWith("data:");

const uri1 = "data:text/html;charset=utf-8,<h1>1</h1>";
// For whatever reason going back on load event doesn't work so timeout it is :(
const uri2 =
  "data:text/html;charset=utf-8,<h1>2</h1><script>setTimeout(SpecialPowers.wrap(window).back,100)</script>";
const uri3 = "data:text/html;charset=utf-8,<h1>3</h1>";

const uri4 = "chrome://browser/content/license.html";

const test = Test(async function () {
  let documentInteractive = receive(
    "content-document-interactive",
    isData,
    d => {
      // This test is executed synchronously when the event is received.
      is(d.readyState, "interactive", "document is interactive");
      is(d.URL, uri1, "document.URL matches tab url");
    }
  );
  let documentLoaded = receive("content-document-loaded", isData);
  let pageShown = receive("content-page-shown", isData);

  info("open: uri#1");
  const tab1 = openTab(uri1);
  const browser1 = gBrowser.getBrowserForTab(tab1);

  let interactiveDocument1 = await documentInteractive;

  let loadedDocument1 = await documentLoaded;
  is(loadedDocument1.readyState, "complete", "document is loaded");
  is(interactiveDocument1, loadedDocument1, "interactive document is loaded");

  let shownPage = await pageShown;
  is(interactiveDocument1, shownPage, "loaded document is shown");

  // Wait until history entry is created before loading new uri.
  await receive("sessionstore-state-write-complete");

  info("load uri#2");

  documentInteractive = receive("content-document-interactive", isData, d => {
    // This test is executed synchronously when the event is received.
    is(d.readyState, "interactive", "document is interactive");
    is(d.URL, uri2, "document.URL matches URL loaded");
  });
  documentLoaded = receive("content-document-loaded", isData);
  pageShown = receive("content-page-shown", isData);
  let pageHidden = receive("content-page-hidden", isData);

  BrowserTestUtils.startLoadingURIString(browser1, uri2);

  let hiddenPage = await pageHidden;
  is(interactiveDocument1, hiddenPage, "loaded document is hidden");

  let interactiveDocument2 = await documentInteractive;

  let loadedDocument2 = await documentLoaded;
  is(loadedDocument2.readyState, "complete", "document is loaded");
  is(interactiveDocument2, loadedDocument2, "interactive document is loaded");

  shownPage = await pageShown;
  is(interactiveDocument2, shownPage, "loaded document is shown");

  info("go back to uri#1");

  documentInteractive = receive("content-document-interactive", isData, d => {
    // This test is executed synchronously when the event is received.
    is(d.readyState, "interactive", "document is interactive");
    is(d.URL, uri3, "document.URL matches URL loaded");
  });
  documentLoaded = receive("content-document-loaded", isData);
  pageShown = receive("content-page-shown", isData);
  pageHidden = receive("content-page-hidden", isData);

  hiddenPage = await pageHidden;
  is(interactiveDocument2, hiddenPage, "new document is hidden");

  shownPage = await pageShown;
  is(interactiveDocument1, shownPage, "previous document is shown");

  info("load uri#3");

  BrowserTestUtils.startLoadingURIString(browser1, uri3);

  pageShown = receive("content-page-shown", isData);

  let interactiveDocument3 = await documentInteractive;

  let loadedDocument3 = await documentLoaded;
  is(loadedDocument3.readyState, "complete", "document is loaded");
  is(interactiveDocument3, loadedDocument3, "interactive document is loaded");

  shownPage = await pageShown;
  is(interactiveDocument3, shownPage, "previous document is shown");

  gBrowser.removeTab(tab1);

  info("load chrome uri");

  const tab2 = openTab(uri4);
  documentInteractive = receive("chrome-document-interactive", null, d => {
    // This test is executed synchronously when the event is received.
    is(d.readyState, "interactive", "document is interactive");
    is(d.URL, uri4, "document.URL matches URL loaded");
  });
  documentLoaded = receive("chrome-document-loaded");
  pageShown = receive("chrome-page-shown");

  const interactiveDocument4 = await documentInteractive;

  let loadedDocument4 = await documentLoaded;
  is(loadedDocument4.readyState, "complete", "document is loaded");
  is(interactiveDocument4, loadedDocument4, "interactive document is loaded");

  shownPage = await pageShown;
  is(interactiveDocument4, shownPage, "loaded chrome document is shown");

  pageHidden = receive("chrome-page-hidden");
  gBrowser.removeTab(tab2);

  hiddenPage = await pageHidden;
  is(interactiveDocument4, hiddenPage, "chrome document hidden");
});