summaryrefslogtreecommitdiffstats
path: root/layout/base/tests/browser_onbeforeunload_only_after_interaction_in_frame.js
blob: bf9ab6725447a97a0793ae182b99def8a7005c1d (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
const { PromptTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PromptTestUtils.sys.mjs"
);

function pageScript() {
  window.addEventListener(
    "beforeunload",
    function (event) {
      var str = "Some text that causes the beforeunload dialog to be shown";
      event.returnValue = str;
      return str;
    },
    true
  );
}

SpecialPowers.pushPrefEnv({
  set: [
    ["dom.require_user_interaction_for_beforeunload", true],
    ["security.allow_eval_with_system_principal", true],
  ],
});

const FRAME_URL =
  "data:text/html," + encodeURIComponent("<body>Just a frame</body>");

const PAGE_URL =
  "data:text/html," +
  encodeURIComponent(
    "<iframe src='" +
      FRAME_URL +
      "'></iframe><script>(" +
      pageScript.toSource() +
      ")();</script>"
  );

add_task(async function doClick() {
  // The onbeforeunload dialog should appear.
  let dialogPromise = PromptTestUtils.waitForPrompt(null, {
    modalType: Services.prompt.MODAL_TYPE_CONTENT,
    promptType: "confirmEx",
  });

  let openPagePromise = openPage(true);
  let dialog = await dialogPromise;
  Assert.ok(true, "Showed the beforeunload dialog.");

  await PromptTestUtils.handlePrompt(dialog, { buttonNumClick: 0 });
  await openPagePromise;
});

add_task(async function noClick() {
  // The onbeforeunload dialog should NOT appear.
  await openPage(false);
  info("If we time out here, then the dialog was shown...");
});

async function openPage(shouldClick) {
  // Open about:blank in a new tab.
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:blank" },
    async function (browser) {
      // Load the page.
      BrowserTestUtils.startLoadingURIString(browser, PAGE_URL);
      await BrowserTestUtils.browserLoaded(browser);

      let frameBC = browser.browsingContext.children[0];
      if (shouldClick) {
        await BrowserTestUtils.synthesizeMouse("body", 2, 2, {}, frameBC);
      }
      let hasInteractedWith = await SpecialPowers.spawn(
        frameBC,
        [],
        function () {
          return [
            content.document.userHasInteracted,
            content.document.userHasInteracted,
          ];
        }
      );
      is(
        shouldClick,
        hasInteractedWith[0],
        "Click should update parent interactivity state"
      );
      is(
        shouldClick,
        hasInteractedWith[1],
        "Click should update frame interactivity state"
      );
      // And then navigate away.
      BrowserTestUtils.startLoadingURIString(browser, "http://example.com/");
      await BrowserTestUtils.browserLoaded(browser);
    }
  );
}