dom/browser-element/mochitest/browserElement_PromptConfirm.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/browser-element/mochitest/browserElement_PromptConfirm.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     1.4 +/* Any copyright is dedicated to the public domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +// Test that prompt and confirm work.  In particular, we're concerned that we
     1.8 +// get correct return values out of them.
     1.9 +//
    1.10 +// We use alert() to communicate the return values of prompt/confirm back to
    1.11 +// ourselves.
    1.12 +"use strict";
    1.13 +
    1.14 +SimpleTest.waitForExplicitFinish();
    1.15 +browserElementTestHelpers.setEnabledPref(true);
    1.16 +browserElementTestHelpers.addPermission();
    1.17 +
    1.18 +function runTest() {
    1.19 +  var iframe = document.createElement('iframe');
    1.20 +  SpecialPowers.wrap(iframe).mozbrowser = true;
    1.21 +  document.body.appendChild(iframe);
    1.22 +
    1.23 +  var prompts = [
    1.24 +    {msg: 1, type: 'alert', rv: 42, expected: 'undefined'},
    1.25 +    {msg: 2, type: 'confirm', rv: true, expected: 'true'},
    1.26 +    {msg: 3, type: 'confirm', rv: false, expected: 'false'},
    1.27 +
    1.28 +    // rv == 42 should be coerced to 'true' for confirm.
    1.29 +    {msg: 4, type: 'confirm', rv: 42, expected: 'true'},
    1.30 +    {msg: 5, type: 'prompt', rv: 'worked', expected: 'worked'},
    1.31 +    {msg: 6, type: 'prompt', rv: null, expected: 'null'},
    1.32 +    {msg: 7, type: 'prompt', rv: '', expected: ''}
    1.33 +  ];
    1.34 +
    1.35 +  iframe.addEventListener("mozbrowsershowmodalprompt", function(e) {
    1.36 +    var curPrompt = prompts[0];
    1.37 +    if (!curPrompt.waitingForResponse) {
    1.38 +      curPrompt.waitingForResponse = true;
    1.39 +
    1.40 +      is(e.detail.message, curPrompt.msg, "prompt message");
    1.41 +      is(e.detail.promptType, curPrompt.type, "prompt type");
    1.42 +
    1.43 +      if (e.detail.promptType == 'prompt') {
    1.44 +        ok(e.detail.returnValue === null, "prompt's returnValue should be null");
    1.45 +        is(e.detail.initialValue, "initial", "prompt's initial value.");
    1.46 +      }
    1.47 +      else {
    1.48 +        ok(e.detail.returnValue === undefined,
    1.49 +           "Other than for prompt, shouldn't have initial value.");
    1.50 +      }
    1.51 +
    1.52 +      // Block the child until we call e.detail.unblock().
    1.53 +      e.preventDefault();
    1.54 +
    1.55 +      SimpleTest.executeSoon(function() {
    1.56 +        e.detail.returnValue = curPrompt.rv;
    1.57 +        e.detail.unblock();
    1.58 +      });
    1.59 +    }
    1.60 +    else {
    1.61 +      prompts.shift();
    1.62 +
    1.63 +      // |e| now corresponds to an alert() containing the return value we just
    1.64 +      // sent for this prompt.
    1.65 +
    1.66 +      is(e.detail.message, 'RESULT:' + curPrompt.expected,
    1.67 +         "expected rv for msg " + curPrompt.msg);
    1.68 +
    1.69 +      if (prompts.length == 0) {
    1.70 +        SimpleTest.finish();
    1.71 +      }
    1.72 +    }
    1.73 +  });
    1.74 +
    1.75 +  iframe.src =
    1.76 +    'data:text/html,<html><body><script>\
    1.77 +      function sendVal(val) { \
    1.78 +        alert("RESULT:" + val); \
    1.79 +      } \
    1.80 +      sendVal(alert("1")); \
    1.81 +      sendVal(confirm("2")); \
    1.82 +      sendVal(confirm("3")); \
    1.83 +      sendVal(confirm("4")); \
    1.84 +      sendVal(prompt("5", "initial")); \
    1.85 +      sendVal(prompt("6", "initial")); \
    1.86 +      sendVal(prompt("7", "initial")); \
    1.87 +    </scr' + 'ipt></body></html>';
    1.88 +}
    1.89 +
    1.90 +addEventListener('testready', runTest);

mercurial