2009-12-11 21:17:40 -08:00
|
|
|
function test() {
|
|
|
|
waitForExplicitFinish();
|
|
|
|
|
2009-12-15 08:49:17 -08:00
|
|
|
let secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
|
|
|
|
.getService(Components.interfaces
|
|
|
|
.nsIScriptSecurityManager);
|
|
|
|
|
2009-12-11 21:17:40 -08:00
|
|
|
let fm = Components.classes["@mozilla.org/focus-manager;1"]
|
|
|
|
.getService(Components.interfaces.nsIFocusManager);
|
|
|
|
|
2009-12-13 12:46:38 -08:00
|
|
|
let tabs = [ gBrowser.selectedTab, gBrowser.addTab() ];
|
2009-12-11 21:17:40 -08:00
|
|
|
|
|
|
|
let testingList = [
|
|
|
|
{ uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><input id='target'></body>",
|
|
|
|
tagName: "INPUT", methodName: "focus" },
|
|
|
|
{ uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').select(); }, 10);\"><input id='target'></body>",
|
|
|
|
tagName: "INPUT", methodName: "select" },
|
|
|
|
{ uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><a href='about:blank' id='target'>anchor</a></body>",
|
|
|
|
tagName: "A", methodName: "focus" },
|
|
|
|
{ uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><button id='target'>button</button></body>",
|
|
|
|
tagName: "BUTTON", methodName: "focus" },
|
|
|
|
{ uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><select id='target'><option>item1</option></select></body>",
|
|
|
|
tagName: "SELECT", methodName: "focus" },
|
|
|
|
{ uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><textarea id='target'>textarea</textarea></body>",
|
|
|
|
tagName: "TEXTAREA", methodName: "focus" },
|
|
|
|
{ uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').select(); }, 10);\"><textarea id='target'>textarea</textarea></body>",
|
|
|
|
tagName: "TEXTAREA", methodName: "select" },
|
|
|
|
{ uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><label id='target'><input></label></body>",
|
|
|
|
tagName: "INPUT", methodName: "focus of label element" },
|
|
|
|
{ uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><fieldset><legend id='target'>legend</legend><input></fieldset></body>",
|
|
|
|
tagName: "INPUT", methodName: "focus of legend element" },
|
|
|
|
{ uri: "data:text/html,<body onload=\"setTimeout(function () {" +
|
|
|
|
" var element = document.getElementById('target');" +
|
|
|
|
" var event = document.createEvent('MouseEvent');" +
|
|
|
|
" event.initMouseEvent('click', true, true, window," +
|
|
|
|
" 1, 0, 0, 0, 0, false, false, false, false, 0, element);" +
|
|
|
|
" element.dispatchEvent(event); }, 10);\">" +
|
|
|
|
"<label id='target'><input></label></body>",
|
|
|
|
tagName: "INPUT", methodName: "click event on the label element" },
|
|
|
|
];
|
|
|
|
|
2010-06-21 05:37:34 -07:00
|
|
|
if (navigator.platform.indexOf("Mac") == -1) {
|
|
|
|
// clicking buttons doesn't focus on mac, so skip this test
|
|
|
|
testingList.push(
|
|
|
|
{ uri: "data:text/html,<body onload=\"setTimeout(function () {" +
|
|
|
|
" var element = document.getElementById('target');" +
|
|
|
|
" var event = document.createEvent('MouseEvent');" +
|
|
|
|
" event.initMouseEvent('mousedown', true, true, window," +
|
|
|
|
" 0, 0, 0, 0, 0, false, false, false, false, 0, element);" +
|
|
|
|
" element.dispatchEvent(event); }, 10);\">" +
|
|
|
|
"<button id='target'>button</button></body>",
|
|
|
|
tagName: "BUTTON", methodName: "mousedown event on the button element" });
|
|
|
|
}
|
|
|
|
|
2009-12-11 21:17:40 -08:00
|
|
|
let testingIndex = -1;
|
|
|
|
let canRetry;
|
|
|
|
let callback;
|
|
|
|
let loadedCount;
|
2010-01-30 05:05:42 -08:00
|
|
|
let setFocusToChrome;
|
2009-12-11 21:17:40 -08:00
|
|
|
|
|
|
|
function runNextTest() {
|
|
|
|
if (++testingIndex >= testingList.length) {
|
|
|
|
// cleaning-up...
|
2009-12-14 02:07:36 -08:00
|
|
|
gBrowser.addTab();
|
2009-12-11 21:17:40 -08:00
|
|
|
for (let i = 0; i < tabs.length; i++) {
|
|
|
|
gBrowser.removeTab(tabs[i]);
|
|
|
|
}
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback = doTest1;
|
2010-01-30 05:05:42 -08:00
|
|
|
loadTestPage(false);
|
2009-12-11 21:17:40 -08:00
|
|
|
}
|
|
|
|
|
2010-01-30 05:05:42 -08:00
|
|
|
function loadTestPage(aSetFocusToChrome) {
|
2009-12-11 21:17:40 -08:00
|
|
|
loadedCount = 0;
|
|
|
|
canRetry = 10;
|
2010-01-30 05:05:42 -08:00
|
|
|
setFocusToChrome = aSetFocusToChrome;
|
2009-12-11 21:17:40 -08:00
|
|
|
// Set the focus to the contents.
|
|
|
|
tabs[0].linkedBrowser.focus();
|
2010-01-30 05:05:42 -08:00
|
|
|
// 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.
|
2010-09-07 05:25:18 -07:00
|
|
|
document.getElementById("urlbar").focus();
|
2009-12-11 21:17:40 -08:00
|
|
|
}
|
2010-01-30 05:05:42 -08:00
|
|
|
onLoadComplete();
|
2009-12-11 21:17:40 -08:00
|
|
|
}
|
|
|
|
|
2010-01-30 05:05:42 -08:00
|
|
|
function onLoadBackgroundTab() {
|
|
|
|
tabs[1].linkedBrowser.removeEventListener("load", onLoadBackgroundTab, true);
|
|
|
|
onLoadComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onLoadComplete() {
|
2009-12-11 21:17:40 -08:00
|
|
|
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;
|
2010-01-30 05:05:42 -08:00
|
|
|
loadTestPage(true);
|
2009-12-11 21:17:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
2009-12-13 12:46:15 -08:00
|
|
|
}
|