summaryrefslogtreecommitdiffstats
path: root/docshell/test/navigation/test_bug1699721.html
blob: c6ae8e88d36b788a2b4855d86f17f50536df1c02 (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
<!DOCTYPE html>
<html>
<head>
    <script src="/tests/SimpleTest/SimpleTest.js"></script>
    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script type="text/javascript">
  add_task(async function() {
    // Induce a process switching behavior for example.com
    // with isolateHighValue isolation strategy
    // (because we test specifically process switching behavior here)
    await SpecialPowers.pushPermissions([
      {
        type: "highValueCOOP",
        allow: SpecialPowers.Ci.nsIPermissionManager.ALLOW_ACTION,
        context: "https://example.com",
      }
    ]);

    let popup = window.open("blank.html");

    info("opened popup");
    await new Promise(resolve => {
      popup.addEventListener("load", resolve, { once: true });
    });

    info("popup blank.html loaded");
    let tell_opener = new URL("file_tell_opener.html", location.href);
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    let xorigin_url = new URL(tell_opener.pathname, "https://example.com");

    let resolveStartedUnload;
    let startedUnload = new Promise(resolve => {
      resolveStartedUnload = resolve;
    });
    let didFinishUnload = false;

    let finishUnload = false;
    popup.addEventListener("unload", function() {
      resolveStartedUnload();
      try {
        // Spin a nested event loop in unload until we set `finishUnload`.
        SpecialPowers.Services.tm.spinEventLoopUntil(
          "Test(test_switch_back_nested.html)", () => finishUnload);
      } finally {
        info("exiting from unload nested event loop...");
        didFinishUnload = true;
      }
    });

    info("wait for message from popup");
    let messagePromise = new Promise(resolve => {
      addEventListener("message", evt => {
        resolve();
      }, { once: true });
    });
    popup.location = xorigin_url.href;
    await messagePromise;

    info("popup loaded, ensuring we're in unload");
    await startedUnload;
    is(didFinishUnload, false, "unload shouldn't have finished");

    let switchStarted = SpecialPowers.spawnChrome([], async () => {
      await new Promise(resolve => {
        async function observer(subject, topic) {
          is(topic, "http-on-examine-response");

          let uri = subject.QueryInterface(Ci.nsIChannel).URI;
          if (!uri.filePath.endsWith("file_tell_opener.html")) {
            return;
          }

          Services.obs.removeObserver(observer, "http-on-examine-response");

          // spin the event loop a few times to ensure we resolve after the process switch
          for (let i = 0; i < 10; ++i) {
            await new Promise(res => Services.tm.dispatchToMainThread(res));
          }

          info("resolving!");
          resolve();
        }
        Services.obs.addObserver(observer, "http-on-examine-response");
      });
    });

    info("Navigating back to the current process");
    await SpecialPowers.spawn(popup, [tell_opener.href], (href) => {
      content.location.href = href;
    });

    let messagePromise2 = new Promise(resolve => {
      addEventListener("message", evt => {
        resolve();
      }, { once: true });
    });

    info("Waiting for the process switch to start");
    await switchStarted;

    // Finish unloading, and wait for the unload to complete
    is(didFinishUnload, false, "unload shouldn't be finished");
    finishUnload = true;
    await new Promise(resolve => setTimeout(resolve, 0));
    is(didFinishUnload, true, "unload should be finished");

    info("waiting for navigation to complete");
    await messagePromise2;

    info("closing popup");
    popup.close();

    ok(true, "Didn't crash");
  });
</script>
</pre>
</body>
</html>