|
1 /* Any copyright is dedicated to the public domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 // Test that an iframe with the |mozbrowser| attribute does bubble some |
|
5 // whitelisted key events. |
|
6 "use strict"; |
|
7 |
|
8 let Ci = SpecialPowers.Ci; |
|
9 |
|
10 let whitelistedKeyCodes = [ |
|
11 Ci.nsIDOMKeyEvent.DOM_VK_ESCAPE, // Back button. |
|
12 Ci.nsIDOMKeyEvent.DOM_VK_SLEEP, // Power button |
|
13 Ci.nsIDOMKeyEvent.DOM_VK_CONTEXT_MENU, |
|
14 Ci.nsIDOMKeyEvent.DOM_VK_F5, // Search button. |
|
15 Ci.nsIDOMKeyEvent.DOM_VK_PAGE_UP, // Volume up. |
|
16 Ci.nsIDOMKeyEvent.DOM_VK_PAGE_DOWN // Volume down. |
|
17 ]; |
|
18 |
|
19 let blacklistedKeyCodes = [ |
|
20 Ci.nsIDOMKeyEvent.DOM_VK_A, |
|
21 Ci.nsIDOMKeyEvent.DOM_VK_B |
|
22 ]; |
|
23 |
|
24 SimpleTest.waitForExplicitFinish(); |
|
25 browserElementTestHelpers.setEnabledPref(true); |
|
26 browserElementTestHelpers.addPermission(); |
|
27 |
|
28 // Number of expected events at which point we will consider the test as done. |
|
29 var nbEvents = whitelistedKeyCodes.length * 3; |
|
30 |
|
31 var iframe; |
|
32 var finished = false; |
|
33 function runTest() { |
|
34 iframe = document.createElement('iframe'); |
|
35 SpecialPowers.wrap(iframe).mozbrowser = true; |
|
36 iframe.src = browserElementTestHelpers.focusPage; |
|
37 |
|
38 var gotFocus = false; |
|
39 var gotLoadend = false; |
|
40 |
|
41 function maybeTest2() { |
|
42 if (gotFocus && gotLoadend) { |
|
43 SimpleTest.executeSoon(test2); |
|
44 } |
|
45 } |
|
46 |
|
47 iframe.addEventListener('mozbrowserloadend', function() { |
|
48 gotLoadend = true; |
|
49 maybeTest2(); |
|
50 }); |
|
51 |
|
52 document.body.appendChild(iframe); |
|
53 |
|
54 SimpleTest.waitForFocus(function() { |
|
55 iframe.focus(); |
|
56 gotFocus = true; |
|
57 maybeTest2(); |
|
58 }); |
|
59 } |
|
60 |
|
61 function eventHandler(e) { |
|
62 if (whitelistedKeyCodes.indexOf(e.keyCode) == -1 && |
|
63 blacklistedKeyCodes.indexOf(e.keyCode) == -1) { |
|
64 // See bug 856006: We sometimes get unexpected key presses, and we don't |
|
65 // know why. Don't turn the test orange over this. |
|
66 ok(true, "Ignoring unexpected " + e.type + |
|
67 " with keyCode " + e.keyCode + "."); |
|
68 return; |
|
69 } |
|
70 |
|
71 ok(e.type == 'keydown' || e.type == 'keypress' || e.type == 'keyup', |
|
72 "e.type was " + e.type + ", expected keydown, keypress, or keyup"); |
|
73 ok(!e.defaultPrevented, "expected !e.defaultPrevented"); |
|
74 ok(whitelistedKeyCodes.indexOf(e.keyCode) != -1, |
|
75 "Expected a whitelited keycode, but got " + e.keyCode + " instead."); |
|
76 |
|
77 nbEvents--; |
|
78 |
|
79 if (nbEvents == 0) { |
|
80 SimpleTest.finish(); |
|
81 return; |
|
82 } |
|
83 |
|
84 if (nbEvents < 0 && !finished) { |
|
85 ok(false, "got an unexpected event! " + e.type + " " + e.keyCode); |
|
86 } |
|
87 } |
|
88 |
|
89 function test2() { |
|
90 is(document.activeElement, iframe, "iframe should be focused"); |
|
91 |
|
92 addEventListener('keydown', eventHandler); |
|
93 addEventListener('keypress', eventHandler); |
|
94 addEventListener('keyup', eventHandler); |
|
95 |
|
96 // These events should not be received because they are not whitelisted. |
|
97 synthesizeKey("VK_A", {}); |
|
98 synthesizeKey("VK_B", {}); |
|
99 |
|
100 // These events should not be received because preventDefault is called. |
|
101 synthesizeKey("VK_ESCAPE", {}); |
|
102 |
|
103 // These events should be received. |
|
104 synthesizeKey("VK_F5", {}); |
|
105 synthesizeKey("VK_ESCAPE", {}); |
|
106 synthesizeKey("VK_PAGE_UP", {}); |
|
107 synthesizeKey("VK_PAGE_DOWN", {}); |
|
108 synthesizeKey("VK_CONTEXT_MENU", {}); |
|
109 synthesizeKey("VK_SLEEP", {}); |
|
110 finished = true; |
|
111 } |
|
112 |
|
113 addEventListener('testready', runTest); |