summaryrefslogtreecommitdiffstats
path: root/dom/ipc/tests/file_broadcast_currenturi_onload.html
blob: e258fbdf83a5ceb7b39267613e4b5e31e1743f4b (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
<!doctype html>
<body>
<script>

const url = new URL(location.href);

// Create a popup to broadcast the load's completion to the test document.
//
// NOTE: We're using a popup to ensure that the new document has the same origin
// (http://mochi.test:8888/) as the original test document, so that we can use a
// BroadcastChannel to communicate with the test page. We can't use an iframe as
// the mixed content blocker will prevent embedding this URL in https://
// documents.
function sendPayload(payload) {
  let broadcastURL = new URL(url.pathname, "http://mochi.test:8888/");
  broadcastURL.search = "?payload=" + encodeURIComponent(JSON.stringify(payload));
  window.open(broadcastURL.href);
}

async function getURIs() {
  // Run the test and fetch the relevant information.
  const browsingContext = SpecialPowers.wrap(window).browsingContext;
  let [docURI, curURI] = await SpecialPowers.spawnChrome(
    [browsingContext.id], async id => {
      let bc = BrowsingContext.get(id);
      return [
        bc.currentWindowGlobal.documentURI.spec,
        bc.currentURI.spec,
      ];
    }
  );
  return { location: location.href, docURI, curURI };
}

addEventListener("load", async () => {
  // If a payload parameter was included, just send the message.
  const payloadStr = url.searchParams.get("payload");
  if (payloadStr) {
    const chan = new BroadcastChannel("test_broadcast_onload");
    chan.postMessage(JSON.parse(payloadStr));
    window.close();
    return;
  }

  // collect the initial set of URIs
  const result1 = await getURIs();

  const pushstateURL = new URL("after_pushstate", url.href);
  history.pushState({}, "After PushState!", pushstateURL.href);
  await new Promise(resolve => setTimeout(resolve, 0));

  // Collect the set of URIs after pushstate
  const result2 = await getURIs();

  window.location.hash = "#after_hashchange";
  await new Promise(resolve => setTimeout(resolve, 0));

  // Collect the set of URIs after a hash change
  const result3 = await getURIs();

  sendPayload([result1, result2, result3]);
  window.close();
});
</script>
</body>