Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 iframe1 = document.createElement('iframe'); |
michael@0 | 13 | SpecialPowers.wrap(iframe1).mozbrowser = true; |
michael@0 | 14 | document.body.appendChild(iframe1); |
michael@0 | 15 | iframe1.src = 'data:text/html,<html>' + |
michael@0 | 16 | '<body style="background:green">hello</body></html>'; |
michael@0 | 17 | |
michael@0 | 18 | var screenshotImageDatas = []; |
michael@0 | 19 | var numLoaded = 0; |
michael@0 | 20 | |
michael@0 | 21 | function screenshotTaken(screenshotImageData) { |
michael@0 | 22 | screenshotImageDatas.push(screenshotImageData); |
michael@0 | 23 | if (screenshotImageDatas.length === 1) { |
michael@0 | 24 | ok(true, 'Got initial non blank screenshot'); |
michael@0 | 25 | |
michael@0 | 26 | var view = screenshotImageData.data; |
michael@0 | 27 | if (view[3] !== 255) { |
michael@0 | 28 | ok(false, 'The first pixel of initial screenshot is not opaque'); |
michael@0 | 29 | SimpleTest.finish(); |
michael@0 | 30 | return; |
michael@0 | 31 | } |
michael@0 | 32 | ok(true, 'Verified the first pixel of initial screenshot is opaque'); |
michael@0 | 33 | |
michael@0 | 34 | iframe1.src = 'data:text/html,<html>' + |
michael@0 | 35 | '<body style="background:transparent">hello</body></html>'; |
michael@0 | 36 | |
michael@0 | 37 | // Wait until screenshotImageData !== screenshotImageDatas[0]. |
michael@0 | 38 | waitForScreenshot(function(screenshotImageData) { |
michael@0 | 39 | var view1 = screenshotImageData.data; |
michael@0 | 40 | var view2 = screenshotImageDatas[0].data; |
michael@0 | 41 | |
michael@0 | 42 | if (view1.length != view2.length) { |
michael@0 | 43 | return true; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | for (var i = 0; i < view1.length; i++) { |
michael@0 | 47 | if (view1[i] != view2[i]) { |
michael@0 | 48 | return true; |
michael@0 | 49 | } |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | return false; |
michael@0 | 53 | }, 'image/png'); |
michael@0 | 54 | } |
michael@0 | 55 | else if (screenshotImageDatas.length === 2) { |
michael@0 | 56 | ok(true, 'Got updated screenshot after source page changed'); |
michael@0 | 57 | |
michael@0 | 58 | var view = screenshotImageData.data; |
michael@0 | 59 | if (view[3] !== 0) { |
michael@0 | 60 | // The case here will always fail when oop'd on Firefox Desktop, |
michael@0 | 61 | // but not on B2G Emulator |
michael@0 | 62 | // See https://bugzil.la/878003#c20 |
michael@0 | 63 | |
michael@0 | 64 | var isB2G = (navigator.platform === ''); |
michael@0 | 65 | info('navigator.platform: ' + navigator.platform); |
michael@0 | 66 | if (!isB2G && browserElementTestHelpers.getOOPByDefaultPref()) { |
michael@0 | 67 | todo(false, 'The first pixel of updated screenshot is not transparent'); |
michael@0 | 68 | } else { |
michael@0 | 69 | ok(false, 'The first pixel of updated screenshot is not transparent'); |
michael@0 | 70 | } |
michael@0 | 71 | SimpleTest.finish(); |
michael@0 | 72 | return; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | ok(true, 'Verified the first pixel of updated screenshot is transparent'); |
michael@0 | 76 | SimpleTest.finish(); |
michael@0 | 77 | } |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | // We continually take screenshots until we get one that we are |
michael@0 | 81 | // happy with. |
michael@0 | 82 | function waitForScreenshot(filter, mimeType) { |
michael@0 | 83 | function gotImage(e) { |
michael@0 | 84 | // |this| is the Image. |
michael@0 | 85 | |
michael@0 | 86 | URL.revokeObjectURL(this.src); |
michael@0 | 87 | |
michael@0 | 88 | if (e.type === 'error' || !this.width || !this.height) { |
michael@0 | 89 | tryAgain(); |
michael@0 | 90 | |
michael@0 | 91 | return; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | var canvas = document.createElement('canvas'); |
michael@0 | 95 | canvas.width = canvas.height = 1000; |
michael@0 | 96 | var ctx = canvas.getContext('2d'); |
michael@0 | 97 | ctx.drawImage(this, 0, 0); |
michael@0 | 98 | var imageData = ctx.getImageData(0, 0, 1000, 1000); |
michael@0 | 99 | |
michael@0 | 100 | if (filter(imageData)) { |
michael@0 | 101 | screenshotTaken(imageData); |
michael@0 | 102 | return; |
michael@0 | 103 | } |
michael@0 | 104 | tryAgain(); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | function tryAgain() { |
michael@0 | 108 | if (--attempts === 0) { |
michael@0 | 109 | ok(false, 'Timed out waiting for correct screenshot'); |
michael@0 | 110 | SimpleTest.finish(); |
michael@0 | 111 | } else { |
michael@0 | 112 | setTimeout(function() { |
michael@0 | 113 | iframe1.getScreenshot(1000, 1000, mimeType).onsuccess = |
michael@0 | 114 | getScreenshotImageData; |
michael@0 | 115 | }, 200); |
michael@0 | 116 | } |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | function getScreenshotImageData(e) { |
michael@0 | 120 | var blob = e.target.result; |
michael@0 | 121 | if (blob.type !== mimeType) { |
michael@0 | 122 | ok(false, 'MIME type of screenshot taken incorrect'); |
michael@0 | 123 | SimpleTest.finish(); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | if (blob.size === 0) { |
michael@0 | 127 | tryAgain(); |
michael@0 | 128 | |
michael@0 | 129 | return; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | var img = new Image(); |
michael@0 | 133 | img.src = URL.createObjectURL(blob); |
michael@0 | 134 | img.onload = img.onerror = gotImage; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | var attempts = 10; |
michael@0 | 138 | iframe1.getScreenshot(1000, 1000, mimeType).onsuccess = |
michael@0 | 139 | getScreenshotImageData; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | function iframeLoadedHandler() { |
michael@0 | 143 | numLoaded++; |
michael@0 | 144 | if (numLoaded === 2) { |
michael@0 | 145 | waitForScreenshot(function(screenshotImageData) { |
michael@0 | 146 | return true; |
michael@0 | 147 | }, 'image/jpeg'); |
michael@0 | 148 | } |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | iframe1.addEventListener('mozbrowserloadend', iframeLoadedHandler); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | addEventListener('testready', runTest); |