4 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
5 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
9 <script type=
"text/javascript">
10 add_task(async
function() {
11 let popup
= window
.open("blank.html");
14 await
new Promise(resolve
=> {
15 popup
.addEventListener("load", resolve
, { once
: true });
18 info("popup blank.html loaded");
19 let tell_opener
= new URL("file_tell_opener.html", location
.href
);
20 // eslint-disable-next-line @microsoft/sdl/no-insecure-url
21 let xorigin_url
= new URL(tell_opener
.pathname
, "http://example.com");
23 let resolveStartedUnload
;
24 let startedUnload
= new Promise(resolve
=> {
25 resolveStartedUnload
= resolve
;
27 let didFinishUnload
= false;
29 let finishUnload
= false;
30 popup
.addEventListener("unload", function() {
31 resolveStartedUnload();
33 // Spin a nested event loop in unload until we set `finishUnload`.
34 SpecialPowers
.Services
.tm
.spinEventLoopUntil(
35 "Test(test_switch_back_nested.html)", () => finishUnload
);
37 info("exiting from unload nested event loop...");
38 didFinishUnload
= true;
42 info("wait for message from popup");
43 let messagePromise
= new Promise(resolve
=> {
44 addEventListener("message", evt
=> {
48 popup
.location
= xorigin_url
.href
;
51 info("popup loaded, ensuring we're in unload");
53 is(didFinishUnload
, false, "unload shouldn't have finished");
55 let switchStarted
= SpecialPowers
.spawnChrome([], async () => {
56 await
new Promise(resolve
=> {
57 async
function observer(subject
, topic
) {
58 is(topic
, "http-on-examine-response");
60 let uri
= subject
.QueryInterface(Ci
.nsIChannel
).URI
;
61 if (!uri
.filePath
.endsWith("file_tell_opener.html")) {
65 Services
.obs
.removeObserver(observer
, "http-on-examine-response");
67 // spin the event loop a few times to ensure we resolve after the process switch
68 for (let i
= 0; i
< 10; ++i
) {
69 await
new Promise(res
=> Services
.tm
.dispatchToMainThread(res
));
75 Services
.obs
.addObserver(observer
, "http-on-examine-response");
79 info("Navigating back to the current process");
80 await SpecialPowers
.spawn(popup
, [tell_opener
.href
], (href
) => {
81 content
.location
.href
= href
;
84 let messagePromise2
= new Promise(resolve
=> {
85 addEventListener("message", evt
=> {
90 info("Waiting for the process switch to start");
93 // Finish unloading, and wait for the unload to complete
94 is(didFinishUnload
, false, "unload shouldn't be finished");
96 await
new Promise(resolve
=> setTimeout(resolve
, 0));
97 is(didFinishUnload
, true, "unload should be finished");
99 info("waiting for navigation to complete");
100 await messagePromise2
;
102 info("closing popup");
105 ok(true, "Didn't crash");