summaryrefslogtreecommitdiffstats
path: root/dom/browser-element/mochitest/browserElement_Alert.js
blob: 3c7c507205e854f507861498bede31053f14835f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* Any copyright is dedicated to the public domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test that alert works.
"use strict";

SimpleTest.waitForExplicitFinish();
browserElementTestHelpers.setEnabledPref(true);
browserElementTestHelpers.addPermission();

var numPendingChildTests = 0;
var iframe;
var mm;

function runTest() {
  iframe = document.createElement('iframe');
  iframe.setAttribute('mozbrowser', 'true');
  document.body.appendChild(iframe);

  mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
  mm.addMessageListener('test-success', function(msg) {
    numPendingChildTests--;
    ok(true, SpecialPowers.wrap(msg).json);
  });
  mm.addMessageListener('test-fail', function(msg) {
    numPendingChildTests--;
    ok(false, SpecialPowers.wrap(msg).json);
  });

  // Wait for the initial load to finish, then navigate the page, then wait
  // for that load to finish, then start test1.
  iframe.addEventListener('mozbrowserloadend', function loadend() {
    iframe.removeEventListener('mozbrowserloadend', loadend);
    iframe.src = browserElementTestHelpers.emptyPage1;

    iframe.addEventListener('mozbrowserloadend', function loadend2() {
      iframe.removeEventListener('mozbrowserloadend', loadend2);
      SimpleTest.executeSoon(test1);
    });
  });

}

function test1() {
  iframe.addEventListener('mozbrowsershowmodalprompt', test2);

  // Do window.alert within the iframe, then modify the global |testState|
  // after the alert.
  var script = 'data:,\
    this.testState = 0; \
    content.alert("Hello, world!"); \
    this.testState = 1; \
  ';

  mm.loadFrameScript(script, /* allowDelayedLoad = */ false);

  // Triggers a mozbrowsershowmodalprompt event, which sends us down to test2.
}

// test2 is a mozbrowsershowmodalprompt listener.
function test2(e) {
  iframe.removeEventListener("mozbrowsershowmodalprompt", test2);

  is(e.detail.message, 'Hello, world!');
  e.preventDefault(); // cause the alert to block.

  SimpleTest.executeSoon(function() { test2a(e); });
}

function test2a(e) {
  // The iframe should be blocked on the alert call at the moment, so testState
  // should still be 0.
  var script = 'data:,\
    if (this.testState === 0) { \
      sendAsyncMessage("test-success", "1: Correct testState"); \
    } \
    else { \
      sendAsyncMessage("test-fail", "1: Wrong testState: " + this.testState); \
    }';

  mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
  numPendingChildTests++;

  waitForPendingTests(function() { test3(e); });
}

function test3(e) {
  // Now unblock the iframe and check that the script completed.
  e.detail.unblock();

  var script2 = 'data:,\
    if (this.testState === 1) { \
      sendAsyncMessage("test-success", "2: Correct testState"); \
    } \
    else { \
      sendAsyncMessage("test-try-again", "2: Wrong testState (for now): " + this.testState); \
    }';

  // Urgh.  e.unblock() didn't necessarily unblock us immediately, so we have
  // to spin and wait.
  function onTryAgain() {
    SimpleTest.executeSoon(function() {
      //dump('onTryAgain\n');
      mm.loadFrameScript(script2, /* allowDelayedLoad = */ false);
    });
  }

  mm.addMessageListener('test-try-again', onTryAgain);
  numPendingChildTests++;

  onTryAgain();
  waitForPendingTests(function() {
    mm.removeMessageListener('test-try-again', onTryAgain);
    test4();
  });
}

function test4() {
  // Navigate the iframe while an alert is pending.  This shouldn't screw
  // things up.

  iframe.addEventListener("mozbrowsershowmodalprompt", test5);

  var script = 'data:,content.alert("test4");';
  mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
}

// test4 is a mozbrowsershowmodalprompt listener.
function test5(e) {
  iframe.removeEventListener('mozbrowsershowmodalprompt', test5);

  is(e.detail.message, 'test4');
  e.preventDefault(); // cause the page to block.

  SimpleTest.executeSoon(test5a);
}

function test5a() {
  iframe.addEventListener('mozbrowserloadend', test5b);
  iframe.src = browserElementTestHelpers.emptyPage2;
}

function test5b() {
  iframe.removeEventListener('mozbrowserloadend', test5b);
  SimpleTest.executeSoon(test6);
}

// Test nested alerts
var promptBlockers = [];
function test6() {
  iframe.addEventListener("mozbrowsershowmodalprompt", test6a);

  var script = 'data:,\
    this.testState = 0; \
    content.alert(1); \
    this.testState = 3; \
  ';
  mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
}

function test6a(e) {
  iframe.removeEventListener("mozbrowsershowmodalprompt", test6a);

  is(e.detail.message, '1');
  e.preventDefault(); // cause the alert to block.
  promptBlockers.push(e);

  SimpleTest.executeSoon(test6b);
}

function test6b() {
  var script = 'data:,\
    if (this.testState === 0) { \
      sendAsyncMessage("test-success", "1: Correct testState"); \
    } \
    else { \
      sendAsyncMessage("test-fail", "1: Wrong testState: " + this.testState); \
    }';
  mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
  numPendingChildTests++;

  waitForPendingTests(test6c);
}

function test6c() {
  iframe.addEventListener("mozbrowsershowmodalprompt", test6d);

  var script = 'data:,\
    this.testState = 1; \
    content.alert(2); \
    this.testState = 2; \
  ';
  mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
}

function test6d(e) {
  iframe.removeEventListener("mozbrowsershowmodalprompt", test6d);

  is(e.detail.message, '2');
  e.preventDefault(); // cause the alert to block.
  promptBlockers.push(e);

  SimpleTest.executeSoon(test6e);
}

function test6e() {
  var script = 'data:,\
    if (this.testState === 1) { \
      sendAsyncMessage("test-success", "2: Correct testState"); \
    } \
    else { \
      sendAsyncMessage("test-fail", "2: Wrong testState: " + this.testState); \
    }';
  mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
  numPendingChildTests++;

  waitForPendingTests(test6f);
}

function test6f() {
  var e = promptBlockers.pop();
  // Now unblock the iframe and check that the script completed.
  e.detail.unblock();

  var script2 = 'data:,\
    if (this.testState === 2) { \
      sendAsyncMessage("test-success", "3: Correct testState"); \
    } \
    else { \
      sendAsyncMessage("test-try-again", "3: Wrong testState (for now): " + this.testState); \
    }';

  // Urgh.  e.unblock() didn't necessarily unblock us immediately, so we have
  // to spin and wait.
  function onTryAgain() {
    SimpleTest.executeSoon(function() {
      //dump('onTryAgain\n');
      mm.loadFrameScript(script2, /* allowDelayedLoad = */ false);
    });
  }

  mm.addMessageListener('test-try-again', onTryAgain);
  numPendingChildTests++;

  onTryAgain();
  waitForPendingTests(function() {
    mm.removeMessageListener('test-try-again', onTryAgain);
    test6g();
  });
}

function test6g() {
  var e = promptBlockers.pop();
  // Now unblock the iframe and check that the script completed.
  e.detail.unblock();

  var script2 = 'data:,\
    if (this.testState === 3) { \
      sendAsyncMessage("test-success", "4: Correct testState"); \
    } \
    else { \
      sendAsyncMessage("test-try-again", "4: Wrong testState (for now): " + this.testState); \
    }';

  // Urgh.  e.unblock() didn't necessarily unblock us immediately, so we have
  // to spin and wait.
  function onTryAgain() {
    SimpleTest.executeSoon(function() {
      //dump('onTryAgain\n');
      mm.loadFrameScript(script2, /* allowDelayedLoad = */ false);
    });
  }

  mm.addMessageListener('test-try-again', onTryAgain);
  numPendingChildTests++;

  onTryAgain();
  waitForPendingTests(function() {
    mm.removeMessageListener('test-try-again', onTryAgain);
    test6h();
  });
}

function test6h() {
  SimpleTest.finish();
}

var prevNumPendingTests = null;
function waitForPendingTests(next) {
  if (numPendingChildTests !== prevNumPendingTests) {
    dump("Waiting for end; " + numPendingChildTests + " pending tests\n");
    prevNumPendingTests = numPendingChildTests;
  }

  if (numPendingChildTests > 0) {
    SimpleTest.executeSoon(function() { waitForPendingTests(next); });
    return;
  }

  prevNumPendingTests = null;
  next();
}

addEventListener('testready', runTest);