Bug 1100291 - Fix browser/base/content/test/general/browser_locationBarCommand.js to work with an async getShortcutOrURIAndPostData() r=margaret

This commit is contained in:
Tim Taubert 2015-03-09 14:23:36 +01:00
parent 0e23f5b03e
commit d895f40730

View File

@ -4,209 +4,211 @@
const TEST_VALUE = "example.com"; const TEST_VALUE = "example.com";
const START_VALUE = "example.org"; const START_VALUE = "example.org";
let gFocusManager = Services.focus; add_task(function* setup() {
function test() {
waitForExplicitFinish();
registerCleanupFunction(function () {
Services.prefs.clearUserPref("browser.altClickSave");
});
Services.prefs.setBoolPref("browser.altClickSave", true); Services.prefs.setBoolPref("browser.altClickSave", true);
runAltLeftClickTest(); registerCleanupFunction(() => {
} Services.prefs.clearUserPref("browser.altClickSave");
});
});
// Monkey patch saveURL to avoid dealing with file save code paths add_task(function* alt_left_click_test() {
var oldSaveURL = saveURL; info("Running test: Alt left click");
saveURL = function() {
// Monkey patch saveURL() to avoid dealing with file save code paths.
let oldSaveURL = saveURL;
let saveURLPromise = new Promise(resolve => {
saveURL = () => {
// Restore old saveURL() value.
saveURL = oldSaveURL;
resolve();
};
});
triggerCommand(true, {altKey: true});
yield saveURLPromise;
ok(true, "SaveURL was called"); ok(true, "SaveURL was called");
is(gURLBar.value, "", "Urlbar reverted to original value"); is(gURLBar.value, "", "Urlbar reverted to original value");
saveURL = oldSaveURL; });
runShiftLeftClickTest();
} add_task(function* shift_left_click_test() {
function runAltLeftClickTest() { info("Running test: Shift left click");
info("Running test: Alt left click");
triggerCommand(true, { altKey: true }); let newWindowPromise = promiseWaitForNewWindow();
} triggerCommand(true, {shiftKey: true});
let win = yield newWindowPromise;
// Wait for the initial browser to load.
let browser = win.gBrowser.selectedBrowser;
yield BrowserTestUtils.browserLoaded(browser);
function runShiftLeftClickTest() {
let listener = new BrowserWindowListener(getBrowserURL(), function(aWindow) {
Services.wm.removeListener(listener);
addPageShowListener(aWindow.gBrowser.selectedBrowser, function() {
executeSoon(function () {
info("URL should be loaded in a new window"); info("URL should be loaded in a new window");
is(gURLBar.value, "", "Urlbar reverted to original value"); is(gURLBar.value, "", "Urlbar reverted to original value");
is(gFocusManager.focusedElement, null, "There should be no focused element"); is(Services.focus.focusedElement, null, "There should be no focused element");
is(gFocusManager.focusedWindow, aWindow.gBrowser.contentWindow, "Content window should be focused"); is(Services.focus.focusedWindow, win.gBrowser.contentWindow, "Content window should be focused");
is(aWindow.gURLBar.textValue, TEST_VALUE, "New URL is loaded in new window"); is(win.gURLBar.textValue, TEST_VALUE, "New URL is loaded in new window");
aWindow.close(); // Cleanup.
yield promiseWindowClosed(win);
});
// Continue testing when the original window has focus again. add_task(function* right_click_test() {
whenWindowActivated(window, runNextTest); info("Running test: Right click on go button");
});
}, "http://example.com/");
});
Services.wm.addListener(listener);
info("Running test: Shift left click"); // Add a new tab.
triggerCommand(true, { shiftKey: true }); let tab = gBrowser.selectedTab = gBrowser.addTab("about:blank");
} yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
function runNextTest() { triggerCommand(true, {button: 2});
let test = gTests.shift();
if (!test) {
finish();
return;
}
info("Running test: " + test.desc); // Right click should do nothing (context menu will be shown).
// Tab will be blank if test.startValue is null
let tab = gBrowser.selectedTab = gBrowser.addTab(test.startValue);
addPageShowListener(gBrowser.selectedBrowser, function() {
triggerCommand(test.click, test.event);
test.check(tab);
// Clean up
while (gBrowser.tabs.length > 1)
gBrowser.removeTab(gBrowser.selectedTab)
runNextTest();
});
}
let gTests = [
{ desc: "Right click on go button",
click: true,
event: { button: 2 },
check: function(aTab) {
// Right click should do nothing (context menu will be shown)
is(gURLBar.value, TEST_VALUE, "Urlbar still has the value we entered"); is(gURLBar.value, TEST_VALUE, "Urlbar still has the value we entered");
}
},
{ desc: "Left click on go button", // Cleanup.
click: true, gBrowser.removeCurrentTab();
event: {}, });
check: checkCurrent
},
{ desc: "Ctrl/Cmd left click on go button", add_task(function* shift_accel_left_click_test() {
click: true, info("Running test: Shift+Ctrl/Cmd left click on go button");
event: { accelKey: true },
check: checkNewTab
},
{ desc: "Shift+Ctrl/Cmd left click on go button", // Add a new tab.
click: true, let tab = gBrowser.selectedTab = gBrowser.addTab("about:blank");
event: { accelKey: true, shiftKey: true }, yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
check: function(aTab) {
let loadStartedPromise = promiseLoadStarted();
triggerCommand(true, {accelKey: true, shiftKey: true});
yield loadStartedPromise;
// Check the load occurred in a new background tab.
info("URL should be loaded in a new background tab"); info("URL should be loaded in a new background tab");
is(gURLBar.value, "", "Urlbar reverted to original value"); is(gURLBar.value, "", "Urlbar reverted to original value");
ok(!gURLBar.focused, "Urlbar is no longer focused after urlbar command"); ok(!gURLBar.focused, "Urlbar is no longer focused after urlbar command");
is(gBrowser.selectedTab, aTab, "Focus did not change to the new tab"); is(gBrowser.selectedTab, tab, "Focus did not change to the new tab");
// Select the new background tab // Select the new background tab
gBrowser.selectedTab = gBrowser.selectedTab.nextSibling; gBrowser.selectedTab = gBrowser.selectedTab.nextSibling;
is(gURLBar.value, TEST_VALUE, "New URL is loaded in new tab"); is(gURLBar.value, TEST_VALUE, "New URL is loaded in new tab");
// Cleanup.
gBrowser.removeCurrentTab();
gBrowser.removeCurrentTab();
});
add_task(function* load_in_current_tab_test() {
let tests = [
{desc: "Simple return keypress"},
{desc: "Left click on go button", click: true},
{desc: "Ctrl/Cmd+Return keypress", event: {accelKey: true}},
{desc: "Alt+Return keypress in a blank tab", event: {altKey: true}}
];
for (let test of tests) {
info(`Running test: ${test.desc}`);
// Add a new tab.
let tab = gBrowser.selectedTab = gBrowser.addTab("about:blank");
yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
// Trigger a load and check it occurs in the current tab.
let loadStartedPromise = promiseLoadStarted();
triggerCommand(test.click || false, test.event || {});
yield loadStartedPromise;
info("URL should be loaded in the current tab");
is(gURLBar.value, TEST_VALUE, "Urlbar still has the value we entered");
is(Services.focus.focusedElement, null, "There should be no focused element");
is(Services.focus.focusedWindow, gBrowser.contentWindow, "Content window should be focused");
is(gBrowser.selectedTab, tab, "New URL was loaded in the current tab");
// Cleanup.
gBrowser.removeCurrentTab();
} }
}, });
{ desc: "Simple return keypress", add_task(function* load_in_new_tab_test() {
event: {}, let tests = [
check: checkCurrent {desc: "Ctrl/Cmd left click on go button", click: true, event: {accelKey: true}},
}, {desc: "Alt+Return keypress in a dirty tab", event: {altKey: true}, url: START_VALUE}
];
{ desc: "Alt+Return keypress in a blank tab", for (let test of tests) {
event: { altKey: true }, info(`Running test: ${test.desc}`);
check: checkCurrent
},
{ desc: "Alt+Return keypress in a dirty tab", // Add a new tab.
event: { altKey: true }, let tab = gBrowser.selectedTab = gBrowser.addTab(test.url || "about:blank");
check: checkNewTab, yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
startValue: START_VALUE
},
{ desc: "Ctrl/Cmd+Return keypress", // Trigger a load and check it occurs in the current tab.
event: { accelKey: true }, let tabSelectedPromise = promiseNewTabSelected();
check: checkCurrent triggerCommand(test.click || false, test.event || {});
yield tabSelectedPromise;
// Check the load occurred in a new tab.
info("URL should be loaded in a new focused tab");
is(gURLBar.value, TEST_VALUE, "Urlbar still has the value we entered");
is(Services.focus.focusedElement, null, "There should be no focused element");
is(Services.focus.focusedWindow, gBrowser.contentWindow, "Content window should be focused");
isnot(gBrowser.selectedTab, tab, "New URL was loaded in a new tab");
// Cleanup.
gBrowser.removeCurrentTab();
gBrowser.removeCurrentTab();
} }
] });
let gGoButton = document.getElementById("urlbar-go-button"); function triggerCommand(shouldClick, event) {
function triggerCommand(aClick, aEvent) {
gURLBar.value = TEST_VALUE; gURLBar.value = TEST_VALUE;
gURLBar.focus(); gURLBar.focus();
if (aClick) { if (shouldClick) {
is(gURLBar.getAttribute("pageproxystate"), "invalid", is(gURLBar.getAttribute("pageproxystate"), "invalid",
"page proxy state must be invalid for go button to be visible"); "page proxy state must be invalid for go button to be visible");
EventUtils.synthesizeMouseAtCenter(gGoButton, aEvent);
let goButton = document.getElementById("urlbar-go-button");
EventUtils.synthesizeMouseAtCenter(goButton, event);
} else {
EventUtils.synthesizeKey("VK_RETURN", event);
} }
else
EventUtils.synthesizeKey("VK_RETURN", aEvent);
} }
/* Checks that the URL was loaded in the current tab */ function promiseLoadStarted() {
function checkCurrent(aTab) { return new Promise(resolve => {
info("URL should be loaded in the current tab"); gBrowser.addTabsProgressListener({
is(gURLBar.value, TEST_VALUE, "Urlbar still has the value we entered"); onStateChange(browser, webProgress, req, flags, status) {
is(gFocusManager.focusedElement, null, "There should be no focused element"); if (flags & Ci.nsIWebProgressListener.STATE_START) {
is(gFocusManager.focusedWindow, gBrowser.contentWindow, "Content window should be focused"); gBrowser.removeTabsProgressListener(this);
is(gBrowser.selectedTab, aTab, "New URL was loaded in the current tab"); resolve();
} }
}
/* Checks that the URL was loaded in a new focused tab */ });
function checkNewTab(aTab) {
info("URL should be loaded in a new focused tab");
is(gURLBar.value, TEST_VALUE, "Urlbar still has the value we entered");
is(gFocusManager.focusedElement, null, "There should be no focused element");
is(gFocusManager.focusedWindow, gBrowser.contentWindow, "Content window should be focused");
isnot(gBrowser.selectedTab, aTab, "New URL was loaded in a new tab");
}
function addPageShowListener(browser, cb, expectedURL) {
browser.addEventListener("pageshow", function pageShowListener() {
info("pageshow: " + browser.currentURI.spec);
if (expectedURL && browser.currentURI.spec != expectedURL)
return; // ignore pageshows for non-expected URLs
browser.removeEventListener("pageshow", pageShowListener, false);
cb();
}); });
} }
function whenWindowActivated(win, cb) { function promiseNewTabSelected() {
if (Services.focus.activeWindow == win) { return new Promise(resolve => {
executeSoon(cb); gBrowser.tabContainer.addEventListener("TabSelect", function onSelect() {
return; gBrowser.tabContainer.removeEventListener("TabSelect", onSelect);
} resolve();
});
win.addEventListener("activate", function onActivate() {
win.removeEventListener("activate", onActivate);
executeSoon(cb);
}); });
} }
function BrowserWindowListener(aURL, aCallback) { function promiseWaitForNewWindow() {
this.callback = aCallback; return new Promise(resolve => {
this.url = aURL; let listener = {
} onOpenWindow(xulWindow) {
BrowserWindowListener.prototype = { let win = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor)
onOpenWindow: function(aXULWindow) {
let cb = () => this.callback(domwindow);
let domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow); .getInterface(Ci.nsIDOMWindow);
let numWait = 2; Services.wm.removeListener(listener);
function maybeRunCallback() { whenDelayedStartupFinished(win, () => resolve(win));
if (--numWait == 0)
cb();
}
whenWindowActivated(domwindow, maybeRunCallback);
whenDelayedStartupFinished(domwindow, maybeRunCallback);
}, },
onCloseWindow: function(aXULWindow) {},
onWindowTitleChange: function(aXULWindow, aNewTitle) {} onCloseWindow() {},
onWindowTitleChange() {}
};
Services.wm.addListener(listener);
});
} }