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