dom/browser-element/mochitest/browserElement_PromptConfirm.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

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 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";
    11 SimpleTest.waitForExplicitFinish();
    12 browserElementTestHelpers.setEnabledPref(true);
    13 browserElementTestHelpers.addPermission();
    15 function runTest() {
    16   var iframe = document.createElement('iframe');
    17   SpecialPowers.wrap(iframe).mozbrowser = true;
    18   document.body.appendChild(iframe);
    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'},
    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   ];
    32   iframe.addEventListener("mozbrowsershowmodalprompt", function(e) {
    33     var curPrompt = prompts[0];
    34     if (!curPrompt.waitingForResponse) {
    35       curPrompt.waitingForResponse = true;
    37       is(e.detail.message, curPrompt.msg, "prompt message");
    38       is(e.detail.promptType, curPrompt.type, "prompt type");
    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       }
    49       // Block the child until we call e.detail.unblock().
    50       e.preventDefault();
    52       SimpleTest.executeSoon(function() {
    53         e.detail.returnValue = curPrompt.rv;
    54         e.detail.unblock();
    55       });
    56     }
    57     else {
    58       prompts.shift();
    60       // |e| now corresponds to an alert() containing the return value we just
    61       // sent for this prompt.
    63       is(e.detail.message, 'RESULT:' + curPrompt.expected,
    64          "expected rv for msg " + curPrompt.msg);
    66       if (prompts.length == 0) {
    67         SimpleTest.finish();
    68       }
    69     }
    70   });
    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 }
    87 addEventListener('testready', runTest);

mercurial