window-open-popup-during-pageshow.html (2151B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="/common/utils.js"></script> 6 7 <body> 8 <script> 9 "use strict"; 10 promise_test(async t => { 11 // Generating a new one instead of hard-coding makes running tests manually a bit easier. 12 const windowName = token(); 13 14 const code = ` 15 window.onpageshow = () => opener.navigateMe(); 16 opener.postMessage("arrived at start URL", "*"); 17 `; 18 19 const startURL = "resources/slow-code-injector.html?pipe=sub(none)&code=" + encodeURIComponent(code); 20 const absoluteStartURL = (new URL(startURL, location.href)).href; 21 22 const afterReplacementURL = "resources/message-opener.html"; 23 const absoluteAfterReplacementURL = (new URL(afterReplacementURL, location.href)).href; 24 25 window.navigateMe = () => { 26 window.open(absoluteAfterReplacementURL, windowName); 27 }; 28 29 // First message sent is ignored; we only check it after navigating back. 30 const w = window.open(startURL, windowName); 31 t.add_cleanup(() => w.close()); 32 33 // Wait to get past any initial about:blank 34 while (true) { 35 if (w.location.href === absoluteStartURL) { 36 break; 37 } 38 await new Promise(r => t.step_timeout(r, 0)); 39 } 40 41 assert_equals(w.onloadFired, undefined, "onload must not yet have fired"); 42 assert_equals(w.history.length, 1, "history.length for the opened window must start at 1"); 43 44 await new Promise(r => { 45 window.addEventListener("message", t.step_func(e => { 46 if (e.data === "ready") { 47 resolve(); 48 } 49 })); 50 }); 51 52 assert_equals(w.history.length, 2, "history.length must increase"); 53 assert_equals(w.location.href, absoluteAfterReplacementURL); 54 55 const promise = new Promise(resolve => { 56 window.addEventListener("message", t.step_func(e => { 57 assert_equals(e.data, "arrived at start URL"); 58 resolve(); 59 })); 60 }); 61 62 w.history.back(); 63 64 await promise; 65 assert_equals(w.location.href, absoluteStartURL, "1 second after attempting to go back, it indeed went back"); 66 }, "No replace before load, triggered by window.open() on a non-_self window"); 67 </script>