script-closable.html (2768B)
1 <!doctype html> 2 <title>Test whether a window is script-closable</title> 3 <link rel="help" href="https://html.spec.whatwg.org/#script-closable"/> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <div id="log"></div> 7 8 <script> 9 let uid = 0; 10 11 async function withChannel(createWindow) { 12 const channel_name = `channel_${++uid}`; 13 const channel = new BroadcastChannel(channel_name); 14 15 const closedPromise = new Promise(resolve => { 16 channel.addEventListener("message", ({data}) => { 17 if (data.name === 'load') { 18 assert_true(true, 'window was loaded'); 19 } else { 20 assert_not_equals(data.history, 1, "If script-closable, then not due to history length"); 21 resolve(data.closed); 22 } 23 }); 24 }); 25 26 await createWindow(channel_name); 27 return closedPromise; 28 } 29 30 const Actions = { 31 async form(noopener) { 32 return withChannel((channel_name) => { 33 const form = document.createElement("form"); 34 form.action = "self-close.html"; 35 form.target = '_blank'; 36 if (noopener) { 37 form.rel = "noopener"; 38 } 39 // ?navs=1 40 const inp1 = document.createElement("input"); 41 inp1.name = "navs"; 42 inp1.value = "1"; 43 form.appendChild(inp1); 44 // ?channel=channel_name 45 const inp2 = document.createElement("input"); 46 inp2.name = "channel"; 47 inp2.value = channel_name; 48 form.appendChild(inp2); 49 document.body.appendChild(form); 50 51 form.submit(); 52 form.remove(); 53 }); 54 }, 55 async link(noopener) { 56 return withChannel((channel_name) => { 57 const anchor = document.createElement("a"); 58 anchor.href = `self-close.html?navs=1&channel=${channel_name}`; 59 anchor.target = '_blank'; 60 if (noopener) { 61 anchor.rel = "noopener"; 62 } 63 document.body.appendChild(anchor); 64 65 anchor.click(); 66 67 anchor.remove(); 68 }); 69 }, 70 async script(noopener) { 71 return withChannel((channel_name) => { 72 const url = `self-close.html?navs=1&channel=${channel_name}`; 73 window.open(url, "_blank", noopener ? 'noopener' : ''); 74 }); 75 } 76 } 77 78 for (const action of [Actions.form, Actions.link, Actions.script]) { 79 for (const noopener of [false, true]) 80 promise_test(async () => { 81 const closed = await action(noopener); 82 assert_true(closed, "window closed"); 83 }, `Window created by ${action.name} with ${noopener ? 'noopener' : 'opener'} is script-closable`); 84 } 85 </script>