1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/browser-element/mochitest/browserElement_GetScreenshot.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,154 @@ 1.4 +/* Any copyright is dedicated to the public domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +// Test the getScreenshot property for mozbrowser 1.8 +"use strict"; 1.9 + 1.10 +SimpleTest.waitForExplicitFinish(); 1.11 +browserElementTestHelpers.setEnabledPref(true); 1.12 +browserElementTestHelpers.addPermission(); 1.13 + 1.14 +function runTest() { 1.15 + var iframe1 = document.createElement('iframe'); 1.16 + SpecialPowers.wrap(iframe1).mozbrowser = true; 1.17 + document.body.appendChild(iframe1); 1.18 + iframe1.src = 'data:text/html,<html>' + 1.19 + '<body style="background:green">hello</body></html>'; 1.20 + 1.21 + var screenshotImageDatas = []; 1.22 + var numLoaded = 0; 1.23 + 1.24 + function screenshotTaken(screenshotImageData) { 1.25 + screenshotImageDatas.push(screenshotImageData); 1.26 + if (screenshotImageDatas.length === 1) { 1.27 + ok(true, 'Got initial non blank screenshot'); 1.28 + 1.29 + var view = screenshotImageData.data; 1.30 + if (view[3] !== 255) { 1.31 + ok(false, 'The first pixel of initial screenshot is not opaque'); 1.32 + SimpleTest.finish(); 1.33 + return; 1.34 + } 1.35 + ok(true, 'Verified the first pixel of initial screenshot is opaque'); 1.36 + 1.37 + iframe1.src = 'data:text/html,<html>' + 1.38 + '<body style="background:transparent">hello</body></html>'; 1.39 + 1.40 + // Wait until screenshotImageData !== screenshotImageDatas[0]. 1.41 + waitForScreenshot(function(screenshotImageData) { 1.42 + var view1 = screenshotImageData.data; 1.43 + var view2 = screenshotImageDatas[0].data; 1.44 + 1.45 + if (view1.length != view2.length) { 1.46 + return true; 1.47 + } 1.48 + 1.49 + for (var i = 0; i < view1.length; i++) { 1.50 + if (view1[i] != view2[i]) { 1.51 + return true; 1.52 + } 1.53 + } 1.54 + 1.55 + return false; 1.56 + }, 'image/png'); 1.57 + } 1.58 + else if (screenshotImageDatas.length === 2) { 1.59 + ok(true, 'Got updated screenshot after source page changed'); 1.60 + 1.61 + var view = screenshotImageData.data; 1.62 + if (view[3] !== 0) { 1.63 + // The case here will always fail when oop'd on Firefox Desktop, 1.64 + // but not on B2G Emulator 1.65 + // See https://bugzil.la/878003#c20 1.66 + 1.67 + var isB2G = (navigator.platform === ''); 1.68 + info('navigator.platform: ' + navigator.platform); 1.69 + if (!isB2G && browserElementTestHelpers.getOOPByDefaultPref()) { 1.70 + todo(false, 'The first pixel of updated screenshot is not transparent'); 1.71 + } else { 1.72 + ok(false, 'The first pixel of updated screenshot is not transparent'); 1.73 + } 1.74 + SimpleTest.finish(); 1.75 + return; 1.76 + } 1.77 + 1.78 + ok(true, 'Verified the first pixel of updated screenshot is transparent'); 1.79 + SimpleTest.finish(); 1.80 + } 1.81 + } 1.82 + 1.83 + // We continually take screenshots until we get one that we are 1.84 + // happy with. 1.85 + function waitForScreenshot(filter, mimeType) { 1.86 + function gotImage(e) { 1.87 + // |this| is the Image. 1.88 + 1.89 + URL.revokeObjectURL(this.src); 1.90 + 1.91 + if (e.type === 'error' || !this.width || !this.height) { 1.92 + tryAgain(); 1.93 + 1.94 + return; 1.95 + } 1.96 + 1.97 + var canvas = document.createElement('canvas'); 1.98 + canvas.width = canvas.height = 1000; 1.99 + var ctx = canvas.getContext('2d'); 1.100 + ctx.drawImage(this, 0, 0); 1.101 + var imageData = ctx.getImageData(0, 0, 1000, 1000); 1.102 + 1.103 + if (filter(imageData)) { 1.104 + screenshotTaken(imageData); 1.105 + return; 1.106 + } 1.107 + tryAgain(); 1.108 + } 1.109 + 1.110 + function tryAgain() { 1.111 + if (--attempts === 0) { 1.112 + ok(false, 'Timed out waiting for correct screenshot'); 1.113 + SimpleTest.finish(); 1.114 + } else { 1.115 + setTimeout(function() { 1.116 + iframe1.getScreenshot(1000, 1000, mimeType).onsuccess = 1.117 + getScreenshotImageData; 1.118 + }, 200); 1.119 + } 1.120 + } 1.121 + 1.122 + function getScreenshotImageData(e) { 1.123 + var blob = e.target.result; 1.124 + if (blob.type !== mimeType) { 1.125 + ok(false, 'MIME type of screenshot taken incorrect'); 1.126 + SimpleTest.finish(); 1.127 + } 1.128 + 1.129 + if (blob.size === 0) { 1.130 + tryAgain(); 1.131 + 1.132 + return; 1.133 + } 1.134 + 1.135 + var img = new Image(); 1.136 + img.src = URL.createObjectURL(blob); 1.137 + img.onload = img.onerror = gotImage; 1.138 + } 1.139 + 1.140 + var attempts = 10; 1.141 + iframe1.getScreenshot(1000, 1000, mimeType).onsuccess = 1.142 + getScreenshotImageData; 1.143 + } 1.144 + 1.145 + function iframeLoadedHandler() { 1.146 + numLoaded++; 1.147 + if (numLoaded === 2) { 1.148 + waitForScreenshot(function(screenshotImageData) { 1.149 + return true; 1.150 + }, 'image/jpeg'); 1.151 + } 1.152 + } 1.153 + 1.154 + iframe1.addEventListener('mozbrowserloadend', iframeLoadedHandler); 1.155 +} 1.156 + 1.157 +addEventListener('testready', runTest);