1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/tests/test_bug428405.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,162 @@ 1.4 +<?xml version="1.0"?> 1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?> 1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> 1.7 + 1.8 +<window id="window1" title="Test Bug 428405" 1.9 + onload="setGlobals(); loadFirstTab();" 1.10 + xmlns:html="http://www.w3.org/1999/xhtml" 1.11 + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 1.12 + 1.13 + <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> 1.14 + <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> 1.15 + <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/NativeKeyCodes.js"/> 1.16 + 1.17 + <tabbox id="tabbox" flex="100%"> 1.18 + <tabs> 1.19 + <tab label="Tab 1"/> 1.20 + <tab label="Tab 2"/> 1.21 + </tabs> 1.22 + <tabpanels flex="100%"> 1.23 + <browser onload="configureFirstTab();" id="tab1browser" flex="100%"/> 1.24 + <browser onload="configureSecondTab();" id="tab2browser" flex="100%"/> 1.25 + </tabpanels> 1.26 + </tabbox> 1.27 + 1.28 + <script type="application/javascript"><![CDATA[ 1.29 + 1.30 + SimpleTest.waitForExplicitFinish(); 1.31 + 1.32 + var gCmdOptYReceived = false; 1.33 + 1.34 + // Look for a cmd-opt-y event. 1.35 + function onKeyPress(aEvent) { 1.36 + gCmdOptYReceived = false; 1.37 + if (String.fromCharCode(aEvent.charCode) != 'y') 1.38 + return; 1.39 + if (aEvent.ctrlKey || aEvent.shiftKey || !aEvent.metaKey || !aEvent.altKey) 1.40 + return; 1.41 + gCmdOptYReceived = true; 1.42 + } 1.43 + 1.44 + function setGlobals() { 1.45 + var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]. 1.46 + getService(Components.interfaces.nsIWindowMediator); 1.47 + gChromeWindow = wm.getMostRecentWindow("navigator:browser"); 1.48 + // For some reason, a global <key> element's oncommand handler only gets 1.49 + // invoked if the focus is outside both of the <browser> elements 1.50 + // (tab1browser and tab2browser). So, to make sure we can see a 1.51 + // cmd-opt-y event in window1 (if one is available), regardless of where 1.52 + // the focus is in this window, we need to add a "keypress" event 1.53 + // listener to gChromeWindow, and then check (in onKeyPress()) to see if 1.54 + // it's a cmd-opt-y event. 1.55 + gChromeWindow.addEventListener("keypress", onKeyPress, false); 1.56 + } 1.57 + 1.58 + // 1) Start loading first tab. 1.59 + // 6) Start reloading first tab. 1.60 + function loadFirstTab() { 1.61 + var browser = document.getElementById("tab1browser"); 1.62 + browser.loadURI("data:text/html;charset=utf-8,<body><h2>First Tab</h2><p><input type='submit' value='Button' id='button1'/></body>", null, null); 1.63 + } 1.64 + 1.65 + function configureFirstTab() { 1.66 + try { 1.67 + var button = document.getElementById("tab1browser").contentDocument.getElementById("button1"); 1.68 + button.addEventListener("click", onFirstTabButtonClicked, false); 1.69 + button.focus(); 1.70 + if (document.getElementById("tabbox").selectedIndex == 0) { 1.71 + // 2) When first tab has finished loading (while first tab is 1.72 + // focused), hit Return to trigger the action of first tab's 1.73 + // button. 1.74 + synthesizeNativeReturnKey(); 1.75 + } else { 1.76 + // 7) When first tab has finished reloading (while second tab is 1.77 + // focused), start loading second tab. 1.78 + loadSecondTab(); 1.79 + } 1.80 + } catch(e) { 1.81 + } 1.82 + } 1.83 + 1.84 + // 8) Start loading second tab. 1.85 + function loadSecondTab() { 1.86 + var browser = document.getElementById("tab2browser"); 1.87 + browser.loadURI("data:text/html;charset=utf-8,<body><h2>Second Tab</h2><p><input type='submit' value='Button' id='button1'/></body>", null, null); 1.88 + } 1.89 + 1.90 + function configureSecondTab() { 1.91 + try { 1.92 + var button = document.getElementById("tab2browser").contentDocument.getElementById("button1"); 1.93 + button.addEventListener("click", onSecondTabButtonClicked, false); 1.94 + button.focus(); 1.95 + if (document.getElementById("tabbox").selectedIndex == 1) { 1.96 + // 9) When second tab has finished loading (while second tab is 1.97 + // focused), hit Return to trigger action of second tab's 1.98 + // button. 1.99 + synthesizeNativeReturnKey(); 1.100 + } 1.101 + } catch(e) { 1.102 + } 1.103 + } 1.104 + 1.105 + // 3) First tab's button clicked. 1.106 + function onFirstTabButtonClicked() { 1.107 + switchToSecondTabAndReloadFirst(); 1.108 + } 1.109 + 1.110 + // 10) Second tab's button clicked. 1.111 + function onSecondTabButtonClicked() { 1.112 + switchToFirstTab(); 1.113 + } 1.114 + 1.115 + function switchToSecondTabAndReloadFirst() { 1.116 + // 4) Switch to second tab. 1.117 + document.getElementById("tabbox").selectedIndex = 1; 1.118 + // 5) Start reloading first tab (while second tab is focused). 1.119 + loadFirstTab(); 1.120 + } 1.121 + 1.122 + function switchToFirstTab() { 1.123 + // 11) Switch back to first tab. 1.124 + document.getElementById("tabbox").selectedIndex = 0; 1.125 + finishTest(); 1.126 + } 1.127 + 1.128 + function finishTest() { 1.129 + // 12) Back in first tab, try cmd-y. 1.130 + gCmdOptYReceived = false; 1.131 + synthesizeNativeCmdOptY(); 1.132 + 1.133 + // 13) Check result. 1.134 + is(gCmdOptYReceived, true); 1.135 + 1.136 + SimpleTest.finish(); 1.137 + } 1.138 + 1.139 + // synthesizeNativeReturnKey() and synthesizeNativeCmdOptY() are needed 1.140 + // because their synthesizeKey() counterparts don't work properly -- the 1.141 + // latter make this test succeed when it should fail. 1.142 + 1.143 + // The 'aNativeKeyCode', 'aCharacters' and 'aUnmodifiedCharacters' 1.144 + // parameters used below (in synthesizeNativeReturnKey() and 1.145 + // synthesizeNativeCmdOptY()) were confirmed accurate using the 1.146 + // DebugEventsPlugin v1.01 from bmo bug 441880. 1.147 + 1.148 + function synthesizeNativeReturnKey() { 1.149 + synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_Return, {}, "\u000a", "\u000a"); 1.150 + } 1.151 + 1.152 + function synthesizeNativeCmdOptY() { 1.153 + synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_Y, {metaKey:1, altKey:1}, "y", "y"); 1.154 + } 1.155 + 1.156 + ]]></script> 1.157 + 1.158 + <!-- test results are displayed in the html:body --> 1.159 + <body xmlns="http://www.w3.org/1999/xhtml"> 1.160 + <p id="display"></p> 1.161 + <div id="content" style="display: none"></div> 1.162 + <pre id="test"></pre> 1.163 + </body> 1.164 + 1.165 +</window>