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 dppxPref = 'layout.css.devPixelsPerPx'; michael@0: var cssPixelWidth = 600; michael@0: var cssPixelHeight = 400; michael@0: michael@0: var iframe1 = document.createElement('iframe'); michael@0: iframe1.setAttribute('width', cssPixelWidth); michael@0: iframe1.setAttribute('height', cssPixelHeight); michael@0: SpecialPowers.wrap(iframe1).mozbrowser = true; michael@0: michael@0: iframe1.src = 'data:text/html,hello'; michael@0: document.body.appendChild(iframe1); michael@0: michael@0: var images = []; michael@0: michael@0: function screenshotTaken(image) { michael@0: images.push(image); michael@0: if (images.length === 1) { michael@0: ok(true, 'Got initial non blank screenshot'); michael@0: michael@0: if (image.width !== cssPixelWidth || image.height !== cssPixelHeight) { michael@0: ok(false, 'The pixel width of the image received is not correct'); michael@0: SimpleTest.finish(); michael@0: return; michael@0: } michael@0: ok(true, 'The pixel width of the image received is correct'); michael@0: michael@0: SpecialPowers.pushPrefEnv( michael@0: {'set': [['layout.css.devPixelsPerPx', 2]]}, takeScreenshot); michael@0: } michael@0: else if (images.length === 2) { michael@0: ok(true, 'Got updated screenshot after source page changed'); michael@0: michael@0: if (image.width !== cssPixelWidth * 2 || michael@0: image.height !== cssPixelHeight * 2) { michael@0: ok(false, 'The pixel width of the 2dppx image received is not correct'); michael@0: SimpleTest.finish(); michael@0: return; michael@0: } michael@0: ok(true, 'The pixel width of the 2dppx image received is correct'); michael@0: SimpleTest.finish(); michael@0: } michael@0: } michael@0: michael@0: function takeScreenshot() { 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: screenshotTaken(this); 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(cssPixelWidth, cssPixelHeight).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.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(cssPixelWidth, cssPixelHeight).onsuccess = michael@0: getScreenshotImageData; michael@0: } michael@0: michael@0: function iframeLoadedHandler() { michael@0: SpecialPowers.pushPrefEnv( michael@0: {'set': [['layout.css.devPixelsPerPx', 1]]}, takeScreenshot); michael@0: } michael@0: michael@0: iframe1.addEventListener('mozbrowserloadend', iframeLoadedHandler); michael@0: } michael@0: michael@0: addEventListener('testready', runTest);