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