test_SpecialPowersLoadChromeScript_function.html (1585B)
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 SimpleTest.waitForExplicitFinish(); 13 14 15 var script = SpecialPowers.loadChromeScript(function loadChromeScriptTest() { 16 /* eslint-env mozilla/chrome-script */ 17 // Copied from SpecialPowersLoadChromeScript.js 18 19 // Just receive 'foo' message and forward it back 20 // as 'bar' message 21 addMessageListener("foo", function (message) { 22 sendAsyncMessage("bar", message); 23 }); 24 25 addMessageListener("valid-assert", function () { 26 assert.ok(true, "valid assertion"); 27 assert.equal(1, 1, "another valid assertion"); 28 sendAsyncMessage("valid-assert-done"); 29 }); 30 31 addMessageListener("sync-message", () => { 32 return "Received a synchronous message."; 33 }); 34 }); 35 36 var MESSAGE = { bar: true }; 37 script.addMessageListener("bar", function (message) { 38 is(JSON.stringify(message), JSON.stringify(MESSAGE), 39 "received back message from the chrome script"); 40 41 checkAssert(); 42 }); 43 44 function checkAssert() { 45 script.sendAsyncMessage("valid-assert"); 46 script.addMessageListener("valid-assert-done", endOfTest); 47 } 48 49 async function endOfTest() { 50 is(await script.sendQuery("sync-message"), "Received a synchronous message.", 51 "Check sync return value"); 52 53 script.destroy(); 54 SimpleTest.finish(); 55 } 56 57 script.sendAsyncMessage("foo", MESSAGE); 58 59 </script> 60 </pre> 61 </body> 62 </html>