test_SpecialPowersLoadChromeScript.html (1795B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test for SpecialPowers.loadChromeScript</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 7 </head> 8 <body> 9 10 <pre id="test"> 11 <script class="testbody" type="text/javascript"> 12 13 SimpleTest.waitForExplicitFinish(); 14 15 var url = SimpleTest.getTestFileURL("SpecialPowersLoadChromeScript.js"); 16 var script = SpecialPowers.loadChromeScript(url); 17 18 var MESSAGE = { bar: true }; 19 script.addMessageListener("bar", function (message) { 20 is(JSON.stringify(message), JSON.stringify(MESSAGE), 21 "received back message from the chrome script"); 22 23 checkAssert(); 24 }); 25 26 function checkAssert() { 27 script.sendAsyncMessage("valid-assert"); 28 script.addMessageListener("valid-assert-done", endOfFirstTest); 29 } 30 31 var script2; 32 33 function endOfFirstTest() { 34 script.destroy(); 35 36 // wantGlobalProperties should add the specified properties to the sandbox 37 // that is used to run the chrome script. 38 script2 = SpecialPowers.loadChromeScript(_ => { 39 /* eslint-env mozilla/chrome-script */ 40 addMessageListener("valid-assert", function () { 41 assert.equal(typeof XMLHttpRequest, "function", "XMLHttpRequest is defined"); 42 assert.equal(typeof CSS, "undefined", "CSS is not defined"); 43 sendAsyncMessage("valid-assert-done"); 44 }); 45 }, { wantGlobalProperties: ["ChromeUtils", "XMLHttpRequest"] }); 46 47 script2.sendAsyncMessage("valid-assert"); 48 script2.addMessageListener("valid-assert-done", endOfTest); 49 50 } 51 52 async function endOfTest() { 53 is(await script.sendQuery("sync-message"), "Received a synchronous message.", 54 "Check sync return value"); 55 56 script2.destroy(); 57 SimpleTest.finish(); 58 } 59 60 script.sendAsyncMessage("foo", MESSAGE); 61 62 </script> 63 </pre> 64 </body> 65 </html>