/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

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