helpers.js (5086B)
1 // Returns a promise that asserts the "load" and "pageshow" events are not 2 // fired on |target|. 3 function assertNoLoadAndPageshowEvent(t, target) { 4 target.addEventListener("load", t.unreached_func("load should not be fired")); 5 target.addEventListener("pageshow", t.unreached_func("pageshow should not be fired")); 6 return new Promise(resolve => { 7 // Wait 50ms to ensure events fired after asynchronous navigations are 8 // also captured. 9 setTimeout(resolve, 50); 10 }); 11 } 12 13 const url204 = "/common/blank.html?pipe=status(204)"; 14 const postMessageToOpenerOnLoad = ` 15 window.onload = () => { 16 window.opener.postMessage("loaded", "*") 17 } 18 `; 19 20 // -- Start of helpers for iframe initial empty document tests. 21 22 // Creates an iframe with an unset src and appends it to the document. 23 window.insertIframe = (t) => { 24 const iframe = document.createElement("iframe"); 25 t.add_cleanup(() => iframe.remove()); 26 document.body.append(iframe); 27 return iframe; 28 }; 29 30 // Creates an iframe with src set to a URL that doesn't commit a new document 31 // (results in a HTTP 204 response) and appends it to the document. 32 window.insertIframeWith204Src = (t) => { 33 const iframe = document.createElement("iframe"); 34 iframe.src = url204; 35 t.add_cleanup(() => iframe.remove()); 36 document.body.append(iframe); 37 return iframe; 38 }; 39 40 // Creates an iframe with src="about:blank" and appends it to the document. The 41 // resulting document in the iframe is the initial about:blank Document [1], 42 // because during the "process the iframe attributes" algorithm [2], `src` 43 // navigations to `about:blank` are caught and result in a synchronous load 44 // event; not a SEPARATE navigation to a non-initial `about:blank` Document. See 45 // the documentation in [3] as well. 46 // 47 // [1]: https://html.spec.whatwg.org/#is-initial-about:blank 48 // [2]: https://html.spec.whatwg.org/#process-the-iframe-attributes 49 // [3]: https://html.spec.whatwg.org/#completely-finish-loading 50 window.insertIframeWithAboutBlankSrc = (t) => { 51 const iframe = document.createElement("iframe"); 52 t.add_cleanup(() => iframe.remove()); 53 iframe.src = "about:blank"; 54 document.body.append(iframe); 55 return iframe; 56 }; 57 58 // Creates an iframe with src="about:blank", appends it to the document, and 59 // waits for initial about:blank Document to finish loading. 60 window.insertIframeWithAboutBlankSrcWaitForLoad = async (t) => { 61 const iframe = insertIframeWithAboutBlankSrc(t); 62 const aboutBlankLoad = new Promise(resolve => { 63 // In some browsers, when the initial about:blank Document is influenced by 64 // a `src=about:blank` attribute, the about:blank navigation commits 65 // asynchronously, while in other browsers, it would commit synchronously. 66 // 67 // This means we can't wait for the "load" event as it might have already 68 // ran. Instead, just wait for 100ms before resolving, as the non-initial 69 // about:blank navigation will most likely take less than 100 ms to commit. 70 t.step_timeout(resolve, 100); 71 }); 72 await aboutBlankLoad; 73 return iframe; 74 }; 75 76 // Waits for the "load" event for |urlRelativeToThisDocument| to run on 77 // |iframe|. 78 window.waitForLoad = (t, iframe, urlRelativeToThisDocument) => { 79 return new Promise(resolve => { 80 iframe.addEventListener("load", t.step_func(() => { 81 assert_equals(iframe.contentWindow.location.href, (new URL(urlRelativeToThisDocument, location.href)).href); 82 83 // Wait a bit longer to ensure all history stuff has settled, e.g. the document is "completely loaded" 84 // (which happens from a queued task). 85 setTimeout(resolve, 0); 86 }), { once: true }); 87 }); 88 }; 89 90 // -- End of helpers for iframe initial empty document tests. 91 92 // -- Start of helpers for opened windows' initial empty document tests. 93 94 // window.open() to a URL that doesn't load a new document (results in a HTTP 95 // 204 response). This should create a new window that stays on the initial 96 // empty document. 97 window.windowOpen204 = (t) => { 98 const openedWindow = window.open(url204); 99 t.add_cleanup(() => openedWindow.close()); 100 return openedWindow; 101 }; 102 103 // window.open() (no URL set). This should create a new window that stays on 104 // the initial empty document as it won't trigger a non-initial about:blank 105 // navigation. 106 window.windowOpenNoURL = (t) => { 107 const openedWindow = window.open(); 108 t.add_cleanup(() => openedWindow.close()); 109 return openedWindow; 110 }; 111 112 // window.open("about:blank"). This should create a new window that stays on 113 // the initial empty document as it won't trigger a non-initial about:blank 114 // navigation. 115 window.windowOpenAboutBlank = (t) => { 116 const openedWindow = window.open("about:blank"); 117 t.add_cleanup(() => openedWindow.close()); 118 return openedWindow; 119 }; 120 121 // Waits for a postMessage with data set to |message| is received on the current 122 // window. 123 window.waitForMessage = (t, message) => { 124 return new Promise(resolve => { 125 window.addEventListener("message", t.step_func((event) => { 126 if (event.data == message) 127 resolve(); 128 })); 129 }); 130 }; 131 132 // -- End of helpers for opened windows' initial empty document tests.