Bug 1308931 - Make accel+k focus the urlbar if search bar is not present. r=Dao
authorKaffeekaethe <katharina.utecht@student.hpi.de>
Wed, 02 Nov 2016 15:44:11 +0100
changeset 347390 5738d9db61023acb4a0fdb09af2001606f57b650
parent 347389 ca33d22a82418e7ba388839f09042801e1671b6e
child 347391 9bf565c746a01bff51ed3ce3c076267871ec04bf
push id10298
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:33:03 +0000
treeherdermozilla-aurora@7e29173b1641 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersDao
bugs1308931
milestone52.0a1
Bug 1308931 - Make accel+k focus the urlbar if search bar is not present. r=Dao
browser/base/content/browser.js
browser/base/content/test/general/browser_aboutHome.js
browser/base/content/test/newtab/browser.ini
browser/base/content/test/newtab/browser_newtab_search.js
browser/base/content/test/urlbar/browser.ini
browser/base/content/test/urlbar/browser_urlbarFocusedCmdK.js
browser/components/customizableui/test/browser_901207_searchbar_in_panel.js
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -3571,36 +3571,28 @@ const BrowserSearch = {
         }
         win = window.openDialog(getBrowserURL(), "_blank",
                                 "chrome,all,dialog=no", "about:blank");
         Services.obs.addObserver(observer, "browser-delayed-startup-finished", false);
       }
       return;
     }
 
-    let openSearchPageIfFieldIsNotActive = function(aSearchBar) {
+    let focusUrlBarIfSearchFieldIsNotActive = function(aSearchBar) {
       if (!aSearchBar || document.activeElement != aSearchBar.textbox.inputField) {
-        let url = gBrowser.currentURI.spec.toLowerCase();
-        let mm = gBrowser.selectedBrowser.messageManager;
-        let newTabRemoted = Services.prefs.getBoolPref("browser.newtabpage.remote");
-        let localNewTabEnabled = url === "about:newtab" && !newTabRemoted && NewTabUtils.allPages.enabled;
-        if (url === "about:home" || localNewTabEnabled) {
-          ContentSearch.focusInput(mm);
-        } else {
-          openUILinkIn("about:home", "current");
-        }
+        focusAndSelectUrlBar();
       }
     };
 
     let searchBar = this.searchBar;
     let placement = CustomizableUI.getPlacementOfWidget("search-container");
     let focusSearchBar = () => {
       searchBar = this.searchBar;
       searchBar.select();
-      openSearchPageIfFieldIsNotActive(searchBar);
+      focusUrlBarIfSearchFieldIsNotActive(searchBar);
     };
     if (placement && placement.area == CustomizableUI.AREA_PANEL) {
       // The panel is not constructed until the first time it is shown.
       PanelUI.show().then(focusSearchBar);
       return;
     }
     if (placement && placement.area == CustomizableUI.AREA_NAVBAR && searchBar &&
         searchBar.parentNode.getAttribute("overflowedItem") == "true") {
@@ -3610,17 +3602,17 @@ const BrowserSearch = {
       });
       return;
     }
     if (searchBar) {
       if (window.fullScreen)
         FullScreen.showNavToolbox();
       searchBar.select();
     }
-    openSearchPageIfFieldIsNotActive(searchBar);
+    focusUrlBarIfSearchFieldIsNotActive(searchBar);
   },
 
   /**
    * Loads a search results page, given a set of search terms. Uses the current
    * engine if the search bar is visible, or the default engine otherwise.
    *
    * @param searchText
    *        The search terms to use for the search.
--- a/browser/base/content/test/general/browser_aboutHome.js
+++ b/browser/base/content/test/general/browser_aboutHome.js
@@ -471,44 +471,16 @@ add_task(function* () {
       yield ContentTaskUtils.waitForCondition(() => doc.activeElement === searchInput,
         "Search input should be the active element.");
       is(searchInput.value, "a", "Search input should be 'a'.");
     });
   });
 });
 
 add_task(function* () {
-  info("Cmd+k should focus the search box in the page when the search box in the toolbar is absent");
-
-  // Remove the search bar from toolbar
-  CustomizableUI.removeWidgetFromArea("search-container");
-
-  yield BrowserTestUtils.withNewTab({ gBrowser, url: "about:home" }, function* (browser) {
-    yield BrowserTestUtils.synthesizeMouseAtCenter("#brandLogo", {}, browser);
-    yield ContentTask.spawn(browser, null, function* () {
-      let doc = content.document;
-      isnot(doc.getElementById("searchText"), doc.activeElement,
-        "Search input should not be the active element.");
-    });
-
-    EventUtils.synthesizeKey("k", { accelKey: true });
-
-    yield ContentTask.spawn(browser, null, function* () {
-      let doc = content.document;
-      let searchInput = doc.getElementById("searchText");
-
-      yield ContentTaskUtils.waitForCondition(() => doc.activeElement === searchInput,
-        "Search input should be the active element.");
-    });
-  });
-
-  CustomizableUI.reset();
-});
-
-add_task(function* () {
   info("Cmd+k should focus the search box in the toolbar when it's present");
 
   yield BrowserTestUtils.withNewTab({ gBrowser, url: "about:home" }, function* (browser) {
     yield BrowserTestUtils.synthesizeMouseAtCenter("#brandLogo", {}, browser);
 
     let doc = window.document;
     let searchInput = doc.getElementById("searchbar").textbox.inputField;
     isnot(searchInput, doc.activeElement, "Search bar should not be the active element.");
--- a/browser/base/content/test/newtab/browser.ini
+++ b/browser/base/content/test/newtab/browser.ini
@@ -40,11 +40,12 @@ support-files =
   searchEngineNoLogo.xml
   searchEngineFavicon.xml
   searchEngine1xLogo.xml
   searchEngine2xLogo.xml
   searchEngine1x2xLogo.xml
   ../general/searchSuggestionEngine.xml
   ../general/searchSuggestionEngine.sjs
 [browser_newtab_sponsored_icon_click.js]
+skip-if = true # Bug 1314619
 [browser_newtab_undo.js]
 [browser_newtab_unpin.js]
 [browser_newtab_update.js]
--- a/browser/base/content/test/newtab/browser_newtab_search.js
+++ b/browser/base/content/test/newtab/browser_newtab_search.js
@@ -173,75 +173,16 @@ add_task(function* () {
   EventUtils.synthesizeKey("a", { accelKey: true });
   EventUtils.synthesizeKey("VK_DELETE", {});
 
   yield ContentTask.spawn(gBrowser.selectedBrowser, {}, function* () {
     Assert.ok(content.document.getElementById("searchSuggestionTable").hidden,
       "Search suggestion table hidden");
   });
 
-  // Remove the search bar from toolbar
-  CustomizableUI.removeWidgetFromArea("search-container");
-  // Focus a different element than the search input from the page.
-  yield BrowserTestUtils.synthesizeMouseAtCenter("#newtab-customize-button", { }, gBrowser.selectedBrowser);
-
-  yield ContentTask.spawn(gBrowser.selectedBrowser, { }, function* () {
-    let input = content.document.getElementById("newtab-search-text");
-    Assert.notEqual(input, content.document.activeElement, "Search input should not be focused");
-  });
-
-  // Test that Ctrl/Cmd + K will focus the input field from the page.
-  let focusPromise = promiseSearchEvents(["FocusInput"]);
-  EventUtils.synthesizeKey("k", { accelKey: true });
-  yield focusPromise;
-
-  yield ContentTask.spawn(gBrowser.selectedBrowser, { }, function* () {
-    let input = content.document.getElementById("newtab-search-text");
-    Assert.equal(input, content.document.activeElement, "Search input should be focused");
-  });
-
-  // Reset changes made to toolbar
-  CustomizableUI.reset();
-
-  // Test that Ctrl/Cmd + K will focus the search bar from toolbar.
-  EventUtils.synthesizeKey("k", { accelKey: true });
-  let searchBar = document.getElementById("searchbar");
-  is(searchBar.textbox.inputField, document.activeElement, "Toolbar's search bar should be focused");
-
-  // Test that Ctrl/Cmd + K will focus the search bar from a new about:home page if
-  // the newtab is disabled from `NewTabUtils.allPages.enabled`.
-  let tab = yield* addNewTabPageTab();
-  // Remove the search bar from toolbar
-  CustomizableUI.removeWidgetFromArea("search-container");
-  NewTabUtils.allPages.enabled = false;
-  EventUtils.synthesizeKey("k", { accelKey: true });
-
-
-  let aboutHomeLoaded = new Promise(resolve => {
-    tab.linkedBrowser.addEventListener("AboutHomeLoadSnippetsCompleted", function loadListener(event) {
-      tab.linkedBrowser.removeEventListener("AboutHomeLoadSnippetsCompleted", loadListener, true);
-      resolve();
-    }, true, true);
-  });
-
-  tab.linkedBrowser.loadURI("about:home");
-  yield aboutHomeLoaded;
-
-  yield ContentTask.spawn(gBrowser.selectedBrowser, { }, function* () {
-    Assert.equal(content.document.documentURI.toLowerCase(), "about:home",
-      "New tab's uri should be about:home");
-    let searchInput = content.document.getElementById("searchText");
-    Assert.equal(searchInput, content.document.activeElement,
-      "Search input must be the selected element");
-  });
-
-  NewTabUtils.allPages.enabled = true;
-  CustomizableUI.reset();
-  yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
-
   // Done.  Revert the current engine and remove the new engines.
   searchEventsPromise = promiseSearchEvents(["CurrentEngine"]);
   Services.search.currentEngine = oldCurrentEngine;
   yield searchEventsPromise;
 
   let events = Array(gNewEngines.length).fill("CurrentState", 0, gNewEngines.length);
   searchEventsPromise = promiseSearchEvents(events);
 
--- a/browser/base/content/test/urlbar/browser.ini
+++ b/browser/base/content/test/urlbar/browser.ini
@@ -50,16 +50,17 @@ skip-if = os == 'linux' # Bug 1104755
 subsuite = clipboard
 support-files =
   authenticate.sjs
 [browser_urlbarDecode.js]
 [browser_urlbarDelete.js]
 [browser_urlbarEnter.js]
 [browser_urlbarEnterAfterMouseOver.js]
 skip-if = os == "linux" # Bug 1073339 - Investigate autocomplete test unreliability on Linux/e10s
+[browser_urlbarFocusedCmdK.js]
 [browser_urlbarHashChangeProxyState.js]
 [browser_urlbarKeepStateAcrossTabSwitches.js]
 [browser_urlbarOneOffs.js]
 [browser_urlbarPrivateBrowsingWindowChange.js]
 [browser_urlbarRaceWithTabs.js]
 [browser_urlbarRevert.js]
 [browser_urlbarSearchSingleWordNotification.js]
 [browser_urlbarSearchSuggestions.js]
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/urlbar/browser_urlbarFocusedCmdK.js
@@ -0,0 +1,17 @@
+/* Any copyright is dedicated to the Public Domain.
+* http://creativecommons.org/publicdomain/zero/1.0/ */
+
+add_task(function*() {
+  // Remove the search bar from toolbar
+  CustomizableUI.removeWidgetFromArea("search-container");
+
+  // Test that Ctrl/Cmd + K will focus the url bar
+  let focusPromise = BrowserTestUtils.waitForEvent(gURLBar, "focus");
+  EventUtils.synthesizeKey("k", { accelKey: true });
+  yield focusPromise;
+  Assert.equal(document.activeElement, gURLBar.inputField, "URL Bar should be focused");
+
+  // Reset changes made to toolbar
+  CustomizableUI.reset();
+});
+
--- a/browser/components/customizableui/test/browser_901207_searchbar_in_panel.js
+++ b/browser/components/customizableui/test/browser_901207_searchbar_in_panel.js
@@ -1,25 +1,14 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
-var openUILinkInCalled = false;
-var expectOpenUILinkInCall = false;
-this.originalOpenUILinkIn = openUILinkIn;
-openUILinkIn = (aUrl, aWhichTab) => {
-  is(aUrl, "about:home", "about:home should be requested to open.");
-  is(aWhichTab, "current", "Should use the current tab for the search page.");
-  openUILinkInCalled = true;
-  if (!expectOpenUILinkInCall) {
-    ok(false, "OpenUILinkIn was called when it shouldn't have been.");
-  }
-};
 logActiveElement();
 
 function* waitForSearchBarFocus()
 {
   let searchbar = document.getElementById("searchbar");
   yield waitForCondition(function () {
     logActiveElement();
     return document.activeElement === searchbar.textbox.inputField;
@@ -100,40 +89,16 @@ add_task(function*() {
   let placement = CustomizableUI.getPlacementOfWidget("search-container");
   is(placement.area, CustomizableUI.AREA_NAVBAR, "Should be in nav-bar");
 
   sendWebSearchKeyCommand();
 
   yield waitForSearchBarFocus();
 });
 
-// Ctrl+K should open the search page if the search bar has been customized out.
-add_task(function*() {
-  try {
-    expectOpenUILinkInCall = true;
-    CustomizableUI.removeWidgetFromArea("search-container");
-    let placement = CustomizableUI.getPlacementOfWidget("search-container");
-    is(placement, null, "Search container should be in palette");
-
-    openUILinkInCalled = false;
-
-    sendWebSearchKeyCommand();
-    yield waitForCondition(() => openUILinkInCalled);
-    ok(openUILinkInCalled, "The search page should have been opened.")
-    expectOpenUILinkInCall = false;
-  } catch (e) {
-    ok(false, e);
-  }
-  CustomizableUI.reset();
-});
-
-registerCleanupFunction(function() {
-  openUILinkIn = this.originalOpenUILinkIn;
-  delete this.originalOpenUILinkIn;
-});
 
 function sendWebSearchKeyCommand() {
   if (Services.appinfo.OS === "Darwin")
     EventUtils.synthesizeKey("k", { accelKey: true });
   else
     EventUtils.synthesizeKey("k", { ctrlKey: true });
 }