summaryrefslogtreecommitdiffstats
path: root/dom/ipc/tests/browser_cancel_content_js.js
blob: d319de87e472c72c73ce124b54a82130ec74108d (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

requestLongerTimeout(10);

const TEST_PAGE =
  "http://mochi.test:8888/browser/dom/ipc/tests/file_cancel_content_js.html";
const NEXT_PAGE = "http://mochi.test:8888/browser/dom/ipc/tests/";
const JS_URI = "javascript:void(document.title = 'foo')";

async function test_navigation(nextPage, cancelContentJSPref, shouldCancel) {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["dom.ipc.cancel_content_js_when_navigating", cancelContentJSPref],
      ["dom.max_script_run_time", 20],
    ],
  });
  let tab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: TEST_PAGE,
  });

  const loopEnded = ContentTask.spawn(tab.linkedBrowser, [], async function() {
    await new Promise(resolve => {
      content.addEventListener("LongLoopEnded", resolve, {
        once: true,
      });
    });
  });

  // Wait for the test page's long-running JS loop to start.
  await ContentTask.spawn(tab.linkedBrowser, [], function() {
    content.dispatchEvent(new content.Event("StartLongLoop"));
  });

  info(
    `navigating to ${nextPage} with cancel content JS ${
      cancelContentJSPref ? "enabled" : "disabled"
    }`
  );
  const nextPageLoaded = BrowserTestUtils.waitForContentEvent(
    tab.linkedBrowser,
    "DOMTitleChanged"
  );
  BrowserTestUtils.loadURI(gBrowser, nextPage);

  const result = await Promise.race([
    nextPageLoaded,
    loopEnded.then(() => "timeout"),
  ]);

  const timedOut = result === "timeout";
  if (shouldCancel) {
    ok(timedOut === false, "expected next page to be loaded");
  } else {
    ok(timedOut === true, "expected timeout");
  }

  BrowserTestUtils.removeTab(tab);
}

add_task(async () => test_navigation(NEXT_PAGE, true, true));
add_task(async () => test_navigation(NEXT_PAGE, false, false));
add_task(async () => test_navigation(JS_URI, true, false));
add_task(async () => test_navigation(JS_URI, false, false));