summaryrefslogtreecommitdiffstats
path: root/docshell/test/browser/browser_onbeforeunload_navigation.js
blob: 23dc3b528e77a0e96a83d4dd19fd4be1c62c8686 (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
"use strict";

const TEST_PAGE =
  "http://mochi.test:8888/browser/docshell/test/browser/file_bug1046022.html";
const TARGETED_PAGE =
  "data:text/html," +
  encodeURIComponent("<body>Shouldn't be seeing this</body>");

const { PromptTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PromptTestUtils.sys.mjs"
);

var loadStarted = false;
var tabStateListener = {
  resolveLoad: null,
  expectLoad: null,

  onStateChange(webprogress, request, flags, status) {
    const WPL = Ci.nsIWebProgressListener;
    if (flags & WPL.STATE_IS_WINDOW) {
      if (flags & WPL.STATE_START) {
        loadStarted = true;
      } else if (flags & WPL.STATE_STOP) {
        let url = request.QueryInterface(Ci.nsIChannel).URI.spec;
        is(url, this.expectLoad, "Should only see expected document loads");
        if (url == this.expectLoad) {
          this.resolveLoad();
        }
      }
    }
  },
  QueryInterface: ChromeUtils.generateQI([
    "nsIWebProgressListener",
    "nsISupportsWeakReference",
  ]),
};

function promiseLoaded(url, callback) {
  if (tabStateListener.expectLoad) {
    throw new Error("Can't wait for multiple loads at once");
  }
  tabStateListener.expectLoad = url;
  return new Promise(resolve => {
    tabStateListener.resolveLoad = resolve;
    if (callback) {
      callback();
    }
  }).then(() => {
    tabStateListener.expectLoad = null;
    tabStateListener.resolveLoad = null;
  });
}

function promiseStayOnPagePrompt(browser, acceptNavigation) {
  return PromptTestUtils.handleNextPrompt(
    browser,
    { modalType: Services.prompt.MODAL_TYPE_CONTENT, promptType: "confirmEx" },
    { buttonNumClick: acceptNavigation ? 0 : 1 }
  );
}

add_task(async function test() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.require_user_interaction_for_beforeunload", false]],
  });

  let testTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    TEST_PAGE,
    false,
    true
  );
  let browser = testTab.linkedBrowser;
  browser.addProgressListener(
    tabStateListener,
    Ci.nsIWebProgress.NOTIFY_STATE_WINDOW
  );

  const NUM_TESTS = 7;
  await SpecialPowers.spawn(browser, [NUM_TESTS], testCount => {
    let { testFns } = this.content.wrappedJSObject;
    Assert.equal(
      testFns.length,
      testCount,
      "Should have the correct number of test functions"
    );
  });

  for (let allowNavigation of [false, true]) {
    for (let i = 0; i < NUM_TESTS; i++) {
      info(
        `Running test ${i} with navigation ${
          allowNavigation ? "allowed" : "forbidden"
        }`
      );

      if (allowNavigation) {
        // If we're allowing navigations, we need to re-load the test
        // page after each test, since the tests will each navigate away
        // from it.
        await promiseLoaded(TEST_PAGE, () => {
          browser.loadURI(Services.io.newURI(TEST_PAGE), {
            triggeringPrincipal: document.nodePrincipal,
          });
        });
      }

      let promptPromise = promiseStayOnPagePrompt(browser, allowNavigation);
      let loadPromise;
      if (allowNavigation) {
        loadPromise = promiseLoaded(TARGETED_PAGE);
      }

      let winID = await SpecialPowers.spawn(
        browser,
        [i, TARGETED_PAGE],
        (testIdx, url) => {
          let { testFns } = this.content.wrappedJSObject;
          this.content.onbeforeunload = testFns[testIdx];
          this.content.location = url;
          return this.content.windowGlobalChild.innerWindowId;
        }
      );

      await promptPromise;
      await loadPromise;

      if (allowNavigation) {
        await SpecialPowers.spawn(
          browser,
          [TARGETED_PAGE, winID],
          (url, winID) => {
            this.content.onbeforeunload = null;
            Assert.equal(
              this.content.location.href,
              url,
              "Page should have navigated to the correct URL"
            );
            Assert.notEqual(
              this.content.windowGlobalChild.innerWindowId,
              winID,
              "Page should have a new inner window"
            );
          }
        );
      } else {
        await SpecialPowers.spawn(browser, [TEST_PAGE, winID], (url, winID) => {
          this.content.onbeforeunload = null;
          Assert.equal(
            this.content.location.href,
            url,
            "Page should have the same URL"
          );
          Assert.equal(
            this.content.windowGlobalChild.innerWindowId,
            winID,
            "Page should have the same inner window"
          );
        });
      }
    }
  }

  gBrowser.removeTab(testTab);
});