javascript-url-no-beforeunload.window.js (2616B)
1 // META: script=../resources/helpers.js 2 3 for (const stringCompletion of [false, true]) { 4 const testNameSuffix = stringCompletion ? ": string completion" : ": undefined completion"; 5 6 testNoBeforeunload( 7 { testRunnerWindow: "top", stringCompletion }, 8 async (t, urlToSet) => { 9 const iframe = await addIframe(); 10 iframe.contentWindow.location.href = urlToSet; 11 12 return iframe.contentWindow; 13 }, 14 `Navigating an iframe via location.href to a javascript: URL must not fire beforeunload${testNameSuffix}` 15 ); 16 17 testNoBeforeunload( 18 { testRunnerWindow: "top", stringCompletion }, 19 async (t, urlToSet) => { 20 const iframe = await addIframe(); 21 iframe.src = urlToSet; 22 23 return iframe.contentWindow; 24 }, 25 `Navigating an iframe via src="" to a javascript: URL after insertion must not fire beforeunload${testNameSuffix}` 26 ); 27 28 testNoBeforeunload( 29 { testRunnerWindow: "opener", stringCompletion }, 30 async (t, urlToSet) => { 31 const w = await openWindow("/common/blank.html", t); 32 w.location.href = urlToSet; 33 34 return w; 35 }, 36 `Navigating an opened window via location.href to a javascript: URL must not fire beforeunload${testNameSuffix}` 37 ); 38 39 40 testNoBeforeunload( 41 { testRunnerWindow: "opener", stringCompletion }, 42 async (t, urlToSet) => { 43 const w = await openWindow("../resources/has-iframe.html", t); 44 w.frames[0].onbeforeunload = t.unreached_func("beforeunload must not fire on the iframe"); 45 w.location.href = urlToSet; 46 47 return w; 48 }, 49 `Navigating an opened window with an iframe via location.href to a javascript: URL must not fire beforeunload on the iframe${testNameSuffix}` 50 ); 51 } 52 53 function testNoBeforeunload({ testRunnerWindow, stringCompletion }, setupAndNavigateFunc, description) { 54 promise_test(async t => { 55 t.add_cleanup(() => { 56 delete window.resolveTestPromise; 57 }); 58 59 const ranPromise = new Promise(resolve => { 60 window.resolveTestPromise = resolve; 61 }); 62 63 const urlToSet = makeURL({ testRunnerWindow, stringCompletion }); 64 const w = await setupAndNavigateFunc(t, urlToSet); 65 w.onbeforeunload = t.unreached_func("beforeunload must not fire"); 66 67 await ranPromise; 68 if (stringCompletion) { 69 await waitForMessage(w); 70 } 71 }, description); 72 } 73 74 function makeURL({ testRunnerWindow, stringCompletion }) { 75 const completion = stringCompletion ? 76 `"a string<script>window.${testRunnerWindow}.postMessage('ready', '*');</script>";` : 77 `undefined;`; 78 79 return `javascript:window.${testRunnerWindow}.resolveTestPromise();${completion};`; 80 }