Bug 867550 - Make browser_aboutHome.js not fail if it starts before but runs past midnight; r=mak
authorTim Taubert <ttaubert@mozilla.com>
Fri, 12 Jul 2013 11:03:00 +0200
changeset 138373 6554c78cf0497599272d8900645486795ed803ff
parent 138372 011811f23b7f3c9b9ffce9bf0bddcdd94d0e3946
child 138374 0d144e4351b56bb9a7ae52da9c2f77dedb1dd7df
push id30945
push userryanvm@gmail.com
push dateFri, 12 Jul 2013 19:51:45 +0000
treeherdermozilla-inbound@35fca0404633 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs867550
milestone25.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 867550 - Make browser_aboutHome.js not fail if it starts before but runs past midnight; r=mak
browser/base/content/test/browser_aboutHome.js
--- a/browser/base/content/test/browser_aboutHome.js
+++ b/browser/base/content/test/browser_aboutHome.js
@@ -84,87 +84,55 @@ let gTests = [
        "Search engine logo's alt text is a nonempty string");
 
     isnot(altText, "undefined",
           "Search engine logo's alt text shouldn't be the string 'undefined'");
   }
 },
 
 {
-  desc: "Check that performing a search fires a search event.",
-  setup: function () { },
-  run: function () {
-    let deferred = Promise.defer();
-    let doc = gBrowser.contentDocument;
-
-    doc.addEventListener("AboutHomeSearchEvent", function onSearch(e) {
-      is(e.detail, doc.documentElement.getAttribute("searchEngineName"), "Detail is search engine name");
-
-      gBrowser.stop();
-      deferred.resolve();
-    }, true, true);
-
-    doc.getElementById("searchText").value = "it works";
-    doc.getElementById("searchSubmit").click();
-    return deferred.promise;
-  }
-},
-
-{
-  desc: "Check that performing a search records to Firefox Health Report.",
+  desc: "Check that performing a search fires a search event and records to " +
+        "Firefox Health Report.",
   setup: function () { },
   run: function () {
     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 numSearchesBefore = 0;
     let deferred = Promise.defer();
     let doc = gBrowser.contentDocument;
 
     // We rely on the listener in browser.js being installed and fired before
     // this one. If this ever changes, we should add an executeSoon() or similar.
     doc.addEventListener("AboutHomeSearchEvent", function onSearch(e) {
-      executeSoon(gBrowser.stop.bind(gBrowser));
-      let reporter = Components.classes["@mozilla.org/datareporting/service;1"]
-                                       .getService()
-                                       .wrappedJSObject
-                                       .healthReporter;
-      ok(reporter, "Health Reporter instance available.");
-
-      reporter.onInit().then(function onInit() {
-        let provider = reporter.getProvider("org.mozilla.searches");
-        ok(provider, "Searches provider is available.");
-
-        let engineName = doc.documentElement.getAttribute("searchEngineName");
-        let id = Services.search.getEngineByName(engineName).identifier;
+      let engineName = doc.documentElement.getAttribute("searchEngineName");
+      is(e.detail, engineName, "Detail is search engine name");
 
-        let m = provider.getMeasurement("counts", 2);
-        m.getValues().then(function onValues(data) {
-          let now = new Date();
-          ok(data.days.hasDay(now), "Have data for today.");
+      gBrowser.stop();
 
-          let day = data.days.getDay(now);
-          let field = id + ".abouthome";
-          ok(day.has(field), "Have data for about home on this engine.");
-
-          // Note the search from the previous test.
-          is(day.get(field), 2, "Have searches recorded.");
-
-          deferred.resolve();
-        });
-
+      getNumberOfSearches().then(num => {
+        is(num, numSearchesBefore + 1, "One more search recorded.");
+        deferred.resolve();
       });
     }, true, true);
 
-    doc.getElementById("searchText").value = "a search";
-    doc.getElementById("searchSubmit").click();
+    // Get the current number of recorded searches.
+    getNumberOfSearches().then(num => {
+      numSearchesBefore = num;
+
+      info("Perform a search.");
+      doc.getElementById("searchText").value = "a search";
+      doc.getElementById("searchSubmit").click();
+    });
+
     return deferred.promise;
   }
 },
 
 {
   desc: "Check snippets map is cleared if cached version is old",
   setup: function (aSnippetsMap)
   {
@@ -417,8 +385,59 @@ function promiseBrowserAttributes(aTab)
       }
     }
   });
   info("Add attributes observer");
   observer.observe(docElt, { attributes: true });
 
   return deferred.promise;
 }
+
+/**
+ * Retrieves the number of about:home searches recorded for the current day.
+ *
+ * @return {Promise} Returns a promise resolving to the number of searches.
+ */
+function getNumberOfSearches() {
+  let reporter = Components.classes["@mozilla.org/datareporting/service;1"]
+                                   .getService()
+                                   .wrappedJSObject
+                                   .healthReporter;
+  ok(reporter, "Health Reporter instance available.");
+
+  return reporter.onInit().then(function onInit() {
+    let provider = reporter.getProvider("org.mozilla.searches");
+    ok(provider, "Searches provider is available.");
+
+    let m = provider.getMeasurement("counts", 2);
+    return m.getValues().then(data => {
+      let now = new Date();
+      let yday = new Date(now);
+      yday.setDate(yday.getDate() - 1);
+
+      // Add the number of searches recorded yesterday to the number of searches
+      // recorded today. This makes the test not fail intermittently when it is
+      // run at midnight and we accidentally compare the number of searches from
+      // different days. Tests are always run with an empty profile so there
+      // are no searches from yesterday, normally. Should the test happen to run
+      // past midnight we make sure to count them in as well.
+      return getNumberOfSearchesByDate(data, now) +
+             getNumberOfSearchesByDate(data, yday);
+    });
+  });
+}
+
+function getNumberOfSearchesByDate(aData, aDate) {
+  if (aData.days.hasDay(aDate)) {
+    let doc = gBrowser.contentDocument;
+    let engineName = doc.documentElement.getAttribute("searchEngineName");
+    let id = Services.search.getEngineByName(engineName).identifier;
+
+    let day = aData.days.getDay(aDate);
+    let field = id + ".abouthome";
+
+    if (day.has(field)) {
+      return day.get(field) || 0;
+    }
+  }
+
+  return 0; // No records found.
+}