Bug 1054776: Ctrl+K should focus the search bar if it is in the toolbar. r=adw
authorAlex Bardas <abardas@mozilla.com>
Mon, 18 Aug 2014 17:27:33 -0700
changeset 200174 11d845395638751d789a18ad41d58cd0e05508b1
parent 200173 c1754ed575cb7657cc03028fe1a162f872ae0f3e
child 200175 976ddf70355ad91b16bec1feb83b5e6ff63ab802
push id8264
push userdwillcoxon@mozilla.com
push dateTue, 19 Aug 2014 00:28:08 +0000
treeherderfx-team@976ddf70355a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw
bugs1054776
milestone34.0a1
Bug 1054776: Ctrl+K should focus the search bar if it is in the toolbar. r=adw
browser/base/content/browser.js
browser/base/content/test/general/browser_aboutHome.js
browser/base/content/test/newtab/browser_newtab_search.js
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -3092,26 +3092,26 @@ const BrowserSearch = {
         win = window.openDialog(getBrowserURL(), "_blank",
                                 "chrome,all,dialog=no", "about:blank");
         Services.obs.addObserver(observer, "browser-delayed-startup-finished", false);
       }
       return;
     }
 #endif
     let openSearchPageIfFieldIsNotActive = function(aSearchBar) {
-      let doc = gBrowser.selectedBrowser.contentDocument;
-      let url = doc.documentURI.toLowerCase();
-      let mm = gBrowser.selectedBrowser.messageManager;
-
-      if (url === "about:home") {
-        AboutHome.focusInput(mm);
-      } else if (url === "about:newtab") {
-        ContentSearch.focusInput(mm);
-      } else if (!aSearchBar || document.activeElement != aSearchBar.textbox.inputField) {
-        openUILinkIn("about:home", "current");
+      if (!aSearchBar || document.activeElement != aSearchBar.textbox.inputField) {
+        let url = gBrowser.currentURI.spec.toLowerCase();
+        let mm = gBrowser.selectedBrowser.messageManager;
+        if (url === "about:home") {
+          AboutHome.focusInput(mm);
+        } else if (url === "about:newtab") {
+          ContentSearch.focusInput(mm);
+        } else {
+          openUILinkIn("about:home", "current");
+        }
       }
     };
 
     let searchBar = this.searchBar;
     let placement = CustomizableUI.getPlacementOfWidget("search-container");
     let focusSearchBar = () => {
       searchBar = this.searchBar;
       searchBar.select();
--- a/browser/base/content/test/general/browser_aboutHome.js
+++ b/browser/base/content/test/general/browser_aboutHome.js
@@ -415,31 +415,51 @@ let gTests = [
       // Empty the search input, causing the suggestions to be hidden.
       EventUtils.synthesizeKey("a", { accelKey: true });
       EventUtils.synthesizeKey("VK_DELETE", {});
       ok(table.hidden, "Search suggestion table hidden");
     });
   }
 },
 {
-  desc: "Cmd+k should focus the search bar element",
-  setup: function () {},
+  desc: "Cmd+k should focus the search box in the page when the search box in the toolbar is absent",
+  setup: function () {
+    // Remove the search bar from toolbar
+    CustomizableUI.removeWidgetFromArea("search-container");
+  },
   run: Task.async(function* () {
     let doc = gBrowser.selectedTab.linkedBrowser.contentDocument;
     let logo = doc.getElementById("brandLogo");
     let searchInput = doc.getElementById("searchText");
 
     EventUtils.synthesizeMouseAtCenter(logo, {});
     isnot(searchInput, doc.activeElement, "Search input should not be the active element.");
 
     EventUtils.synthesizeKey("k", { accelKey: true });
     yield promiseWaitForCondition(() => doc.activeElement === searchInput);
     is(searchInput, doc.activeElement, "Search input should be the active element.");
+    CustomizableUI.reset();
   })
 },
+{
+  desc: "Cmd+k should focus the search box in the toolbar when it's present",
+  setup: function () {},
+  run: Task.async(function* () {
+    let logo = gBrowser.selectedTab.linkedBrowser.contentDocument.getElementById("brandLogo");
+    let doc = window.document;
+    let searchInput = doc.getElementById("searchbar").textbox.inputField;
+
+    EventUtils.synthesizeMouseAtCenter(logo, {});
+    isnot(searchInput, doc.activeElement, "Search bar should not be the active element.");
+
+    EventUtils.synthesizeKey("k", { accelKey: true });
+    yield promiseWaitForCondition(() => doc.activeElement === searchInput);
+    is(searchInput, doc.activeElement, "Search bar should be the active element.");
+  })
+}
 
 ];
 
 function test()
 {
   waitForExplicitFinish();
   requestLongerTimeout(2);
   ignoreAllUncaughtExceptions();
--- a/browser/base/content/test/newtab/browser_newtab_search.js
+++ b/browser/base/content/test/newtab/browser_newtab_search.js
@@ -181,25 +181,34 @@ function runTests() {
   yield undefined;
   yield suggestionsPromise.then(TestRunner.next);
 
   // Empty the search input, causing the suggestions to be hidden.
   EventUtils.synthesizeKey("a", { accelKey: true });
   EventUtils.synthesizeKey("VK_DELETE", {});
   ok(table.hidden, "Search suggestion table hidden");
 
-  // Focus a different element than the search input.
+  // Remove the search bar from toolbar
+  CustomizableUI.removeWidgetFromArea("search-container");
+  // Focus a different element than the search input from the page.
   let btn = getContentDocument().getElementById("newtab-customize-button");
   yield promiseClick(btn).then(TestRunner.next);
 
   isnot(input, getContentDocument().activeElement, "Search input should not be focused");
-  // Test that Ctrl/Cmd + K will focus the input field.
+  // Test that Ctrl/Cmd + K will focus the input field from the page.
   EventUtils.synthesizeKey("k", { accelKey: true });
   yield promiseSearchEvents(["FocusInput"]).then(TestRunner.next);
   is(input, getContentDocument().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.
+  let searchBar = gWindow.document.getElementById("searchbar");
+  EventUtils.synthesizeKey("k", { accelKey: true });
+  is(searchBar.textbox.inputField, gWindow.document.activeElement, "Toolbar's search bar should be focused");
 
   // Done.  Revert the current engine and remove the new engines.
   Services.search.currentEngine = oldCurrentEngine;
   yield promiseSearchEvents(["CurrentEngine"]).then(TestRunner.next);
 
   let events = [];
   for (let engine of gNewEngines) {
     Services.search.removeEngine(engine);