dom/browser-element/mochitest/browserElement_BadScreenshot.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/browser-element/mochitest/browserElement_BadScreenshot.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,72 @@
     1.4 +/* Any copyright is dedicated to the public domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +// Bug 800170 - Test that we get errors when we pass bad arguments to
     1.8 +// mozbrowser's getScreenshot.
     1.9 +"use strict";
    1.10 +
    1.11 +SimpleTest.waitForExplicitFinish();
    1.12 +browserElementTestHelpers.setEnabledPref(true);
    1.13 +browserElementTestHelpers.addPermission();
    1.14 +
    1.15 +var iframe;
    1.16 +var numPendingTests = 0;
    1.17 +
    1.18 +// Call iframe.getScreenshot with the given args.  If expectSuccess is true, we
    1.19 +// expect the screenshot's onsuccess handler to fire.  Otherwise, we expect
    1.20 +// getScreenshot() to throw an exception.
    1.21 +function checkScreenshotResult(expectSuccess, args) {
    1.22 +  var req;
    1.23 +  try {
    1.24 +    req = iframe.getScreenshot.apply(iframe, args);
    1.25 +  }
    1.26 +  catch(e) {
    1.27 +    ok(!expectSuccess, "getScreenshot(" + JSON.stringify(args) + ") threw an exception.");
    1.28 +    return;
    1.29 +  }
    1.30 +
    1.31 +  numPendingTests++;
    1.32 +  req.onsuccess = function() {
    1.33 +    ok(expectSuccess, "getScreenshot(" + JSON.stringify(args) + ") succeeded.");
    1.34 +    numPendingTests--;
    1.35 +    if (numPendingTests == 0) {
    1.36 +      SimpleTest.finish();
    1.37 +    }
    1.38 +  };
    1.39 +
    1.40 +  // We never expect to see onerror.
    1.41 +  req.onerror = function() {
    1.42 +    ok(false, "getScreenshot(" + JSON.stringify(args) + ") ran onerror.");
    1.43 +    numPendingTests--;
    1.44 +    if (numPendingTests == 0) {
    1.45 +      SimpleTest.finish();
    1.46 +    }
    1.47 +  };
    1.48 +}
    1.49 +
    1.50 +function runTest() {
    1.51 +  iframe = document.createElement('iframe');
    1.52 +  SpecialPowers.wrap(iframe).mozbrowser = true;
    1.53 +  document.body.appendChild(iframe);
    1.54 +  iframe.src = 'data:text/html,<html>' +
    1.55 +    '<body style="background:green">hello</body></html>';
    1.56 +
    1.57 +  iframe.addEventListener('mozbrowserfirstpaint', function() {
    1.58 +    // This one should succeed.
    1.59 +    checkScreenshotResult(true, [100, 100]);
    1.60 +
    1.61 +    // These should fail.
    1.62 +    checkScreenshotResult(false, []);
    1.63 +    checkScreenshotResult(false, [100]);
    1.64 +    checkScreenshotResult(false, ['a', 100]);
    1.65 +    checkScreenshotResult(false, [100, 'a']);
    1.66 +    checkScreenshotResult(false, [-1, 100]);
    1.67 +    checkScreenshotResult(false, [100, -1]);
    1.68 +
    1.69 +    if (numPendingTests == 0) {
    1.70 +      SimpleTest.finish();
    1.71 +    }
    1.72 +  });
    1.73 +}
    1.74 +
    1.75 +addEventListener('testready', runTest);

mercurial