Bug 1131562, rework browser_tabfocus.js to handle tabs in other processes and re-enable the test, r=felipe

This commit is contained in:
Neil Deakin 2015-03-03 09:17:00 -05:00
parent de74fcc752
commit d768d0fcaf
2 changed files with 479 additions and 198 deletions

View File

@ -420,7 +420,6 @@ skip-if = buildapp == 'mulet'
skip-if = os == "linux" || os == "mac" # No tabs in titlebar on linux
# Disabled on OS X because of bug 967917
[browser_tabfocus.js]
skip-if = e10s # Bug 921935 - focusmanager issues with e10s (test calls getFocusedElementForWindow with a content window)
[browser_tabkeynavigation.js]
skip-if = e10s
[browser_tabopen_reflows.js]

View File

@ -2,155 +2,350 @@
* This test checks that focus is adjusted properly when switching tabs.
*/
let testPage1 = "data:text/html,<html id='tab1'><body><button id='button1'>Tab 1</button></body></html>";
let testPage2 = "data:text/html,<html id='tab2'><body><button id='button2'>Tab 2</button></body></html>";
let testPage3 = "data:text/html,<html id='tab3'><body><button id='button3'>Tab 3</button></body></html>";
let testPage1 = "<html id='html1'><body id='body1'><button id='button1'>Tab 1</button></body></html>";
let testPage2 = "<html id='html2'><body id='body2'><button id='button2'>Tab 2</button></body></html>";
let testPage3 = "<html id='html3'><body id='body3'><button id='button3'>Tab 3</button></body></html>";
function test() {
waitForExplicitFinish();
const fm = Services.focus;
var tab1 = gBrowser.addTab();
var browser1 = gBrowser.getBrowserForTab(tab1);
let tab1 = null;
let tab2 = null;
let browser1 = null;
let browser2 = null;
let _browser_tabfocus_test_lastfocus;
let _browser_tabfocus_test_lastfocuswindow = null;
let actualEvents = [];
let expectedEvents = [];
let currentTestName = "";
let _expectedElement = null;
let _expectedWindow = null;
var tab2 = gBrowser.addTab();
var browser2 = gBrowser.getBrowserForTab(tab2);
let currentPromiseResolver = null;
gURLBar.focus();
function* getFocusedElementForBrowser(browser, dontCheckExtraFocus = false)
{
if (gMultiProcessBrowser) {
return new Promise((resolve, reject) => {
messageManager.addMessageListener("Browser:GetCurrentFocus", function getCurrentFocus(message) {
messageManager.removeMessageListener("Browser:GetCurrentFocus", getCurrentFocus);
resolve(message.data.details);
});
var loadCount = 0;
function check()
// The dontCheckExtraFocus flag is used to indicate not to check some
// additional focus related properties. This is needed as both URLs are
// loaded using the same child process and share focus managers.
browser.messageManager.sendAsyncMessage("Browser:GetFocusedElement",
{ dontCheckExtraFocus : dontCheckExtraFocus });
});
}
else {
var focusedWindow = {};
var node = fm.getFocusedElementForWindow(browser.contentWindow, false, focusedWindow);
return "Focus is " + (node ? node.id : "<none>");
}
}
function focusInChild()
{
var fm = Components.classes["@mozilla.org/focus-manager;1"].
getService(Components.interfaces.nsIFocusManager);
function getWindowDocId(target)
{
// wait for both tabs to load
if (++loadCount != 2)
return;
browser1.removeEventListener("load", check, true);
browser2.removeEventListener("load", check, true);
executeSoon(_run_focus_tests);
return (String(target.location).indexOf("1") >= 0) ? "window1" : "window2";
}
function _run_focus_tests() {
window.focus();
function eventListener(event) {
var id;
if (event.target instanceof Components.interfaces.nsIDOMWindow)
id = getWindowDocId(event.originalTarget) + "-window";
else if (event.target instanceof Components.interfaces.nsIDOMDocument)
id = getWindowDocId(event.originalTarget) + "-document";
else
id = event.originalTarget.id;
sendSyncMessage("Browser:FocusChanged", { details : event.type + ": " + id });
}
_browser_tabfocus_test_lastfocus = gURLBar;
_browser_tabfocus_test_lastfocuswindow = window;
addEventListener("focus", eventListener, true);
addEventListener("blur", eventListener, true);
window.addEventListener("focus", _browser_tabfocus_test_eventOccured, true);
window.addEventListener("blur", _browser_tabfocus_test_eventOccured, true);
addMessageListener("Browser:ChangeFocus", function changeFocus(message) {
content.document.getElementById(message.data.id)[message.data.type]();
});
// make sure that the focus initially starts out blank
var fm = Services.focus;
addMessageListener("Browser:GetFocusedElement", function getFocusedElement(message) {
var focusedWindow = {};
is(fm.getFocusedElementForWindow(browser1.contentWindow, false, focusedWindow), null, "initial focus in tab 1");
is(focusedWindow.value, browser1.contentWindow, "initial frame focus in tab 1");
is(fm.getFocusedElementForWindow(browser2.contentWindow, false, focusedWindow), null, "initial focus in tab 2");
is(focusedWindow.value, browser2.contentWindow, "initial frame focus in tab 2");
var node = fm.getFocusedElementForWindow(content, false, focusedWindow);
var details = "Focus is " + (node ? node.id : "<none>");
expectFocusShift(function () gBrowser.selectedTab = tab2,
browser2.contentWindow, null, true,
"focusedElement after tab change, focus in new tab");
/* Check focus manager properties. Add an error onto the string if they are
not what is expected which will cause matching to fail in the parent process. */
let doc = content.document;
if (!message.data.dontCheckExtraFocus) {
if (fm.focusedElement != node) {
details += "<ERROR: focusedElement doesn't match>";
}
if (fm.focusedWindow && fm.focusedWindow != content) {
details += "<ERROR: focusedWindow doesn't match>";
}
if ((fm.focusedWindow == content) != doc.hasFocus()) {
details += "<ERROR: child hasFocus() is not correct>";
}
if ((fm.focusedElement && doc.activeElement != fm.focusedElement) ||
(!fm.focusedElement && doc.activeElement != doc.body)) {
details += "<ERROR: child activeElement is not correct>";
}
}
// switching tabs when nothing in the new tab is focused
// should focus the browser
expectFocusShift(function () gBrowser.selectedTab = tab1,
browser1.contentWindow, null, true,
"focusedElement after tab change, focus in new tab");
sendSyncMessage("Browser:GetCurrentFocus", { details : details });
});
}
// focusing a button in the current tab should focus it
var button1 = browser1.contentDocument.getElementById("button1");
expectFocusShift(function () button1.focus(),
browser1.contentWindow, button1, true,
"focusedWindow after focus in focused tab");
function focusElementInChild(elementid, type)
{
let browser = (elementid.indexOf("1") >= 0) ? browser1 : browser2;
if (gMultiProcessBrowser) {
browser.messageManager.sendAsyncMessage("Browser:ChangeFocus",
{ id: elementid, type: type });
}
else {
browser.contentDocument.getElementById(elementid)[type]();
}
}
// focusing a button in a background tab should not change the actual
// focus, but should set the focus that would be in that background tab to
// that button.
var button2 = browser2.contentDocument.getElementById("button2");
button2.focus();
add_task(function*() {
tab1 = gBrowser.addTab();
browser1 = gBrowser.getBrowserForTab(tab1);
expectFocusShift(function () button2.focus(),
browser1.contentWindow, button1, false,
"focusedWindow after focus in unfocused tab");
is(fm.getFocusedElementForWindow(browser2.contentWindow, false, {}), button2, "focus in unfocused tab");
tab2 = gBrowser.addTab();
browser2 = gBrowser.getBrowserForTab(tab2);
// switching tabs should now make the button in the other tab focused
expectFocusShift(function () gBrowser.selectedTab = tab2,
browser2.contentWindow, button2, true,
"focusedWindow after tab change");
yield promiseTabLoadEvent(tab1, "data:text/html," + escape(testPage1));
yield promiseTabLoadEvent(tab2, "data:text/html," + escape(testPage2));
// blurring an element in a background tab should not change the active
// focus, but should clear the focus in that tab.
expectFocusShift(function () button1.blur(),
browser2.contentWindow, button2, false,
"focusedWindow after blur in unfocused tab");
is(fm.getFocusedElementForWindow(browser1.contentWindow, false, {}), null, "blur in unfocused tab");
var childFocusScript = "data:,(" + focusInChild.toString() + ")();";
browser1.messageManager.loadFrameScript(childFocusScript, true);
browser2.messageManager.loadFrameScript(childFocusScript, true);
// When focus is in the tab bar, it should be retained there
expectFocusShift(function () gBrowser.selectedTab.focus(),
window, gBrowser.selectedTab, true,
"focusing tab element");
expectFocusShift(function () gBrowser.selectedTab = tab1,
window, tab1, true,
"tab change when selected tab element was focused");
expectFocusShift(function () gBrowser.selectedTab = tab2,
window, tab2, true,
"tab change when selected tab element was focused");
expectFocusShift(function () gBrowser.selectedTab.blur(),
window, null, true,
"blurring tab element");
gURLBar.focus();
yield SimpleTest.promiseFocus();
// focusing the url field should switch active focus away from the browser but
// not clear what would be the focus in the browser
button1.focus();
expectFocusShift(function () gURLBar.focus(),
window, gURLBar.inputField, true,
"focusedWindow after url field focused");
is(fm.getFocusedElementForWindow(browser2.contentWindow, false, {}), button2, "url field focused, button in browser");
expectFocusShift(function () gURLBar.blur(),
window, null, true,
"blurring url field");
var messages = "";
if (gMultiProcessBrowser) {
messageManager.addMessageListener("Browser:FocusChanged", message => {
actualEvents.push(message.data.details);
compareFocusResults();
});
}
// when a chrome element is focused, switching tabs to a tab with a button
// with the current focus should focus the button
expectFocusShift(function () gBrowser.selectedTab = tab1,
browser1.contentWindow, button1, true,
"focusedWindow after tab change, focus in url field, button focused in new tab");
is(fm.getFocusedElementForWindow(browser2.contentWindow, false, {}), button2, "after switch tab, focus in unfocused tab");
_browser_tabfocus_test_lastfocus = "urlbar";
_browser_tabfocus_test_lastfocuswindow = "main-window";
// blurring an element in the current tab should clear the active focus
expectFocusShift(function () button1.blur(),
browser1.contentWindow, null, true,
"focusedWindow after blur in focused tab");
window.addEventListener("focus", _browser_tabfocus_test_eventOccured, true);
window.addEventListener("blur", _browser_tabfocus_test_eventOccured, true);
// blurring an non-focused url field should have no effect
expectFocusShift(function () gURLBar.blur(),
browser1.contentWindow, null, false,
"focusedWindow after blur in unfocused url field");
// make sure that the focus initially starts out blank
var fm = Services.focus;
var focusedWindow = {};
// switch focus to a tab with a currently focused element
expectFocusShift(function () gBrowser.selectedTab = tab2,
browser2.contentWindow, button2, true,
"focusedWindow after switch from unfocused to focused tab");
let focused = yield getFocusedElementForBrowser(browser1);
is(focused, "Focus is <none>", "initial focus in tab 1");
// clearing focus on the chrome window should switch the focus to the
// chrome window
expectFocusShift(function () fm.clearFocus(window),
window, null, true,
"focusedWindow after switch to chrome with no focused element");
focused = yield getFocusedElementForBrowser(browser2);
is(focused, "Focus is <none>", "initial focus in tab 2");
// switch focus to another tab when neither have an active focus
expectFocusShift(function () gBrowser.selectedTab = tab1,
browser1.contentWindow, null, true,
"focusedWindow after tab switch from no focus to no focus");
is(document.activeElement, gURLBar.inputField, "focus after loading two tabs");
yield expectFocusShift(function () gBrowser.selectedTab = tab2,
"window2", null, true,
"after tab change, focus in new tab");
focused = yield getFocusedElementForBrowser(browser2);
is(focused, "Focus is <none>", "focusedElement after tab change, focus in new tab");
// switching tabs when nothing in the new tab is focused
// should focus the browser
yield expectFocusShift(function () gBrowser.selectedTab = tab1,
"window1", null, true,
"after tab change, focus in original tab");
focused = yield getFocusedElementForBrowser(browser1);
is(focused, "Focus is <none>", "focusedElement after tab change, focus in original tab");
// focusing a button in the current tab should focus it
yield expectFocusShift(() => focusElementInChild("button1", "focus"),
"window1", "button1", true,
"after button focused");
focused = yield getFocusedElementForBrowser(browser1);
is(focused, "Focus is button1", "focusedElement in first browser after button focused");
// focusing a button in a background tab should not change the actual
// focus, but should set the focus that would be in that background tab to
// that button.
yield expectFocusShift(() => focusElementInChild("button2", "focus"),
"window1", "button1", false,
"after button focus in unfocused tab");
focused = yield getFocusedElementForBrowser(browser1, false);
is(focused, "Focus is button1", "focusedElement in first browser after button focus in unfocused tab");
focused = yield getFocusedElementForBrowser(browser2, true);
is(focused, "Focus is button2", "focusedElement in second browser after button focus in unfocused tab");
// switching tabs should now make the button in the other tab focused
yield expectFocusShift(function () gBrowser.selectedTab = tab2,
"window2", "button2", true,
"after tab change with button focused");
// blurring an element in a background tab should not change the active
// focus, but should clear the focus in that tab.
yield expectFocusShift(() => focusElementInChild("button1", "blur"),
"window2", "button2", false,
"focusedWindow after blur in unfocused tab");
focused = yield getFocusedElementForBrowser(browser1, true);
is(focused, "Focus is <none>", "focusedElement in first browser after focus in unfocused tab");
focused = yield getFocusedElementForBrowser(browser2, false);
is(focused, "Focus is button2", "focusedElement in second browser after focus in unfocused tab");
// When focus is in the tab bar, it should be retained there
yield expectFocusShift(function () gBrowser.selectedTab.focus(),
"main-window", "tab2", true,
"focusing tab element");
yield expectFocusShift(function () gBrowser.selectedTab = tab1,
"main-window", "tab1", true,
"tab change when selected tab element was focused");
let paintWaiter;
if (gMultiProcessBrowser) {
paintWaiter = new Promise((resolve, reject) => {
browser2.addEventListener("MozAfterRemotePaint", function paintListener() {
browser2.removeEventListener("MozAfterRemotePaint", paintListener, false);
executeSoon(resolve);
}, false);
browser2.QueryInterface(Ci.nsIFrameLoaderOwner).frameLoader.requestNotifyAfterRemotePaint();
});
}
yield expectFocusShift(function () gBrowser.selectedTab = tab2,
"main-window", "tab2", true,
"another tab change when selected tab element was focused");
// When this a remote browser, wait for the paint on the second browser so that
// any post tab-switching stuff has time to complete before blurring the tab.
// Otherwise, the _adjustFocusAfterTabSwitch in tabbrowser gets confused and
// isn't sure what tab is really focused.
if (gMultiProcessBrowser) {
yield paintWaiter;
}
yield expectFocusShift(function () gBrowser.selectedTab.blur(),
"main-window", null, true,
"blurring tab element");
// focusing the url field should switch active focus away from the browser but
// not clear what would be the focus in the browser
focusElementInChild("button1", "focus");
yield expectFocusShift(function () gURLBar.focus(),
"main-window", "urlbar", true,
"focusedWindow after url field focused");
focused = yield getFocusedElementForBrowser(browser1, true);
is(focused, "Focus is button1", "focusedElement after url field focused, first browser");
focused = yield getFocusedElementForBrowser(browser2, true);
is(focused, "Focus is button2", "focusedElement after url field focused, second browser");
yield expectFocusShift(function () gURLBar.blur(),
"main-window", null, true,
"blurring url field");
// when a chrome element is focused, switching tabs to a tab with a button
// with the current focus should focus the button
yield expectFocusShift(function () gBrowser.selectedTab = tab1,
"window1", "button1", true,
"after tab change, focus in url field, button focused in new tab");
focused = yield getFocusedElementForBrowser(browser1, false);
is(focused, "Focus is button1", "after switch tab, focus in unfocused tab, first browser");
focused = yield getFocusedElementForBrowser(browser2, true);
is(focused, "Focus is button2", "after switch tab, focus in unfocused tab, second browser");
// blurring an element in the current tab should clear the active focus
yield expectFocusShift(() => focusElementInChild("button1", "blur"),
"window1", null, true,
"after blur in focused tab");
focused = yield getFocusedElementForBrowser(browser1, false);
is(focused, "Focus is <none>", "focusedWindow after blur in focused tab, child");
focusedWindow = {};
is(fm.getFocusedElementForWindow(window, false, focusedWindow), browser1, "focusedElement after blur in focused tab, parent");
// blurring an non-focused url field should have no effect
yield expectFocusShift(function () gURLBar.blur(),
"window1", null, false,
"after blur in unfocused url field");
focusedWindow = {};
is(fm.getFocusedElementForWindow(window, false, focusedWindow), browser1, "focusedElement after blur in unfocused url field");
// switch focus to a tab with a currently focused element
yield expectFocusShift(function () gBrowser.selectedTab = tab2,
"window2", "button2", true,
"after switch from unfocused to focused tab");
focused = yield getFocusedElementForBrowser(browser2, true);
is(focused, "Focus is button2", "focusedElement after switch from unfocused to focused tab");
// clearing focus on the chrome window should switch the focus to the
// chrome window
yield expectFocusShift(function () fm.clearFocus(window),
"main-window", null, true,
"after switch to chrome with no focused element");
focusedWindow = {};
is(fm.getFocusedElementForWindow(window, false, focusedWindow), null, "focusedElement after switch to chrome with no focused element");
// switch focus to another tab when neither have an active focus
yield expectFocusShift(function () gBrowser.selectedTab = tab1,
"window1", null, true,
"focusedWindow after tab switch from no focus to no focus");
focused = yield getFocusedElementForBrowser(browser1, false);
is(focused, "Focus is <none>", "after tab switch from no focus to no focus, first browser");
focused = yield getFocusedElementForBrowser(browser2, true);
is(focused, "Focus is button2", "after tab switch from no focus to no focus, second browser");
// next, check whether navigating forward, focusing the urlbar and then
// navigating back maintains the focus in the urlbar.
yield expectFocusShift(() => focusElementInChild("button1", "focus"),
"window1", "button1", true,
"focus button");
yield promiseTabLoadEvent(tab1, "data:text/html," + escape(testPage3));
// now go back again
gURLBar.focus();
yield new Promise((resolve, reject) => {
window.addEventListener("pageshow", function navigationOccured(event) {
window.removeEventListener("pageshow", navigationOccured, true);
resolve();
}, true);
document.getElementById('Browser:Back').doCommand();
});
is(window.document.activeElement, gURLBar.inputField, "urlbar still focused after navigating back");
// Document navigation with F6 does not yet work in mutli-process browsers.
if (!gMultiProcessBrowser) {
gURLBar.focus();
_browser_tabfocus_test_events = "";
_browser_tabfocus_test_lastfocus = gURLBar;
_browser_tabfocus_test_lastfocuswindow = window;
actualEvents = [];
_browser_tabfocus_test_lastfocus = "urlbar";
_browser_tabfocus_test_lastfocuswindow = "main-window";
yield expectFocusShift(function () EventUtils.synthesizeKey("VK_F6", { }),
"window1", "html1",
true, "switch document forward with f6");
expectFocusShift(function () EventUtils.synthesizeKey("VK_F6", { }),
browser1.contentWindow, browser1.contentDocument.documentElement,
true, "switch document forward with f6");
EventUtils.synthesizeKey("VK_F6", { });
is(fm.focusedWindow, window, "switch document forward again with f6");
@ -159,120 +354,207 @@ function test() {
EventUtils.synthesizeKey("VK_F6", { });
is(fm.focusedWindow, window, "switch document forward again with f6 when browser non-focusable");
window.removeEventListener("focus", _browser_tabfocus_test_eventOccured, true);
window.removeEventListener("blur", _browser_tabfocus_test_eventOccured, true);
// next, check whether navigating forward, focusing the urlbar and then
// navigating back maintains the focus in the urlbar.
browser1.addEventListener("pageshow", _browser_tabfocus_navigation_test_eventOccured, true);
button1.focus();
browser1.contentWindow.location = testPage3;
browser1.style.MozUserFocus = "normal";
browser1.clientWidth;
}
browser1.addEventListener("load", check, true);
browser2.addEventListener("load", check, true);
browser1.contentWindow.location = testPage1;
browser2.contentWindow.location = testPage2;
}
window.removeEventListener("focus", _browser_tabfocus_test_eventOccured, true);
window.removeEventListener("blur", _browser_tabfocus_test_eventOccured, true);
var _browser_tabfocus_test_lastfocus;
var _browser_tabfocus_test_lastfocuswindow = null;
var _browser_tabfocus_test_events = "";
gBrowser.removeCurrentTab();
gBrowser.removeCurrentTab();
finish();
});
function getWindowDocId(target)
{
if (target == browser1.contentWindow || target == browser1.contentDocument) {
return "window1";
}
if (target == browser2.contentWindow || target == browser2.contentDocument) {
return "window2";
}
return "main-window";
}
function _browser_tabfocus_test_eventOccured(event)
{
var id;
// Some focus events from the child bubble up? Ignore them.
if (Cu.isCrossProcessWrapper(event.originalTarget))
return;
if (event.target instanceof Window)
id = event.originalTarget.document.documentElement.id + "-window";
id = getWindowDocId(event.originalTarget) + "-window";
else if (event.target instanceof Document)
id = event.originalTarget.documentElement.id + "-document";
id = getWindowDocId(event.originalTarget) + "-document";
else if (event.target.id == "urlbar" && event.originalTarget.localName == "input")
id = "urlbar";
else if (event.originalTarget.localName == "browser")
id = (event.originalTarget == browser1) ? "browser1" : "browser2";
else if (event.originalTarget.localName == "tab")
id = (event.originalTarget == tab1) ? "tab1" : "tab2";
else
id = event.originalTarget.id;
if (_browser_tabfocus_test_events)
_browser_tabfocus_test_events += " ";
_browser_tabfocus_test_events += event.type + ": " + id;
}
function _browser_tabfocus_navigation_test_eventOccured(event)
{
if (event.target instanceof Document) {
var contentwin = event.target.defaultView;
if (contentwin.location.toString().indexOf("3") > 0) {
// just moved forward, so focus the urlbar and go back
gURLBar.focus();
setTimeout(function () contentwin.history.back(), 0);
}
else if (contentwin.location.toString().indexOf("2") > 0) {
event.currentTarget.removeEventListener("pageshow", _browser_tabfocus_navigation_test_eventOccured, true);
is(window.document.activeElement, gURLBar.inputField, "urlbar still focused after navigating back");
gBrowser.removeCurrentTab();
gBrowser.removeCurrentTab();
finish();
}
}
actualEvents.push(event.type + ": " + id);
compareFocusResults();
}
function getId(element)
{
if (!element) {
return null;
}
if (element.localName == "browser") {
return element == browser1 ? "browser1" : "browser2";
}
if (element.localName == "tab") {
return element == tab1 ? "tab1" : "tab2";
}
return (element.localName == "input") ? "urlbar" : element.id;
}
function expectFocusShift(callback, expectedWindow, expectedElement, focusChanged, testid)
function compareFocusResults()
{
var expectedEvents = "";
if (!currentPromiseResolver)
return;
if (actualEvents.length < expectedEvents.length)
return;
for (let e = 0; e < expectedEvents.length; e++) {
// When remote browsers are used, the child process events can occur before or after the
// browser focusing. Allow either order.
if (gMultiProcessBrowser && actualEvents[e] != expectedEvents[e] &&
actualEvents[e].startsWith("focus: browser")) {
// Ensure that this event is expected later, and remove the later expected event and
// reinsert it at the current index.
let foundLaterIndex = expectedEvents.indexOf(actualEvents[e], e + 1);
if (foundLaterIndex > e) {
expectedEvents.splice(e, 0, expectedEvents.splice(foundLaterIndex, 1)[0]);
}
}
is(actualEvents[e], expectedEvents[e], currentTestName + " events [event " + e + "]");
}
actualEvents = [];
// Use executeSoon as this will be called during a focus/blur event handler
executeSoon(() => {
let matchWindow = window;
if (gMultiProcessBrowser) {
is(_expectedWindow, "main-window", "main-window is always expected");
}
else if (_expectedWindow != "main-window") {
matchWindow = (_expectedWindow == "window1" ? browser1.contentWindow : browser2.contentWindow);
}
var focusedElement = fm.focusedElement;
is(getId(focusedElement), _expectedElement, currentTestName + " focusedElement");
is(fm.focusedWindow, matchWindow, currentTestName + " focusedWindow");
var focusedWindow = {};
is(getId(fm.getFocusedElementForWindow(matchWindow, false, focusedWindow)),
_expectedElement, currentTestName + " getFocusedElementForWindow");
is(focusedWindow.value, matchWindow, currentTestName + " getFocusedElementForWindow frame");
is(matchWindow.document.hasFocus(), true, currentTestName + " hasFocus");
var expectedActive = _expectedElement;
if (!expectedActive) {
expectedActive = matchWindow.document instanceof XULDocument ?
"main-window" : getId(matchWindow.document.body);
}
is(getId(matchWindow.document.activeElement), expectedActive, currentTestName + " activeElement");
currentPromiseResolver();
currentPromiseResolver = null;
});
}
function* expectFocusShift(callback, expectedWindow, expectedElement, focusChanged, testid)
{
currentPromiseResolver = null;
currentTestName = testid;
expectedEvents = [];
if (focusChanged) {
if (_browser_tabfocus_test_lastfocus)
expectedEvents += "blur: " + getId(_browser_tabfocus_test_lastfocus);
_expectedElement = expectedElement;
_expectedWindow = expectedWindow;
// When the content is in a child process, the expected element in the chrome window
// will always be the urlbar or a browser element.
if (gMultiProcessBrowser) {
if (_expectedWindow == "window1") {
_expectedElement = "browser1";
}
else if (_expectedWindow == "window2") {
_expectedElement = "browser2";
}
_expectedWindow = "main-window";
}
if (gMultiProcessBrowser && _browser_tabfocus_test_lastfocuswindow != "main-window" &&
_browser_tabfocus_test_lastfocuswindow != expectedWindow) {
let browserid = _browser_tabfocus_test_lastfocuswindow == "window1" ? "browser1" : "browser2";
expectedEvents.push("blur: " + browserid);
}
var newElementIsFocused = (expectedElement && !expectedElement.startsWith("html"));
if (newElementIsFocused && gMultiProcessBrowser &&
_browser_tabfocus_test_lastfocuswindow != "main-window" &&
expectedWindow == "main-window") {
// When switching from a child to a chrome element, the focus on the element will arrive first.
expectedEvents.push("focus: " + expectedElement);
newElementIsFocused = false;
}
if (_browser_tabfocus_test_lastfocus && _browser_tabfocus_test_lastfocus != _expectedElement)
expectedEvents.push("blur: " + _browser_tabfocus_test_lastfocus);
if (_browser_tabfocus_test_lastfocuswindow &&
_browser_tabfocus_test_lastfocuswindow != expectedWindow) {
if (expectedEvents)
expectedEvents += " ";
var windowid = _browser_tabfocus_test_lastfocuswindow.document.documentElement.id;
expectedEvents += "blur: " + windowid + "-document " +
"blur: " + windowid + "-window";
if (!gMultiProcessBrowser || _browser_tabfocus_test_lastfocuswindow != "main-window") {
expectedEvents.push("blur: " + _browser_tabfocus_test_lastfocuswindow + "-document");
expectedEvents.push("blur: " + _browser_tabfocus_test_lastfocuswindow + "-window");
}
}
if (expectedWindow && _browser_tabfocus_test_lastfocuswindow != expectedWindow) {
if (expectedEvents)
expectedEvents += " ";
var windowid = expectedWindow.document.documentElement.id;
expectedEvents += "focus: " + windowid + "-document " +
"focus: " + windowid + "-window";
if (gMultiProcessBrowser && expectedWindow != "main-window") {
let browserid = expectedWindow == "window1" ? "browser1" : "browser2";
expectedEvents.push("focus: " + browserid);
}
if (!gMultiProcessBrowser || expectedWindow != "main-window") {
expectedEvents.push("focus: " + expectedWindow + "-document");
expectedEvents.push("focus: " + expectedWindow + "-window");
}
}
if (expectedElement && expectedElement != expectedElement.ownerDocument.documentElement) {
if (expectedEvents)
expectedEvents += " ";
expectedEvents += "focus: " + getId(expectedElement);
if (newElementIsFocused) {
expectedEvents.push("focus: " + expectedElement);
}
_browser_tabfocus_test_lastfocus = expectedElement;
_browser_tabfocus_test_lastfocuswindow = expectedWindow;
}
callback();
return new Promise((resolve, reject) => {
currentPromiseResolver = resolve;
callback();
is(_browser_tabfocus_test_events, expectedEvents, testid + " events");
_browser_tabfocus_test_events = "";
var fm = Services.focus;
var focusedElement = fm.focusedElement;
is(focusedElement ? getId(focusedElement) : "none",
expectedElement ? getId(expectedElement) : "none", testid + " focusedElement");
is(fm.focusedWindow, expectedWindow, testid + " focusedWindow");
var focusedWindow = {};
is(fm.getFocusedElementForWindow(expectedWindow, false, focusedWindow),
expectedElement, testid + " getFocusedElementForWindow");
is(focusedWindow.value, expectedWindow, testid + " getFocusedElementForWindow frame");
is(expectedWindow.document.hasFocus(), true, testid + " hasFocus");
var expectedActive = expectedElement;
if (!expectedActive)
expectedActive = expectedWindow.document instanceof XULDocument ?
expectedWindow.document.documentElement : expectedWindow.document.body;
is(expectedWindow.document.activeElement, expectedActive, testid + " activeElement");
// No events are expected, so resolve the promise immediately.
if (!expectedEvents.length) {
currentPromiseResolver();
currentPromiseResolver = null;
}
});
}