dom/browser-element/mochitest/browserElement_PromptConfirm.js

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:61f241cbd8b1
1 /* Any copyright is dedicated to the public domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 // Test that prompt and confirm work. In particular, we're concerned that we
5 // get correct return values out of them.
6 //
7 // We use alert() to communicate the return values of prompt/confirm back to
8 // ourselves.
9 "use strict";
10
11 SimpleTest.waitForExplicitFinish();
12 browserElementTestHelpers.setEnabledPref(true);
13 browserElementTestHelpers.addPermission();
14
15 function runTest() {
16 var iframe = document.createElement('iframe');
17 SpecialPowers.wrap(iframe).mozbrowser = true;
18 document.body.appendChild(iframe);
19
20 var prompts = [
21 {msg: 1, type: 'alert', rv: 42, expected: 'undefined'},
22 {msg: 2, type: 'confirm', rv: true, expected: 'true'},
23 {msg: 3, type: 'confirm', rv: false, expected: 'false'},
24
25 // rv == 42 should be coerced to 'true' for confirm.
26 {msg: 4, type: 'confirm', rv: 42, expected: 'true'},
27 {msg: 5, type: 'prompt', rv: 'worked', expected: 'worked'},
28 {msg: 6, type: 'prompt', rv: null, expected: 'null'},
29 {msg: 7, type: 'prompt', rv: '', expected: ''}
30 ];
31
32 iframe.addEventListener("mozbrowsershowmodalprompt", function(e) {
33 var curPrompt = prompts[0];
34 if (!curPrompt.waitingForResponse) {
35 curPrompt.waitingForResponse = true;
36
37 is(e.detail.message, curPrompt.msg, "prompt message");
38 is(e.detail.promptType, curPrompt.type, "prompt type");
39
40 if (e.detail.promptType == 'prompt') {
41 ok(e.detail.returnValue === null, "prompt's returnValue should be null");
42 is(e.detail.initialValue, "initial", "prompt's initial value.");
43 }
44 else {
45 ok(e.detail.returnValue === undefined,
46 "Other than for prompt, shouldn't have initial value.");
47 }
48
49 // Block the child until we call e.detail.unblock().
50 e.preventDefault();
51
52 SimpleTest.executeSoon(function() {
53 e.detail.returnValue = curPrompt.rv;
54 e.detail.unblock();
55 });
56 }
57 else {
58 prompts.shift();
59
60 // |e| now corresponds to an alert() containing the return value we just
61 // sent for this prompt.
62
63 is(e.detail.message, 'RESULT:' + curPrompt.expected,
64 "expected rv for msg " + curPrompt.msg);
65
66 if (prompts.length == 0) {
67 SimpleTest.finish();
68 }
69 }
70 });
71
72 iframe.src =
73 'data:text/html,<html><body><script>\
74 function sendVal(val) { \
75 alert("RESULT:" + val); \
76 } \
77 sendVal(alert("1")); \
78 sendVal(confirm("2")); \
79 sendVal(confirm("3")); \
80 sendVal(confirm("4")); \
81 sendVal(prompt("5", "initial")); \
82 sendVal(prompt("6", "initial")); \
83 sendVal(prompt("7", "initial")); \
84 </scr' + 'ipt></body></html>';
85 }
86
87 addEventListener('testready', runTest);

mercurial