michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://gre/modules/PlacesUtils.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://services-sync/engines.js"); michael@0: Cu.import("resource://services-sync/constants.js"); michael@0: Cu.import("resource://services-sync/engines/history.js"); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: Cu.import("resource://services-sync/util.js"); michael@0: michael@0: function onScoreUpdated(callback) { michael@0: Svc.Obs.add("weave:engine:score:updated", function observer() { michael@0: Svc.Obs.remove("weave:engine:score:updated", observer); michael@0: try { michael@0: callback(); michael@0: } catch (ex) { michael@0: do_throw(ex); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: Service.engineManager.clear(); michael@0: Service.engineManager.register(HistoryEngine); michael@0: let engine = Service.engineManager.get("history"); michael@0: let tracker = engine._tracker; michael@0: michael@0: // Don't write out by default. michael@0: tracker.persistChangedIDs = false; michael@0: michael@0: let _counter = 0; michael@0: function addVisit() { michael@0: let uriString = "http://getfirefox.com/" + _counter++; michael@0: let uri = Utils.makeURI(uriString); michael@0: _("Adding visit for URI " + uriString); michael@0: let place = { michael@0: uri: uri, michael@0: visits: [ { michael@0: visitDate: Date.now() * 1000, michael@0: transitionType: PlacesUtils.history.TRANSITION_LINK michael@0: } ] michael@0: }; michael@0: michael@0: let cb = Async.makeSpinningCallback(); michael@0: PlacesUtils.asyncHistory.updatePlaces(place, { michael@0: handleError: function () { michael@0: _("Error adding visit for " + uriString); michael@0: cb(new Error("Error adding history entry")); michael@0: }, michael@0: michael@0: handleResult: function () { michael@0: }, michael@0: michael@0: handleCompletion: function () { michael@0: _("Added visit for " + uriString); michael@0: cb(); michael@0: } michael@0: }); michael@0: michael@0: // Spin the event loop to embed this async call in a sync API. michael@0: cb.wait(); michael@0: return uri; michael@0: } michael@0: michael@0: function run_test() { michael@0: initTestLogging("Trace"); michael@0: Log.repository.getLogger("Sync.Tracker.History").level = Log.Level.Trace; michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_test(function test_empty() { michael@0: _("Verify we've got an empty, disabled tracker to work with."); michael@0: do_check_empty(tracker.changedIDs); michael@0: do_check_eq(tracker.score, 0); michael@0: do_check_false(tracker._isTracking); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_not_tracking(next) { michael@0: _("Create history item. Won't show because we haven't started tracking yet"); michael@0: addVisit(); michael@0: Utils.nextTick(function() { michael@0: do_check_empty(tracker.changedIDs); michael@0: do_check_eq(tracker.score, 0); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_start_tracking() { michael@0: _("Add hook for save completion."); michael@0: tracker.persistChangedIDs = true; michael@0: tracker.onSavedChangedIDs = function () { michael@0: _("changedIDs written to disk. Proceeding."); michael@0: // Turn this back off. michael@0: tracker.persistChangedIDs = false; michael@0: delete tracker.onSavedChangedIDs; michael@0: run_next_test(); michael@0: }; michael@0: michael@0: _("Tell the tracker to start tracking changes."); michael@0: onScoreUpdated(function() { michael@0: _("Score updated in test_start_tracking."); michael@0: do_check_attribute_count(tracker.changedIDs, 1); michael@0: do_check_eq(tracker.score, SCORE_INCREMENT_SMALL); michael@0: }); michael@0: michael@0: Svc.Obs.notify("weave:engine:start-tracking"); michael@0: addVisit(); michael@0: }); michael@0: michael@0: add_test(function test_start_tracking_twice() { michael@0: _("Verifying preconditions from test_start_tracking."); michael@0: do_check_attribute_count(tracker.changedIDs, 1); michael@0: do_check_eq(tracker.score, SCORE_INCREMENT_SMALL); michael@0: michael@0: _("Notifying twice won't do any harm."); michael@0: onScoreUpdated(function() { michael@0: _("Score updated in test_start_tracking_twice."); michael@0: do_check_attribute_count(tracker.changedIDs, 2); michael@0: do_check_eq(tracker.score, 2 * SCORE_INCREMENT_SMALL); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: Svc.Obs.notify("weave:engine:start-tracking"); michael@0: addVisit(); michael@0: }); michael@0: michael@0: add_test(function test_track_delete() { michael@0: _("Deletions are tracked."); michael@0: michael@0: // This isn't present because we weren't tracking when it was visited. michael@0: let uri = Utils.makeURI("http://getfirefox.com/0"); michael@0: let guid = engine._store.GUIDForUri(uri); michael@0: do_check_false(guid in tracker.changedIDs); michael@0: michael@0: onScoreUpdated(function() { michael@0: do_check_true(guid in tracker.changedIDs); michael@0: do_check_attribute_count(tracker.changedIDs, 3); michael@0: do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE + 2 * SCORE_INCREMENT_SMALL); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: do_check_eq(tracker.score, 2 * SCORE_INCREMENT_SMALL); michael@0: PlacesUtils.history.removePage(uri); michael@0: }); michael@0: michael@0: add_test(function test_dont_track_expiration() { michael@0: _("Expirations are not tracked."); michael@0: let uriToExpire = addVisit(); michael@0: let guidToExpire = engine._store.GUIDForUri(uriToExpire); michael@0: let uriToRemove = addVisit(); michael@0: let guidToRemove = engine._store.GUIDForUri(uriToRemove); michael@0: michael@0: tracker.clearChangedIDs(); michael@0: do_check_false(guidToExpire in tracker.changedIDs); michael@0: do_check_false(guidToRemove in tracker.changedIDs); michael@0: michael@0: onScoreUpdated(function() { michael@0: do_check_false(guidToExpire in tracker.changedIDs); michael@0: do_check_true(guidToRemove in tracker.changedIDs); michael@0: do_check_attribute_count(tracker.changedIDs, 1); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: // Observe expiration. michael@0: Services.obs.addObserver(function onExpiration(aSubject, aTopic, aData) { michael@0: Services.obs.removeObserver(onExpiration, aTopic); michael@0: // Remove the remaining page to update its score. michael@0: PlacesUtils.history.removePage(uriToRemove); michael@0: }, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false); michael@0: michael@0: // Force expiration of 1 entry. michael@0: Services.prefs.setIntPref("places.history.expiration.max_pages", 0); michael@0: Cc["@mozilla.org/places/expiration;1"] michael@0: .getService(Ci.nsIObserver) michael@0: .observe(null, "places-debug-start-expiration", 1); michael@0: }); michael@0: michael@0: add_test(function test_stop_tracking() { michael@0: _("Let's stop tracking again."); michael@0: tracker.clearChangedIDs(); michael@0: Svc.Obs.notify("weave:engine:stop-tracking"); michael@0: addVisit(); michael@0: Utils.nextTick(function() { michael@0: do_check_empty(tracker.changedIDs); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_stop_tracking_twice() { michael@0: _("Notifying twice won't do any harm."); michael@0: Svc.Obs.notify("weave:engine:stop-tracking"); michael@0: addVisit(); michael@0: Utils.nextTick(function() { michael@0: do_check_empty(tracker.changedIDs); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function cleanup() { michael@0: _("Clean up."); michael@0: PlacesUtils.history.removeAllPages(); michael@0: run_next_test(); michael@0: });