services/sync/tests/unit/test_bookmark_tracker.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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://services-sync/constants.js");
michael@0 6 Cu.import("resource://services-sync/engines/bookmarks.js");
michael@0 7 Cu.import("resource://services-sync/engines.js");
michael@0 8 Cu.import("resource://services-sync/service.js");
michael@0 9 Cu.import("resource://services-sync/util.js");
michael@0 10
michael@0 11 Service.engineManager.register(BookmarksEngine);
michael@0 12 let engine = Service.engineManager.get("bookmarks");
michael@0 13 let store = engine._store;
michael@0 14 let tracker = engine._tracker;
michael@0 15
michael@0 16 store.wipe();
michael@0 17 tracker.persistChangedIDs = false;
michael@0 18
michael@0 19 function test_tracking() {
michael@0 20 _("Verify we've got an empty tracker to work with.");
michael@0 21 let tracker = engine._tracker;
michael@0 22 do_check_empty(tracker.changedIDs);
michael@0 23
michael@0 24 let folder = PlacesUtils.bookmarks.createFolder(
michael@0 25 PlacesUtils.bookmarks.bookmarksMenuFolder,
michael@0 26 "Test Folder", PlacesUtils.bookmarks.DEFAULT_INDEX);
michael@0 27 function createBmk() {
michael@0 28 return PlacesUtils.bookmarks.insertBookmark(
michael@0 29 folder, Utils.makeURI("http://getfirefox.com"),
michael@0 30 PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
michael@0 31 }
michael@0 32
michael@0 33 try {
michael@0 34 _("Create bookmark. Won't show because we haven't started tracking yet");
michael@0 35 createBmk();
michael@0 36 do_check_empty(tracker.changedIDs);
michael@0 37 do_check_eq(tracker.score, 0);
michael@0 38
michael@0 39 _("Tell the tracker to start tracking changes.");
michael@0 40 Svc.Obs.notify("weave:engine:start-tracking");
michael@0 41 createBmk();
michael@0 42 // We expect two changed items because the containing folder
michael@0 43 // changed as well (new child).
michael@0 44 do_check_attribute_count(tracker.changedIDs, 2);
michael@0 45 do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 2);
michael@0 46
michael@0 47 _("Notifying twice won't do any harm.");
michael@0 48 Svc.Obs.notify("weave:engine:start-tracking");
michael@0 49 createBmk();
michael@0 50 do_check_attribute_count(tracker.changedIDs, 3);
michael@0 51 do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 4);
michael@0 52
michael@0 53 _("Let's stop tracking again.");
michael@0 54 tracker.clearChangedIDs();
michael@0 55 tracker.resetScore();
michael@0 56 Svc.Obs.notify("weave:engine:stop-tracking");
michael@0 57 createBmk();
michael@0 58 do_check_empty(tracker.changedIDs);
michael@0 59 do_check_eq(tracker.score, 0);
michael@0 60
michael@0 61 _("Notifying twice won't do any harm.");
michael@0 62 Svc.Obs.notify("weave:engine:stop-tracking");
michael@0 63 createBmk();
michael@0 64 do_check_empty(tracker.changedIDs);
michael@0 65 do_check_eq(tracker.score, 0);
michael@0 66
michael@0 67 } finally {
michael@0 68 _("Clean up.");
michael@0 69 store.wipe();
michael@0 70 tracker.clearChangedIDs();
michael@0 71 tracker.resetScore();
michael@0 72 Svc.Obs.notify("weave:engine:stop-tracking");
michael@0 73 }
michael@0 74 }
michael@0 75
michael@0 76 function test_onItemChanged() {
michael@0 77 // Anno that's in ANNOS_TO_TRACK.
michael@0 78 const DESCRIPTION_ANNO = "bookmarkProperties/description";
michael@0 79
michael@0 80 _("Verify we've got an empty tracker to work with.");
michael@0 81 let tracker = engine._tracker;
michael@0 82 do_check_empty(tracker.changedIDs);
michael@0 83 do_check_eq(tracker.score, 0);
michael@0 84
michael@0 85 try {
michael@0 86 Svc.Obs.notify("weave:engine:stop-tracking");
michael@0 87 let folder = PlacesUtils.bookmarks.createFolder(
michael@0 88 PlacesUtils.bookmarks.bookmarksMenuFolder, "Parent",
michael@0 89 PlacesUtils.bookmarks.DEFAULT_INDEX);
michael@0 90 _("Track changes to annos.");
michael@0 91 let b = PlacesUtils.bookmarks.insertBookmark(
michael@0 92 folder, Utils.makeURI("http://getfirefox.com"),
michael@0 93 PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
michael@0 94 let bGUID = engine._store.GUIDForId(b);
michael@0 95 _("New item is " + b);
michael@0 96 _("GUID: " + bGUID);
michael@0 97
michael@0 98 Svc.Obs.notify("weave:engine:start-tracking");
michael@0 99 PlacesUtils.annotations.setItemAnnotation(
michael@0 100 b, DESCRIPTION_ANNO, "A test description", 0,
michael@0 101 PlacesUtils.annotations.EXPIRE_NEVER);
michael@0 102 do_check_true(tracker.changedIDs[bGUID] > 0);
michael@0 103 do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE);
michael@0 104
michael@0 105 } finally {
michael@0 106 _("Clean up.");
michael@0 107 store.wipe();
michael@0 108 tracker.clearChangedIDs();
michael@0 109 tracker.resetScore();
michael@0 110 Svc.Obs.notify("weave:engine:stop-tracking");
michael@0 111 }
michael@0 112 }
michael@0 113
michael@0 114 function test_onItemMoved() {
michael@0 115 _("Verify we've got an empty tracker to work with.");
michael@0 116 let tracker = engine._tracker;
michael@0 117 do_check_empty(tracker.changedIDs);
michael@0 118 do_check_eq(tracker.score, 0);
michael@0 119
michael@0 120 try {
michael@0 121 let fx_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 122 PlacesUtils.bookmarks.bookmarksMenuFolder,
michael@0 123 Utils.makeURI("http://getfirefox.com"),
michael@0 124 PlacesUtils.bookmarks.DEFAULT_INDEX,
michael@0 125 "Get Firefox!");
michael@0 126 let fx_guid = engine._store.GUIDForId(fx_id);
michael@0 127 let tb_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 128 PlacesUtils.bookmarks.bookmarksMenuFolder,
michael@0 129 Utils.makeURI("http://getthunderbird.com"),
michael@0 130 PlacesUtils.bookmarks.DEFAULT_INDEX,
michael@0 131 "Get Thunderbird!");
michael@0 132 let tb_guid = engine._store.GUIDForId(tb_id);
michael@0 133
michael@0 134 Svc.Obs.notify("weave:engine:start-tracking");
michael@0 135
michael@0 136 // Moving within the folder will just track the folder.
michael@0 137 PlacesUtils.bookmarks.moveItem(
michael@0 138 tb_id, PlacesUtils.bookmarks.bookmarksMenuFolder, 0);
michael@0 139 do_check_true(tracker.changedIDs['menu'] > 0);
michael@0 140 do_check_eq(tracker.changedIDs['toolbar'], undefined);
michael@0 141 do_check_eq(tracker.changedIDs[fx_guid], undefined);
michael@0 142 do_check_eq(tracker.changedIDs[tb_guid], undefined);
michael@0 143 do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE);
michael@0 144 tracker.clearChangedIDs();
michael@0 145 tracker.resetScore();
michael@0 146
michael@0 147 // Moving a bookmark to a different folder will track the old
michael@0 148 // folder, the new folder and the bookmark.
michael@0 149 PlacesUtils.bookmarks.moveItem(tb_id, PlacesUtils.bookmarks.toolbarFolder,
michael@0 150 PlacesUtils.bookmarks.DEFAULT_INDEX);
michael@0 151 do_check_true(tracker.changedIDs['menu'] > 0);
michael@0 152 do_check_true(tracker.changedIDs['toolbar'] > 0);
michael@0 153 do_check_eq(tracker.changedIDs[fx_guid], undefined);
michael@0 154 do_check_true(tracker.changedIDs[tb_guid] > 0);
michael@0 155 do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 3);
michael@0 156
michael@0 157 } finally {
michael@0 158 _("Clean up.");
michael@0 159 store.wipe();
michael@0 160 tracker.clearChangedIDs();
michael@0 161 tracker.resetScore();
michael@0 162 Svc.Obs.notify("weave:engine:stop-tracking");
michael@0 163 }
michael@0 164
michael@0 165 }
michael@0 166
michael@0 167 function run_test() {
michael@0 168 initTestLogging("Trace");
michael@0 169
michael@0 170 Log.repository.getLogger("Sync.Engine.Bookmarks").level = Log.Level.Trace;
michael@0 171 Log.repository.getLogger("Sync.Store.Bookmarks").level = Log.Level.Trace;
michael@0 172 Log.repository.getLogger("Sync.Tracker.Bookmarks").level = Log.Level.Trace;
michael@0 173
michael@0 174 test_tracking();
michael@0 175 test_onItemChanged();
michael@0 176 test_onItemMoved();
michael@0 177 }
michael@0 178

mercurial