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