dom/browser-element/mochitest/browserElement_BadScreenshot.js

changeset 2
7e26c7da4463
equal deleted inserted replaced
-1:000000000000 0:b015cfeb7774
1 /* Any copyright is dedicated to the public domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 // Bug 800170 - Test that we get errors when we pass bad arguments to
5 // mozbrowser's getScreenshot.
6 "use strict";
7
8 SimpleTest.waitForExplicitFinish();
9 browserElementTestHelpers.setEnabledPref(true);
10 browserElementTestHelpers.addPermission();
11
12 var iframe;
13 var numPendingTests = 0;
14
15 // Call iframe.getScreenshot with the given args. If expectSuccess is true, we
16 // expect the screenshot's onsuccess handler to fire. Otherwise, we expect
17 // getScreenshot() to throw an exception.
18 function checkScreenshotResult(expectSuccess, args) {
19 var req;
20 try {
21 req = iframe.getScreenshot.apply(iframe, args);
22 }
23 catch(e) {
24 ok(!expectSuccess, "getScreenshot(" + JSON.stringify(args) + ") threw an exception.");
25 return;
26 }
27
28 numPendingTests++;
29 req.onsuccess = function() {
30 ok(expectSuccess, "getScreenshot(" + JSON.stringify(args) + ") succeeded.");
31 numPendingTests--;
32 if (numPendingTests == 0) {
33 SimpleTest.finish();
34 }
35 };
36
37 // We never expect to see onerror.
38 req.onerror = function() {
39 ok(false, "getScreenshot(" + JSON.stringify(args) + ") ran onerror.");
40 numPendingTests--;
41 if (numPendingTests == 0) {
42 SimpleTest.finish();
43 }
44 };
45 }
46
47 function runTest() {
48 iframe = document.createElement('iframe');
49 SpecialPowers.wrap(iframe).mozbrowser = true;
50 document.body.appendChild(iframe);
51 iframe.src = 'data:text/html,<html>' +
52 '<body style="background:green">hello</body></html>';
53
54 iframe.addEventListener('mozbrowserfirstpaint', function() {
55 // This one should succeed.
56 checkScreenshotResult(true, [100, 100]);
57
58 // These should fail.
59 checkScreenshotResult(false, []);
60 checkScreenshotResult(false, [100]);
61 checkScreenshotResult(false, ['a', 100]);
62 checkScreenshotResult(false, [100, 'a']);
63 checkScreenshotResult(false, [-1, 100]);
64 checkScreenshotResult(false, [100, -1]);
65
66 if (numPendingTests == 0) {
67 SimpleTest.finish();
68 }
69 });
70 }
71
72 addEventListener('testready', runTest);

mercurial