|
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /** |
|
8 * Tests that nsBrowserGlue is correctly interpreting the preferences settable |
|
9 * by the user or by other components. |
|
10 */ |
|
11 |
|
12 const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion"; |
|
13 const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML"; |
|
14 const PREF_IMPORT_BOOKMARKS_HTML = "browser.places.importBookmarksHTML"; |
|
15 const PREF_RESTORE_DEFAULT_BOOKMARKS = "browser.bookmarks.restore_default_bookmarks"; |
|
16 |
|
17 const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark"; |
|
18 |
|
19 /** |
|
20 * Rebuilds smart bookmarks listening to console output to report any message or |
|
21 * exception generated when calling ensurePlacesDefaultQueriesInitialized(). |
|
22 */ |
|
23 function rebuildSmartBookmarks() { |
|
24 let consoleListener = { |
|
25 observe: function(aMsg) { |
|
26 print("Got console message: " + aMsg.message); |
|
27 }, |
|
28 |
|
29 QueryInterface: XPCOMUtils.generateQI([ |
|
30 Ci.nsIConsoleListener |
|
31 ]), |
|
32 }; |
|
33 Services.console.reset(); |
|
34 Services.console.registerListener(consoleListener); |
|
35 Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIBrowserGlue) |
|
36 .ensurePlacesDefaultQueriesInitialized(); |
|
37 Services.console.unregisterListener(consoleListener); |
|
38 } |
|
39 |
|
40 |
|
41 let tests = []; |
|
42 //------------------------------------------------------------------------------ |
|
43 |
|
44 tests.push({ |
|
45 description: "All smart bookmarks are created if smart bookmarks version is 0.", |
|
46 exec: function() { |
|
47 // Sanity check: we should have default bookmark. |
|
48 do_check_neq(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0), -1); |
|
49 do_check_neq(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0), -1); |
|
50 |
|
51 // Set preferences. |
|
52 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0); |
|
53 |
|
54 rebuildSmartBookmarks(); |
|
55 |
|
56 // Count items. |
|
57 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), |
|
58 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); |
|
59 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), |
|
60 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); |
|
61 |
|
62 // Check version has been updated. |
|
63 do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION), |
|
64 SMART_BOOKMARKS_VERSION); |
|
65 |
|
66 next_test(); |
|
67 } |
|
68 }); |
|
69 |
|
70 //------------------------------------------------------------------------------ |
|
71 |
|
72 tests.push({ |
|
73 description: "An existing smart bookmark is replaced when version changes.", |
|
74 exec: function() { |
|
75 // Sanity check: we have a smart bookmark on the toolbar. |
|
76 let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); |
|
77 do_check_neq(itemId, -1); |
|
78 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); |
|
79 // Change its title. |
|
80 PlacesUtils.bookmarks.setItemTitle(itemId, "new title"); |
|
81 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "new title"); |
|
82 |
|
83 // Sanity check items. |
|
84 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), |
|
85 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); |
|
86 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), |
|
87 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); |
|
88 |
|
89 // Set preferences. |
|
90 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1); |
|
91 |
|
92 rebuildSmartBookmarks(); |
|
93 |
|
94 // Count items. |
|
95 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), |
|
96 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); |
|
97 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), |
|
98 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); |
|
99 |
|
100 // Check smart bookmark has been replaced, itemId has changed. |
|
101 itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); |
|
102 do_check_neq(itemId, -1); |
|
103 do_check_neq(PlacesUtils.bookmarks.getItemTitle(itemId), "new title"); |
|
104 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); |
|
105 |
|
106 // Check version has been updated. |
|
107 do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION), |
|
108 SMART_BOOKMARKS_VERSION); |
|
109 |
|
110 next_test(); |
|
111 } |
|
112 }); |
|
113 |
|
114 //------------------------------------------------------------------------------ |
|
115 |
|
116 tests.push({ |
|
117 description: "bookmarks position is retained when version changes.", |
|
118 exec: function() { |
|
119 // Sanity check items. |
|
120 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), |
|
121 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); |
|
122 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), |
|
123 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); |
|
124 |
|
125 let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0); |
|
126 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); |
|
127 let firstItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId); |
|
128 |
|
129 itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1); |
|
130 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); |
|
131 let secondItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId); |
|
132 |
|
133 // Set preferences. |
|
134 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1); |
|
135 |
|
136 rebuildSmartBookmarks(); |
|
137 |
|
138 // Count items. |
|
139 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), |
|
140 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); |
|
141 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), |
|
142 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); |
|
143 |
|
144 // Check smart bookmarks are still in correct position. |
|
145 itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0); |
|
146 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); |
|
147 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), firstItemTitle); |
|
148 |
|
149 itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1); |
|
150 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); |
|
151 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), secondItemTitle); |
|
152 |
|
153 // Check version has been updated. |
|
154 do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION), |
|
155 SMART_BOOKMARKS_VERSION); |
|
156 |
|
157 next_test(); |
|
158 } |
|
159 }); |
|
160 |
|
161 //------------------------------------------------------------------------------ |
|
162 |
|
163 tests.push({ |
|
164 description: "moved bookmarks position is retained when version changes.", |
|
165 exec: function() { |
|
166 // Sanity check items. |
|
167 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), |
|
168 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); |
|
169 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), |
|
170 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); |
|
171 |
|
172 let itemId1 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0); |
|
173 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId1, SMART_BOOKMARKS_ANNO)); |
|
174 let firstItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId1); |
|
175 |
|
176 let itemId2 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1); |
|
177 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId2, SMART_BOOKMARKS_ANNO)); |
|
178 let secondItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId2); |
|
179 |
|
180 // Move the first smart bookmark to the end of the menu. |
|
181 PlacesUtils.bookmarks.moveItem(itemId1, PlacesUtils.bookmarksMenuFolderId, |
|
182 PlacesUtils.bookmarks.DEFAULT_INDEX); |
|
183 |
|
184 do_check_eq(itemId1, PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, |
|
185 PlacesUtils.bookmarks.DEFAULT_INDEX)); |
|
186 |
|
187 // Set preferences. |
|
188 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1); |
|
189 |
|
190 rebuildSmartBookmarks(); |
|
191 |
|
192 // Count items. |
|
193 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), |
|
194 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); |
|
195 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), |
|
196 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); |
|
197 |
|
198 // Check smart bookmarks are still in correct position. |
|
199 itemId2 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0); |
|
200 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId2, SMART_BOOKMARKS_ANNO)); |
|
201 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId2), secondItemTitle); |
|
202 |
|
203 itemId1 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, |
|
204 PlacesUtils.bookmarks.DEFAULT_INDEX); |
|
205 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId1, SMART_BOOKMARKS_ANNO)); |
|
206 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId1), firstItemTitle); |
|
207 |
|
208 // Move back the smart bookmark to the original position. |
|
209 PlacesUtils.bookmarks.moveItem(itemId1, PlacesUtils.bookmarksMenuFolderId, 1); |
|
210 |
|
211 // Check version has been updated. |
|
212 do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION), |
|
213 SMART_BOOKMARKS_VERSION); |
|
214 |
|
215 next_test(); |
|
216 } |
|
217 }); |
|
218 |
|
219 //------------------------------------------------------------------------------ |
|
220 |
|
221 tests.push({ |
|
222 description: "An explicitly removed smart bookmark should not be recreated.", |
|
223 exec: function() { |
|
224 // Remove toolbar's smart bookmarks |
|
225 PlacesUtils.bookmarks.removeItem(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0)); |
|
226 |
|
227 // Sanity check items. |
|
228 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), |
|
229 DEFAULT_BOOKMARKS_ON_TOOLBAR); |
|
230 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), |
|
231 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); |
|
232 |
|
233 // Set preferences. |
|
234 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1); |
|
235 |
|
236 rebuildSmartBookmarks(); |
|
237 |
|
238 // Count items. |
|
239 // We should not have recreated the smart bookmark on toolbar. |
|
240 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), |
|
241 DEFAULT_BOOKMARKS_ON_TOOLBAR); |
|
242 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), |
|
243 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); |
|
244 |
|
245 // Check version has been updated. |
|
246 do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION), |
|
247 SMART_BOOKMARKS_VERSION); |
|
248 |
|
249 next_test(); |
|
250 } |
|
251 }); |
|
252 |
|
253 //------------------------------------------------------------------------------ |
|
254 |
|
255 tests.push({ |
|
256 description: "Even if a smart bookmark has been removed recreate it if version is 0.", |
|
257 exec: function() { |
|
258 // Sanity check items. |
|
259 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), |
|
260 DEFAULT_BOOKMARKS_ON_TOOLBAR); |
|
261 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), |
|
262 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); |
|
263 |
|
264 // Set preferences. |
|
265 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0); |
|
266 |
|
267 rebuildSmartBookmarks(); |
|
268 |
|
269 // Count items. |
|
270 // We should not have recreated the smart bookmark on toolbar. |
|
271 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), |
|
272 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); |
|
273 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), |
|
274 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); |
|
275 |
|
276 // Check version has been updated. |
|
277 do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION), |
|
278 SMART_BOOKMARKS_VERSION); |
|
279 |
|
280 next_test(); |
|
281 } |
|
282 }); |
|
283 //------------------------------------------------------------------------------ |
|
284 |
|
285 function countFolderChildren(aFolderItemId) { |
|
286 let rootNode = PlacesUtils.getFolderContents(aFolderItemId).root; |
|
287 let cc = rootNode.childCount; |
|
288 // Dump contents. |
|
289 for (let i = 0; i < cc ; i++) { |
|
290 let node = rootNode.getChild(i); |
|
291 let title = PlacesUtils.nodeIsSeparator(node) ? "---" : node.title; |
|
292 print("Found child(" + i + "): " + title); |
|
293 } |
|
294 rootNode.containerOpen = false; |
|
295 return cc; |
|
296 } |
|
297 |
|
298 function next_test() { |
|
299 if (tests.length) { |
|
300 // Execute next test. |
|
301 let test = tests.shift(); |
|
302 print("\nTEST: " + test.description); |
|
303 test.exec(); |
|
304 } |
|
305 else { |
|
306 // Clean up database from all bookmarks. |
|
307 remove_all_bookmarks(); |
|
308 do_test_finished(); |
|
309 } |
|
310 } |
|
311 |
|
312 function run_test() { |
|
313 do_test_pending(); |
|
314 |
|
315 remove_bookmarks_html(); |
|
316 remove_all_JSON_backups(); |
|
317 |
|
318 // Initialize browserGlue, but remove it's listener to places-init-complete. |
|
319 let bg = Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIObserver); |
|
320 // Initialize Places. |
|
321 PlacesUtils.history; |
|
322 // Observes Places initialisation complete. |
|
323 Services.obs.addObserver(function waitPlaceInitComplete() { |
|
324 Services.obs.removeObserver(waitPlaceInitComplete, "places-browser-init-complete"); |
|
325 |
|
326 // Ensure preferences status. |
|
327 do_check_false(Services.prefs.getBoolPref(PREF_AUTO_EXPORT_HTML)); |
|
328 do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); |
|
329 try { |
|
330 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); |
|
331 do_throw("importBookmarksHTML pref should not exist"); |
|
332 } |
|
333 catch(ex) {} |
|
334 |
|
335 waitForImportAndSmartBookmarks(next_test); |
|
336 }, "places-browser-init-complete", false); |
|
337 |
|
338 // Usually places init would async notify to glue, but we want to avoid |
|
339 // randomness here, thus we fire the notification synchronously. |
|
340 bg.observe(null, "places-init-complete", null); |
|
341 } |
|
342 |
|
343 function waitForImportAndSmartBookmarks(aCallback) { |
|
344 Services.obs.addObserver(function waitImport() { |
|
345 Services.obs.removeObserver(waitImport, "bookmarks-restore-success"); |
|
346 // Delay to test eventual smart bookmarks creation. |
|
347 do_execute_soon(function () { |
|
348 promiseAsyncUpdates().then(aCallback); |
|
349 }); |
|
350 }, "bookmarks-restore-success", false); |
|
351 } |