summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_alltabslistener.js
blob: 0c9677306d1f0aa3de56973336f5c17749950674 (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
const gCompleteState =
  Ci.nsIWebProgressListener.STATE_STOP +
  Ci.nsIWebProgressListener.STATE_IS_NETWORK;

function getOriginalURL(request) {
  return request && request.QueryInterface(Ci.nsIChannel).originalURI.spec;
}

var gFrontProgressListener = {
  onProgressChange(
    aWebProgress,
    aRequest,
    aCurSelfProgress,
    aMaxSelfProgress,
    aCurTotalProgress,
    aMaxTotalProgress
  ) {},

  onStateChange(aWebProgress, aRequest, aStateFlags, aStatus) {
    var url = getOriginalURL(aRequest);
    if (url == "about:blank") {
      return;
    }
    var state = "onStateChange";
    info(
      "FrontProgress (" + url + "): " + state + " 0x" + aStateFlags.toString(16)
    );
    assertCorrectBrowserAndEventOrderForFront(state);
  },

  onLocationChange(aWebProgress, aRequest, aLocationURI, aFlags) {
    var url = getOriginalURL(aRequest);
    if (url == "about:blank") {
      return;
    }
    var state = "onLocationChange";
    info("FrontProgress: " + state + " " + aLocationURI.spec);
    assertCorrectBrowserAndEventOrderForFront(state);
  },

  onSecurityChange(aWebProgress, aRequest, aState) {
    var url = getOriginalURL(aRequest);
    if (url == "about:blank") {
      return;
    }
    var state = "onSecurityChange";
    info("FrontProgress (" + url + "): " + state + " 0x" + aState.toString(16));
    assertCorrectBrowserAndEventOrderForFront(state);
  },
};

function assertCorrectBrowserAndEventOrderForFront(aEventName) {
  Assert.less(
    gFrontNotificationsPos,
    gFrontNotifications.length,
    "Got an expected notification for the front notifications listener"
  );
  is(
    aEventName,
    gFrontNotifications[gFrontNotificationsPos],
    "Got a notification for the front notifications listener"
  );
  gFrontNotificationsPos++;
}

var gAllProgressListener = {
  onStateChange(aBrowser, aWebProgress, aRequest, aStateFlags, aStatus) {
    var url = getOriginalURL(aRequest);
    if (url == "about:blank") {
      // ignore initial about blank
      return;
    }
    var state = "onStateChange";
    info(
      "AllProgress (" + url + "): " + state + " 0x" + aStateFlags.toString(16)
    );
    assertCorrectBrowserAndEventOrderForAll(state, aBrowser);
    assertReceivedFlags(
      state,
      gAllNotifications[gAllNotificationsPos],
      aStateFlags
    );
    gAllNotificationsPos++;

    if ((aStateFlags & gCompleteState) == gCompleteState) {
      is(
        gAllNotificationsPos,
        gAllNotifications.length,
        "Saw the expected number of notifications"
      );
      is(
        gFrontNotificationsPos,
        gFrontNotifications.length,
        "Saw the expected number of frontnotifications"
      );
      executeSoon(gNextTest);
    }
  },

  onLocationChange(aBrowser, aWebProgress, aRequest, aLocationURI, aFlags) {
    var url = getOriginalURL(aRequest);
    if (url == "about:blank") {
      // ignore initial about blank
      return;
    }
    var state = "onLocationChange";
    info("AllProgress: " + state + " " + aLocationURI.spec);
    assertCorrectBrowserAndEventOrderForAll(state, aBrowser);
    assertReceivedFlags(
      "onLocationChange",
      gAllNotifications[gAllNotificationsPos],
      aFlags
    );
    gAllNotificationsPos++;
  },

  onSecurityChange(aBrowser, aWebProgress, aRequest, aState) {
    var url = getOriginalURL(aRequest);
    if (url == "about:blank") {
      // ignore initial about blank
      return;
    }
    var state = "onSecurityChange";
    info("AllProgress (" + url + "): " + state + " 0x" + aState.toString(16));
    assertCorrectBrowserAndEventOrderForAll(state, aBrowser);
    is(
      state,
      gAllNotifications[gAllNotificationsPos],
      "Got a notification for the all notifications listener"
    );
    gAllNotificationsPos++;
  },
};

function assertCorrectBrowserAndEventOrderForAll(aState, aBrowser) {
  ok(
    aBrowser == gTestBrowser,
    aState + " notification came from the correct browser"
  );
  Assert.less(
    gAllNotificationsPos,
    gAllNotifications.length,
    "Got an expected notification for the all notifications listener"
  );
}

function assertReceivedFlags(aState, aObjOrEvent, aFlags) {
  if (aObjOrEvent !== null && typeof aObjOrEvent === "object") {
    is(
      aState,
      aObjOrEvent.state,
      "Got a notification for the all notifications listener"
    );
    is(aFlags, aFlags & aObjOrEvent.flags, `Got correct flags for ${aState}`);
  } else {
    is(
      aState,
      aObjOrEvent,
      "Got a notification for the all notifications listener"
    );
  }
}

var gFrontNotifications,
  gAllNotifications,
  gFrontNotificationsPos,
  gAllNotificationsPos;
var gBackgroundTab,
  gForegroundTab,
  gBackgroundBrowser,
  gForegroundBrowser,
  gTestBrowser;
var gTestPage =
  "/browser/browser/base/content/test/general/alltabslistener.html";
const kBasePage =
  "http://mochi.test:8888/browser/browser/base/content/test/general/dummy_page.html";
var gNextTest;

async function test() {
  waitForExplicitFinish();

  gBackgroundTab = BrowserTestUtils.addTab(gBrowser);
  gForegroundTab = BrowserTestUtils.addTab(gBrowser);
  gBackgroundBrowser = gBrowser.getBrowserForTab(gBackgroundTab);
  gForegroundBrowser = gBrowser.getBrowserForTab(gForegroundTab);
  gBrowser.selectedTab = gForegroundTab;

  gAllNotifications = [
    "onStateChange",
    "onLocationChange",
    "onSecurityChange",
    "onStateChange",
  ];

  // We must wait until a page has completed loading before
  // starting tests or we get notifications from that
  let promises = [
    BrowserTestUtils.browserStopped(gBackgroundBrowser, kBasePage),
    BrowserTestUtils.browserStopped(gForegroundBrowser, kBasePage),
  ];
  BrowserTestUtils.loadURIString(gBackgroundBrowser, kBasePage);
  BrowserTestUtils.loadURIString(gForegroundBrowser, kBasePage);
  await Promise.all(promises);
  // If we process switched, the tabbrowser may still be processing the state_stop
  // notification here because of how microtasks work. Ensure that that has
  // happened before starting to test (which would add listeners to the tabbrowser
  // which would get confused by being called about kBasePage loading).
  await new Promise(executeSoon);
  startTest1();
}

function runTest(browser, url, next) {
  gFrontNotificationsPos = 0;
  gAllNotificationsPos = 0;
  gNextTest = next;
  gTestBrowser = browser;
  BrowserTestUtils.loadURIString(browser, url);
}

function startTest1() {
  info("\nTest 1");
  gBrowser.addProgressListener(gFrontProgressListener);
  gBrowser.addTabsProgressListener(gAllProgressListener);

  gFrontNotifications = gAllNotifications;
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  runTest(gForegroundBrowser, "http://example.org" + gTestPage, startTest2);
}

function startTest2() {
  info("\nTest 2");
  gFrontNotifications = gAllNotifications;
  runTest(gForegroundBrowser, "https://example.com" + gTestPage, startTest3);
}

function startTest3() {
  info("\nTest 3");
  gFrontNotifications = [];
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  runTest(gBackgroundBrowser, "http://example.org" + gTestPage, startTest4);
}

function startTest4() {
  info("\nTest 4");
  gFrontNotifications = [];
  runTest(gBackgroundBrowser, "https://example.com" + gTestPage, startTest5);
}

function startTest5() {
  info("\nTest 5");
  // Switch the foreground browser
  [gForegroundBrowser, gBackgroundBrowser] = [
    gBackgroundBrowser,
    gForegroundBrowser,
  ];
  [gForegroundTab, gBackgroundTab] = [gBackgroundTab, gForegroundTab];
  // Avoid the onLocationChange this will fire
  gBrowser.removeProgressListener(gFrontProgressListener);
  gBrowser.selectedTab = gForegroundTab;
  gBrowser.addProgressListener(gFrontProgressListener);

  gFrontNotifications = gAllNotifications;
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  runTest(gForegroundBrowser, "http://example.org" + gTestPage, startTest6);
}

function startTest6() {
  info("\nTest 6");
  gFrontNotifications = [];
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  runTest(gBackgroundBrowser, "http://example.org" + gTestPage, startTest7);
}

// Navigate from remote to non-remote
function startTest7() {
  info("\nTest 7");
  gFrontNotifications = [];
  gAllNotifications = [
    "onStateChange",
    "onLocationChange",
    "onSecurityChange",
    {
      state: "onLocationChange",
      flags: Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT,
    }, // dummy onLocationChange event
    "onStateChange",
  ];
  runTest(gBackgroundBrowser, "about:preferences", startTest8);
}

// Navigate from non-remote to non-remote
function startTest8() {
  info("\nTest 8");
  gFrontNotifications = [];
  gAllNotifications = [
    "onStateChange",
    {
      state: "onStateChange",
      flags:
        Ci.nsIWebProgressListener.STATE_IS_REDIRECTED_DOCUMENT |
        Ci.nsIWebProgressListener.STATE_IS_REQUEST |
        Ci.nsIWebProgressListener.STATE_START,
    },
    "onLocationChange",
    "onSecurityChange",
    "onStateChange",
  ];
  runTest(gBackgroundBrowser, "about:config", startTest9);
}

// Navigate from non-remote to remote
function startTest9() {
  info("\nTest 9");
  gFrontNotifications = [];
  gAllNotifications = [
    "onStateChange",
    "onLocationChange",
    "onSecurityChange",
    "onStateChange",
  ];
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  runTest(gBackgroundBrowser, "http://example.org" + gTestPage, finishTest);
}

function finishTest() {
  gBrowser.removeProgressListener(gFrontProgressListener);
  gBrowser.removeTabsProgressListener(gAllProgressListener);
  gBrowser.removeTab(gBackgroundTab);
  gBrowser.removeTab(gForegroundTab);
  finish();
}