diff -r 000000000000 -r 6474c204b198 dom/tests/browser/browser_focus_steal_from_chrome.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dom/tests/browser/browser_focus_steal_from_chrome.js Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,188 @@ +function test() { + waitForExplicitFinish(); + + let secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"] + .getService(Components.interfaces + .nsIScriptSecurityManager); + + let fm = Components.classes["@mozilla.org/focus-manager;1"] + .getService(Components.interfaces.nsIFocusManager); + + let tabs = [ gBrowser.addTab(), gBrowser.addTab() ]; + gBrowser.selectedTab = tabs[0]; + + let testingList = [ + { uri: "data:text/html,", + tagName: "INPUT", methodName: "focus" }, + { uri: "data:text/html,", + tagName: "INPUT", methodName: "select" }, + { uri: "data:text/html,anchor", + tagName: "A", methodName: "focus" }, + { uri: "data:text/html,", + tagName: "BUTTON", methodName: "focus" }, + { uri: "data:text/html,", + tagName: "SELECT", methodName: "focus" }, + { uri: "data:text/html,", + tagName: "TEXTAREA", methodName: "focus" }, + { uri: "data:text/html,", + tagName: "TEXTAREA", methodName: "select" }, + { uri: "data:text/html,", + tagName: "INPUT", methodName: "focus of label element" }, + { uri: "data:text/html,
legend
", + tagName: "INPUT", methodName: "focus of legend element" }, + { uri: "data:text/html," + + "", + tagName: "INPUT", methodName: "click event on the label element" }, + ]; + + if (navigator.platform.indexOf("Mac") == -1) { + // clicking buttons doesn't focus on mac, so skip this test + testingList.push( + { uri: "data:text/html," + + "", + tagName: "BUTTON", methodName: "mousedown event on the button element" }); + } + + let testingIndex = -1; + let canRetry; + let callback; + let loadedCount; + let setFocusToChrome; + + function runNextTest() { + if (++testingIndex >= testingList.length) { + // cleaning-up... + for (let i = 0; i < tabs.length; i++) { + gBrowser.removeTab(tabs[i]); + } + finish(); + return; + } + callback = doTest1; + loadTestPage(false); + } + + function loadTestPage(aSetFocusToChrome) { + loadedCount = 0; + canRetry = 10; + setFocusToChrome = aSetFocusToChrome; + // Set the focus to the contents. + tabs[0].linkedBrowser.focus(); + // Load on the tabs + tabs[0].linkedBrowser.addEventListener("load", onLoadForegroundTab, true); + tabs[0].linkedBrowser.loadURI(testingList[testingIndex].uri); + tabs[1].linkedBrowser.addEventListener("load", onLoadBackgroundTab, true); + tabs[1].linkedBrowser.loadURI(testingList[testingIndex].uri); + } + + function onLoadForegroundTab() { + tabs[0].linkedBrowser.removeEventListener("load", onLoadForegroundTab, true); + if (setFocusToChrome) { + // Set focus to a chrome element before the loaded content tries to move + // focus. + document.getElementById("urlbar").focus(); + } + onLoadComplete(); + } + + function onLoadBackgroundTab() { + tabs[1].linkedBrowser.removeEventListener("load", onLoadBackgroundTab, true); + onLoadComplete(); + } + + function onLoadComplete() { + if (++loadedCount == tabs.length) { + setTimeout(callback, 20); + } + } + + function isPrepared() { + for (let i = 0; i < tabs.length; i++) { + if (tabs[i].linkedBrowser.contentDocument.activeElement.tagName != + testingList[testingIndex].tagName) { + return false; + } + } + return true; + } + + function doTest1() { + if (canRetry-- > 0 && !isPrepared()) { + setTimeout(callback, 10); // retry + return; + } + + // The contents should be able to steal the focus from content. + + // in foreground tab + let e = tabs[0].linkedBrowser.contentDocument.activeElement; + is(e.tagName, testingList[testingIndex].tagName, + "the foreground tab's " + testingList[testingIndex].tagName + + " element is not active by the " + testingList[testingIndex].methodName + + " (Test1: content can steal focus)"); + is(fm.focusedElement, e, + "the " + testingList[testingIndex].tagName + + " element isn't focused by the " + testingList[testingIndex].methodName + + " (Test1: content can steal focus)"); + + // in background tab + e = tabs[1].linkedBrowser.contentDocument.activeElement; + is(e.tagName, testingList[testingIndex].tagName, + "the background tab's " + testingList[testingIndex].tagName + + " element is not active by the " + testingList[testingIndex].methodName + + " (Test1: content can steal focus)"); + isnot(fm.focusedElement, e, + "the " + testingList[testingIndex].tagName + + " element is focused by the " + testingList[testingIndex].methodName + + " (Test1: content can steal focus)"); + + callback = doTest2; + loadTestPage(true); + } + + + function doTest2() { + if (canRetry-- > 0 && !isPrepared()) { + setTimeout(callback, 10); // retry + return; + } + + // The contents shouldn't be able to steal the focus from chrome. + + // in foreground tab + let e = tabs[0].linkedBrowser.contentDocument.activeElement; + is(e.tagName, testingList[testingIndex].tagName, + "the foreground tab's " + testingList[testingIndex].tagName + + " element is not active by the " + testingList[testingIndex].methodName + + " (Test2: content can NOT steal focus)"); + isnot(fm.focusedElement, e, + "the " + testingList[testingIndex].tagName + + " element is focused by the " + testingList[testingIndex].methodName + + " (Test2: content can NOT steal focus)"); + + // in background tab + e = tabs[1].linkedBrowser.contentDocument.activeElement; + is(e.tagName, testingList[testingIndex].tagName, + "the background tab's " + testingList[testingIndex].tagName + + " element is not active by the " + testingList[testingIndex].methodName + + " (Test2: content can NOT steal focus)"); + isnot(fm.focusedElement, e, + "the " + testingList[testingIndex].tagName + + " element is focused by the " + testingList[testingIndex].methodName + + " (Test2: content can NOT steal focus)"); + + runNextTest(); + } + + runNextTest(); +}