Bug 1295072 - Focus urlbar after opening an empty new tab r=kmag
MozReview-Commit-ID: 3dzCzHTxQrh
--- a/browser/components/extensions/ext-tabs.js
+++ b/browser/components/extensions/ext-tabs.js
@@ -413,16 +413,20 @@ extensions.registerSchemaAPI("tabs", "ad
if (createProperties.index !== null) {
window.gBrowser.moveTabTo(tab, createProperties.index);
}
if (createProperties.pinned) {
window.gBrowser.pinTab(tab);
}
+ if (active && !url) {
+ window.focusAndSelectUrlBar();
+ }
+
if (createProperties.url && createProperties.url !== window.BROWSER_NEW_TAB_URL) {
// We can't wait for a location change event for about:newtab,
// since it may be pre-rendered, in which case its initial
// location change event has already fired.
// Mark the tab as initializing, so that operations like
// `executeScript` wait until the requested URL is loaded in
// the tab before dispatching messages to the inner window
--- a/browser/components/extensions/test/browser/browser_ext_tabs_create.js
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_create.js
@@ -1,13 +1,13 @@
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
-add_task(function* () {
+add_task(function* test_create_options() {
let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:robots");
gBrowser.selectedTab = tab;
// TODO: Multiple windows.
// Using pre-loaded new tab pages interferes with onUpdated events.
// It probably shouldn't.
SpecialPowers.setBoolPref("browser.newtab.preload", false);
@@ -159,8 +159,49 @@ add_task(function* () {
yield extension.startup();
yield extension.awaitFinish("tabs.create");
yield extension.unload();
yield BrowserTestUtils.removeTab(tab);
});
+add_task(function* test_urlbar_focus() {
+ const extension = ExtensionTestUtils.loadExtension({
+ background() {
+ browser.test.onMessage.addListener(async (cmd, ...args) => {
+ const result = await browser.tabs[cmd](...args);
+ browser.test.sendMessage("result", result);
+ });
+ },
+ });
+
+ yield extension.startup();
+
+ // Test content is focused after opening a regular url
+ extension.sendMessage("create", {url: "https://example.com"});
+ const tab1 = yield extension.awaitMessage("result");
+
+ is(document.activeElement.tagName, "browser", "Content focused after opening a web page");
+
+ extension.sendMessage("remove", tab1.id);
+ yield extension.awaitMessage("result");
+
+ // Test urlbar is focused after opening an empty tab
+ extension.sendMessage("create", {});
+ const tab2 = yield extension.awaitMessage("result");
+
+ const active = document.activeElement;
+ info(`Active element: ${active.tagName}, id: ${active.id}, class: ${active.className}`);
+
+ const parent = active.parentNode;
+ info(`Parent element: ${parent.tagName}, id: ${parent.id}, class: ${parent.className}`);
+
+ info(`After opening an empty tab, gURLBar.focused: ${gURLBar.focused}`);
+
+ is(active.tagName, "html:input", "Input element focused");
+ ok(active.classList.contains("urlbar-input"), "Urlbar focused");
+
+ extension.sendMessage("remove", tab2.id);
+ yield extension.awaitMessage("result");
+
+ yield extension.unload();
+});