summaryrefslogtreecommitdiffstats
path: root/docshell/test/navigation/test_blockBFCache.html
blob: 3d4a418369eca56c01da4ad76ab96dc0ebcc877b (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Blocking pages from entering BFCache</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="">
<script>

const getUserMediaPrefs = {
  set: [
    ["media.devices.insecure.enabled", true],
    ["media.getusermedia.insecure.enabled", true],
    ["media.navigator.permission.disabled", true],
  ],
};
const msePrefs = {
  set: [
    ["media.mediasource.enabled", true],
    ["media.audio-max-decode-error", 0],
    ["media.video-max-decode-error", 0],
  ]
};

const blockBFCacheTests = [
  {
    name: "Request",
    test: () => {
      return new Promise((resolve) => {
        const xhr = new XMLHttpRequest();
        xhr.open("GET", "slow.sjs");
        xhr.addEventListener("progress", () => { resolve(xhr); }, { once: true });
        xhr.send();
      });
    },
  },
  {
    name: "Background request",
    test: () => {
      return new Promise((resolve) => {
        const xhr = new XMLHttpRequest();
        xhr.open("GET", "slow.sjs");
        xhr.addEventListener("readystatechange", () => { if (xhr.readyState == xhr.HEADERS_RECEIVED) resolve(xhr); });
        xhr.send();
      });
    },
  },
  {
    name: "getUserMedia",
    prefs: getUserMediaPrefs,
    test: () => {
      return navigator.mediaDevices.getUserMedia({ audio: true, fake: true });
    },
  },
  {
    name: "RTCPeerConnection",
    test: () => {
      let pc = new RTCPeerConnection();
      return pc.createOffer();
    },
  },
  {
    name: "MSE",
    prefs: msePrefs,
    test: () => {
      const ms = new MediaSource();
      const el = document.createElement("video");
      el.src = URL.createObjectURL(ms);
      el.preload = "auto";
      return el;
    },
  },
  {
    name: "WebSpeech",
    test: () => {
      return new Promise((resolve) => {
        const utterance = new SpeechSynthesisUtterance('bfcache');
        utterance.lang = 'it-IT-noend';
        utterance.addEventListener('start', () => { resolve(utterance); })
        speechSynthesis.speak(utterance);
      });
    },
  },
  {
    name: "WebVR",
    prefs: {
      set: [
        ["dom.vr.test.enabled", true],
        ["dom.vr.puppet.enabled", true],
        ["dom.vr.require-gesture", false],
      ],
    },
    test: () => {
      return navigator.requestVRServiceTest();
    }
  },
];

if (SpecialPowers.Services.appinfo.fissionAutostart) {
  blockBFCacheTests.push({
    name: "Loading OOP iframe",
    test: () => {
      return new Promise((resolve) => {
        const el = document.body.appendChild(document.createElement("iframe"));
        el.id = "frame";
        addEventListener("message", ({ data }) => {
          if (data == "onload") {
            resolve();
          }
        });
        el.src = "https://example.com/tests/docshell/test/navigation/iframe_slow_onload.html";
      });
    },
    waitForDone: () => {
      SimpleTest.requestFlakyTimeout("Test has a loop in an onload handler that runs for 5000ms, we need to make sure the loop is done before moving to the next test.");
      return new Promise(resolve => {
        setTimeout(resolve, 5000);
      });
    },
  });
}

const dontBlockBFCacheTests = [
  {
    name: "getUserMedia",
    prefs: getUserMediaPrefs,
    test: () => {
      return navigator.mediaDevices.getUserMedia({ video: true, fake: true }).then(stream => {
        stream.getTracks().forEach(track => track.stop());
        return stream;
      });
    },
  },
/*
  Disabled because MediaKeys rely on being destroyed by the CC before they
  notify their window, so the test would intermittently fail depending on
  when the CC runs.

  {
    name: "MSE",
    prefs: msePrefs,
    test: () => {
      return new Promise((resolve) => {
        const ms = new MediaSource();
        const el = document.createElement("video");
        ms.addEventListener("sourceopen", () => { resolve(el) }, { once: true });
        el.src = URL.createObjectURL(ms);
        el.preload = "auto";
      }).then(el => {
        el.src = "";
        return el;
      });
    },
  },
*/
];



function executeTest() {

  let bc = new BroadcastChannel("bfcache_blocking");

  function promiseMessage(type) {
    return new Promise((resolve, reject) => {
      bc.addEventListener("message", (e) => {
        if (e.data.type == type) {
          resolve(e.data);
        }
      }, { once: true });
    });
  }

  function promisePageShow(shouldBePersisted) {
    return promiseMessage("pageshow").then(data => data.persisted == shouldBePersisted);
  }

  function promisePageShowFromBFCache(e) {
    return promisePageShow(true);
  }

  function promisePageShowNotFromBFCache(e) {
    return promisePageShow(false);
  }

  function runTests(testArray, shouldBlockBFCache) {
    for (const { name, prefs = {}, test, waitForDone } of testArray) {
      add_task(async function() {
        await SpecialPowers.pushPrefEnv(prefs, async function() {
          // Load a mostly blank page that we can communicate with over
          // BroadcastChannel (though it will close the BroadcastChannel after
          // receiving the next "load" message, to avoid blocking BFCache).
          let loaded = promisePageShowNotFromBFCache();
          window.open("file_blockBFCache.html", "", "noopener");
          await loaded;

          // Load the same page with a different URL.
          loaded = promisePageShowNotFromBFCache();
          bc.postMessage({ message: "load", url: `file_blockBFCache.html?${name}_${shouldBlockBFCache}` });
          await loaded;

          // Run test script in the second page.
          bc.postMessage({ message: "runScript", fun: test.toString() });
          await promiseMessage("runScriptDone");

          // Go back to the first page (this should just come from the BFCache).
          let goneBack = promisePageShowFromBFCache();
          bc.postMessage({ message: "back" });
          await goneBack;

          // Go forward again to the second page and check that it does/doesn't come
          // from the BFCache.
          let goneForward = promisePageShow(!shouldBlockBFCache);
          bc.postMessage({ message: "forward" });
          let result = await goneForward;
          ok(result, `Page ${shouldBlockBFCache ? "should" : "should not"} have been blocked from going into the BFCache (${name})`);

          // If the test will keep running after navigation, then we need to make
          // sure it's completely done before moving to the next test, to avoid
          // interfering with any following tests. If waitForDone is defined then
          // it'll return a Promise that we can use to wait for the end of the
          // test.
          if (waitForDone) {
            await waitForDone();
          }

          // Do a similar test, but replace the bfcache test page with a new page,
          // not a page coming from the session history.

          // Load the same page with a different URL.
          loaded = promisePageShowNotFromBFCache();
          bc.postMessage({ message: "load", url: `file_blockBFCache.html?p2_${name}_${shouldBlockBFCache}` });
          await loaded;

          // Run the test script.
          bc.postMessage({ message: "runScript", fun: test.toString() });
          await promiseMessage("runScriptDone");

          // Load a new page.
          loaded = promisePageShowNotFromBFCache();
          bc.postMessage({ message: "load", url: "file_blockBFCache.html" });
          await loaded;

          // Go back to the previous page and check that it does/doesn't come
          // from the BFCache.
          goneBack = promisePageShow(!shouldBlockBFCache);
          bc.postMessage({ message: "back" });
          result = await goneBack;
          ok(result, `Page ${shouldBlockBFCache ? "should" : "should not"} have been blocked from going into the BFCache (${name})`);

          if (waitForDone) {
            await waitForDone();
          }

          bc.postMessage({ message: "close" });

          SpecialPowers.popPrefEnv();
        });
      });
    }
  }

  // If Fission is disabled, the pref is no-op.
  SpecialPowers.pushPrefEnv({set: [["fission.bfcacheInParent", true]]}, () => {
    runTests(blockBFCacheTests, true);
    runTests(dontBlockBFCacheTests, false);
  });
}

if (isXOrigin) {
  // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5)
  // Acquire storage access permission here so that the BroadcastChannel used to
  // communicate with the opened windows works in xorigin tests. Otherwise,
  // the iframe containing this page is isolated from first-party storage access,
  // which isolates BroadcastChannel communication.
  SpecialPowers.wrap(document).notifyUserGestureActivation();
  SpecialPowers.addPermission("storageAccessAPI", true, window.location.href).then(() => {
    SpecialPowers.wrap(document).requestStorageAccess().then(() => {
      SpecialPowers.pushPrefEnv({
        set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]]
      }).then(() => {
        executeTest();
      });
    });
  });
} else {
  executeTest();
}

</script>
</body>
</html>