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