test_browserGlue_smartBookmarks.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 var bg = Cc["@mozilla.org/browser/browserglue;1"].
46  getService(Ci.nsIBrowserGlue);
47 
48 // Initialize Places.
49 var hs = Cc["@mozilla.org/browser/nav-history-service;1"].
50  getService(Ci.nsINavHistoryService);
51 var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
52  getService(Ci.nsINavBookmarksService);
53 
54 // Get other services.
55 var ps = Cc["@mozilla.org/preferences-service;1"].
56  getService(Ci.nsIPrefBranch);
57 var os = Cc["@mozilla.org/observer-service;1"].
58  getService(Ci.nsIObserverService);
59 var as = Cc["@mozilla.org/browser/annotation-service;1"].
60  getService(Ci.nsIAnnotationService);
61 
62 const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion";
63 const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark";
64 
65 const TOPIC_PLACES_INIT_COMPLETE = "places-init-complete";
66 
67 var tests = [];
68 
69 //------------------------------------------------------------------------------
70 
71 tests.push({
72  description: "All smart bookmarks are created if smart bookmarks version is 0.",
73  exec: function() {
74  // Sanity check: we should not have any bookmark on the toolbar.
75  do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1);
76  // Sanity check: we should not have any bookmark on the menu.
77  do_check_eq(bs.getIdForItemAt(bs.bookmarksMenuFolder, 0), -1);
78 
79  // Set preferences.
80  ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0);
81  // Force nsBrowserGlue::_initPlaces().
82  os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
83 
84  // Count items on toolbar.
86  // Count items on menu.
88 
89  // Check version has been updated.
91 
92  next_test();
93  }
94 });
95 
96 //------------------------------------------------------------------------------
97 
98 tests.push({
99  description: "An existing smart bookmark is replaced when version changes.",
100  exec: function() {
101  // Sanity check: we have a smart bookmark on the toolbar.
102  var itemId = bs.getIdForItemAt(bs.toolbarFolder, 0);
103  do_check_neq(itemId, -1);
104  do_check_true(as.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
105 
106  // Change its title.
107  bs.setItemTitle(itemId, "new title");
108  do_check_eq(bs.getItemTitle(itemId), "new title");
109 
110  // Set preferences.
111  ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1);
112  // Force nsBrowserGlue::_initPlaces().
113  os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
114 
115  // Count items on toolbar.
117  // Count items on menu.
119 
120  // check smart bookmark has been replaced, itemId has changed.
121  itemId = bs.getIdForItemAt(bs.toolbarFolder, 0);
122  do_check_neq(itemId, -1);
123  do_check_neq(bs.getItemTitle(itemId), "new title");
124  do_check_true(as.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
125 
126  // Check version has been updated.
128 
129  next_test();
130  }
131 });
132 
133 //------------------------------------------------------------------------------
134 
135 tests.push({
136  description: "An explicitly removed smart bookmark should not be recreated.",
137  exec: function() {
138  // Set preferences.
139  ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1);
140  // Remove toolbar's smart bookmarks
141  bs.removeFolderChildren(bs.toolbarFolder);
142 
143  // Force nsBrowserGlue::_initPlaces().
144  os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
145 
146  // Count items on toolbar, we should not have recreated the smart bookmark.
147  do_check_eq(countFolderChildren(bs.toolbarFolder), 0);
148  // Count items on menu.
150 
151  // Check version has been updated.
153 
154  next_test();
155  }
156 });
157 
158 //------------------------------------------------------------------------------
159 
160 tests.push({
161  description: "Even if a smart bookmark has been removed recreate it if version is 0.",
162  exec: function() {
163  // Set preferences.
164  ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0);
165 
166  // Force nsBrowserGlue::_initPlaces().
167  os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
168 
169  // Count items on toolbar, we should not have recreated the smart bookmark.
171  // Count items on menu.
173 
174  // Check version has been updated.
176 
177  finish_test();
178  }
179 });
180 //------------------------------------------------------------------------------
181 
182 function countFolderChildren(aFolderItemId) {
183  var query = hs.getNewQuery();
184  query.setFolders([aFolderItemId], 1);
185  var options = hs.getNewQueryOptions();
186  var rootNode = hs.executeQuery(query, options).root;
187  rootNode.containerOpen = true;
188  var cc = rootNode.childCount;
189  rootNode.containerOpen = false;
190  return cc;
191 }
192 
193 function finish_test() {
194  // Clean up database from all bookmarks.
196  // Simulate application closing to remove the idle observer and avoid leaks.
197  os.notifyObservers(null, "quit-application-granted", null);
198  do_test_finished();
199 }
200 
201 var testIndex = 0;
202 function next_test() {
203  // Simulate application closing to remove the idle observer and avoid leaks.
204  os.notifyObservers(null, "quit-application-granted", null);
205 
206  // nsBrowserGlue stops observing topics after first notification,
207  // so we add back the observer to test additional runs.
208  os.addObserver(bg, TOPIC_PLACES_INIT_COMPLETE, false);
209 
210  // Execute next test.
211  var test = tests.shift();
212  dump("\nTEST " + (++testIndex) + ": " + test.description);
213  test.exec();
214 }
215 
216 function run_test() {
217  // Clean up database from all bookmarks.
219 
220  // Kick-off tests.
221  do_test_pending();
222  next_test();
223 }
const Cc
do_check_eq(typeof PlacesUtils,"object")
const SMART_BOOKMARKS_VERSION
const PREF_SMART_BOOKMARKS_VERSION
function remove_all_bookmarks()
const SMART_BOOKMARKS_ON_TOOLBAR
getService(Ci.sbIFaceplateManager)
const SMART_BOOKMARKS_ANNO
function run_test()
const TOPIC_PLACES_INIT_COMPLETE
function next_test()
return null
Definition: FeedWriter.js:1143
function finish_test()
foldersync options
Definition: options.js:13
const Ci
const SMART_BOOKMARKS_ON_MENU
function countFolderChildren(aFolderItemId)