Bug 1097942 - add UITour mechanism for selecting an engine, r=Unfocused a=gavin
authorGavin Sharp <gavin@gavinsharp.com>
Wed, 12 Nov 2014 12:53:15 -0800
changeset 234100 eb8b2a6c5ea01628fceaacbebe7d90f738fdcef0
parent 234099 d334f8d9129746203424293d20c2488e91f58ed3
child 234101 0e019d4f460121f04fd4d7dbb65ca8dacb273894
push id1
push usersledru@mozilla.com
push dateThu, 04 Dec 2014 17:57:20 +0000
reviewersUnfocused, gavin
bugs1097942
milestone35.0
Bug 1097942 - add UITour mechanism for selecting an engine, r=Unfocused a=gavin
browser/modules/UITour.jsm
browser/modules/test/browser_UITour.js
browser/modules/test/uitour.js
--- a/browser/modules/UITour.jsm
+++ b/browser/modules/UITour.jsm
@@ -488,16 +488,22 @@ this.UITour = {
       case "addNavBarWidget": {
         // Add a widget to the toolbar
         let targetPromise = this.getTarget(window, data.name);
         targetPromise.then(target => {
           this.addNavBarWidget(target, contentDocument, data.callbackID);
         }).then(null, Cu.reportError);
         break;
       }
+
+      case "setDefaultSearchEngine": {
+        let enginePromise = this.selectSearchEngine(data.identifier);
+        enginePromise.catch(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);
@@ -1386,16 +1392,36 @@ this.UITour = {
     for (let mutation of aMutations) {
       // Remove both attributes at once and ignore remaining mutations to be proccessed.
       mutation.target.removeAttribute("width");
       mutation.target.removeAttribute("height");
       return;
     }
   },
 
+  selectSearchEngine(aID) {
+    return new Promise((resolve, reject) => {
+      Services.search.init((rv) => {
+        if (!Components.isSuccessCode(rv)) {
+          reject("selectSearchEngine: search service init failed: " + rv);
+          return;
+        }
+
+        let engines = Services.search.getVisibleEngines();
+        for (let engine of engines) {
+          if (engine.identifier == aID) {
+            Services.search.defaultEngine = engine;
+            return resolve();
+          }
+        }
+        reject("selectSearchEngine could not find engine with given ID");
+      });
+    });
+  },
+
   getAvailableSearchEngineTargets(aWindow) {
     return new Promise(resolve => {
       this.getTarget(aWindow, "search").then(searchTarget => {
         if (!searchTarget.node || this.targetIsInAppMenu(searchTarget))
           return resolve([]);
 
         Services.search.init(() => {
           let engines = Services.search.getVisibleEngines();
--- a/browser/modules/test/browser_UITour.js
+++ b/browser/modules/test/browser_UITour.js
@@ -366,15 +366,46 @@ let tests = [
 
           // Cleanup
           CustomizableUI.removeWidgetFromArea("panic-button");
           done();
         });
       });
     });
   },
+  function test_select_search_engine(done) {
+    Services.search.init(rv => {
+      if (!Components.isSuccessCode(rv)) {
+        ok(false, "search service init failed: " + rv);
+        done();
+        return;
+      }
+      let defaultEngine = Services.search.defaultEngine;
+      gContentAPI.getConfiguration("availableTargets", data => {
+        let searchEngines = data.targets.filter(t => t.startsWith("searchEngine-"));
+        let someOtherEngineID = searchEngines.filter(t => t != "searchEngine-" + defaultEngine.identifier)[0];
+        someOtherEngineID = someOtherEngineID.replace(/^searchEngine-/, "");
+
+        let observe = function (subject, topic, verb) {
+          info("browser-search-engine-modified: " + verb);
+          if (verb == "engine-current") {
+            is(Services.search.defaultEngine.identifier, someOtherEngineID, "correct engine was switched to");
+            done();
+          }
+        };
+        Services.obs.addObserver(observe, "browser-search-engine-modified", false);
+        registerCleanupFunction(() => {
+          // Clean up
+          Services.obs.removeObserver(observe, "browser-search-engine-modified");
+          Services.search.defaultEngine = defaultEngine;
+        });
+
+        gContentAPI.setDefaultSearchEngine(someOtherEngineID);
+      });
+    });
+  },
 
   // Make sure this test is last in the file so the appMenu gets left open and done will confirm it got tore down.
   function cleanupMenus(done) {
     gContentAPI.showMenu("appMenu");
     done();
   },
 ];
--- a/browser/modules/test/uitour.js
+++ b/browser/modules/test/uitour.js
@@ -202,9 +202,15 @@ if (typeof Mozilla == 'undefined') {
 
 	Mozilla.UITour.addNavBarWidget= function(name, callback) {
 		_sendEvent('addNavBarWidget', {
 			name: name,
 			callbackID: _waitForCallback(callback),
 		});
 	};
 
+	Mozilla.UITour.setDefaultSearchEngine = function(identifier) {
+		_sendEvent('setDefaultSearchEngine', {
+			identifier: identifier,
+		});
+	};
+
 })();