browser/components/places/tests/unit/test_browserGlue_prefs.js

branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
equal deleted inserted replaced
-1:000000000000 0:44b35fd6f940
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 /**
5 * Tests that nsBrowserGlue is correctly interpreting the preferences settable
6 * by the user or by other components.
7 */
8
9 const PREF_IMPORT_BOOKMARKS_HTML = "browser.places.importBookmarksHTML";
10 const PREF_RESTORE_DEFAULT_BOOKMARKS = "browser.bookmarks.restore_default_bookmarks";
11 const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion";
12 const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML";
13
14 const TOPIC_BROWSERGLUE_TEST = "browser-glue-test";
15 const TOPICDATA_FORCE_PLACES_INIT = "force-places-init";
16
17 let bg = Cc["@mozilla.org/browser/browserglue;1"].
18 getService(Ci.nsIBrowserGlue);
19
20 function waitForImportAndSmartBookmarks(aCallback) {
21 Services.obs.addObserver(function waitImport() {
22 Services.obs.removeObserver(waitImport, "bookmarks-restore-success");
23 // Delay to test eventual smart bookmarks creation.
24 do_execute_soon(function () {
25 promiseAsyncUpdates().then(aCallback);
26 });
27 }, "bookmarks-restore-success", false);
28 }
29
30 [
31
32 // This test must be the first one.
33 function test_checkPreferences() {
34 // Initialize Places through the History Service and check that a new
35 // database has been created.
36 do_check_eq(PlacesUtils.history.databaseStatus,
37 PlacesUtils.history.DATABASE_STATUS_CREATE);
38
39 // Wait for Places init notification.
40 Services.obs.addObserver(function(aSubject, aTopic, aData) {
41 Services.obs.removeObserver(arguments.callee,
42 "places-browser-init-complete");
43 do_execute_soon(function () {
44 // Ensure preferences status.
45 do_check_false(Services.prefs.getBoolPref(PREF_AUTO_EXPORT_HTML));
46
47 try {
48 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
49 do_throw("importBookmarksHTML pref should not exist");
50 }
51 catch(ex) {}
52
53 try {
54 do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
55 do_throw("importBookmarksHTML pref should not exist");
56 }
57 catch(ex) {}
58
59 run_next_test();
60 });
61 }, "places-browser-init-complete", false);
62 },
63
64 function test_import()
65 {
66 do_log_info("Import from bookmarks.html if importBookmarksHTML is true.");
67
68 remove_all_bookmarks();
69 // Sanity check: we should not have any bookmark on the toolbar.
70 let itemId =
71 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
72 do_check_eq(itemId, -1);
73
74 // Set preferences.
75 Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
76
77 waitForImportAndSmartBookmarks(function () {
78 // Check bookmarks.html has been imported, and a smart bookmark has been
79 // created.
80 itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
81 SMART_BOOKMARKS_ON_TOOLBAR);
82 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example");
83 // Check preferences have been reverted.
84 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
85
86 run_next_test();
87 });
88 // Force nsBrowserGlue::_initPlaces().
89 do_log_info("Simulate Places init");
90 bg.QueryInterface(Ci.nsIObserver).observe(null,
91 TOPIC_BROWSERGLUE_TEST,
92 TOPICDATA_FORCE_PLACES_INIT);
93 },
94
95 function test_import_noSmartBookmarks()
96 {
97 do_log_info("import from bookmarks.html, but don't create smart bookmarks \
98 if they are disabled");
99
100 remove_all_bookmarks();
101 // Sanity check: we should not have any bookmark on the toolbar.
102 let itemId =
103 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
104 do_check_eq(itemId, -1);
105
106 // Set preferences.
107 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, -1);
108 Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
109
110 waitForImportAndSmartBookmarks(function () {
111 // Check bookmarks.html has been imported, but smart bookmarks have not
112 // been created.
113 itemId =
114 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
115 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example");
116 // Check preferences have been reverted.
117 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
118
119 run_next_test();
120 });
121 // Force nsBrowserGlue::_initPlaces().
122 do_log_info("Simulate Places init");
123 bg.QueryInterface(Ci.nsIObserver).observe(null,
124 TOPIC_BROWSERGLUE_TEST,
125 TOPICDATA_FORCE_PLACES_INIT);
126 },
127
128 function test_import_autoExport_updatedSmartBookmarks()
129 {
130 do_log_info("Import from bookmarks.html, but don't create smart bookmarks \
131 if autoExportHTML is true and they are at latest version");
132
133 remove_all_bookmarks();
134 // Sanity check: we should not have any bookmark on the toolbar.
135 let itemId =
136 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
137 do_check_eq(itemId, -1);
138
139 // Set preferences.
140 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 999);
141 Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, true);
142 Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
143
144 waitForImportAndSmartBookmarks(function () {
145 // Check bookmarks.html has been imported, but smart bookmarks have not
146 // been created.
147 itemId =
148 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
149 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example");
150 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
151 // Check preferences have been reverted.
152 Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, false);
153
154 run_next_test();
155 });
156 // Force nsBrowserGlue::_initPlaces()
157 do_log_info("Simulate Places init");
158 bg.QueryInterface(Ci.nsIObserver).observe(null,
159 TOPIC_BROWSERGLUE_TEST,
160 TOPICDATA_FORCE_PLACES_INIT);
161 },
162
163 function test_import_autoExport_oldSmartBookmarks()
164 {
165 do_log_info("Import from bookmarks.html, and create smart bookmarks if \
166 autoExportHTML is true and they are not at latest version.");
167
168 remove_all_bookmarks();
169 // Sanity check: we should not have any bookmark on the toolbar.
170 let itemId =
171 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
172 do_check_eq(itemId, -1);
173
174 // Set preferences.
175 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0);
176 Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, true);
177 Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
178
179 waitForImportAndSmartBookmarks(function () {
180 // Check bookmarks.html has been imported, but smart bookmarks have not
181 // been created.
182 itemId =
183 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
184 SMART_BOOKMARKS_ON_TOOLBAR);
185 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example");
186 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
187 // Check preferences have been reverted.
188 Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, false);
189
190 run_next_test();
191 });
192 // Force nsBrowserGlue::_initPlaces()
193 do_log_info("Simulate Places init");
194 bg.QueryInterface(Ci.nsIObserver).observe(null,
195 TOPIC_BROWSERGLUE_TEST,
196 TOPICDATA_FORCE_PLACES_INIT);
197 },
198
199 function test_restore()
200 {
201 do_log_info("restore from default bookmarks.html if \
202 restore_default_bookmarks is true.");
203
204 remove_all_bookmarks();
205 // Sanity check: we should not have any bookmark on the toolbar.
206 let itemId =
207 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
208 do_check_eq(itemId, -1);
209
210 // Set preferences.
211 Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true);
212
213 waitForImportAndSmartBookmarks(function () {
214 // Check bookmarks.html has been restored.
215 itemId =
216 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
217 SMART_BOOKMARKS_ON_TOOLBAR);
218 do_check_true(itemId > 0);
219 // Check preferences have been reverted.
220 do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
221
222 run_next_test();
223 });
224 // Force nsBrowserGlue::_initPlaces()
225 do_log_info("Simulate Places init");
226 bg.QueryInterface(Ci.nsIObserver).observe(null,
227 TOPIC_BROWSERGLUE_TEST,
228 TOPICDATA_FORCE_PLACES_INIT);
229
230 },
231
232 function test_restore_import()
233 {
234 do_log_info("setting both importBookmarksHTML and \
235 restore_default_bookmarks should restore defaults.");
236
237 remove_all_bookmarks();
238 // Sanity check: we should not have any bookmark on the toolbar.
239 let itemId =
240 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
241 do_check_eq(itemId, -1);
242
243 // Set preferences.
244 Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
245 Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true);
246
247 waitForImportAndSmartBookmarks(function () {
248 // Check bookmarks.html has been restored.
249 itemId =
250 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
251 SMART_BOOKMARKS_ON_TOOLBAR);
252 do_check_true(itemId > 0);
253 // Check preferences have been reverted.
254 do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
255 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
256
257 run_next_test();
258 });
259 // Force nsBrowserGlue::_initPlaces()
260 do_log_info("Simulate Places init");
261 bg.QueryInterface(Ci.nsIObserver).observe(null,
262 TOPIC_BROWSERGLUE_TEST,
263 TOPICDATA_FORCE_PLACES_INIT);
264 }
265
266 ].forEach(add_test);
267
268 do_register_cleanup(function () {
269 remove_all_bookmarks();
270 remove_bookmarks_html();
271 remove_all_JSON_backups();
272 });
273
274 function run_test()
275 {
276 // Create our bookmarks.html from bookmarks.glue.html.
277 create_bookmarks_html("bookmarks.glue.html");
278 remove_all_JSON_backups();
279 // Create our JSON backup from bookmarks.glue.json.
280 create_JSON_backup("bookmarks.glue.json");
281
282 run_next_test();
283 }

mercurial