Merge bug 631891 from fx-sync. a=blocking-final
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Thu, 10 Feb 2011 09:22:13 -0800
changeset 62336 ded3773978117b307326be642c4a48242ea0995d
parent 62334 36ba0ce7bdb9d6e6f8209a9026cad74e7e8b4ba5 (current diff)
parent 62335 74db19f6be874cd0b2e7e44af98a70f44a1ff61d (diff)
child 62338 8930fec7d1c6828c7b7819736bd7b0ea63dc1b45
push id1
push userroot
push dateTue, 10 Dec 2013 15:46:25 +0000
reviewersblocking-final
bugs631891
milestone2.0b12pre
Merge bug 631891 from fx-sync. a=blocking-final
services/sync/modules/engines/history.js
services/sync/tests/unit/test_history_tracker.js
--- a/services/sync/modules/engines/history.js
+++ b/services/sync/modules/engines/history.js
@@ -664,29 +664,37 @@ HistoryTracker.prototype = {
   _upScore: function BMT__upScore() {
     this.score += 1;
   },
 
   onVisit: function HT_onVisit(uri, vid, time, session, referrer, trans) {
     if (this.ignoreAll)
       return;
     this._log.trace("onVisit: " + uri.spec);
-    if (this.addChangedID(this._GUIDForUri(uri, true)))
-      this._upScore();
+    let self = this;
+    Utils.delay(function() {
+      if (self.addChangedID(self._GUIDForUri(uri, true))) {
+        self._upScore();
+      }
+    }, 0);
   },
   onDeleteVisits: function onDeleteVisits() {
   },
   onPageExpired: function HT_onPageExpired(uri, time, entry) {
   },
   onBeforeDeleteURI: function onBeforeDeleteURI(uri) {
     if (this.ignoreAll)
       return;
     this._log.trace("onBeforeDeleteURI: " + uri.spec);
-    if (this.addChangedID(this._GUIDForUri(uri, true)))
-      this._upScore();
+    let self = this;
+    Utils.delay(function() {
+      if (self.addChangedID(self._GUIDForUri(uri, true))) {
+        self._upScore();
+      }
+    }, 0);
   },
   onDeleteURI: function HT_onDeleteURI(uri) {
   },
   onClearHistory: function HT_onClearHistory() {
     this._log.trace("onClearHistory");
     this.score += 500;
   }
 };
--- a/services/sync/tests/unit/test_history_tracker.js
+++ b/services/sync/tests/unit/test_history_tracker.js
@@ -1,49 +1,89 @@
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/engines/history.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);
+    }
+  });
+}
+
 function run_test() {
   Engines.register(HistoryEngine);
   let tracker = Engines.get("history")._tracker;
 
   _("Verify we've got an empty tracker to work with.");
   do_check_eq([id for (id in tracker.changedIDs)].length, 0);
 
   let _counter = 0;
   function addVisit() {
     Svc.History.addVisit(Utils.makeURI("http://getfirefox.com/" + _counter),
                          Date.now() * 1000, null, 1, false, 0);
     _counter += 1;
   }
 
-  try {
+  do_test_pending();
+  asyncChainTests(function (next) {
+
     _("Create history item. Won't show because we haven't started tracking yet");
     addVisit();
-    do_check_eq([id for (id in tracker.changedIDs)].length, 0);
+    Utils.delay(function() {
+      do_check_eq([id for (id in tracker.changedIDs)].length, 0);
+      next();
+    }, 0);
+
+  }, function (next) {
 
     _("Tell the tracker to start tracking changes.");
+    onScoreUpdated(function() {
+      do_check_eq([id for (id in tracker.changedIDs)].length, 1);
+      next();
+    });
     Svc.Obs.notify("weave:engine:start-tracking");
     addVisit();
-    do_check_eq([id for (id in tracker.changedIDs)].length, 1);
+
+  }, function (next) {
 
     _("Notifying twice won't do any harm.");
+    onScoreUpdated(function() {
+      do_check_eq([id for (id in tracker.changedIDs)].length, 2);
+      next();
+    });
     Svc.Obs.notify("weave:engine:start-tracking");
     addVisit();
-    do_check_eq([id for (id in tracker.changedIDs)].length, 2);
+
+  }, function (next) {
 
     _("Let's stop tracking again.");
     tracker.clearChangedIDs();
     Svc.Obs.notify("weave:engine:stop-tracking");
     addVisit();
-    do_check_eq([id for (id in tracker.changedIDs)].length, 0);
+    Utils.delay(function() {
+      do_check_eq([id for (id in tracker.changedIDs)].length, 0);
+      next();
+    }, 0);
+
+  }, function (next) {
 
     _("Notifying twice won't do any harm.");
     Svc.Obs.notify("weave:engine:stop-tracking");
     addVisit();
-    do_check_eq([id for (id in tracker.changedIDs)].length, 0);
-  } finally {
+    Utils.delay(function() {
+      do_check_eq([id for (id in tracker.changedIDs)].length, 0);
+      next();
+    }, 0);
+
+  }, function (next) {
+
     _("Clean up.");
     Svc.History.removeAllPages();
-  }
+    do_test_finished();
+
+  })();
 }