file_testcommon.js (2169B)
1 "use strict"; 2 3 const SCRIPT_URL = SimpleTest.getTestFileURL("file_chromecommon.js"); 4 5 var gExpectedCookies; 6 var gExpectedLoads; 7 8 var gPopup; 9 10 var gScript; 11 12 var gLoads = 0; 13 14 function setupTest(uri, cookies, loads) { 15 SimpleTest.waitForExplicitFinish(); 16 17 var prefSet = new Promise(resolve => { 18 SpecialPowers.pushPrefEnv( 19 { 20 set: [ 21 ["network.cookie.cookieBehavior", 1], 22 // cookieBehavior 1 allows cookies from chrome script if we enable 23 // exceptions. 24 // Bug 1617611: Fix all the tests broken by "cookies SameSite=lax by default" 25 ["network.cookie.sameSite.laxByDefault", false], 26 ], 27 }, 28 resolve 29 ); 30 }); 31 32 gScript = SpecialPowers.loadChromeScript(SCRIPT_URL); 33 gExpectedCookies = cookies; 34 gExpectedLoads = loads; 35 36 // Listen for MessageEvents. 37 window.addEventListener("message", messageReceiver); 38 39 prefSet.then(() => { 40 // load a window which contains an iframe; each will attempt to set 41 // cookies from their respective domains. 42 gPopup = window.open(uri, "hai", "width=100,height=100"); 43 }); 44 } 45 46 function finishTest() { 47 gScript.destroy(); 48 SpecialPowers.clearUserPref("network.cookie.sameSite.laxByDefault"); 49 SimpleTest.finish(); 50 } 51 52 /** Receives MessageEvents to this window. */ 53 // Count and check loads. 54 function messageReceiver(evt) { 55 is(evt.data, "message", "message data received from popup"); 56 if (evt.data != "message") { 57 gPopup.close(); 58 window.removeEventListener("message", messageReceiver); 59 60 finishTest(); 61 return; 62 } 63 64 // only run the test when all our children are done loading & setting cookies 65 if (++gLoads == gExpectedLoads) { 66 gPopup.close(); 67 window.removeEventListener("message", messageReceiver); 68 69 runTest(); 70 } 71 } 72 73 // runTest() is run by messageReceiver(). 74 // Count and check cookies. 75 function runTest() { 76 // set a cookie from a domain of "localhost" 77 document.cookie = "oh=hai"; 78 79 gScript.addMessageListener("getCookieCountAndClear:return", ({ count }) => { 80 is(count, gExpectedCookies, "total number of cookies"); 81 finishTest(); 82 }); 83 gScript.sendAsyncMessage("getCookieCountAndClear"); 84 }