Bug 1062096 - browser_aboutHome should use a fake search engine instead of google to test FHR reporting. r=adw, a=test-only
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Thu, 30 Oct 2014 13:09:54 +0000
changeset 226005 e32540fb1289
parent 226004 00f9c65b2f83
child 226006 db6d19e2b8e6
push id4105
push userryanvm@gmail.com
push date2014-11-10 18:36 +0000
treeherdermozilla-beta@fa1706ebf845 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw, test-only
bugs1062096
milestone34.0
Bug 1062096 - browser_aboutHome should use a fake search engine instead of google to test FHR reporting. r=adw, a=test-only
browser/base/content/test/general/browser_aboutHome.js
--- a/browser/base/content/test/general/browser_aboutHome.js
+++ b/browser/base/content/test/general/browser_aboutHome.js
@@ -89,34 +89,43 @@ let gTests = [
   }
 },
 
 // Disabled on Linux for intermittent issues with FHR, see Bug 945667.
 {
   desc: "Check that performing a search fires a search event and records to " +
         "Firefox Health Report.",
   setup: function () { },
-  run: function () {
+  run: function* () {
     // Skip this test on Linux.
     if (navigator.platform.indexOf("Linux") == 0) {
       return Promise.resolve();
     }
 
     try {
       let cm = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
       cm.getCategoryEntry("healthreport-js-provider-default", "SearchesProvider");
     } catch (ex) {
       // Health Report disabled, or no SearchesProvider.
       return Promise.resolve();
     }
 
+    let engine = yield promiseNewEngine("searchSuggestionEngine.xml");
+    // Make this actually work in healthreport by giving it an ID:
+    engine.wrappedJSObject._identifier = 'org.mozilla.testsearchsuggestions';
+
+    let promise = promiseBrowserAttributes(gBrowser.selectedTab);
+    Services.search.currentEngine = engine;
+    yield promise;
+
     let numSearchesBefore = 0;
     let searchEventDeferred = Promise.defer();
     let doc = gBrowser.contentDocument;
     let engineName = doc.documentElement.getAttribute("searchEngineName");
+    is(engine.name, engineName, "Engine name in DOM should match engine we just added");
     let mm = gBrowser.selectedTab.linkedBrowser.messageManager;
 
     mm.loadFrameScript(TEST_CONTENT_HELPER, false);
 
     mm.addMessageListener("AboutHomeTest:CheckRecordedSearch", function (msg) {
       let data = JSON.parse(msg.data);
       is(data.engineName, engineName, "Detail is search engine name");
 
@@ -136,17 +145,26 @@ let gTests = [
       doc.getElementById("searchSubmit").click();
     });
 
     let expectedURL = Services.search.currentEngine.
                       getSubmission(searchStr, null, "homepage").
                       uri.spec;
     let loadPromise = waitForDocLoadAndStopIt(expectedURL);
 
-    return Promise.all([searchEventDeferred.promise, loadPromise]);
+    try {
+      yield Promise.all([searchEventDeferred.promise, loadPromise]);
+    } catch (ex) {
+      Cu.reportError(ex);
+      ok(false, "An error occurred waiting for the search to be performed: " + ex);
+    } finally {
+      try {
+        Services.search.removeEngine(engine);
+      } catch (ex) {}
+    }
   }
 },
 
 {
   desc: "Check snippets map is cleared if cached version is old",
   setup: function (aSnippetsMap)
   {
     aSnippetsMap.set("snippets", "test");
@@ -631,17 +649,21 @@ function waitForLoad(cb) {
 
 function promiseNewEngine(basename) {
   info("Waiting for engine to be added: " + basename);
   let addDeferred = Promise.defer();
   let url = getRootDirectory(gTestPath) + basename;
   Services.search.addEngine(url, Ci.nsISearchEngine.TYPE_MOZSEARCH, "", false, {
     onSuccess: function (engine) {
       info("Search engine added: " + basename);
-      registerCleanupFunction(() => Services.search.removeEngine(engine));
+      registerCleanupFunction(() => {
+        try {
+          Services.search.removeEngine(engine);
+        } catch (ex) { /* Can't remove the engine more than once */ }
+      });
       addDeferred.resolve(engine);
     },
     onError: function (errCode) {
       ok(false, "addEngine failed with error code " + errCode);
       addDeferred.reject();
     },
   });
   return addDeferred.promise;