dom/browser-element/mochitest/browserElement_Alert.js

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:b538c3c0e78a
1 /* Any copyright is dedicated to the public domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 // Test that alert works.
5 "use strict";
6
7 SimpleTest.waitForExplicitFinish();
8 browserElementTestHelpers.setEnabledPref(true);
9 browserElementTestHelpers.addPermission();
10
11 var numPendingChildTests = 0;
12 var iframe;
13 var mm;
14
15 function runTest() {
16 iframe = document.createElement('iframe');
17 SpecialPowers.wrap(iframe).mozbrowser = true;
18 document.body.appendChild(iframe);
19
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 });
29
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;
35
36 iframe.addEventListener('mozbrowserloadend', function loadend2() {
37 iframe.removeEventListener('mozbrowserloadend', loadend2);
38 SimpleTest.executeSoon(test1);
39 });
40 });
41
42 }
43
44 function test1() {
45 iframe.addEventListener('mozbrowsershowmodalprompt', test2);
46
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 ';
54
55 mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
56
57 // Triggers a mozbrowsershowmodalprompt event, which sends us down to test2.
58 }
59
60 // test2 is a mozbrowsershowmodalprompt listener.
61 function test2(e) {
62 iframe.removeEventListener("mozbrowsershowmodalprompt", test2);
63
64 is(e.detail.message, 'Hello, world!');
65 e.preventDefault(); // cause the alert to block.
66
67 SimpleTest.executeSoon(function() { test2a(e); });
68 }
69
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 }';
80
81 mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
82 numPendingChildTests++;
83
84 waitForPendingTests(function() { test3(e); });
85 }
86
87 function test3(e) {
88 // Now unblock the iframe and check that the script completed.
89 e.detail.unblock();
90
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 }';
98
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 }
107
108 mm.addMessageListener('test-try-again', onTryAgain);
109 numPendingChildTests++;
110
111 onTryAgain();
112 waitForPendingTests(function() { test4(); });
113 }
114
115 function test4() {
116 // Navigate the iframe while an alert is pending. This shouldn't screw
117 // things up.
118
119 iframe.addEventListener("mozbrowsershowmodalprompt", test5);
120
121 var script = 'data:,content.alert("test4");';
122 mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
123 }
124
125 // test4 is a mozbrowsershowmodalprompt listener.
126 function test5(e) {
127 iframe.removeEventListener('mozbrowsershowmodalprompt', test4);
128
129 is(e.detail.message, 'test4');
130 e.preventDefault(); // cause the page to block.
131
132 SimpleTest.executeSoon(test5a);
133 }
134
135 function test5a() {
136 iframe.addEventListener('mozbrowserloadend', test5b);
137 iframe.src = browserElementTestHelpers.emptyPage2;
138 }
139
140 function test5b() {
141 iframe.removeEventListener('mozbrowserloadend', test5b);
142 SimpleTest.finish();
143 }
144
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 }
151
152 if (numPendingChildTests > 0) {
153 SimpleTest.executeSoon(function() { waitForPendingTests(next); });
154 return;
155 }
156
157 prevNumPendingTests = null;
158 next();
159 }
160
161 addEventListener('testready', runTest);

mercurial