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