test_browserGlue_prefs.js
Go to the documentation of this file.
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 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Places Unit Test code.
17  *
18  * The Initial Developer of the Original Code is Mozilla Corp.
19  * Portions created by the Initial Developer are Copyright (C) 2009
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Marco Bonardo <mak77@bonardo.net>
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
44 // Initialize browserGlue.
45 let bg = Cc["@mozilla.org/browser/browserglue;1"].
46  getService(Ci.nsIBrowserGlue);
47 
48 // Initialize Places through Bookmarks Service.
49 let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
50  getService(Ci.nsINavBookmarksService);
51 
52 // Get other services.
53 let ps = Cc["@mozilla.org/preferences-service;1"].
54  getService(Ci.nsIPrefBranch);
55 let os = Cc["@mozilla.org/observer-service;1"].
56  getService(Ci.nsIObserverService);
57 
58 const PREF_IMPORT_BOOKMARKS_HTML = "browser.places.importBookmarksHTML";
59 const PREF_RESTORE_DEFAULT_BOOKMARKS = "browser.bookmarks.restore_default_bookmarks";
60 const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion";
61 const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML";
62 
63 const TOPIC_PLACES_INIT_COMPLETE = "places-init-complete";
64 
65 let tests = [];
66 
67 //------------------------------------------------------------------------------
68 
69 tests.push({
70  description: "Import from bookmarks.html if importBookmarksHTML is true.",
71  exec: function() {
72  // Sanity check: we should not have any bookmark on the toolbar.
73  do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1);
74  // Set preferences.
75  ps.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
76  // Force nsBrowserGlue::_initPlaces().
77  os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
78 
79  // Check bookmarks.html has been imported, and a smart bookmark has been
80  // created.
81  let itemId = bs.getIdForItemAt(bs.toolbarFolder,
83  do_check_eq(bs.getItemTitle(itemId), "example");
84  // Check preferences have been reverted.
85  do_check_false(ps.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
86 
87  next_test();
88  }
89 });
90 
91 //------------------------------------------------------------------------------
92 
93 tests.push({
94  description: "import from bookmarks.html, but don't create smart bookmarks if they are disabled",
95  exec: function() {
96  // Sanity check: we should not have any bookmark on the toolbar.
97  do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1);
98  // Set preferences.
99  ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, -1);
100  ps.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
101  // Force nsBrowserGlue::_initPlaces().
102  os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
103 
104  // Check bookmarks.html has been imported, but smart bookmarks have not
105  // been created.
106  let itemId = bs.getIdForItemAt(bs.toolbarFolder, 0);
107  do_check_eq(bs.getItemTitle(itemId), "example");
108  // Check preferences have been reverted.
109  do_check_false(ps.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
110 
111  next_test();
112  }
113 });
114 
115 //------------------------------------------------------------------------------
116 
117 tests.push({
118  description: "Import from bookmarks.html, but don't create smart bookmarks if autoExportHTML is true and they are at latest version",
119  exec: function() {
120  // Sanity check: we should not have any bookmark on the toolbar.
121  do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1);
122  // Set preferences.
123  ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 999);
124  ps.setBoolPref(PREF_AUTO_EXPORT_HTML, true);
125  ps.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
126  // Force nsBrowserGlue::_initPlaces()
127  os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
128 
129  // Check bookmarks.html has been imported, but smart bookmarks have not
130  // been created.
131  let itemId = bs.getIdForItemAt(bs.toolbarFolder, 0);
132  do_check_eq(bs.getItemTitle(itemId), "example");
133  do_check_false(ps.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
134  // Check preferences have been reverted.
135  ps.setBoolPref(PREF_AUTO_EXPORT_HTML, false);
136 
137  next_test();
138  }
139 });
140 
141 //------------------------------------------------------------------------------
142 
143 tests.push({
144  description: "Import from bookmarks.html, and create smart bookmarks if autoExportHTML is true and they are not at latest version.",
145  exec: function() {
146  // Sanity check: we should not have any bookmark on the toolbar.
147  do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1);
148  // Set preferences.
149  ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0);
150  ps.setBoolPref(PREF_AUTO_EXPORT_HTML, true);
151  ps.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
152  // Force nsBrowserGlue::_initPlaces()
153  os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
154 
155  // Check bookmarks.html has been imported, but smart bookmarks have not
156  // been created.
157  let itemId = bs.getIdForItemAt(bs.toolbarFolder, SMART_BOOKMARKS_ON_TOOLBAR);
158  do_check_eq(bs.getItemTitle(itemId), "example");
159  do_check_false(ps.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
160  // Check preferences have been reverted.
161  ps.setBoolPref(PREF_AUTO_EXPORT_HTML, false);
162 
163  next_test();
164  }
165 });
166 
167 //------------------------------------------------------------------------------
168 tests.push({
169  description: "restore from default bookmarks.html if restore_default_bookmarks is true.",
170  exec: function() {
171  // Sanity check: we should not have any bookmark on the toolbar.
172  do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1);
173  // Set preferences.
174  ps.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true);
175  // Force nsBrowserGlue::_initPlaces()
176  os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
177 
178  // Check bookmarks.html has been restored.
179  let itemId = bs.getIdForItemAt(bs.toolbarFolder, SMART_BOOKMARKS_ON_TOOLBAR + 1);
180  do_check_true(itemId > 0);
181  // Check preferences have been reverted.
182  do_check_false(ps.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
183 
184  next_test();
185  }
186 });
187 
188 //------------------------------------------------------------------------------
189 
190 tests.push({
191  description: "setting both importBookmarksHTML and restore_default_bookmarks should restore defaults.",
192  exec: function() {
193  // Sanity check: we should not have any bookmark on the toolbar.
194  do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1);
195  // Set preferences.
196  ps.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
197  ps.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true);
198  // Force nsBrowserGlue::_initPlaces()
199  os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
200 
201  // Check bookmarks.html has been restored.
202  let itemId = bs.getIdForItemAt(bs.toolbarFolder, SMART_BOOKMARKS_ON_TOOLBAR + 1);
203  do_check_true(itemId > 0);
204  // Check preferences have been reverted.
205  do_check_false(ps.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
206  do_check_false(ps.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
207 
208  finish_test();
209  }
210 });
211 
212 //------------------------------------------------------------------------------
213 
214 function finish_test() {
215  // Simulate application closing to remove the idle observer and avoid leaks.
216  os.notifyObservers(null, "quit-application-granted", null);
217  do_test_finished();
218 }
219 
220 var testIndex = 0;
221 function next_test() {
222  // Clean up database from all bookmarks.
224 
225  // Simulate application closing to remove the idle observer and avoid leaks.
226  os.notifyObservers(null, "quit-application-granted", null);
227 
228  // nsBrowserGlue stops observing topics after first notification,
229  // so we add back the observer to test additional runs.
230  os.addObserver(bg, TOPIC_PLACES_INIT_COMPLETE, false);
231 
232  // Execute next test.
233  let test = tests.shift();
234  dump("\nTEST " + (++testIndex) + ": " + test.description);
235  test.exec();
236 }
237 
238 function run_test() {
239  // Create our bookmarks.html from bookmarks.glue.html.
240  create_bookmarks_html("bookmarks.glue.html");
241 
242  // Create our JSON backup from bookmarks.glue.json.
243  create_JSON_backup("bookmarks.glue.json");
244 
245  // Kick-off tests.
246  do_test_pending();
247  next_test();
248 }
const Cc
do_check_eq(typeof PlacesUtils,"object")
function create_bookmarks_html(aFilename)
function remove_all_bookmarks()
function next_test()
function run_test()
const SMART_BOOKMARKS_ON_TOOLBAR
getService(Ci.sbIFaceplateManager)
const PREF_RESTORE_DEFAULT_BOOKMARKS
const PREF_SMART_BOOKMARKS_VERSION
function finish_test()
return null
Definition: FeedWriter.js:1143
const PREF_IMPORT_BOOKMARKS_HTML
function create_JSON_backup(aFilename)
const Ci
const PREF_AUTO_EXPORT_HTML
const TOPIC_PLACES_INIT_COMPLETE