Bug 1029031 - Rewrite test to use tasks. r=MattN, a=sledru
authorGregory Szorc <gps@mozilla.com>
Wed, 25 Jun 2014 12:38:37 -0700
changeset 208471 28c71f97aaf13b0ec103383c8dbe7b8ae355f27a
parent 208470 990b3688423b1da56806c10eacec77718f74901c
child 208472 98a0b7def57a86c22b71d72b1797892a28bae59e
push id494
push userraliiev@mozilla.com
push dateMon, 25 Aug 2014 18:42:16 +0000
treeherdermozilla-release@a3cc3e46b571 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN, sledru
bugs1029031
milestone32.0a2
Bug 1029031 - Rewrite test to use tasks. r=MattN, a=sledru An upcoming patch will add more code to browser_urlbar_search_healthreport.js. We now have support for running tasks in bc tests, so let's use it to make my life easier.
browser/base/content/test/general/browser_urlbar_search_healthreport.js
--- a/browser/base/content/test/general/browser_urlbar_search_healthreport.js
+++ b/browser/base/content/test/general/browser_urlbar_search_healthreport.js
@@ -1,91 +1,64 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
-function test() {
-  waitForExplicitFinish();
+add_task(function* test_healthreport_search_recording() {
   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.
     ok(true, "Firefox Health Report is not enabled.");
-    finish();
     return;
   }
 
   let reporter = Cc["@mozilla.org/datareporting/service;1"]
                    .getService()
                    .wrappedJSObject
                    .healthReporter;
   ok(reporter, "Health Reporter available.");
-  reporter.onInit().then(function onInit() {
-    let provider = reporter.getProvider("org.mozilla.searches");
-    ok(provider, "Searches provider is available.");
-    let m = provider.getMeasurement("counts", 3);
+  yield reporter.onInit();
+  let provider = reporter.getProvider("org.mozilla.searches");
+  ok(provider, "Searches provider is available.");
+  let m = provider.getMeasurement("counts", 3);
 
-    m.getValues().then(function onData(data) {
-      let now = new Date();
-      let oldCount = 0;
-
-      // This will to be need changed if default search engine is not Google.
-      let field = "google.urlbar";
+  let data = yield m.getValues();
+  let now = new Date();
+  let oldCount = 0;
 
-      if (data.days.hasDay(now)) {
-        let day = data.days.getDay(now);
-        if (day.has(field)) {
-          oldCount = day.get(field);
-        }
-      }
+  // This will to be need changed if default search engine is not Google.
+  let field = "google.urlbar";
 
-      let tab = gBrowser.addTab();
-      gBrowser.selectedTab = tab;
+  if (data.days.hasDay(now)) {
+    let day = data.days.getDay(now);
+    if (day.has(field)) {
+      oldCount = day.get(field);
+    }
+  }
 
-      let searchStr = "firefox health report";
-      let expectedURL = Services.search.currentEngine.
-                        getSubmission(searchStr, "", "keyword").uri.spec;
-
-      // Expect the search URL to load but stop it as soon as it starts.
-      let loadPromise = waitForDocLoadAndStopIt(expectedURL);
+  let tab = gBrowser.addTab();
+  gBrowser.selectedTab = tab;
 
-      // Meanwhile, poll for the new measurement.
-      let count = 0;
-      let measurementDeferred = Promise.defer();
-      function getNewMeasurement() {
-        if (count++ >= 10) {
-          ok(false, "Timed out waiting for new measurement");
-          measurementDeferred.resolve();
-          return;
-        }
-        m.getValues().then(function onData(data) {
-          if (data.days.hasDay(now)) {
-            let day = data.days.getDay(now);
-            if (day.has(field)) {
-              let newCount = day.get(field);
-              if (newCount > oldCount) {
-                is(newCount, oldCount + 1,
-                   "Exactly one search has been recorded.");
-                measurementDeferred.resolve();
-                return;
-              }
-            }
-          }
-          executeSoon(getNewMeasurement);
-        });
-      }
-      executeSoon(getNewMeasurement);
+  let searchStr = "firefox health report";
+  let expectedURL = Services.search.currentEngine.
+                    getSubmission(searchStr, "", "keyword").uri.spec;
+
+  // Expect the search URL to load but stop it as soon as it starts.
+  let docLoadPromise = waitForDocLoadAndStopIt(expectedURL);
+
+  // Trigger the search.
+  gURLBar.value = searchStr;
+  gURLBar.handleCommand();
 
-      // Trigger the search.
-      gURLBar.value = searchStr;
-      gURLBar.handleCommand();
+  yield docLoadPromise;
 
-      // Wait for the page load and new measurement.
-      Promise.all([loadPromise, measurementDeferred.promise]).then(() => {
-        gBrowser.removeTab(tab);
-        finish();
-      });
-    });
-  });
-}
+  data = yield m.getValues();
+  ok(data.days.hasDay(now), "We have a search measurement for today.");
+  let day = data.days.getDay(now);
+  ok(day.has(field), "Have a search count for the urlbar.");
+  let newCount = day.get(field);
+  is(newCount, oldCount + 1, "We recorded one new search.");
 
+  gBrowser.removeTab(tab);
+});