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