Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | function test() { |
michael@0 | 2 | waitForExplicitFinish(); |
michael@0 | 3 | |
michael@0 | 4 | let secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"] |
michael@0 | 5 | .getService(Components.interfaces |
michael@0 | 6 | .nsIScriptSecurityManager); |
michael@0 | 7 | |
michael@0 | 8 | let fm = Components.classes["@mozilla.org/focus-manager;1"] |
michael@0 | 9 | .getService(Components.interfaces.nsIFocusManager); |
michael@0 | 10 | |
michael@0 | 11 | let tabs = [ gBrowser.addTab(), gBrowser.addTab() ]; |
michael@0 | 12 | gBrowser.selectedTab = tabs[0]; |
michael@0 | 13 | |
michael@0 | 14 | let testingList = [ |
michael@0 | 15 | { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><input id='target'></body>", |
michael@0 | 16 | tagName: "INPUT", methodName: "focus" }, |
michael@0 | 17 | { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').select(); }, 10);\"><input id='target'></body>", |
michael@0 | 18 | tagName: "INPUT", methodName: "select" }, |
michael@0 | 19 | { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><a href='about:blank' id='target'>anchor</a></body>", |
michael@0 | 20 | tagName: "A", methodName: "focus" }, |
michael@0 | 21 | { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><button id='target'>button</button></body>", |
michael@0 | 22 | tagName: "BUTTON", methodName: "focus" }, |
michael@0 | 23 | { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><select id='target'><option>item1</option></select></body>", |
michael@0 | 24 | tagName: "SELECT", methodName: "focus" }, |
michael@0 | 25 | { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><textarea id='target'>textarea</textarea></body>", |
michael@0 | 26 | tagName: "TEXTAREA", methodName: "focus" }, |
michael@0 | 27 | { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').select(); }, 10);\"><textarea id='target'>textarea</textarea></body>", |
michael@0 | 28 | tagName: "TEXTAREA", methodName: "select" }, |
michael@0 | 29 | { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><label id='target'><input></label></body>", |
michael@0 | 30 | tagName: "INPUT", methodName: "focus of label element" }, |
michael@0 | 31 | { uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><fieldset><legend id='target'>legend</legend><input></fieldset></body>", |
michael@0 | 32 | tagName: "INPUT", methodName: "focus of legend element" }, |
michael@0 | 33 | { uri: "data:text/html,<body onload=\"setTimeout(function () {" + |
michael@0 | 34 | " var element = document.getElementById('target');" + |
michael@0 | 35 | " var event = document.createEvent('MouseEvent');" + |
michael@0 | 36 | " event.initMouseEvent('click', true, true, window," + |
michael@0 | 37 | " 1, 0, 0, 0, 0, false, false, false, false, 0, element);" + |
michael@0 | 38 | " element.dispatchEvent(event); }, 10);\">" + |
michael@0 | 39 | "<label id='target'><input></label></body>", |
michael@0 | 40 | tagName: "INPUT", methodName: "click event on the label element" }, |
michael@0 | 41 | ]; |
michael@0 | 42 | |
michael@0 | 43 | if (navigator.platform.indexOf("Mac") == -1) { |
michael@0 | 44 | // clicking buttons doesn't focus on mac, so skip this test |
michael@0 | 45 | testingList.push( |
michael@0 | 46 | { uri: "data:text/html,<body onload=\"setTimeout(function () {" + |
michael@0 | 47 | " var element = document.getElementById('target');" + |
michael@0 | 48 | " var event = document.createEvent('MouseEvent');" + |
michael@0 | 49 | " event.initMouseEvent('mousedown', true, true, window," + |
michael@0 | 50 | " 0, 0, 0, 0, 0, false, false, false, false, 0, element);" + |
michael@0 | 51 | " element.dispatchEvent(event); }, 10);\">" + |
michael@0 | 52 | "<button id='target'>button</button></body>", |
michael@0 | 53 | tagName: "BUTTON", methodName: "mousedown event on the button element" }); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | let testingIndex = -1; |
michael@0 | 57 | let canRetry; |
michael@0 | 58 | let callback; |
michael@0 | 59 | let loadedCount; |
michael@0 | 60 | let setFocusToChrome; |
michael@0 | 61 | |
michael@0 | 62 | function runNextTest() { |
michael@0 | 63 | if (++testingIndex >= testingList.length) { |
michael@0 | 64 | // cleaning-up... |
michael@0 | 65 | for (let i = 0; i < tabs.length; i++) { |
michael@0 | 66 | gBrowser.removeTab(tabs[i]); |
michael@0 | 67 | } |
michael@0 | 68 | finish(); |
michael@0 | 69 | return; |
michael@0 | 70 | } |
michael@0 | 71 | callback = doTest1; |
michael@0 | 72 | loadTestPage(false); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | function loadTestPage(aSetFocusToChrome) { |
michael@0 | 76 | loadedCount = 0; |
michael@0 | 77 | canRetry = 10; |
michael@0 | 78 | setFocusToChrome = aSetFocusToChrome; |
michael@0 | 79 | // Set the focus to the contents. |
michael@0 | 80 | tabs[0].linkedBrowser.focus(); |
michael@0 | 81 | // Load on the tabs |
michael@0 | 82 | tabs[0].linkedBrowser.addEventListener("load", onLoadForegroundTab, true); |
michael@0 | 83 | tabs[0].linkedBrowser.loadURI(testingList[testingIndex].uri); |
michael@0 | 84 | tabs[1].linkedBrowser.addEventListener("load", onLoadBackgroundTab, true); |
michael@0 | 85 | tabs[1].linkedBrowser.loadURI(testingList[testingIndex].uri); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | function onLoadForegroundTab() { |
michael@0 | 89 | tabs[0].linkedBrowser.removeEventListener("load", onLoadForegroundTab, true); |
michael@0 | 90 | if (setFocusToChrome) { |
michael@0 | 91 | // Set focus to a chrome element before the loaded content tries to move |
michael@0 | 92 | // focus. |
michael@0 | 93 | document.getElementById("urlbar").focus(); |
michael@0 | 94 | } |
michael@0 | 95 | onLoadComplete(); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | function onLoadBackgroundTab() { |
michael@0 | 99 | tabs[1].linkedBrowser.removeEventListener("load", onLoadBackgroundTab, true); |
michael@0 | 100 | onLoadComplete(); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | function onLoadComplete() { |
michael@0 | 104 | if (++loadedCount == tabs.length) { |
michael@0 | 105 | setTimeout(callback, 20); |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | function isPrepared() { |
michael@0 | 110 | for (let i = 0; i < tabs.length; i++) { |
michael@0 | 111 | if (tabs[i].linkedBrowser.contentDocument.activeElement.tagName != |
michael@0 | 112 | testingList[testingIndex].tagName) { |
michael@0 | 113 | return false; |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | return true; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | function doTest1() { |
michael@0 | 120 | if (canRetry-- > 0 && !isPrepared()) { |
michael@0 | 121 | setTimeout(callback, 10); // retry |
michael@0 | 122 | return; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | // The contents should be able to steal the focus from content. |
michael@0 | 126 | |
michael@0 | 127 | // in foreground tab |
michael@0 | 128 | let e = tabs[0].linkedBrowser.contentDocument.activeElement; |
michael@0 | 129 | is(e.tagName, testingList[testingIndex].tagName, |
michael@0 | 130 | "the foreground tab's " + testingList[testingIndex].tagName + |
michael@0 | 131 | " element is not active by the " + testingList[testingIndex].methodName + |
michael@0 | 132 | " (Test1: content can steal focus)"); |
michael@0 | 133 | is(fm.focusedElement, e, |
michael@0 | 134 | "the " + testingList[testingIndex].tagName + |
michael@0 | 135 | " element isn't focused by the " + testingList[testingIndex].methodName + |
michael@0 | 136 | " (Test1: content can steal focus)"); |
michael@0 | 137 | |
michael@0 | 138 | // in background tab |
michael@0 | 139 | e = tabs[1].linkedBrowser.contentDocument.activeElement; |
michael@0 | 140 | is(e.tagName, testingList[testingIndex].tagName, |
michael@0 | 141 | "the background tab's " + testingList[testingIndex].tagName + |
michael@0 | 142 | " element is not active by the " + testingList[testingIndex].methodName + |
michael@0 | 143 | " (Test1: content can steal focus)"); |
michael@0 | 144 | isnot(fm.focusedElement, e, |
michael@0 | 145 | "the " + testingList[testingIndex].tagName + |
michael@0 | 146 | " element is focused by the " + testingList[testingIndex].methodName + |
michael@0 | 147 | " (Test1: content can steal focus)"); |
michael@0 | 148 | |
michael@0 | 149 | callback = doTest2; |
michael@0 | 150 | loadTestPage(true); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | |
michael@0 | 154 | function doTest2() { |
michael@0 | 155 | if (canRetry-- > 0 && !isPrepared()) { |
michael@0 | 156 | setTimeout(callback, 10); // retry |
michael@0 | 157 | return; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | // The contents shouldn't be able to steal the focus from chrome. |
michael@0 | 161 | |
michael@0 | 162 | // in foreground tab |
michael@0 | 163 | let e = tabs[0].linkedBrowser.contentDocument.activeElement; |
michael@0 | 164 | is(e.tagName, testingList[testingIndex].tagName, |
michael@0 | 165 | "the foreground tab's " + testingList[testingIndex].tagName + |
michael@0 | 166 | " element is not active by the " + testingList[testingIndex].methodName + |
michael@0 | 167 | " (Test2: content can NOT steal focus)"); |
michael@0 | 168 | isnot(fm.focusedElement, e, |
michael@0 | 169 | "the " + testingList[testingIndex].tagName + |
michael@0 | 170 | " element is focused by the " + testingList[testingIndex].methodName + |
michael@0 | 171 | " (Test2: content can NOT steal focus)"); |
michael@0 | 172 | |
michael@0 | 173 | // in background tab |
michael@0 | 174 | e = tabs[1].linkedBrowser.contentDocument.activeElement; |
michael@0 | 175 | is(e.tagName, testingList[testingIndex].tagName, |
michael@0 | 176 | "the background tab's " + testingList[testingIndex].tagName + |
michael@0 | 177 | " element is not active by the " + testingList[testingIndex].methodName + |
michael@0 | 178 | " (Test2: content can NOT steal focus)"); |
michael@0 | 179 | isnot(fm.focusedElement, e, |
michael@0 | 180 | "the " + testingList[testingIndex].tagName + |
michael@0 | 181 | " element is focused by the " + testingList[testingIndex].methodName + |
michael@0 | 182 | " (Test2: content can NOT steal focus)"); |
michael@0 | 183 | |
michael@0 | 184 | runNextTest(); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | runNextTest(); |
michael@0 | 188 | } |