dom/browser-element/mochitest/browserElement_GetScreenshotDppx.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

michael@0 1 /* Any copyright is dedicated to the public domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 // Test the getScreenshot property for mozbrowser
michael@0 5 "use strict";
michael@0 6
michael@0 7 SimpleTest.waitForExplicitFinish();
michael@0 8 browserElementTestHelpers.setEnabledPref(true);
michael@0 9 browserElementTestHelpers.addPermission();
michael@0 10
michael@0 11 function runTest() {
michael@0 12 var dppxPref = 'layout.css.devPixelsPerPx';
michael@0 13 var cssPixelWidth = 600;
michael@0 14 var cssPixelHeight = 400;
michael@0 15
michael@0 16 var iframe1 = document.createElement('iframe');
michael@0 17 iframe1.setAttribute('width', cssPixelWidth);
michael@0 18 iframe1.setAttribute('height', cssPixelHeight);
michael@0 19 SpecialPowers.wrap(iframe1).mozbrowser = true;
michael@0 20
michael@0 21 iframe1.src = 'data:text/html,<html><body>hello</body></html>';
michael@0 22 document.body.appendChild(iframe1);
michael@0 23
michael@0 24 var images = [];
michael@0 25
michael@0 26 function screenshotTaken(image) {
michael@0 27 images.push(image);
michael@0 28 if (images.length === 1) {
michael@0 29 ok(true, 'Got initial non blank screenshot');
michael@0 30
michael@0 31 if (image.width !== cssPixelWidth || image.height !== cssPixelHeight) {
michael@0 32 ok(false, 'The pixel width of the image received is not correct');
michael@0 33 SimpleTest.finish();
michael@0 34 return;
michael@0 35 }
michael@0 36 ok(true, 'The pixel width of the image received is correct');
michael@0 37
michael@0 38 SpecialPowers.pushPrefEnv(
michael@0 39 {'set': [['layout.css.devPixelsPerPx', 2]]}, takeScreenshot);
michael@0 40 }
michael@0 41 else if (images.length === 2) {
michael@0 42 ok(true, 'Got updated screenshot after source page changed');
michael@0 43
michael@0 44 if (image.width !== cssPixelWidth * 2 ||
michael@0 45 image.height !== cssPixelHeight * 2) {
michael@0 46 ok(false, 'The pixel width of the 2dppx image received is not correct');
michael@0 47 SimpleTest.finish();
michael@0 48 return;
michael@0 49 }
michael@0 50 ok(true, 'The pixel width of the 2dppx image received is correct');
michael@0 51 SimpleTest.finish();
michael@0 52 }
michael@0 53 }
michael@0 54
michael@0 55 function takeScreenshot() {
michael@0 56 function gotImage(e) {
michael@0 57 // |this| is the Image.
michael@0 58
michael@0 59 URL.revokeObjectURL(this.src);
michael@0 60
michael@0 61 if (e.type === 'error' || !this.width || !this.height) {
michael@0 62 tryAgain();
michael@0 63
michael@0 64 return;
michael@0 65 }
michael@0 66
michael@0 67 screenshotTaken(this);
michael@0 68 }
michael@0 69
michael@0 70 function tryAgain() {
michael@0 71 if (--attempts === 0) {
michael@0 72 ok(false, 'Timed out waiting for correct screenshot');
michael@0 73 SimpleTest.finish();
michael@0 74 } else {
michael@0 75 setTimeout(function() {
michael@0 76 iframe1.getScreenshot(cssPixelWidth, cssPixelHeight).onsuccess =
michael@0 77 getScreenshotImageData;
michael@0 78 }, 200);
michael@0 79 }
michael@0 80 }
michael@0 81
michael@0 82 function getScreenshotImageData(e) {
michael@0 83 var blob = e.target.result;
michael@0 84 if (blob.size === 0) {
michael@0 85 tryAgain();
michael@0 86
michael@0 87 return;
michael@0 88 }
michael@0 89
michael@0 90 var img = new Image();
michael@0 91 img.src = URL.createObjectURL(blob);
michael@0 92 img.onload = img.onerror = gotImage;
michael@0 93 }
michael@0 94
michael@0 95 var attempts = 10;
michael@0 96 iframe1.getScreenshot(cssPixelWidth, cssPixelHeight).onsuccess =
michael@0 97 getScreenshotImageData;
michael@0 98 }
michael@0 99
michael@0 100 function iframeLoadedHandler() {
michael@0 101 SpecialPowers.pushPrefEnv(
michael@0 102 {'set': [['layout.css.devPixelsPerPx', 1]]}, takeScreenshot);
michael@0 103 }
michael@0 104
michael@0 105 iframe1.addEventListener('mozbrowserloadend', iframeLoadedHandler);
michael@0 106 }
michael@0 107
michael@0 108 addEventListener('testready', runTest);

mercurial