Bug 1101670 - UITour: ability to set a search term and show the search popup. r=dolske a=gavin
authorFelipe Gomes <felipc@gmail.com>
Fri, 21 Nov 2014 19:19:31 -0200
changeset 234104 40e8c4ddca702173d081cd44ef92b9854d3de8cb
parent 234103 cf974839f00a668055a062938c41d04365a5c41b
child 234105 f74374acec82eafbcdd79adc0c36896d824e5841
push id1
push usersledru@mozilla.com
push dateThu, 04 Dec 2014 17:57:20 +0000
reviewersdolske, gavin
bugs1101670
milestone35.0
Bug 1101670 - UITour: ability to set a search term and show the search popup. r=dolske a=gavin
browser/components/search/content/search.xml
browser/modules/UITour.jsm
browser/modules/test/browser_UITour3.js
browser/modules/test/uitour.js
--- a/browser/components/search/content/search.xml
+++ b/browser/components/search/content/search.xml
@@ -628,16 +628,22 @@
 
       <method name="selectEngine">
         <body><![CDATA[
           // Override this method to avoid accidentally changing the default
           // engine using the keyboard shortcuts of the old UI.
         ]]></body>
       </method>
 
+      <method name="inputChanged">
+        <body><![CDATA[
+          this.updateGoButtonVisibility();
+        ]]></body>
+      </method>
+
       <method name="updateGoButtonVisibility">
         <body><![CDATA[
           document.getAnonymousElementByAttribute(this, "anonid",
                                                   "search-go-button")
                   .hidden = !this._textbox.value;
         ]]></body>
       </method>
 
@@ -656,18 +662,18 @@
           }
           else if (aShowOnlySettingsIfEmpty) {
             this.setAttribute("showonlysettings", "true");
           }
         ]]></body>
       </method>
     </implementation>
     <handlers>
-      <handler event="input" action="this.updateGoButtonVisibility();"/>
-      <handler event="drop" action="this.updateGoButtonVisibility();"/>
+      <handler event="input" action="this.inputChanged();"/>
+      <handler event="drop" action="this.inputChanged();"/>
       <handler event="focus">
       <![CDATA[
         if (this._textbox.value)
           this.openSuggestionsPanel();
       ]]></handler>
 
       <handler event="click">
       <![CDATA[
--- a/browser/modules/UITour.jsm
+++ b/browser/modules/UITour.jsm
@@ -517,16 +517,46 @@ this.UITour = {
         let value;
         try {
           value = Services.prefs.getComplexValue("browser.uitour.treatment." + name,
                                                  Ci.nsISupportsString).data;
         } catch (ex) {}
         this.sendPageCallback(contentDocument, data.callbackID, { value: value });
         break;
       }
+
+      case "setSearchTerm": {
+        let targetPromise = this.getTarget(window, "search");
+        targetPromise.then(target => {
+          let searchbar = target.node;
+          searchbar.value = data.term;
+          searchbar.inputChanged();
+        }).then(null, Cu.reportError);
+        break;
+      }
+
+      case "openSearchPanel": {
+        let targetPromise = this.getTarget(window, "search");
+        targetPromise.then(target => {
+          let searchbar = target.node;
+
+          if (searchbar.textbox.open) {
+            this.sendPageCallback(contentDocument, data.callbackID);
+          } else {
+            let onPopupShown = () => {
+              searchbar.textbox.popup.removeEventListener("popupshown", onPopupShown);
+              this.sendPageCallback(contentDocument, data.callbackID);
+            };
+
+            searchbar.textbox.popup.addEventListener("popupshown", onPopupShown);
+            searchbar.openSuggestionsPanel();
+          }
+        }).then(null, Cu.reportError);
+        break;
+      }
     }
 
     if (!this.originTabs.has(window))
       this.originTabs.set(window, new Set());
 
     this.originTabs.get(window).add(tab);
     tab.addEventListener("TabClose", this);
     tab.addEventListener("TabBecomingWindow", this);
--- a/browser/modules/test/browser_UITour3.js
+++ b/browser/modules/test/browser_UITour3.js
@@ -169,9 +169,48 @@ let tests = [
       ok(Components.isSuccessCode(rv), "Search service initialized");
       let engine = Services.search.defaultEngine;
       gContentAPI.getConfiguration("selectedSearchEngine", (data) => {
         is(data.searchEngineIdentifier, engine.identifier, "Correct engine identifier");
         done();
       });
     });
   },
+
+  function test_setSearchTerm(done) {
+    const TERM = "UITour Search Term";
+    gContentAPI.setSearchTerm(TERM);
+
+    let searchbar = document.getElementById("searchbar");
+    // The UITour gets to the searchbar element through a promise, so the value setting
+    // only happens after a tick.
+    waitForCondition(() => searchbar.value == TERM, done, "Correct term set");
+  },
+
+  function test_clearSearchTerm(done) {
+    gContentAPI.setSearchTerm("");
+
+    let searchbar = document.getElementById("searchbar");
+    // The UITour gets to the searchbar element through a promise, so the value setting
+    // only happens after a tick.
+    waitForCondition(() => searchbar.value == "", done, "Search term cleared");
+  },
+
+  function test_openSearchPanel(done) {
+    let searchbar = document.getElementById("searchbar");
+
+    // If suggestions are enabled, the panel will attempt to use the network to connect
+    // to the suggestions provider, causing the test suite to fail.
+    Services.prefs.setBoolPref("browser.search.suggest.enabled", false);
+    registerCleanupFunction(() => {
+      Services.prefs.clearUserPref("browser.search.suggest.enabled");
+    });
+
+    ok(!searchbar.textbox.open, "Popup starts as closed");
+    gContentAPI.openSearchPanel(() => {
+      ok(searchbar.textbox.open, "Popup was opened");
+      searchbar.textbox.closePopup();
+      ok(!searchbar.textbox.open, "Popup was closed");
+      done();
+    });
+  },
+
 ];
--- a/browser/modules/test/uitour.js
+++ b/browser/modules/test/uitour.js
@@ -208,16 +208,28 @@ if (typeof Mozilla == 'undefined') {
 	};
 
 	Mozilla.UITour.setDefaultSearchEngine = function(identifier) {
 		_sendEvent('setDefaultSearchEngine', {
 			identifier: identifier,
 		});
 	};
 
+	Mozilla.UITour.setSearchTerm = function(term) {
+		_sendEvent('setSearchTerm', {
+			term: term
+		});
+	};
+
+	Mozilla.UITour.openSearchPanel = function(callback) {
+		_sendEvent('openSearchPanel', {
+			callbackID: _waitForCallback(callback)
+		});
+	};
+
 	Mozilla.UITour.setTreatmentTag = function(name, value) {
 		_sendEvent('setTreatmentTag', {
 			name: name,
 			value: value
 		});
 	};
 
 	Mozilla.UITour.getTreatmentTag = function(name, callback) {