test_421483.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) 2008
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Marco Bonardo <mak77@supereva.it>
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 
39 // Get bookmarks service
40 try {
41  var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
42  getService(Ci.nsINavBookmarksService);
43 } catch(ex) {
44  do_throw("Could not get Bookmarks service\n");
45 }
46 
47 // Get annotation service
48 try {
49  var annosvc = Cc["@mozilla.org/browser/annotation-service;1"].
50  getService(Ci.nsIAnnotationService);
51 } catch(ex) {
52  do_throw("Could not get Annotation service\n");
53 }
54 
55 // Get browser glue
56 try {
57  var gluesvc = Cc["@mozilla.org/browser/browserglue;1"].
58  getService(Ci.nsIBrowserGlue);
59 } catch(ex) {
60  do_throw("Could not get BrowserGlue service\n");
61 }
62 
63 // Get pref service
64 try {
65  var pref = Cc["@mozilla.org/preferences-service;1"].
66  getService(Ci.nsIPrefBranch);
67 } catch(ex) {
68  do_throw("Could not get Preferences service\n");
69 }
70 
71 const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark";
72 const SMART_BOOKMARKS_PREF = "browser.places.smartBookmarksVersion";
73 
74 // main
75 function run_test() {
76  // TEST 1: smart bookmarks disabled
77  pref.setIntPref("browser.places.smartBookmarksVersion", -1);
78  gluesvc.ensurePlacesDefaultQueriesInitialized();
79  var smartBookmarkItemIds = annosvc.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO, {});
80  do_check_eq(smartBookmarkItemIds.length, 0);
81  // check that pref has not been bumped up
82  do_check_eq(pref.getIntPref("browser.places.smartBookmarksVersion"), -1);
83 
84  // TEST 2: create smart bookmarks
85  pref.setIntPref("browser.places.smartBookmarksVersion", 0);
86  gluesvc.ensurePlacesDefaultQueriesInitialized();
87  smartBookmarkItemIds = annosvc.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO, {});
88  do_check_neq(smartBookmarkItemIds.length, 0);
89  // check that pref has been bumped up
90  do_check_true(pref.getIntPref("browser.places.smartBookmarksVersion") > 0);
91 
92  var smartBookmarksCount = smartBookmarkItemIds.length;
93 
94  // TEST 3: smart bookmarks restore
95  // remove one smart bookmark and restore
96  bmsvc.removeItem(smartBookmarkItemIds[0]);
97  pref.setIntPref("browser.places.smartBookmarksVersion", 0);
98  gluesvc.ensurePlacesDefaultQueriesInitialized();
99  smartBookmarkItemIds = annosvc.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO, {});
100  do_check_eq(smartBookmarkItemIds.length, smartBookmarksCount);
101  // check that pref has been bumped up
102  do_check_true(pref.getIntPref("browser.places.smartBookmarksVersion") > 0);
103 
104  // TEST 4: move a smart bookmark, change its title, then restore
105  // smart bookmark should be restored in place
106  var parent = bmsvc.getFolderIdForItem(smartBookmarkItemIds[0]);
107  var oldTitle = bmsvc.getItemTitle(smartBookmarkItemIds[0]);
108  // create a subfolder and move inside it
109  var newParent = bmsvc.createFolder(parent, "test", bmsvc.DEFAULT_INDEX);
110  bmsvc.moveItem(smartBookmarkItemIds[0], newParent, bmsvc.DEFAULT_INDEX);
111  // change title
112  bmsvc.setItemTitle(smartBookmarkItemIds[0], "new title");
113  // restore
114  pref.setIntPref("browser.places.smartBookmarksVersion", 0);
115  gluesvc.ensurePlacesDefaultQueriesInitialized();
116  smartBookmarkItemIds = annosvc.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO, {});
117  do_check_eq(smartBookmarkItemIds.length, smartBookmarksCount);
118  do_check_eq(bmsvc.getFolderIdForItem(smartBookmarkItemIds[0]), newParent);
119  do_check_eq(bmsvc.getItemTitle(smartBookmarkItemIds[0]), oldTitle);
120  // check that pref has been bumped up
121  do_check_true(pref.getIntPref("browser.places.smartBookmarksVersion") > 0);
122 }
const Cc
do_check_eq(typeof PlacesUtils,"object")
var pref
Definition: openLocation.js:44
const SMART_BOOKMARKS_ANNO
Definition: test_421483.js:71
function run_test()
Definition: test_421483.js:75
getService(Ci.sbIFaceplateManager)
const bmsvc
const SMART_BOOKMARKS_PREF
Definition: test_421483.js:72
const Ci