common.js (3207B)
1 // To use the functions below, be sure to include the following files in your 2 // test: 3 // - "/common/get-host-info.sub.js" to get the different origin values. 4 5 const SAME_ORIGIN = {origin: get_host_info().HTTPS_ORIGIN, name: "SAME_ORIGIN"}; 6 const SAME_SITE = {origin: get_host_info().HTTPS_REMOTE_ORIGIN, name: "SAME_SITE"}; 7 const CROSS_ORIGIN = {origin: get_host_info().HTTPS_NOTSAMESITE_ORIGIN, name: "CROSS_ORIGIN"} 8 9 function addScriptAndTriggerOnload(src, onload){ 10 return `script = document.createElement("script"); 11 script.src= "${src}" ; 12 script.onload = () => { 13 ${onload} 14 }; 15 document.head.append(script);` 16 } 17 18 function verify_window(callback, w, hasOpener) { 19 // If there's no opener, the w must be closed: 20 assert_equals(w.closed, !hasOpener, 'w.closed'); 21 // Opener's access on w.length is possible only if hasOpener: 22 assert_equals(w.length, hasOpener? 1: 0, 'w.length'); 23 callback(); 24 } 25 26 function validate_results(callback, test, w, channelName, hasOpener, openerDOMAccess, payload) { 27 assert_equals(payload.name, hasOpener ? channelName : "", 'name'); 28 assert_equals(payload.opener, hasOpener, 'opener'); 29 // TODO(zcorpan): add openerDOMAccess expectations to all tests 30 if (openerDOMAccess !== undefined) { 31 assert_equals(payload.openerDOMAccess, openerDOMAccess, 'openerDOMAccess'); 32 } 33 34 // The window proxy in Chromium might still reflect the previous frame, 35 // until its unloaded. This delays the verification of w here. 36 if( !w.closed && w.length == 0) { 37 test.step_timeout( () => { 38 verify_window(callback, w, hasOpener); 39 }, 500); 40 } else { 41 verify_window(callback, w, hasOpener); 42 } 43 } 44 45 // Note: This function is deprecated and should not be used by new tests. 46 // Instead, use `dispatcher_url_test()`. 47 function url_test(t, url, channelName, hasOpener, openerDOMAccess, callback) { 48 if (callback === undefined) { 49 callback = () => { t.done(); }; 50 } 51 const bc = new BroadcastChannel(channelName); 52 bc.onmessage = t.step_func(event => { 53 const payload = event.data; 54 validate_results(callback, t, w, channelName, hasOpener, openerDOMAccess, payload); 55 }); 56 57 const w = window.open(url, channelName); 58 59 // Close the popup once the test is complete. 60 // The browsing context might be closed hence use the broadcast channel 61 // to trigger the closure. 62 t.add_cleanup(() => { 63 bc.postMessage("close"); 64 }); 65 } 66 67 // Similar to `url_test()` above except that this uses a dispatcher instead of 68 // BroadcastChannel (useful in cases where the context we are testing in is a 69 // third-party iframe that doesn't share a partition with the top-level 70 // site). 71 async function dispatcher_url_test(t, url, responseToken, iframeToken, hasOpener, openerDOMAccess, callback) { 72 73 const w = window.open(url, responseToken); 74 75 // Close the popup once the test is complete. 76 // The browsing context might be closed hence we'll have the iframe trigger 77 // the closure by sending it a 'close' message. 78 t.add_cleanup(async () => { 79 await send(iframeToken, "close"); 80 }); 81 82 var payload = await receive(responseToken); 83 payload = JSON.parse(payload); 84 validate_results(callback, t, w, responseToken, hasOpener, openerDOMAccess, payload); 85 }