dom/browser-element/mochitest/browserElement_GetScreenshotDppx.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 /* Any copyright is dedicated to the public domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 // Test the getScreenshot property for mozbrowser
     5 "use strict";
     7 SimpleTest.waitForExplicitFinish();
     8 browserElementTestHelpers.setEnabledPref(true);
     9 browserElementTestHelpers.addPermission();
    11 function runTest() {
    12   var dppxPref = 'layout.css.devPixelsPerPx';
    13   var cssPixelWidth = 600;
    14   var cssPixelHeight = 400;
    16   var iframe1 = document.createElement('iframe');
    17   iframe1.setAttribute('width', cssPixelWidth);
    18   iframe1.setAttribute('height', cssPixelHeight);
    19   SpecialPowers.wrap(iframe1).mozbrowser = true;
    21   iframe1.src = 'data:text/html,<html><body>hello</body></html>';
    22   document.body.appendChild(iframe1);
    24   var images = [];
    26   function screenshotTaken(image) {
    27     images.push(image);
    28     if (images.length === 1) {
    29       ok(true, 'Got initial non blank screenshot');
    31       if (image.width !== cssPixelWidth || image.height !== cssPixelHeight) {
    32         ok(false, 'The pixel width of the image received is not correct');
    33         SimpleTest.finish();
    34         return;
    35       }
    36       ok(true, 'The pixel width of the image received is correct');
    38       SpecialPowers.pushPrefEnv(
    39         {'set': [['layout.css.devPixelsPerPx', 2]]}, takeScreenshot);
    40     }
    41     else if (images.length === 2) {
    42       ok(true, 'Got updated screenshot after source page changed');
    44       if (image.width !== cssPixelWidth * 2 ||
    45           image.height !== cssPixelHeight * 2) {
    46         ok(false, 'The pixel width of the 2dppx image received is not correct');
    47         SimpleTest.finish();
    48         return;
    49       }
    50       ok(true, 'The pixel width of the 2dppx image received is correct');
    51       SimpleTest.finish();
    52     }
    53   }
    55   function takeScreenshot() {
    56     function gotImage(e) {
    57       // |this| is the Image.
    59       URL.revokeObjectURL(this.src);
    61       if (e.type === 'error' || !this.width || !this.height) {
    62         tryAgain();
    64         return;
    65       }
    67       screenshotTaken(this);
    68     }
    70     function tryAgain() {
    71       if (--attempts === 0) {
    72         ok(false, 'Timed out waiting for correct screenshot');
    73         SimpleTest.finish();
    74       } else {
    75         setTimeout(function() {
    76           iframe1.getScreenshot(cssPixelWidth, cssPixelHeight).onsuccess =
    77             getScreenshotImageData;
    78         }, 200);
    79       }
    80     }
    82     function getScreenshotImageData(e) {
    83       var blob = e.target.result;
    84       if (blob.size === 0) {
    85         tryAgain();
    87         return;
    88       }
    90       var img = new Image();
    91       img.src = URL.createObjectURL(blob);
    92       img.onload = img.onerror = gotImage;
    93     }
    95     var attempts = 10;
    96     iframe1.getScreenshot(cssPixelWidth, cssPixelHeight).onsuccess =
    97       getScreenshotImageData;
    98   }
   100   function iframeLoadedHandler() {
   101     SpecialPowers.pushPrefEnv(
   102       {'set': [['layout.css.devPixelsPerPx', 1]]}, takeScreenshot);
   103   }
   105   iframe1.addEventListener('mozbrowserloadend', iframeLoadedHandler);
   106 }
   108 addEventListener('testready', runTest);

mercurial