Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* Any copyright is dedicated to the public domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 // Test that alert works.
5 "use strict";
7 SimpleTest.waitForExplicitFinish();
8 browserElementTestHelpers.setEnabledPref(true);
9 browserElementTestHelpers.addPermission();
11 var numPendingChildTests = 0;
12 var iframe;
13 var mm;
15 function runTest() {
16 iframe = document.createElement('iframe');
17 SpecialPowers.wrap(iframe).mozbrowser = true;
18 document.body.appendChild(iframe);
20 mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
21 mm.addMessageListener('test-success', function(msg) {
22 numPendingChildTests--;
23 ok(true, SpecialPowers.wrap(msg).json);
24 });
25 mm.addMessageListener('test-fail', function(msg) {
26 numPendingChildTests--;
27 ok(false, SpecialPowers.wrap(msg).json);
28 });
30 // Wait for the initial load to finish, then navigate the page, then wait
31 // for that load to finish, then start test1.
32 iframe.addEventListener('mozbrowserloadend', function loadend() {
33 iframe.removeEventListener('mozbrowserloadend', loadend);
34 iframe.src = browserElementTestHelpers.emptyPage1;
36 iframe.addEventListener('mozbrowserloadend', function loadend2() {
37 iframe.removeEventListener('mozbrowserloadend', loadend2);
38 SimpleTest.executeSoon(test1);
39 });
40 });
42 }
44 function test1() {
45 iframe.addEventListener('mozbrowsershowmodalprompt', test2);
47 // Do window.alert within the iframe, then modify the global |testState|
48 // after the alert.
49 var script = 'data:,\
50 testState = 0; \
51 content.alert("Hello, world!"); \
52 testState = 1; \
53 ';
55 mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
57 // Triggers a mozbrowsershowmodalprompt event, which sends us down to test2.
58 }
60 // test2 is a mozbrowsershowmodalprompt listener.
61 function test2(e) {
62 iframe.removeEventListener("mozbrowsershowmodalprompt", test2);
64 is(e.detail.message, 'Hello, world!');
65 e.preventDefault(); // cause the alert to block.
67 SimpleTest.executeSoon(function() { test2a(e); });
68 }
70 function test2a(e) {
71 // The iframe should be blocked on the alert call at the moment, so testState
72 // should still be 0.
73 var script = 'data:,\
74 if (testState === 0) { \
75 sendAsyncMessage("test-success", "1: Correct testState"); \
76 } \
77 else { \
78 sendAsyncMessage("test-fail", "1: Wrong testState: " + testState); \
79 }';
81 mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
82 numPendingChildTests++;
84 waitForPendingTests(function() { test3(e); });
85 }
87 function test3(e) {
88 // Now unblock the iframe and check that the script completed.
89 e.detail.unblock();
91 var script2 = 'data:,\
92 if (testState === 1) { \
93 sendAsyncMessage("test-success", "2: Correct testState"); \
94 } \
95 else { \
96 sendAsyncMessage("test-try-again", "2: Wrong testState (for now): " + testState); \
97 }';
99 // Urgh. e.unblock() didn't necessarily unblock us immediately, so we have
100 // to spin and wait.
101 function onTryAgain() {
102 SimpleTest.executeSoon(function() {
103 //dump('onTryAgain\n');
104 mm.loadFrameScript(script2, /* allowDelayedLoad = */ false);
105 });
106 }
108 mm.addMessageListener('test-try-again', onTryAgain);
109 numPendingChildTests++;
111 onTryAgain();
112 waitForPendingTests(function() { test4(); });
113 }
115 function test4() {
116 // Navigate the iframe while an alert is pending. This shouldn't screw
117 // things up.
119 iframe.addEventListener("mozbrowsershowmodalprompt", test5);
121 var script = 'data:,content.alert("test4");';
122 mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
123 }
125 // test4 is a mozbrowsershowmodalprompt listener.
126 function test5(e) {
127 iframe.removeEventListener('mozbrowsershowmodalprompt', test4);
129 is(e.detail.message, 'test4');
130 e.preventDefault(); // cause the page to block.
132 SimpleTest.executeSoon(test5a);
133 }
135 function test5a() {
136 iframe.addEventListener('mozbrowserloadend', test5b);
137 iframe.src = browserElementTestHelpers.emptyPage2;
138 }
140 function test5b() {
141 iframe.removeEventListener('mozbrowserloadend', test5b);
142 SimpleTest.finish();
143 }
145 var prevNumPendingTests = null;
146 function waitForPendingTests(next) {
147 if (numPendingChildTests !== prevNumPendingTests) {
148 dump("Waiting for end; " + numPendingChildTests + " pending tests\n");
149 prevNumPendingTests = numPendingChildTests;
150 }
152 if (numPendingChildTests > 0) {
153 SimpleTest.executeSoon(function() { waitForPendingTests(next); });
154 return;
155 }
157 prevNumPendingTests = null;
158 next();
159 }
161 addEventListener('testready', runTest);