michael@0: /* Any copyright is dedicated to the public domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Test the getScreenshot property for mozbrowser michael@0: "use strict"; michael@0: michael@0: SimpleTest.waitForExplicitFinish(); michael@0: browserElementTestHelpers.setEnabledPref(true); michael@0: browserElementTestHelpers.addPermission(); michael@0: michael@0: function runTest() { michael@0: var iframe1 = document.createElement('iframe'); michael@0: SpecialPowers.wrap(iframe1).mozbrowser = true; michael@0: document.body.appendChild(iframe1); michael@0: iframe1.src = 'data:text/html,' + michael@0: 'hello'; michael@0: michael@0: var screenshotImageDatas = []; michael@0: var numLoaded = 0; michael@0: michael@0: function screenshotTaken(screenshotImageData) { michael@0: screenshotImageDatas.push(screenshotImageData); michael@0: if (screenshotImageDatas.length === 1) { michael@0: ok(true, 'Got initial non blank screenshot'); michael@0: michael@0: var view = screenshotImageData.data; michael@0: if (view[3] !== 255) { michael@0: ok(false, 'The first pixel of initial screenshot is not opaque'); michael@0: SimpleTest.finish(); michael@0: return; michael@0: } michael@0: ok(true, 'Verified the first pixel of initial screenshot is opaque'); michael@0: michael@0: iframe1.src = 'data:text/html,' + michael@0: 'hello'; michael@0: michael@0: // Wait until screenshotImageData !== screenshotImageDatas[0]. michael@0: waitForScreenshot(function(screenshotImageData) { michael@0: var view1 = screenshotImageData.data; michael@0: var view2 = screenshotImageDatas[0].data; michael@0: michael@0: if (view1.length != view2.length) { michael@0: return true; michael@0: } michael@0: michael@0: for (var i = 0; i < view1.length; i++) { michael@0: if (view1[i] != view2[i]) { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: }, 'image/png'); michael@0: } michael@0: else if (screenshotImageDatas.length === 2) { michael@0: ok(true, 'Got updated screenshot after source page changed'); michael@0: michael@0: var view = screenshotImageData.data; michael@0: if (view[3] !== 0) { michael@0: // The case here will always fail when oop'd on Firefox Desktop, michael@0: // but not on B2G Emulator michael@0: // See https://bugzil.la/878003#c20 michael@0: michael@0: var isB2G = (navigator.platform === ''); michael@0: info('navigator.platform: ' + navigator.platform); michael@0: if (!isB2G && browserElementTestHelpers.getOOPByDefaultPref()) { michael@0: todo(false, 'The first pixel of updated screenshot is not transparent'); michael@0: } else { michael@0: ok(false, 'The first pixel of updated screenshot is not transparent'); michael@0: } michael@0: SimpleTest.finish(); michael@0: return; michael@0: } michael@0: michael@0: ok(true, 'Verified the first pixel of updated screenshot is transparent'); michael@0: SimpleTest.finish(); michael@0: } michael@0: } michael@0: michael@0: // We continually take screenshots until we get one that we are michael@0: // happy with. michael@0: function waitForScreenshot(filter, mimeType) { michael@0: function gotImage(e) { michael@0: // |this| is the Image. michael@0: michael@0: URL.revokeObjectURL(this.src); michael@0: michael@0: if (e.type === 'error' || !this.width || !this.height) { michael@0: tryAgain(); michael@0: michael@0: return; michael@0: } michael@0: michael@0: var canvas = document.createElement('canvas'); michael@0: canvas.width = canvas.height = 1000; michael@0: var ctx = canvas.getContext('2d'); michael@0: ctx.drawImage(this, 0, 0); michael@0: var imageData = ctx.getImageData(0, 0, 1000, 1000); michael@0: michael@0: if (filter(imageData)) { michael@0: screenshotTaken(imageData); michael@0: return; michael@0: } michael@0: tryAgain(); michael@0: } michael@0: michael@0: function tryAgain() { michael@0: if (--attempts === 0) { michael@0: ok(false, 'Timed out waiting for correct screenshot'); michael@0: SimpleTest.finish(); michael@0: } else { michael@0: setTimeout(function() { michael@0: iframe1.getScreenshot(1000, 1000, mimeType).onsuccess = michael@0: getScreenshotImageData; michael@0: }, 200); michael@0: } michael@0: } michael@0: michael@0: function getScreenshotImageData(e) { michael@0: var blob = e.target.result; michael@0: if (blob.type !== mimeType) { michael@0: ok(false, 'MIME type of screenshot taken incorrect'); michael@0: SimpleTest.finish(); michael@0: } michael@0: michael@0: if (blob.size === 0) { michael@0: tryAgain(); michael@0: michael@0: return; michael@0: } michael@0: michael@0: var img = new Image(); michael@0: img.src = URL.createObjectURL(blob); michael@0: img.onload = img.onerror = gotImage; michael@0: } michael@0: michael@0: var attempts = 10; michael@0: iframe1.getScreenshot(1000, 1000, mimeType).onsuccess = michael@0: getScreenshotImageData; michael@0: } michael@0: michael@0: function iframeLoadedHandler() { michael@0: numLoaded++; michael@0: if (numLoaded === 2) { michael@0: waitForScreenshot(function(screenshotImageData) { michael@0: return true; michael@0: }, 'image/jpeg'); michael@0: } michael@0: } michael@0: michael@0: iframe1.addEventListener('mozbrowserloadend', iframeLoadedHandler); michael@0: } michael@0: michael@0: addEventListener('testready', runTest);