summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_ext_webnavigation.html
blob: 12c90f8350df28e0268ce8b6568ba5a7886dfa83 (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for simple WebExtension</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

if (AppConstants.platform === "android") {
  SimpleTest.requestLongerTimeout(3);
}

/* globals sendMouseEvent */

function backgroundScript() {
  const BASE = "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest";
  const URL = BASE + "/file_WebNavigation_page1.html";

  const EVENTS = [
    "onTabReplaced",
    "onBeforeNavigate",
    "onCommitted",
    "onDOMContentLoaded",
    "onCompleted",
    "onErrorOccurred",
    "onReferenceFragmentUpdated",
    "onHistoryStateUpdated",
  ];

  let expectedTabId = -1;

  function gotEvent(event, details) {
    if (!details.url.startsWith(BASE)) {
      return;
    }
    browser.test.log(`Got ${event} ${details.url} ${details.frameId} ${details.parentFrameId}`);

    if (expectedTabId == -1) {
      browser.test.assertTrue(details.tabId !== undefined, "tab ID defined");
      expectedTabId = details.tabId;
    }

    browser.test.assertEq(details.tabId, expectedTabId, "correct tab");

    browser.test.sendMessage("received", {url: details.url, event});

    if (details.url == URL) {
      browser.test.assertEq(0, details.frameId, "root frame ID correct");
      browser.test.assertEq(-1, details.parentFrameId, "root parent frame ID correct");
    } else {
      browser.test.assertEq(0, details.parentFrameId, "parent frame ID correct");
      browser.test.assertTrue(details.frameId != 0, "frame ID probably okay");
    }

    browser.test.assertTrue(details.frameId !== undefined, "frameId != undefined");
    browser.test.assertTrue(details.parentFrameId !== undefined, "parentFrameId != undefined");
  }

  let listeners = {};
  for (let event of EVENTS) {
    listeners[event] = gotEvent.bind(null, event);
    browser.webNavigation[event].addListener(listeners[event]);
  }

  browser.test.sendMessage("ready");
}

const BASE = "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest";
const URL = BASE + "/file_WebNavigation_page1.html";
const FORM_URL = URL + "?";
const FRAME = BASE + "/file_WebNavigation_page2.html";
const FRAME2 = BASE + "/file_WebNavigation_page3.html";
const FRAME_PUSHSTATE = BASE + "/file_WebNavigation_page3_pushState.html";
const REDIRECT = BASE + "/redirection.sjs";
const REDIRECTED = BASE + "/dummy_page.html";
const CLIENT_REDIRECT = BASE + "/file_webNavigation_clientRedirect.html";
const CLIENT_REDIRECT_HTTPHEADER = BASE + "/file_webNavigation_clientRedirect_httpHeaders.html";
const FRAME_CLIENT_REDIRECT = BASE + "/file_webNavigation_frameClientRedirect.html";
const FRAME_REDIRECT = BASE + "/file_webNavigation_frameRedirect.html";
const FRAME_MANUAL = BASE + "/file_webNavigation_manualSubframe.html";
const FRAME_MANUAL_PAGE1 = BASE + "/file_webNavigation_manualSubframe_page1.html";
const FRAME_MANUAL_PAGE2 = BASE + "/file_webNavigation_manualSubframe_page2.html";
const INVALID_PAGE = "https://invalid.localhost/";

const REQUIRED = [
  "onBeforeNavigate",
  "onCommitted",
  "onDOMContentLoaded",
  "onCompleted",
];

var received = [];
var completedResolve;
var waitingURL, waitingEvent;

function loadAndWait(win, event, url, script) {
  received = [];
  waitingEvent = event;
  waitingURL = url;
  dump(`RUN ${script}\n`);
  script();
  return new Promise(resolve => { completedResolve = resolve; });
}

add_task(async function webnav_transitions_props() {
  function backgroundScriptTransitions() {
    const EVENTS = [
      "onCommitted",
      "onHistoryStateUpdated",
      "onReferenceFragmentUpdated",
      "onCompleted",
    ];

    function gotEvent(event, details) {
      browser.test.log(`Got ${event} ${details.url} ${details.transitionType} ${details.transitionQualifiers && JSON.stringify(details.transitionQualifiers)}`);

      browser.test.sendMessage("received", {url: details.url, details, event});
    }

    let listeners = {};
    for (let event of EVENTS) {
      listeners[event] = gotEvent.bind(null, event);
      browser.webNavigation[event].addListener(listeners[event]);
    }

    browser.test.sendMessage("ready");
  }

  let extensionData = {
    manifest: {
      permissions: [
        "webNavigation",
      ],
    },
    background: backgroundScriptTransitions,
  };

  let extension = ExtensionTestUtils.loadExtension(extensionData);

  extension.onMessage("received", ({url, event, details}) => {
    received.push({url, event, details});

    if (event == waitingEvent && url == waitingURL) {
      completedResolve();
    }
  });

  await Promise.all([extension.startup(), extension.awaitMessage("ready")]);
  info("webnavigation extension loaded");

  let win = window.open();

  await loadAndWait(win, "onCompleted", URL, () => { win.location = URL; });

  // transitionType: reload
  received = [];
  await loadAndWait(win, "onCompleted", URL, () => { win.location.reload(); });

  let found = received.find((data) => (data.event == "onCommitted" && data.url == URL));

  ok(found, "Got the onCommitted event");

  if (found) {
    is(found.details.transitionType, "reload",
       "Got the expected 'reload' transitionType in the OnCommitted event");
    ok(Array.isArray(found.details.transitionQualifiers),
       "transitionQualifiers found in the OnCommitted events");
  }

  // transitionType: auto_subframe
  found = received.find((data) => (data.event == "onCommitted" && data.url == FRAME));

  ok(found, "Got the sub-frame onCommitted event");

  if (found) {
    is(found.details.transitionType, "auto_subframe",
       "Got the expected 'auto_subframe' transitionType in the OnCommitted event");
    ok(Array.isArray(found.details.transitionQualifiers),
       "transitionQualifiers found in the OnCommitted events");
  }

  // transitionType: form_submit
  received = [];
  await loadAndWait(win, "onCompleted", FORM_URL, () => {
    win.document.querySelector("form").submit();
  });

  found = received.find((data) => (data.event == "onCommitted" && data.url == FORM_URL));

  ok(found, "Got the onCommitted event");

  if (found) {
    is(found.details.transitionType, "form_submit",
       "Got the expected 'form_submit' transitionType in the OnCommitted event");
    ok(Array.isArray(found.details.transitionQualifiers),
       "transitionQualifiers found in the OnCommitted events");
  }

  // transitionQualifier: server_redirect
  received = [];
  await loadAndWait(win, "onCompleted", REDIRECTED, () => { win.location = REDIRECT; });

  found = received.find((data) => (data.event == "onCommitted" && data.url == REDIRECTED));

  ok(found, "Got the onCommitted event");

  if (found) {
    is(found.details.transitionType, "link",
       "Got the expected 'link' transitionType in the OnCommitted event");
    ok(Array.isArray(found.details.transitionQualifiers) &&
       found.details.transitionQualifiers.find((q) => q == "server_redirect"),
       "Got the expected 'server_redirect' transitionQualifiers in the OnCommitted events");
  }

  // transitionQualifier: forward_back
  received = [];
  await loadAndWait(win, "onCompleted", FORM_URL, () => { win.history.back(); });

  found = received.find((data) => (data.event == "onCommitted" && data.url == FORM_URL));

  ok(found, "Got the onCommitted event");

  if (found) {
    is(found.details.transitionType, "link",
       "Got the expected 'link' transitionType in the OnCommitted event");
    ok(Array.isArray(found.details.transitionQualifiers) &&
       found.details.transitionQualifiers.find((q) => q == "forward_back"),
       "Got the expected 'forward_back' transitionQualifiers in the OnCommitted events");
  }

  // transitionQualifier: client_redirect
  // (from meta http-equiv tag)
  received = [];
  await loadAndWait(win, "onCompleted", REDIRECTED, () => {
    win.location = CLIENT_REDIRECT;
  });

  found = received.find((data) => (data.event == "onCommitted" && data.url == REDIRECTED));

  ok(found, "Got the onCommitted event");

  if (found) {
    is(found.details.transitionType, "link",
       "Got the expected 'link' transitionType in the OnCommitted event");
    ok(Array.isArray(found.details.transitionQualifiers) &&
       found.details.transitionQualifiers.find((q) => q == "client_redirect"),
       "Got the expected 'client_redirect' transitionQualifiers in the OnCommitted events");
  }

  // transitionQualifier: client_redirect
  // (from http headers)
  received = [];
  await loadAndWait(win, "onCompleted", REDIRECTED, () => {
    win.location = CLIENT_REDIRECT_HTTPHEADER;
  });

  found = received.find((data) => (data.event == "onCommitted" && data.url == REDIRECTED));

  ok(found, "Got the onCommitted event");

  if (found) {
    is(found.details.transitionType, "link",
       "Got the expected 'link' transitionType in the OnCommitted event");
    ok(Array.isArray(found.details.transitionQualifiers) &&
       found.details.transitionQualifiers.find((q) => q == "client_redirect"),
       "Got the expected 'client_redirect' transitionQualifiers in the OnCommitted events");
  }

  // transitionQualifier: client_redirect (sub-frame)
  // (from meta http-equiv tag)
  received = [];
  await loadAndWait(win, "onCompleted", REDIRECTED, () => {
    win.location = FRAME_CLIENT_REDIRECT;
  });

  found = received.find((data) => (data.event == "onCommitted" && data.url == REDIRECTED));

  ok(found, "Got the onCommitted event");

  if (found) {
    is(found.details.transitionType, "auto_subframe",
       "Got the expected 'auto_subframe' transitionType in the OnCommitted event");
    ok(Array.isArray(found.details.transitionQualifiers) &&
       found.details.transitionQualifiers.find((q) => q == "client_redirect"),
       "Got the expected 'client_redirect' transitionQualifiers in the OnCommitted events");
  }

  // transitionQualifier: server_redirect (sub-frame)
  received = [];
  await loadAndWait(win, "onCompleted", REDIRECTED, () => { win.location = FRAME_REDIRECT; });

  found = received.find((data) => (data.event == "onCommitted" && data.url == REDIRECT));

  ok(found, "Got the onCommitted event");

  if (found) {
    is(found.details.transitionType, "auto_subframe",
       "Got the expected 'auto_subframe' transitionType in the OnCommitted event");
    // TODO BUG 1264936: currently the server_redirect is not detected in sub-frames
    // once we fix it we can test it here:
    //
    // ok(Array.isArray(found.details.transitionQualifiers) &&
    //    found.details.transitionQualifiers.find((q) => q == "server_redirect"),
    //    "Got the expected 'server_redirect' transitionQualifiers in the OnCommitted events");
  }

  // transitionType: manual_subframe
  received = [];
  await loadAndWait(win, "onCompleted", FRAME_MANUAL, () => { win.location = FRAME_MANUAL; });
  found = received.find((data) => (data.event == "onCommitted" &&
                                   data.url == FRAME_MANUAL_PAGE1));

  ok(found, "Got the onCommitted event");

  if (found) {
    is(found.details.transitionType, "auto_subframe",
       "Got the expected 'auto_subframe' transitionType in the OnCommitted event");
  }

  received = [];
  await loadAndWait(win, "onCompleted", FRAME_MANUAL_PAGE2, () => {
    let el = win.document.querySelector("iframe")
                .contentDocument.querySelector("a");
    sendMouseEvent({type: "click"}, el, win);
  });

  found = received.find((data) => (data.event == "onCommitted" &&
                                   data.url == FRAME_MANUAL_PAGE2));

  ok(found, "Got the onCommitted event");

  if (found) {
    if (AppConstants.MOZ_BUILD_APP === "browser") {
      is(found.details.transitionType, "manual_subframe",
         "Got the expected 'manual_subframe' transitionType in the OnCommitted event");
    } else {
      is(found.details.transitionType, "auto_subframe",
         "Got the expected 'manual_subframe' transitionType in the OnCommitted event");
    }
  }

  // Test transitions properties on onHistoryStateUpdated events.

  received = [];
  await loadAndWait(win, "onCompleted", FRAME2, () => { win.location = FRAME2; });

  received = [];
  await loadAndWait(win, "onHistoryStateUpdated", `${FRAME2}/pushState`, () => {
    win.history.pushState({}, "History PushState", `${FRAME2}/pushState`);
  });

  found = received.find((data) => (data.event == "onHistoryStateUpdated" &&
                                   data.url == `${FRAME2}/pushState`));

  ok(found, "Got the onHistoryStateUpdated event");

  if (found) {
    is(typeof found.details.transitionType, "string",
       "Got transitionType in the onHistoryStateUpdated event");
    ok(Array.isArray(found.details.transitionQualifiers),
       "Got transitionQualifiers in the onHistoryStateUpdated event");
  }

  // Test transitions properties on onReferenceFragmentUpdated events.

  received = [];
  await loadAndWait(win, "onReferenceFragmentUpdated", `${FRAME2}/pushState#ref2`, () => {
    win.history.pushState({}, "ReferenceFragment Update", `${FRAME2}/pushState#ref2`);
  });

  found = received.find((data) => (data.event == "onReferenceFragmentUpdated" &&
                                   data.url == `${FRAME2}/pushState#ref2`));

  ok(found, "Got the onReferenceFragmentUpdated event");

  if (found) {
    is(typeof found.details.transitionType, "string",
       "Got transitionType in the onReferenceFragmentUpdated event");
    ok(Array.isArray(found.details.transitionQualifiers),
       "Got transitionQualifiers in the onReferenceFragmentUpdated event");
  }

  // cleanup phase
  win.close();

  await extension.unload();
  info("webnavigation extension unloaded");
});

add_task(async function webnav_ordering() {
  let extensionData = {
    manifest: {
      permissions: [
        "webNavigation",
      ],
    },
    background: backgroundScript,
  };

  let extension = ExtensionTestUtils.loadExtension(extensionData);

  extension.onMessage("received", ({url, event}) => {
    received.push({url, event});

    if (event == waitingEvent && url == waitingURL) {
      completedResolve();
    }
  });

  await extension.startup();
  await extension.awaitMessage("ready");
  info("webnavigation extension loaded");

  let win = window.open();

  await loadAndWait(win, "onCompleted", URL, () => { win.location = URL; });

  function checkRequired(url) {
    for (let event of REQUIRED) {
      let found = false;
      for (let r of received) {
        if (r.url == url && r.event == event) {
          found = true;
        }
      }
      ok(found, `Received event ${event} from ${url}`);
    }
  }

  checkRequired(URL);
  checkRequired(FRAME);

  function checkBefore(action1, action2) {
    function find(action) {
      for (let i = 0; i < received.length; i++) {
        if (received[i].url == action.url && received[i].event == action.event) {
          return i;
        }
      }
      return -1;
    }

    let index1 = find(action1);
    let index2 = find(action2);
    ok(index1 != -1, `Action ${JSON.stringify(action1)} happened`);
    ok(index2 != -1, `Action ${JSON.stringify(action2)} happened`);
    ok(index1 < index2, `Action ${JSON.stringify(action1)} happened before ${JSON.stringify(action2)}`);
  }

  // As required in the webNavigation API documentation:
  // If a navigating frame contains subframes, its onCommitted is fired before any
  // of its children's onBeforeNavigate; while onCompleted is fired after
  // all of its children's onCompleted.
  checkBefore({url: URL, event: "onCommitted"}, {url: FRAME, event: "onBeforeNavigate"});
  checkBefore({url: FRAME, event: "onCompleted"}, {url: URL, event: "onCompleted"});

  // As required in the webNAvigation API documentation, check the event sequence:
  // onBeforeNavigate -> onCommitted -> onDOMContentLoaded -> onCompleted
  let expectedEventSequence = [
    "onBeforeNavigate", "onCommitted", "onDOMContentLoaded", "onCompleted",
  ];

  for (let i = 1; i < expectedEventSequence.length; i++) {
    let after = expectedEventSequence[i];
    let before = expectedEventSequence[i - 1];
    checkBefore({url: URL, event: before}, {url: URL, event: after});
    checkBefore({url: FRAME, event: before}, {url: FRAME, event: after});
  }

  await loadAndWait(win, "onCompleted", FRAME2, () => { win.frames[0].location = FRAME2; });

  checkRequired(FRAME2);

  let navigationSequence = [
    {
      action: () => { win.frames[0].document.getElementById("elt").click(); },
      waitURL: `${FRAME2}#ref`,
      expectedEvent: "onReferenceFragmentUpdated",
      description: "clicked an anchor link",
    },
    {
      action: () => { win.frames[0].history.pushState({}, "History PushState", `${FRAME2}#ref2`); },
      waitURL: `${FRAME2}#ref2`,
      expectedEvent: "onReferenceFragmentUpdated",
      description: "history.pushState, same pathname, different hash",
    },
    {
      action: () => { win.frames[0].history.pushState({}, "History PushState", `${FRAME2}#ref2`); },
      waitURL: `${FRAME2}#ref2`,
      expectedEvent: "onHistoryStateUpdated",
      description: "history.pushState, same pathname, same hash",
    },
    {
      action: () => {
        win.frames[0].history.pushState({}, "History PushState", `${FRAME2}?query_param1=value#ref2`);
      },
      waitURL: `${FRAME2}?query_param1=value#ref2`,
      expectedEvent: "onHistoryStateUpdated",
      description: "history.pushState, same pathname, same hash, different query params",
    },
    {
      action: () => {
        win.frames[0].history.pushState({}, "History PushState", `${FRAME2}?query_param2=value#ref3`);
      },
      waitURL: `${FRAME2}?query_param2=value#ref3`,
      expectedEvent: "onHistoryStateUpdated",
      description: "history.pushState, same pathname, different hash, different query params",
    },
    {
      action: () => { win.frames[0].history.pushState(null, "History PushState", FRAME_PUSHSTATE); },
      waitURL: FRAME_PUSHSTATE,
      expectedEvent: "onHistoryStateUpdated",
      description: "history.pushState, different pathname",
    },
  ];

  for (let navigation of navigationSequence) {
    let {expectedEvent, waitURL, action, description} = navigation;
    info(`Waiting ${expectedEvent} from ${waitURL} - ${description}`);
    await loadAndWait(win, expectedEvent, waitURL, action);
    info(`Received ${expectedEvent} from ${waitURL} - ${description}`);
  }

  for (let i = navigationSequence.length - 1; i > 0; i--) {
    let {waitURL: fromURL, expectedEvent} = navigationSequence[i];
    let {waitURL} = navigationSequence[i - 1];
    info(`Waiting ${expectedEvent} from ${waitURL} - history.back() from ${fromURL} to ${waitURL}`);
    await loadAndWait(win, expectedEvent, waitURL, () => { win.frames[0].history.back(); });
    info(`Received ${expectedEvent} from ${waitURL} - history.back() from ${fromURL} to ${waitURL}`);
  }

  for (let i = 0; i < navigationSequence.length - 1; i++) {
    let {waitURL: fromURL} = navigationSequence[i];
    let {waitURL, expectedEvent} = navigationSequence[i + 1];
    info(`Waiting ${expectedEvent} from ${waitURL} - history.forward() from ${fromURL} to ${waitURL}`);
    await loadAndWait(win, expectedEvent, waitURL, () => { win.frames[0].history.forward(); });
    info(`Received ${expectedEvent} from ${waitURL} - history.forward() from ${fromURL} to ${waitURL}`);
  }

  win.close();

  await extension.unload();
  info("webnavigation extension unloaded");
});

add_task(async function webnav_error_event() {
  function backgroundScriptErrorEvent() {
    browser.webNavigation.onErrorOccurred.addListener((details) => {
      browser.test.log(`Got onErrorOccurred ${details.url} ${details.error}`);

      browser.test.sendMessage("received", {url: details.url, details, event: "onErrorOccurred"});
    });

    browser.test.sendMessage("ready");
  }

  let extensionData = {
    manifest: {
      permissions: [
        "webNavigation",
      ],
    },
    background: backgroundScriptErrorEvent,
  };

  let extension = ExtensionTestUtils.loadExtension(extensionData);

  extension.onMessage("received", ({url, event, details}) => {
    received.push({url, event, details});

    if (event == waitingEvent && url == waitingURL) {
      completedResolve();
    }
  });

  await Promise.all([extension.startup(), extension.awaitMessage("ready")]);
  info("webnavigation extension loaded");

  let win = window.open();

  received = [];
  await loadAndWait(win, "onErrorOccurred", INVALID_PAGE, () => { win.location = INVALID_PAGE; });

  let found = received.find((data) => (data.event == "onErrorOccurred" &&
                                   data.url == INVALID_PAGE));

  ok(found, "Got the onErrorOccurred event");

  if (found) {
    ok(found.details.error.match(/Error code [0-9]+/),
       "Got the expected error string in the onErrorOccurred event");
  }

  // cleanup phase
  win.close();

  await extension.unload();
  info("webnavigation extension unloaded");
});
</script>

</body>
</html>