services/sync/tests/unit/test_history_tracker.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 Cu.import("resource://gre/modules/PlacesUtils.jsm");
michael@0 5 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 6 Cu.import("resource://services-sync/engines.js");
michael@0 7 Cu.import("resource://services-sync/constants.js");
michael@0 8 Cu.import("resource://services-sync/engines/history.js");
michael@0 9 Cu.import("resource://services-sync/service.js");
michael@0 10 Cu.import("resource://services-sync/util.js");
michael@0 11
michael@0 12 function onScoreUpdated(callback) {
michael@0 13 Svc.Obs.add("weave:engine:score:updated", function observer() {
michael@0 14 Svc.Obs.remove("weave:engine:score:updated", observer);
michael@0 15 try {
michael@0 16 callback();
michael@0 17 } catch (ex) {
michael@0 18 do_throw(ex);
michael@0 19 }
michael@0 20 });
michael@0 21 }
michael@0 22
michael@0 23 Service.engineManager.clear();
michael@0 24 Service.engineManager.register(HistoryEngine);
michael@0 25 let engine = Service.engineManager.get("history");
michael@0 26 let tracker = engine._tracker;
michael@0 27
michael@0 28 // Don't write out by default.
michael@0 29 tracker.persistChangedIDs = false;
michael@0 30
michael@0 31 let _counter = 0;
michael@0 32 function addVisit() {
michael@0 33 let uriString = "http://getfirefox.com/" + _counter++;
michael@0 34 let uri = Utils.makeURI(uriString);
michael@0 35 _("Adding visit for URI " + uriString);
michael@0 36 let place = {
michael@0 37 uri: uri,
michael@0 38 visits: [ {
michael@0 39 visitDate: Date.now() * 1000,
michael@0 40 transitionType: PlacesUtils.history.TRANSITION_LINK
michael@0 41 } ]
michael@0 42 };
michael@0 43
michael@0 44 let cb = Async.makeSpinningCallback();
michael@0 45 PlacesUtils.asyncHistory.updatePlaces(place, {
michael@0 46 handleError: function () {
michael@0 47 _("Error adding visit for " + uriString);
michael@0 48 cb(new Error("Error adding history entry"));
michael@0 49 },
michael@0 50
michael@0 51 handleResult: function () {
michael@0 52 },
michael@0 53
michael@0 54 handleCompletion: function () {
michael@0 55 _("Added visit for " + uriString);
michael@0 56 cb();
michael@0 57 }
michael@0 58 });
michael@0 59
michael@0 60 // Spin the event loop to embed this async call in a sync API.
michael@0 61 cb.wait();
michael@0 62 return uri;
michael@0 63 }
michael@0 64
michael@0 65 function run_test() {
michael@0 66 initTestLogging("Trace");
michael@0 67 Log.repository.getLogger("Sync.Tracker.History").level = Log.Level.Trace;
michael@0 68 run_next_test();
michael@0 69 }
michael@0 70
michael@0 71 add_test(function test_empty() {
michael@0 72 _("Verify we've got an empty, disabled tracker to work with.");
michael@0 73 do_check_empty(tracker.changedIDs);
michael@0 74 do_check_eq(tracker.score, 0);
michael@0 75 do_check_false(tracker._isTracking);
michael@0 76 run_next_test();
michael@0 77 });
michael@0 78
michael@0 79 add_test(function test_not_tracking(next) {
michael@0 80 _("Create history item. Won't show because we haven't started tracking yet");
michael@0 81 addVisit();
michael@0 82 Utils.nextTick(function() {
michael@0 83 do_check_empty(tracker.changedIDs);
michael@0 84 do_check_eq(tracker.score, 0);
michael@0 85 run_next_test();
michael@0 86 });
michael@0 87 });
michael@0 88
michael@0 89 add_test(function test_start_tracking() {
michael@0 90 _("Add hook for save completion.");
michael@0 91 tracker.persistChangedIDs = true;
michael@0 92 tracker.onSavedChangedIDs = function () {
michael@0 93 _("changedIDs written to disk. Proceeding.");
michael@0 94 // Turn this back off.
michael@0 95 tracker.persistChangedIDs = false;
michael@0 96 delete tracker.onSavedChangedIDs;
michael@0 97 run_next_test();
michael@0 98 };
michael@0 99
michael@0 100 _("Tell the tracker to start tracking changes.");
michael@0 101 onScoreUpdated(function() {
michael@0 102 _("Score updated in test_start_tracking.");
michael@0 103 do_check_attribute_count(tracker.changedIDs, 1);
michael@0 104 do_check_eq(tracker.score, SCORE_INCREMENT_SMALL);
michael@0 105 });
michael@0 106
michael@0 107 Svc.Obs.notify("weave:engine:start-tracking");
michael@0 108 addVisit();
michael@0 109 });
michael@0 110
michael@0 111 add_test(function test_start_tracking_twice() {
michael@0 112 _("Verifying preconditions from test_start_tracking.");
michael@0 113 do_check_attribute_count(tracker.changedIDs, 1);
michael@0 114 do_check_eq(tracker.score, SCORE_INCREMENT_SMALL);
michael@0 115
michael@0 116 _("Notifying twice won't do any harm.");
michael@0 117 onScoreUpdated(function() {
michael@0 118 _("Score updated in test_start_tracking_twice.");
michael@0 119 do_check_attribute_count(tracker.changedIDs, 2);
michael@0 120 do_check_eq(tracker.score, 2 * SCORE_INCREMENT_SMALL);
michael@0 121 run_next_test();
michael@0 122 });
michael@0 123
michael@0 124 Svc.Obs.notify("weave:engine:start-tracking");
michael@0 125 addVisit();
michael@0 126 });
michael@0 127
michael@0 128 add_test(function test_track_delete() {
michael@0 129 _("Deletions are tracked.");
michael@0 130
michael@0 131 // This isn't present because we weren't tracking when it was visited.
michael@0 132 let uri = Utils.makeURI("http://getfirefox.com/0");
michael@0 133 let guid = engine._store.GUIDForUri(uri);
michael@0 134 do_check_false(guid in tracker.changedIDs);
michael@0 135
michael@0 136 onScoreUpdated(function() {
michael@0 137 do_check_true(guid in tracker.changedIDs);
michael@0 138 do_check_attribute_count(tracker.changedIDs, 3);
michael@0 139 do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE + 2 * SCORE_INCREMENT_SMALL);
michael@0 140 run_next_test();
michael@0 141 });
michael@0 142
michael@0 143 do_check_eq(tracker.score, 2 * SCORE_INCREMENT_SMALL);
michael@0 144 PlacesUtils.history.removePage(uri);
michael@0 145 });
michael@0 146
michael@0 147 add_test(function test_dont_track_expiration() {
michael@0 148 _("Expirations are not tracked.");
michael@0 149 let uriToExpire = addVisit();
michael@0 150 let guidToExpire = engine._store.GUIDForUri(uriToExpire);
michael@0 151 let uriToRemove = addVisit();
michael@0 152 let guidToRemove = engine._store.GUIDForUri(uriToRemove);
michael@0 153
michael@0 154 tracker.clearChangedIDs();
michael@0 155 do_check_false(guidToExpire in tracker.changedIDs);
michael@0 156 do_check_false(guidToRemove in tracker.changedIDs);
michael@0 157
michael@0 158 onScoreUpdated(function() {
michael@0 159 do_check_false(guidToExpire in tracker.changedIDs);
michael@0 160 do_check_true(guidToRemove in tracker.changedIDs);
michael@0 161 do_check_attribute_count(tracker.changedIDs, 1);
michael@0 162 run_next_test();
michael@0 163 });
michael@0 164
michael@0 165 // Observe expiration.
michael@0 166 Services.obs.addObserver(function onExpiration(aSubject, aTopic, aData) {
michael@0 167 Services.obs.removeObserver(onExpiration, aTopic);
michael@0 168 // Remove the remaining page to update its score.
michael@0 169 PlacesUtils.history.removePage(uriToRemove);
michael@0 170 }, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
michael@0 171
michael@0 172 // Force expiration of 1 entry.
michael@0 173 Services.prefs.setIntPref("places.history.expiration.max_pages", 0);
michael@0 174 Cc["@mozilla.org/places/expiration;1"]
michael@0 175 .getService(Ci.nsIObserver)
michael@0 176 .observe(null, "places-debug-start-expiration", 1);
michael@0 177 });
michael@0 178
michael@0 179 add_test(function test_stop_tracking() {
michael@0 180 _("Let's stop tracking again.");
michael@0 181 tracker.clearChangedIDs();
michael@0 182 Svc.Obs.notify("weave:engine:stop-tracking");
michael@0 183 addVisit();
michael@0 184 Utils.nextTick(function() {
michael@0 185 do_check_empty(tracker.changedIDs);
michael@0 186 run_next_test();
michael@0 187 });
michael@0 188 });
michael@0 189
michael@0 190 add_test(function test_stop_tracking_twice() {
michael@0 191 _("Notifying twice won't do any harm.");
michael@0 192 Svc.Obs.notify("weave:engine:stop-tracking");
michael@0 193 addVisit();
michael@0 194 Utils.nextTick(function() {
michael@0 195 do_check_empty(tracker.changedIDs);
michael@0 196 run_next_test();
michael@0 197 });
michael@0 198 });
michael@0 199
michael@0 200 add_test(function cleanup() {
michael@0 201 _("Clean up.");
michael@0 202 PlacesUtils.history.removeAllPages();
michael@0 203 run_next_test();
michael@0 204 });

mercurial