Bug 1527947 - Update search results when the selected one-off button changes. r=dao
authorDrew Willcoxon <adw@mozilla.com>
Mon, 18 Feb 2019 22:32:05 +0000
changeset 459830 56992c2597a4b318ce074061d916444029a8e4e8
parent 459829 bc1a5ca9464b2342b79ad1642b6fbe51139916e7
child 459831 3e81867c3d0e905cbe835d032f46548dfdac6239
push id78418
push userdwillcoxon@mozilla.com
push dateMon, 18 Feb 2019 22:32:44 +0000
treeherderautoland@56992c2597a4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdao
bugs1527947
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1527947 - Update search results when the selected one-off button changes. r=dao Differential Revision: https://phabricator.services.mozilla.com/D20047
browser/components/urlbar/UrlbarView.jsm
browser/components/urlbar/tests/browser/browser_urlbarOneOffs.js
--- a/browser/components/urlbar/UrlbarView.jsm
+++ b/browser/components/urlbar/UrlbarView.jsm
@@ -47,19 +47,22 @@ class UrlbarView {
     this.panel.addEventListener("popupshowing", this);
     this.panel.addEventListener("popuphiding", this);
 
     this.controller.setView(this);
     this.controller.addQueryListener(this);
   }
 
   get oneOffSearchButtons() {
-    return this._oneOffSearchButtons ||
-      (this._oneOffSearchButtons =
-         new this.window.SearchOneOffs(this.panel.querySelector(".search-one-offs")));
+    if (!this._oneOffSearchButtons) {
+      this._oneOffSearchButtons =
+        new this.window.SearchOneOffs(this.panel.querySelector(".search-one-offs"));
+      this._oneOffSearchButtons.addEventListener("SelectedOneOffButtonChanged", this);
+    }
+    return this._oneOffSearchButtons;
   }
 
   /**
    * @returns {boolean}
    *   Whether the panel is open.
    */
   get isOpen() {
     return this.panel.state == "open" || this.panel.state == "showing";
@@ -517,16 +520,61 @@ class UrlbarView {
     } else if (this._oneOffSearchButtons) {
       this.oneOffSearchButtons.telemetryOrigin = null;
       this.oneOffSearchButtons.style.display = "none";
       this.oneOffSearchButtons.textbox = null;
       this.oneOffSearchButtons.view = null;
     }
   }
 
+  _on_SelectedOneOffButtonChanged() {
+    if (!this._queryContext) {
+      return;
+    }
+
+    // Update all search suggestion results to use the newly selected engine, or
+    // if no engine is selected, revert to their original engines.
+    let engine =
+      this._oneOffSearchButtons.selectedButton &&
+      this._oneOffSearchButtons.selectedButton.engine;
+    for (let i = 0; i < this._queryContext.results.length; i++) {
+      let result = this._queryContext.results[i];
+      if (result.type != UrlbarUtils.RESULT_TYPE.SEARCH ||
+          (!result.heuristic && !result.payload.suggestion)) {
+        continue;
+      }
+      if (engine) {
+        if (!result.payload.originalEngine) {
+          result.payload.originalEngine = result.payload.engine;
+        }
+        result.payload.engine = engine.name;
+      } else if (result.payload.originalEngine) {
+        result.payload.engine = result.payload.originalEngine;
+        delete result.payload.originalEngine;
+      }
+      let item = this._rows.children[i];
+      let action = item.querySelector(".urlbarView-action");
+      action.textContent =
+        bundle.formatStringFromName("searchWithEngine",
+          [(engine && engine.name) || result.payload.engine], 1);
+      // If we just changed the engine from the original engine and it had an
+      // icon, then make sure the result now uses the new engine's icon or
+      // failing that the default icon.  If we changed it back to the original
+      // engine, go back to the original or default icon.
+      let favicon = item.querySelector(".urlbarView-favicon");
+      if (engine && result.payload.icon) {
+        favicon.src =
+          (engine.iconURI && engine.iconURI.spec) ||
+          UrlbarUtils.ICON.SEARCH_GLASS;
+      } else if (!engine) {
+        favicon.src = result.payload.icon || UrlbarUtils.ICON.SEARCH_GLASS;
+      }
+    }
+  }
+
   _on_mousedown(event) {
     if (event.button == 2) {
       // Ignore right clicks.
       return;
     }
 
     let row = event.target;
     while (!row.classList.contains("urlbarView-row")) {
--- a/browser/components/urlbar/tests/browser/browser_urlbarOneOffs.js
+++ b/browser/components/urlbar/tests/browser/browser_urlbarOneOffs.js
@@ -160,20 +160,16 @@ add_task(async function() {
   assertState(0, -1, typedValue);
 
   await hidePopup();
 });
 
 // Checks that "Search with Current Search Engine" items are updated to "Search
 // with One-Off Engine" when a one-off is selected.
 add_task(async function searchWith() {
-  // TODO Bug 1527947: Implement "search with" change with button change.
-  if (UrlbarPrefs.get("quantumbar")) {
-    return;
-  }
   let typedValue = "foo";
   await promiseAutocompleteResultPopup(typedValue);
   let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
   assertState(0, -1, typedValue);
 
   Assert.equal(result.displayed.action,
                "Search with " + (await Services.search.getDefault()).name,
                "Sanity check: first result's action text");