Bug 1173359 - Convert the history tracker tests to use async functions. r=markh
authorKit Cambridge <kit@yakshaving.ninja>
Tue, 22 Nov 2016 12:34:02 -0700
changeset 324205 c55ce97752965e67ce78fa6ddefbb23f13168da0
parent 324204 13406705508aea5243e69b42ab097c1e5948d1cf
child 324206 4c39528f81a95acf65bab0825e03e825dcd53539
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersmarkh
bugs1173359
milestone53.0a1
Bug 1173359 - Convert the history tracker tests to use async functions. r=markh MozReview-Commit-ID: CFNLQkRa7Qb
services/sync/tests/unit/test_history_tracker.js
--- a/services/sync/tests/unit/test_history_tracker.js
+++ b/services/sync/tests/unit/test_history_tracker.js
@@ -4,200 +4,215 @@
 Cu.import("resource://gre/modules/PlacesUtils.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://services-sync/engines/history.js");
 Cu.import("resource://services-sync/service.js");
 Cu.import("resource://services-sync/util.js");
 
-function onScoreUpdated(callback) {
-  Svc.Obs.add("weave:engine:score:updated", function observer() {
-    Svc.Obs.remove("weave:engine:score:updated", observer);
-    try {
-      callback();
-    } catch (ex) {
-      do_throw(ex);
-    }
-  });
-}
-
 Service.engineManager.clear();
 Service.engineManager.register(HistoryEngine);
 var engine = Service.engineManager.get("history");
 var tracker = engine._tracker;
 
 // Don't write out by default.
 tracker.persistChangedIDs = false;
 
-var _counter = 0;
-function addVisit() {
-  let uriString = "http://getfirefox.com/" + _counter++;
+async function addVisit(suffix) {
+  let uriString = "http://getfirefox.com/" + suffix;
   let uri = Utils.makeURI(uriString);
   _("Adding visit for URI " + uriString);
-  let place = {
-    uri: uri,
-    visits: [ {
-      visitDate: Date.now() * 1000,
-      transitionType: PlacesUtils.history.TRANSITION_LINK
-    } ]
-  };
 
-  let cb = Async.makeSpinningCallback();
-  PlacesUtils.asyncHistory.updatePlaces(place, {
-    handleError: function () {
-      _("Error adding visit for " + uriString);
-      cb(new Error("Error adding history entry"));
-    },
-
-    handleResult: function () {
-    },
-
-    handleCompletion: function () {
-      _("Added visit for " + uriString);
-      cb();
-    }
+  await PlacesTestUtils.addVisits({
+    uri,
+    visitDate: Date.now() * 1000,
+    transition: PlacesUtils.history.TRANSITION_LINK,
   });
 
-  // Spin the event loop to embed this async call in a sync API.
-  cb.wait();
   return uri;
 }
 
 function run_test() {
   initTestLogging("Trace");
   Log.repository.getLogger("Sync.Tracker.History").level = Log.Level.Trace;
   run_next_test();
 }
 
-add_test(function test_empty() {
-  _("Verify we've got an empty, disabled tracker to work with.");
-  do_check_empty(tracker.changedIDs);
-  do_check_eq(tracker.score, 0);
-  do_check_false(tracker._isTracking);
-  run_next_test();
-});
+async function verifyTrackerEmpty() {
+  let changes = engine.pullNewChanges();
+  equal(changes.count(), 0);
+  equal(tracker.score, 0);
+}
+
+async function verifyTrackedCount(expected) {
+  let changes = engine.pullNewChanges();
+  equal(changes.count(), expected);
+}
+
+async function verifyTrackedItems(tracked) {
+  let changes = engine.pullNewChanges();
+  let trackedIDs = new Set(changes.ids());
+  for (let guid of tracked) {
+    ok(changes.has(guid), `${guid} should be tracked`);
+    ok(changes.getModifiedTimestamp(guid) > 0,
+      `${guid} should have a modified time`);
+    trackedIDs.delete(guid);
+  }
+  equal(trackedIDs.size, 0, `Unhandled tracked IDs: ${
+    JSON.stringify(Array.from(trackedIDs))}`);
+}
 
-add_test(function test_not_tracking(next) {
-  _("Create history item. Won't show because we haven't started tracking yet");
-  addVisit();
-  Utils.nextTick(function() {
-    do_check_empty(tracker.changedIDs);
-    do_check_eq(tracker.score, 0);
-    run_next_test();
-  });
+async function startTracking() {
+  Svc.Obs.notify("weave:engine:start-tracking");
+}
+
+async function stopTracking() {
+  Svc.Obs.notify("weave:engine:stop-tracking");
+}
+
+async function resetTracker() {
+  tracker.clearChangedIDs();
+  tracker.resetScore();
+}
+
+async function cleanup() {
+  await PlacesTestUtils.clearHistory();
+  await resetTracker();
+  await stopTracking();
+}
+
+add_task(async function test_empty() {
+  _("Verify we've got an empty, disabled tracker to work with.");
+  await verifyTrackerEmpty();
+  do_check_false(tracker._isTracking);
+
+  await cleanup();
 });
 
-add_test(function test_start_tracking() {
+add_task(async function test_not_tracking() {
+  _("Create history item. Won't show because we haven't started tracking yet");
+  await addVisit("not_tracking");
+  await verifyTrackerEmpty();
+
+  await cleanup();
+});
+
+add_task(async function test_start_tracking() {
   _("Add hook for save completion.");
-  tracker.persistChangedIDs = true;
-  tracker.onSavedChangedIDs = function () {
-    _("changedIDs written to disk. Proceeding.");
-    // Turn this back off.
-    tracker.persistChangedIDs = false;
-    delete tracker.onSavedChangedIDs;
-    run_next_test();
-  };
+  let savePromise = new Promise(resolve => {
+    tracker.persistChangedIDs = true;
+    tracker.onSavedChangedIDs = function () {
+      // Turn this back off.
+      tracker.persistChangedIDs = false;
+      delete tracker.onSavedChangedIDs;
+      resolve();
+    };
+  });
 
   _("Tell the tracker to start tracking changes.");
-  onScoreUpdated(function() {
-    _("Score updated in test_start_tracking.");
-    do_check_attribute_count(tracker.changedIDs, 1);
-    do_check_eq(tracker.score, SCORE_INCREMENT_SMALL);
-  });
+  await startTracking();
+  let scorePromise = promiseOneObserver("weave:engine:score:updated");
+  await addVisit("start_tracking");
+  await scorePromise;
 
-  Svc.Obs.notify("weave:engine:start-tracking");
-  addVisit();
+  _("Score updated in test_start_tracking.");
+  await verifyTrackedCount(1);
+  do_check_eq(tracker.score, SCORE_INCREMENT_SMALL);
+
+  await savePromise;
+
+  _("changedIDs written to disk. Proceeding.");
+  await cleanup();
 });
 
-add_test(function test_start_tracking_twice() {
-  _("Verifying preconditions from test_start_tracking.");
-  do_check_attribute_count(tracker.changedIDs, 1);
+add_task(async function test_start_tracking_twice() {
+  _("Verifying preconditions.");
+  await startTracking();
+  await addVisit("start_tracking_twice1");
+  await verifyTrackedCount(1);
   do_check_eq(tracker.score, SCORE_INCREMENT_SMALL);
 
   _("Notifying twice won't do any harm.");
-  onScoreUpdated(function() {
-    _("Score updated in test_start_tracking_twice.");
-    do_check_attribute_count(tracker.changedIDs, 2);
-    do_check_eq(tracker.score, 2 * SCORE_INCREMENT_SMALL);
-    run_next_test();
-  });
+  await startTracking();
+  let scorePromise = promiseOneObserver("weave:engine:score:updated");
+  await addVisit("start_tracking_twice2");
+  await scorePromise;
 
-  Svc.Obs.notify("weave:engine:start-tracking");
-  addVisit();
+  _("Score updated in test_start_tracking_twice.");
+  await verifyTrackedCount(2);
+  do_check_eq(tracker.score, 2 * SCORE_INCREMENT_SMALL);
+
+  await cleanup();
 });
 
-add_test(function test_track_delete() {
+add_task(async function test_track_delete() {
   _("Deletions are tracked.");
 
   // This isn't present because we weren't tracking when it was visited.
-  let uri = Utils.makeURI("http://getfirefox.com/0");
+  await addVisit("track_delete");
+  let uri = Utils.makeURI("http://getfirefox.com/track_delete");
   let guid = engine._store.GUIDForUri(uri);
-  do_check_false(guid in tracker.changedIDs);
+  await verifyTrackerEmpty();
 
-  onScoreUpdated(function() {
-    do_check_true(guid in tracker.changedIDs);
-    do_check_attribute_count(tracker.changedIDs, 3);
-    do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE + 2 * SCORE_INCREMENT_SMALL);
-    run_next_test();
-  });
+  await startTracking();
+  let scorePromise = promiseOneObserver("weave:engine:score:updated");
+  PlacesUtils.history.removePage(uri);
+  await scorePromise;
 
-  do_check_eq(tracker.score, 2 * SCORE_INCREMENT_SMALL);
-  PlacesUtils.history.removePage(uri);
+  await verifyTrackedItems([guid]);
+  do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE);
+
+  await cleanup();
 });
 
-add_test(function test_dont_track_expiration() {
+add_task(async function test_dont_track_expiration() {
   _("Expirations are not tracked.");
-  let uriToExpire = addVisit();
+  let uriToExpire = await addVisit("to_expire");
   let guidToExpire = engine._store.GUIDForUri(uriToExpire);
-  let uriToRemove = addVisit();
+  let uriToRemove = await addVisit("to_remove");
   let guidToRemove = engine._store.GUIDForUri(uriToRemove);
 
-  tracker.clearChangedIDs();
-  do_check_false(guidToExpire in tracker.changedIDs);
-  do_check_false(guidToRemove in tracker.changedIDs);
+  await resetTracker();
+  await verifyTrackerEmpty();
 
-  onScoreUpdated(function() {
-    do_check_false(guidToExpire in tracker.changedIDs);
-    do_check_true(guidToRemove in tracker.changedIDs);
-    do_check_attribute_count(tracker.changedIDs, 1);
-    run_next_test();
-  });
+  await startTracking();
+  let scorePromise = promiseOneObserver("weave:engine:score:updated");
 
   // Observe expiration.
   Services.obs.addObserver(function onExpiration(aSubject, aTopic, aData) {
     Services.obs.removeObserver(onExpiration, aTopic);
     // Remove the remaining page to update its score.
     PlacesUtils.history.removePage(uriToRemove);
   }, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
 
   // Force expiration of 1 entry.
   Services.prefs.setIntPref("places.history.expiration.max_pages", 0);
   Cc["@mozilla.org/places/expiration;1"]
     .getService(Ci.nsIObserver)
     .observe(null, "places-debug-start-expiration", 1);
-});
 
-add_test(function test_stop_tracking() {
-  _("Let's stop tracking again.");
-  tracker.clearChangedIDs();
-  Svc.Obs.notify("weave:engine:stop-tracking");
-  addVisit();
-  Utils.nextTick(function() {
-    do_check_empty(tracker.changedIDs);
-    run_next_test();
-  });
+  await scorePromise;
+  await verifyTrackedItems([guidToRemove]);
+
+  await cleanup();
 });
 
-add_test(function test_stop_tracking_twice() {
-  _("Notifying twice won't do any harm.");
-  Svc.Obs.notify("weave:engine:stop-tracking");
-  addVisit();
-  Utils.nextTick(function() {
-    do_check_empty(tracker.changedIDs);
-    run_next_test();
-  });
+add_task(async function test_stop_tracking() {
+  _("Let's stop tracking again.");
+  await stopTracking();
+  await addVisit("stop_tracking");
+  await verifyTrackerEmpty();
+
+  await cleanup();
 });
 
-add_test(function cleanup() {
-   _("Clean up.");
-  PlacesTestUtils.clearHistory().then(run_next_test);
+add_task(async function test_stop_tracking_twice() {
+  await stopTracking();
+  await addVisit("stop_tracking_twice1");
+
+  _("Notifying twice won't do any harm.");
+  await stopTracking();
+  await addVisit("stop_tracking_twice2");
+  await verifyTrackerEmpty();
+
+  await cleanup();
 });