dom/base/test/test_messagemanager_targetchain.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <title>Test for EventTarget chain of MessageManagers</title>
michael@0 5 <script type="application/javascript"
michael@0 6 src="/tests/SimpleTest/SimpleTest.js">
michael@0 7 </script>
michael@0 8 <script type="application/javascript"
michael@0 9 src="/tests/SimpleTest/EventUtils.js">
michael@0 10 </script>
michael@0 11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
michael@0 12 </head>
michael@0 13 <body>
michael@0 14
michael@0 15 <script type="application/javascript;version=1.7">
michael@0 16 "use strict";
michael@0 17
michael@0 18 SimpleTest.waitForExplicitFinish();
michael@0 19
michael@0 20 const browserFrameURL = "file_empty.html";
michael@0 21 const contentFrameURL =
michael@0 22 "data:text/html,<!DOCTYPE HTML><html><body><button id=\"target\">target</button></body></html>";
michael@0 23
michael@0 24 function frameScript() {
michael@0 25 "use strict";
michael@0 26 addEventListener("test-event", function (e) {
michael@0 27 sendSyncMessage("test-event");
michael@0 28 }, true);
michael@0 29 }
michael@0 30
michael@0 31 function runTests() {
michael@0 32 // messageIndex is incremented for each message/event received
michael@0 33 let messageIndex = 0;
michael@0 34
michael@0 35 let iframe = document.createElement("iframe");
michael@0 36 iframe.setAttribute("mozbrowser", true);
michael@0 37 iframe.setAttribute("src", browserFrameURL);
michael@0 38
michael@0 39 iframe.addEventListener("mozbrowserloadend", function () {
michael@0 40 info("First iframe loaded");
michael@0 41 // First message manager
michael@0 42 let mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
michael@0 43 mm.addMessageListener("test-event", function onEvent(message) {
michael@0 44 is(messageIndex, 0,
michael@0 45 "first mm should be the first one to receive the test event");
michael@0 46 messageIndex++;
michael@0 47 });
michael@0 48 mm.loadFrameScript("data:,(" + frameScript.toString() + ")();", false);
michael@0 49
michael@0 50 // Document in the middle
michael@0 51 let doc1 = SpecialPowers.wrap(iframe).contentDocument;
michael@0 52 doc1.addEventListener("test-event", function (e) {
michael@0 53 ok(false, "content document shouldn't receive test event from child");
michael@0 54 }, true);
michael@0 55
michael@0 56 let iframe2 = doc1.createElement("iframe");
michael@0 57 iframe2.setAttribute("mozbrowser", true);
michael@0 58 iframe2.setAttribute("src", browserFrameURL);
michael@0 59
michael@0 60 iframe2.addEventListener("mozbrowserloadend", function () {
michael@0 61 info("Second iframe loaded");
michael@0 62 // Second message manager
michael@0 63 let mm2 = SpecialPowers.getBrowserFrameMessageManager(iframe2);
michael@0 64 mm2.addMessageListener("test-event", function onEvent(message) {
michael@0 65 is(messageIndex, 1,
michael@0 66 "second mm should be the second one to receive the test event");
michael@0 67 messageIndex++;
michael@0 68 });
michael@0 69 mm2.loadFrameScript("data:,(" + frameScript.toString() +")();", false);
michael@0 70
michael@0 71 // Third is the regular iframe
michael@0 72 let doc2 = SpecialPowers.wrap(iframe2).contentDocument;
michael@0 73 let iframe3 = doc2.createElement("iframe");
michael@0 74 iframe3.setAttribute("src", contentFrameURL);
michael@0 75
michael@0 76 iframe3.addEventListener("load", function (e) {
michael@0 77 info("Third iframe loaded");
michael@0 78 let doc3 = SpecialPowers.wrap(iframe3).contentDocument;
michael@0 79 let target = doc3.getElementById("target");
michael@0 80 target.addEventListener("test-event", function onEvent(e) {
michael@0 81 is(messageIndex, 2,
michael@0 82 "target should be the last one to receive the test event");
michael@0 83 messageIndex++;
michael@0 84 SimpleTest.finish();
michael@0 85 });
michael@0 86
michael@0 87 // Fire test event after load
michael@0 88 SimpleTest.executeSoon(function () {
michael@0 89 var event = new Event("test-event");
michael@0 90 SpecialPowers.dispatchEvent(iframe3.contentWindow, target, event);
michael@0 91 });
michael@0 92 });
michael@0 93 doc2.body.appendChild(iframe3);
michael@0 94 });
michael@0 95 doc1.body.appendChild(iframe2);
michael@0 96 });
michael@0 97 document.addEventListener("test-event", function (e) {
michael@0 98 ok(false, "top document shouldn't receive test event from child");
michael@0 99 }, true);
michael@0 100 document.body.appendChild(iframe);
michael@0 101 }
michael@0 102
michael@0 103 addEventListener("load", function() {
michael@0 104 var principal = SpecialPowers.wrap(document).nodePrincipal;
michael@0 105 SpecialPowers.addPermission("browser", true, { url: SpecialPowers.wrap(principal.URI).spec,
michael@0 106 appId: principal.appId,
michael@0 107 isInBrowserElement: false });
michael@0 108 SpecialPowers.addPermission("browser", true, { url: SpecialPowers.wrap(principal.URI).spec,
michael@0 109 appId: principal.appId,
michael@0 110 isInBrowserElement: true });
michael@0 111 SpecialPowers.pushPrefEnv({
michael@0 112 "set": [
michael@0 113 ["dom.mozBrowserFramesEnabled", true],
michael@0 114 ["dom.ipc.browser_frames.oop_by_default", false],
michael@0 115 ]
michael@0 116 }, runTests);
michael@0 117 });
michael@0 118 SimpleTest.registerCleanupFunction(function () {
michael@0 119 var principal = SpecialPowers.wrap(document).nodePrincipal;
michael@0 120 SpecialPowers.removePermission("browser", { url: SpecialPowers.wrap(principal.URI).spec,
michael@0 121 appId: principal.appId,
michael@0 122 isInBrowserElement: false });
michael@0 123 SpecialPowers.removePermission("browser", { url: SpecialPowers.wrap(principal.URI).spec,
michael@0 124 appId: principal.appId,
michael@0 125 isInBrowserElement: true });
michael@0 126 });
michael@0 127 </script>
michael@0 128 </body>
michael@0 129 </html>

mercurial