dom/browser-element/mochitest/browserElement_GetScreenshot.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 the getScreenshot property for mozbrowser
     5 "use strict";
     7 SimpleTest.waitForExplicitFinish();
     8 browserElementTestHelpers.setEnabledPref(true);
     9 browserElementTestHelpers.addPermission();
    11 function runTest() {
    12   var iframe1 = document.createElement('iframe');
    13   SpecialPowers.wrap(iframe1).mozbrowser = true;
    14   document.body.appendChild(iframe1);
    15   iframe1.src = 'data:text/html,<html>' +
    16     '<body style="background:green">hello</body></html>';
    18   var screenshotImageDatas = [];
    19   var numLoaded = 0;
    21   function screenshotTaken(screenshotImageData) {
    22     screenshotImageDatas.push(screenshotImageData);
    23     if (screenshotImageDatas.length === 1) {
    24       ok(true, 'Got initial non blank screenshot');
    26       var view = screenshotImageData.data;
    27       if (view[3] !== 255) {
    28         ok(false, 'The first pixel of initial screenshot is not opaque');
    29         SimpleTest.finish();
    30         return;
    31       }
    32       ok(true, 'Verified the first pixel of initial screenshot is opaque');
    34       iframe1.src = 'data:text/html,<html>' +
    35         '<body style="background:transparent">hello</body></html>';
    37       // Wait until screenshotImageData !== screenshotImageDatas[0].
    38       waitForScreenshot(function(screenshotImageData) {
    39         var view1 = screenshotImageData.data;
    40         var view2 = screenshotImageDatas[0].data;
    42         if (view1.length != view2.length) {
    43           return true;
    44         }
    46         for (var i = 0; i < view1.length; i++) {
    47           if (view1[i] != view2[i]) {
    48             return true;
    49           }
    50         }
    52         return false;
    53       }, 'image/png');
    54     }
    55     else if (screenshotImageDatas.length === 2) {
    56       ok(true, 'Got updated screenshot after source page changed');
    58       var view = screenshotImageData.data;
    59       if (view[3] !== 0) {
    60         // The case here will always fail when oop'd on Firefox Desktop,
    61         // but not on B2G Emulator
    62         // See https://bugzil.la/878003#c20
    64         var isB2G = (navigator.platform === '');
    65         info('navigator.platform: ' + navigator.platform);
    66         if (!isB2G && browserElementTestHelpers.getOOPByDefaultPref()) {
    67           todo(false, 'The first pixel of updated screenshot is not transparent');
    68         } else {
    69           ok(false, 'The first pixel of updated screenshot is not transparent');
    70         }
    71         SimpleTest.finish();
    72         return;
    73       }
    75       ok(true, 'Verified the first pixel of updated screenshot is transparent');
    76       SimpleTest.finish();
    77     }
    78   }
    80   // We continually take screenshots until we get one that we are
    81   // happy with.
    82   function waitForScreenshot(filter, mimeType) {
    83     function gotImage(e) {
    84       // |this| is the Image.
    86       URL.revokeObjectURL(this.src);
    88       if (e.type === 'error' || !this.width || !this.height) {
    89         tryAgain();
    91         return;
    92       }
    94       var canvas = document.createElement('canvas');
    95       canvas.width = canvas.height = 1000;
    96       var ctx = canvas.getContext('2d');
    97       ctx.drawImage(this, 0, 0);
    98       var imageData = ctx.getImageData(0, 0, 1000, 1000);
   100       if (filter(imageData)) {
   101         screenshotTaken(imageData);
   102         return;
   103       }
   104       tryAgain();
   105     }
   107     function tryAgain() {
   108       if (--attempts === 0) {
   109         ok(false, 'Timed out waiting for correct screenshot');
   110         SimpleTest.finish();
   111       } else {
   112         setTimeout(function() {
   113           iframe1.getScreenshot(1000, 1000, mimeType).onsuccess =
   114             getScreenshotImageData;
   115         }, 200);
   116       }
   117     }
   119     function getScreenshotImageData(e) {
   120       var blob = e.target.result;
   121       if (blob.type !== mimeType) {
   122         ok(false, 'MIME type of screenshot taken incorrect');
   123         SimpleTest.finish();
   124       }
   126       if (blob.size === 0) {
   127         tryAgain();
   129         return;
   130       }
   132       var img = new Image();
   133       img.src = URL.createObjectURL(blob);
   134       img.onload = img.onerror = gotImage;
   135     }
   137     var attempts = 10;
   138     iframe1.getScreenshot(1000, 1000, mimeType).onsuccess =
   139       getScreenshotImageData;
   140   }
   142   function iframeLoadedHandler() {
   143     numLoaded++;
   144     if (numLoaded === 2) {
   145       waitForScreenshot(function(screenshotImageData) {
   146         return true;
   147       }, 'image/jpeg');
   148     }
   149   }
   151   iframe1.addEventListener('mozbrowserloadend', iframeLoadedHandler);
   152 }
   154 addEventListener('testready', runTest);

mercurial