test_postMessage_originAttributes.html (1767B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Test window.postMessages from system principal to window with non-default originAttributes</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 7 </head> 8 9 <body> 10 <iframe id="target-iframe"></iframe> 11 <script type="application/javascript"> 12 13 add_task(async function() { 14 let iframe = document.querySelector("#target-iframe"); 15 16 let win = SpecialPowers.wrap(iframe).contentWindow; 17 let docShell = win.docShell; 18 19 // Add private browsing ID to docShell origin and load document. 20 docShell.setOriginAttributes({privateBrowsingId: 1}); 21 22 await new Promise(resolve => { 23 iframe.addEventListener("load", resolve, true); 24 25 iframe.src = SimpleTest.getTestFileURL("file_receiveMessage.html"); 26 }); 27 28 // Set up console monitor to wait for warning. 29 const expectedMessage = ( 30 'Attempting to post a message to window with url ' + 31 '"http://mochi.test:8888/tests/dom/base/test/file_receiveMessage.html" ' + 32 'and origin "http://mochi.test:8888^privateBrowsingId=1" from a system ' + 33 'principal scope with mismatched origin "[System Principal]".'); 34 35 let consolePromise = new Promise(resolve => { 36 SimpleTest.monitorConsole(resolve, [e => e.message == expectedMessage]); 37 }); 38 39 // Post to the content window via SpecialPowers' system principal scope. 40 win.postMessage("Hello. o/", "http://mochi.test:8888"); 41 await new Promise(resolve => setTimeout(resolve, 0)); 42 43 SimpleTest.endMonitorConsole(); 44 await consolePromise; 45 46 // Check that the window received and handled the message. 47 is(win.document.body.textContent, "|Hello. o/", 48 "Content window received the expected message"); 49 }); 50 </script> 51 </body> 52 </html>