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

"use strict";

var { interfaces: Ci, classes: Cc, utils: Cu } = Components;
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();
  Task.spawn(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) => {
  const { promise, resolve, reject } = Promise.defer();
  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);
      }
    }
  };
  const id = setTimeout(timeout, 90000);
  addObserver(observer, topic, false);
  queue.push(topic, resolve);

  return promise;
};
receive.queue = [];

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

const sleep = ms => {
  const { promise, resolve } = Promise.defer();
  setTimeout(resolve, ms);
  return promise;
};

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(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 = yield documentInteractive;

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

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

  // Wait until history entry is created before loading new uri.
  yield 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);

  browser1.loadURI(uri2);

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

  let interactiveDocument2 = yield documentInteractive;

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

  shownPage = yield 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 = yield pageHidden;
  is(interactiveDocument2, hiddenPage, "new document is hidden");

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

  info("load uri#3");

  browser1.loadURI(uri3);

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

  let interactiveDocument3 = yield documentInteractive;

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

  shownPage = yield 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 = yield documentInteractive;

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

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

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

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