PageRenderTime 17ms CodeModel.GetById 8ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/services/sync/tests/unit/test_history_tracker.js

http://github.com/zpao/v8monkey
JavaScript | 147 lines | 125 code | 19 blank | 3 comment | 0 complexity | c617a42d934b0592f3ffb8f77623a62c MD5 | raw file
  1Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  2Cu.import("resource://services-sync/engines.js");
  3Cu.import("resource://services-sync/constants.js");
  4Cu.import("resource://services-sync/engines/history.js");
  5Cu.import("resource://services-sync/util.js");
  6
  7function onScoreUpdated(callback) {
  8  Svc.Obs.add("weave:engine:score:updated", function observer() {
  9    Svc.Obs.remove("weave:engine:score:updated", observer);
 10    try {
 11      callback();
 12    } catch (ex) {
 13      do_throw(ex);
 14    }
 15  });
 16}
 17
 18Engines.register(HistoryEngine);
 19let engine = Engines.get("history");
 20let tracker = engine._tracker;
 21
 22let _counter = 0;
 23function addVisit() {
 24  let uri = Utils.makeURI("http://getfirefox.com/" + _counter);
 25  PlacesUtils.history.addVisit(uri, Date.now() * 1000, null, 1, false, 0);
 26  _counter++;
 27  return uri;
 28}
 29
 30
 31function run_test() {
 32  run_next_test();
 33}
 34
 35add_test(function test_empty() {
 36  _("Verify we've got an empty tracker to work with.");
 37  do_check_empty(tracker.changedIDs);
 38  do_check_eq(tracker.score, 0);
 39  run_next_test();
 40});
 41
 42add_test(function test_not_tracking(next) {
 43  _("Create history item. Won't show because we haven't started tracking yet");
 44  addVisit();
 45  Utils.nextTick(function() {
 46    do_check_empty(tracker.changedIDs);
 47    do_check_eq(tracker.score, 0);
 48    run_next_test();
 49  });
 50});
 51
 52add_test(function test_start_tracking() {
 53  _("Tell the tracker to start tracking changes.");
 54  onScoreUpdated(function() {
 55    do_check_attribute_count(tracker.changedIDs, 1);
 56    do_check_eq(tracker.score, SCORE_INCREMENT_SMALL);
 57    run_next_test();
 58  });
 59  Svc.Obs.notify("weave:engine:start-tracking");
 60  addVisit();
 61});
 62
 63add_test(function test_start_tracking_twice() {
 64  _("Notifying twice won't do any harm.");
 65  onScoreUpdated(function() {
 66    do_check_attribute_count(tracker.changedIDs, 2);
 67    do_check_eq(tracker.score, 2 * SCORE_INCREMENT_SMALL);
 68    run_next_test();
 69  });
 70  Svc.Obs.notify("weave:engine:start-tracking");
 71  addVisit();
 72});
 73
 74add_test(function test_track_delete() {
 75  _("Deletions are tracked.");
 76  let uri = Utils.makeURI("http://getfirefox.com/0");
 77  let guid = engine._store.GUIDForUri(uri);
 78  do_check_false(guid in tracker.changedIDs);
 79
 80  onScoreUpdated(function() {
 81    do_check_true(guid in tracker.changedIDs);
 82    do_check_attribute_count(tracker.changedIDs, 3);
 83    do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE + 2 * SCORE_INCREMENT_SMALL);
 84    run_next_test();
 85  });
 86  do_check_eq(tracker.score, 2 * SCORE_INCREMENT_SMALL);
 87  PlacesUtils.history.removePage(uri);
 88});
 89
 90add_test(function test_dont_track_expiration() {
 91  _("Expirations are not tracked.");
 92  let uriToExpire = addVisit();
 93  let guidToExpire = engine._store.GUIDForUri(uriToExpire);
 94  let uriToRemove = addVisit();
 95  let guidToRemove = engine._store.GUIDForUri(uriToRemove);
 96
 97  tracker.clearChangedIDs();
 98  do_check_false(guidToExpire in tracker.changedIDs);
 99  do_check_false(guidToRemove in tracker.changedIDs);
100
101  onScoreUpdated(function() {
102    do_check_false(guidToExpire in tracker.changedIDs);
103    do_check_true(guidToRemove in tracker.changedIDs);
104    do_check_attribute_count(tracker.changedIDs, 1);
105    run_next_test();
106  });
107
108  // Observe expiration.
109  Services.obs.addObserver(function onExpiration(aSubject, aTopic, aData) {
110    Services.obs.removeObserver(onExpiration, aTopic);
111    // Remove the remaining page to update its score.
112    PlacesUtils.history.removePage(uriToRemove);
113  }, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
114
115  // Force expiration of 1 entry.
116  Services.prefs.setIntPref("places.history.expiration.max_pages", 0);
117  Cc["@mozilla.org/places/expiration;1"]
118    .getService(Ci.nsIObserver)
119    .observe(null, "places-debug-start-expiration", 1);
120});
121
122add_test(function test_stop_tracking() {
123  _("Let's stop tracking again.");
124  tracker.clearChangedIDs();
125  Svc.Obs.notify("weave:engine:stop-tracking");
126  addVisit();
127  Utils.nextTick(function() {
128    do_check_empty(tracker.changedIDs);
129    run_next_test();
130  });
131});
132
133add_test(function test_stop_tracking_twice() {
134  _("Notifying twice won't do any harm.");
135  Svc.Obs.notify("weave:engine:stop-tracking");
136  addVisit();
137  Utils.nextTick(function() {
138    do_check_empty(tracker.changedIDs);
139    run_next_test();
140  });
141});
142
143add_test(function cleanup() {
144   _("Clean up.");
145  PlacesUtils.history.removeAllPages();
146  run_next_test();
147});