1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/browser/browser_focus_steal_from_chrome.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,188 @@ 1.4 +function test() { 1.5 + waitForExplicitFinish(); 1.6 + 1.7 + let secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"] 1.8 + .getService(Components.interfaces 1.9 + .nsIScriptSecurityManager); 1.10 + 1.11 + let fm = Components.classes["@mozilla.org/focus-manager;1"] 1.12 + .getService(Components.interfaces.nsIFocusManager); 1.13 + 1.14 + let tabs = [ gBrowser.addTab(), gBrowser.addTab() ]; 1.15 + gBrowser.selectedTab = tabs[0]; 1.16 + 1.17 + let testingList = [ 1.18 + { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><input id='target'></body>", 1.19 + tagName: "INPUT", methodName: "focus" }, 1.20 + { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').select(); }, 10);\"><input id='target'></body>", 1.21 + tagName: "INPUT", methodName: "select" }, 1.22 + { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><a href='about:blank' id='target'>anchor</a></body>", 1.23 + tagName: "A", methodName: "focus" }, 1.24 + { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><button id='target'>button</button></body>", 1.25 + tagName: "BUTTON", methodName: "focus" }, 1.26 + { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><select id='target'><option>item1</option></select></body>", 1.27 + tagName: "SELECT", methodName: "focus" }, 1.28 + { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><textarea id='target'>textarea</textarea></body>", 1.29 + tagName: "TEXTAREA", methodName: "focus" }, 1.30 + { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').select(); }, 10);\"><textarea id='target'>textarea</textarea></body>", 1.31 + tagName: "TEXTAREA", methodName: "select" }, 1.32 + { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><label id='target'><input></label></body>", 1.33 + tagName: "INPUT", methodName: "focus of label element" }, 1.34 + { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><fieldset><legend id='target'>legend</legend><input></fieldset></body>", 1.35 + tagName: "INPUT", methodName: "focus of legend element" }, 1.36 + { uri: "data:text/html,<body onload=\"setTimeout(function () {" + 1.37 + " var element = document.getElementById('target');" + 1.38 + " var event = document.createEvent('MouseEvent');" + 1.39 + " event.initMouseEvent('click', true, true, window," + 1.40 + " 1, 0, 0, 0, 0, false, false, false, false, 0, element);" + 1.41 + " element.dispatchEvent(event); }, 10);\">" + 1.42 + "<label id='target'><input></label></body>", 1.43 + tagName: "INPUT", methodName: "click event on the label element" }, 1.44 + ]; 1.45 + 1.46 + if (navigator.platform.indexOf("Mac") == -1) { 1.47 + // clicking buttons doesn't focus on mac, so skip this test 1.48 + testingList.push( 1.49 + { uri: "data:text/html,<body onload=\"setTimeout(function () {" + 1.50 + " var element = document.getElementById('target');" + 1.51 + " var event = document.createEvent('MouseEvent');" + 1.52 + " event.initMouseEvent('mousedown', true, true, window," + 1.53 + " 0, 0, 0, 0, 0, false, false, false, false, 0, element);" + 1.54 + " element.dispatchEvent(event); }, 10);\">" + 1.55 + "<button id='target'>button</button></body>", 1.56 + tagName: "BUTTON", methodName: "mousedown event on the button element" }); 1.57 + } 1.58 + 1.59 + let testingIndex = -1; 1.60 + let canRetry; 1.61 + let callback; 1.62 + let loadedCount; 1.63 + let setFocusToChrome; 1.64 + 1.65 + function runNextTest() { 1.66 + if (++testingIndex >= testingList.length) { 1.67 + // cleaning-up... 1.68 + for (let i = 0; i < tabs.length; i++) { 1.69 + gBrowser.removeTab(tabs[i]); 1.70 + } 1.71 + finish(); 1.72 + return; 1.73 + } 1.74 + callback = doTest1; 1.75 + loadTestPage(false); 1.76 + } 1.77 + 1.78 + function loadTestPage(aSetFocusToChrome) { 1.79 + loadedCount = 0; 1.80 + canRetry = 10; 1.81 + setFocusToChrome = aSetFocusToChrome; 1.82 + // Set the focus to the contents. 1.83 + tabs[0].linkedBrowser.focus(); 1.84 + // Load on the tabs 1.85 + tabs[0].linkedBrowser.addEventListener("load", onLoadForegroundTab, true); 1.86 + tabs[0].linkedBrowser.loadURI(testingList[testingIndex].uri); 1.87 + tabs[1].linkedBrowser.addEventListener("load", onLoadBackgroundTab, true); 1.88 + tabs[1].linkedBrowser.loadURI(testingList[testingIndex].uri); 1.89 + } 1.90 + 1.91 + function onLoadForegroundTab() { 1.92 + tabs[0].linkedBrowser.removeEventListener("load", onLoadForegroundTab, true); 1.93 + if (setFocusToChrome) { 1.94 + // Set focus to a chrome element before the loaded content tries to move 1.95 + // focus. 1.96 + document.getElementById("urlbar").focus(); 1.97 + } 1.98 + onLoadComplete(); 1.99 + } 1.100 + 1.101 + function onLoadBackgroundTab() { 1.102 + tabs[1].linkedBrowser.removeEventListener("load", onLoadBackgroundTab, true); 1.103 + onLoadComplete(); 1.104 + } 1.105 + 1.106 + function onLoadComplete() { 1.107 + if (++loadedCount == tabs.length) { 1.108 + setTimeout(callback, 20); 1.109 + } 1.110 + } 1.111 + 1.112 + function isPrepared() { 1.113 + for (let i = 0; i < tabs.length; i++) { 1.114 + if (tabs[i].linkedBrowser.contentDocument.activeElement.tagName != 1.115 + testingList[testingIndex].tagName) { 1.116 + return false; 1.117 + } 1.118 + } 1.119 + return true; 1.120 + } 1.121 + 1.122 + function doTest1() { 1.123 + if (canRetry-- > 0 && !isPrepared()) { 1.124 + setTimeout(callback, 10); // retry 1.125 + return; 1.126 + } 1.127 + 1.128 + // The contents should be able to steal the focus from content. 1.129 + 1.130 + // in foreground tab 1.131 + let e = tabs[0].linkedBrowser.contentDocument.activeElement; 1.132 + is(e.tagName, testingList[testingIndex].tagName, 1.133 + "the foreground tab's " + testingList[testingIndex].tagName + 1.134 + " element is not active by the " + testingList[testingIndex].methodName + 1.135 + " (Test1: content can steal focus)"); 1.136 + is(fm.focusedElement, e, 1.137 + "the " + testingList[testingIndex].tagName + 1.138 + " element isn't focused by the " + testingList[testingIndex].methodName + 1.139 + " (Test1: content can steal focus)"); 1.140 + 1.141 + // in background tab 1.142 + e = tabs[1].linkedBrowser.contentDocument.activeElement; 1.143 + is(e.tagName, testingList[testingIndex].tagName, 1.144 + "the background tab's " + testingList[testingIndex].tagName + 1.145 + " element is not active by the " + testingList[testingIndex].methodName + 1.146 + " (Test1: content can steal focus)"); 1.147 + isnot(fm.focusedElement, e, 1.148 + "the " + testingList[testingIndex].tagName + 1.149 + " element is focused by the " + testingList[testingIndex].methodName + 1.150 + " (Test1: content can steal focus)"); 1.151 + 1.152 + callback = doTest2; 1.153 + loadTestPage(true); 1.154 + } 1.155 + 1.156 + 1.157 + function doTest2() { 1.158 + if (canRetry-- > 0 && !isPrepared()) { 1.159 + setTimeout(callback, 10); // retry 1.160 + return; 1.161 + } 1.162 + 1.163 + // The contents shouldn't be able to steal the focus from chrome. 1.164 + 1.165 + // in foreground tab 1.166 + let e = tabs[0].linkedBrowser.contentDocument.activeElement; 1.167 + is(e.tagName, testingList[testingIndex].tagName, 1.168 + "the foreground tab's " + testingList[testingIndex].tagName + 1.169 + " element is not active by the " + testingList[testingIndex].methodName + 1.170 + " (Test2: content can NOT steal focus)"); 1.171 + isnot(fm.focusedElement, e, 1.172 + "the " + testingList[testingIndex].tagName + 1.173 + " element is focused by the " + testingList[testingIndex].methodName + 1.174 + " (Test2: content can NOT steal focus)"); 1.175 + 1.176 + // in background tab 1.177 + e = tabs[1].linkedBrowser.contentDocument.activeElement; 1.178 + is(e.tagName, testingList[testingIndex].tagName, 1.179 + "the background tab's " + testingList[testingIndex].tagName + 1.180 + " element is not active by the " + testingList[testingIndex].methodName + 1.181 + " (Test2: content can NOT steal focus)"); 1.182 + isnot(fm.focusedElement, e, 1.183 + "the " + testingList[testingIndex].tagName + 1.184 + " element is focused by the " + testingList[testingIndex].methodName + 1.185 + " (Test2: content can NOT steal focus)"); 1.186 + 1.187 + runNextTest(); 1.188 + } 1.189 + 1.190 + runNextTest(); 1.191 +}