1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/browser-element/mochitest/browserElement_GetScreenshotDppx.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 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 dppxPref = 'layout.css.devPixelsPerPx'; 1.16 + var cssPixelWidth = 600; 1.17 + var cssPixelHeight = 400; 1.18 + 1.19 + var iframe1 = document.createElement('iframe'); 1.20 + iframe1.setAttribute('width', cssPixelWidth); 1.21 + iframe1.setAttribute('height', cssPixelHeight); 1.22 + SpecialPowers.wrap(iframe1).mozbrowser = true; 1.23 + 1.24 + iframe1.src = 'data:text/html,<html><body>hello</body></html>'; 1.25 + document.body.appendChild(iframe1); 1.26 + 1.27 + var images = []; 1.28 + 1.29 + function screenshotTaken(image) { 1.30 + images.push(image); 1.31 + if (images.length === 1) { 1.32 + ok(true, 'Got initial non blank screenshot'); 1.33 + 1.34 + if (image.width !== cssPixelWidth || image.height !== cssPixelHeight) { 1.35 + ok(false, 'The pixel width of the image received is not correct'); 1.36 + SimpleTest.finish(); 1.37 + return; 1.38 + } 1.39 + ok(true, 'The pixel width of the image received is correct'); 1.40 + 1.41 + SpecialPowers.pushPrefEnv( 1.42 + {'set': [['layout.css.devPixelsPerPx', 2]]}, takeScreenshot); 1.43 + } 1.44 + else if (images.length === 2) { 1.45 + ok(true, 'Got updated screenshot after source page changed'); 1.46 + 1.47 + if (image.width !== cssPixelWidth * 2 || 1.48 + image.height !== cssPixelHeight * 2) { 1.49 + ok(false, 'The pixel width of the 2dppx image received is not correct'); 1.50 + SimpleTest.finish(); 1.51 + return; 1.52 + } 1.53 + ok(true, 'The pixel width of the 2dppx image received is correct'); 1.54 + SimpleTest.finish(); 1.55 + } 1.56 + } 1.57 + 1.58 + function takeScreenshot() { 1.59 + function gotImage(e) { 1.60 + // |this| is the Image. 1.61 + 1.62 + URL.revokeObjectURL(this.src); 1.63 + 1.64 + if (e.type === 'error' || !this.width || !this.height) { 1.65 + tryAgain(); 1.66 + 1.67 + return; 1.68 + } 1.69 + 1.70 + screenshotTaken(this); 1.71 + } 1.72 + 1.73 + function tryAgain() { 1.74 + if (--attempts === 0) { 1.75 + ok(false, 'Timed out waiting for correct screenshot'); 1.76 + SimpleTest.finish(); 1.77 + } else { 1.78 + setTimeout(function() { 1.79 + iframe1.getScreenshot(cssPixelWidth, cssPixelHeight).onsuccess = 1.80 + getScreenshotImageData; 1.81 + }, 200); 1.82 + } 1.83 + } 1.84 + 1.85 + function getScreenshotImageData(e) { 1.86 + var blob = e.target.result; 1.87 + if (blob.size === 0) { 1.88 + tryAgain(); 1.89 + 1.90 + return; 1.91 + } 1.92 + 1.93 + var img = new Image(); 1.94 + img.src = URL.createObjectURL(blob); 1.95 + img.onload = img.onerror = gotImage; 1.96 + } 1.97 + 1.98 + var attempts = 10; 1.99 + iframe1.getScreenshot(cssPixelWidth, cssPixelHeight).onsuccess = 1.100 + getScreenshotImageData; 1.101 + } 1.102 + 1.103 + function iframeLoadedHandler() { 1.104 + SpecialPowers.pushPrefEnv( 1.105 + {'set': [['layout.css.devPixelsPerPx', 1]]}, takeScreenshot); 1.106 + } 1.107 + 1.108 + iframe1.addEventListener('mozbrowserloadend', iframeLoadedHandler); 1.109 +} 1.110 + 1.111 +addEventListener('testready', runTest);