|
1 <?xml version="1.0"?> |
|
2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?> |
|
3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> |
|
4 |
|
5 <window id="window1" title="Test Bug 428405" |
|
6 onload="setGlobals(); loadFirstTab();" |
|
7 xmlns:html="http://www.w3.org/1999/xhtml" |
|
8 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> |
|
9 |
|
10 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> |
|
11 <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> |
|
12 <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/NativeKeyCodes.js"/> |
|
13 |
|
14 <tabbox id="tabbox" flex="100%"> |
|
15 <tabs> |
|
16 <tab label="Tab 1"/> |
|
17 <tab label="Tab 2"/> |
|
18 </tabs> |
|
19 <tabpanels flex="100%"> |
|
20 <browser onload="configureFirstTab();" id="tab1browser" flex="100%"/> |
|
21 <browser onload="configureSecondTab();" id="tab2browser" flex="100%"/> |
|
22 </tabpanels> |
|
23 </tabbox> |
|
24 |
|
25 <script type="application/javascript"><![CDATA[ |
|
26 |
|
27 SimpleTest.waitForExplicitFinish(); |
|
28 |
|
29 var gCmdOptYReceived = false; |
|
30 |
|
31 // Look for a cmd-opt-y event. |
|
32 function onKeyPress(aEvent) { |
|
33 gCmdOptYReceived = false; |
|
34 if (String.fromCharCode(aEvent.charCode) != 'y') |
|
35 return; |
|
36 if (aEvent.ctrlKey || aEvent.shiftKey || !aEvent.metaKey || !aEvent.altKey) |
|
37 return; |
|
38 gCmdOptYReceived = true; |
|
39 } |
|
40 |
|
41 function setGlobals() { |
|
42 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]. |
|
43 getService(Components.interfaces.nsIWindowMediator); |
|
44 gChromeWindow = wm.getMostRecentWindow("navigator:browser"); |
|
45 // For some reason, a global <key> element's oncommand handler only gets |
|
46 // invoked if the focus is outside both of the <browser> elements |
|
47 // (tab1browser and tab2browser). So, to make sure we can see a |
|
48 // cmd-opt-y event in window1 (if one is available), regardless of where |
|
49 // the focus is in this window, we need to add a "keypress" event |
|
50 // listener to gChromeWindow, and then check (in onKeyPress()) to see if |
|
51 // it's a cmd-opt-y event. |
|
52 gChromeWindow.addEventListener("keypress", onKeyPress, false); |
|
53 } |
|
54 |
|
55 // 1) Start loading first tab. |
|
56 // 6) Start reloading first tab. |
|
57 function loadFirstTab() { |
|
58 var browser = document.getElementById("tab1browser"); |
|
59 browser.loadURI("data:text/html;charset=utf-8,<body><h2>First Tab</h2><p><input type='submit' value='Button' id='button1'/></body>", null, null); |
|
60 } |
|
61 |
|
62 function configureFirstTab() { |
|
63 try { |
|
64 var button = document.getElementById("tab1browser").contentDocument.getElementById("button1"); |
|
65 button.addEventListener("click", onFirstTabButtonClicked, false); |
|
66 button.focus(); |
|
67 if (document.getElementById("tabbox").selectedIndex == 0) { |
|
68 // 2) When first tab has finished loading (while first tab is |
|
69 // focused), hit Return to trigger the action of first tab's |
|
70 // button. |
|
71 synthesizeNativeReturnKey(); |
|
72 } else { |
|
73 // 7) When first tab has finished reloading (while second tab is |
|
74 // focused), start loading second tab. |
|
75 loadSecondTab(); |
|
76 } |
|
77 } catch(e) { |
|
78 } |
|
79 } |
|
80 |
|
81 // 8) Start loading second tab. |
|
82 function loadSecondTab() { |
|
83 var browser = document.getElementById("tab2browser"); |
|
84 browser.loadURI("data:text/html;charset=utf-8,<body><h2>Second Tab</h2><p><input type='submit' value='Button' id='button1'/></body>", null, null); |
|
85 } |
|
86 |
|
87 function configureSecondTab() { |
|
88 try { |
|
89 var button = document.getElementById("tab2browser").contentDocument.getElementById("button1"); |
|
90 button.addEventListener("click", onSecondTabButtonClicked, false); |
|
91 button.focus(); |
|
92 if (document.getElementById("tabbox").selectedIndex == 1) { |
|
93 // 9) When second tab has finished loading (while second tab is |
|
94 // focused), hit Return to trigger action of second tab's |
|
95 // button. |
|
96 synthesizeNativeReturnKey(); |
|
97 } |
|
98 } catch(e) { |
|
99 } |
|
100 } |
|
101 |
|
102 // 3) First tab's button clicked. |
|
103 function onFirstTabButtonClicked() { |
|
104 switchToSecondTabAndReloadFirst(); |
|
105 } |
|
106 |
|
107 // 10) Second tab's button clicked. |
|
108 function onSecondTabButtonClicked() { |
|
109 switchToFirstTab(); |
|
110 } |
|
111 |
|
112 function switchToSecondTabAndReloadFirst() { |
|
113 // 4) Switch to second tab. |
|
114 document.getElementById("tabbox").selectedIndex = 1; |
|
115 // 5) Start reloading first tab (while second tab is focused). |
|
116 loadFirstTab(); |
|
117 } |
|
118 |
|
119 function switchToFirstTab() { |
|
120 // 11) Switch back to first tab. |
|
121 document.getElementById("tabbox").selectedIndex = 0; |
|
122 finishTest(); |
|
123 } |
|
124 |
|
125 function finishTest() { |
|
126 // 12) Back in first tab, try cmd-y. |
|
127 gCmdOptYReceived = false; |
|
128 synthesizeNativeCmdOptY(); |
|
129 |
|
130 // 13) Check result. |
|
131 is(gCmdOptYReceived, true); |
|
132 |
|
133 SimpleTest.finish(); |
|
134 } |
|
135 |
|
136 // synthesizeNativeReturnKey() and synthesizeNativeCmdOptY() are needed |
|
137 // because their synthesizeKey() counterparts don't work properly -- the |
|
138 // latter make this test succeed when it should fail. |
|
139 |
|
140 // The 'aNativeKeyCode', 'aCharacters' and 'aUnmodifiedCharacters' |
|
141 // parameters used below (in synthesizeNativeReturnKey() and |
|
142 // synthesizeNativeCmdOptY()) were confirmed accurate using the |
|
143 // DebugEventsPlugin v1.01 from bmo bug 441880. |
|
144 |
|
145 function synthesizeNativeReturnKey() { |
|
146 synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_Return, {}, "\u000a", "\u000a"); |
|
147 } |
|
148 |
|
149 function synthesizeNativeCmdOptY() { |
|
150 synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_Y, {metaKey:1, altKey:1}, "y", "y"); |
|
151 } |
|
152 |
|
153 ]]></script> |
|
154 |
|
155 <!-- test results are displayed in the html:body --> |
|
156 <body xmlns="http://www.w3.org/1999/xhtml"> |
|
157 <p id="display"></p> |
|
158 <div id="content" style="display: none"></div> |
|
159 <pre id="test"></pre> |
|
160 </body> |
|
161 |
|
162 </window> |