test_browserGlue_migrate.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 
45 const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion";
46 
47 const TOPIC_PLACES_INIT_COMPLETE = "places-init-complete";
48 
49 function run_test() {
50  // Create our bookmarks.html copying bookmarks.glue.html to the profile
51  // folder. It will be ignored.
52  create_bookmarks_html("bookmarks.glue.html");
53 
54  // Remove current database file.
55  let db = gProfD.clone();
56  db.append("places.sqlite");
57  if (db.exists()) {
58  db.remove(false);
59  do_check_false(db.exists());
60  }
61 
62  // Disable Smart Bookmarks creation.
63  let ps = Cc["@mozilla.org/preferences-service;1"].
64  getService(Ci.nsIPrefBranch);
65  ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, -1);
66 
67  // Initialize Places through the History Service.
68  let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
69  getService(Ci.nsINavHistoryService);
70  // Check a new database has been created.
71  // nsBrowserGlue uses databaseStatus to manage initialization.
72  do_check_eq(hs.databaseStatus, hs.DATABASE_STATUS_CREATE);
73 
74  // A migrator would run before nsBrowserGlue, so we mimic that behavior
75  // adding a bookmark.
76  bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
77  getService(Ci.nsINavBookmarksService);
78  bs.insertBookmark(bs.bookmarksMenuFolder, uri("http://mozilla.org/"),
79  bs.DEFAULT_INDEX, "migrated");
80 
81  // Initialize nsBrowserGlue.
82  Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIBrowserGlue);
83 
84  // Places initialization has already happened, so we need to simulate
85  // it. This will force browserGlue::_initPlaces().
86  let os = Cc["@mozilla.org/observer-service;1"].
87  getService(Ci.nsIObserverService);
88  os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
89 
90  // Import could take some time, usually less than 1s, but to be sure we will
91  // check after 3s.
92  do_test_pending();
93  do_timeout(3000, "continue_test();");
94 }
95 
96 function continue_test() {
97  let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
98  getService(Ci.nsINavBookmarksService);
99 
100  // Check the created bookmarks still exist.
101  let itemId = bs.getIdForItemAt(bs.bookmarksMenuFolder, 0);
102  do_check_eq(bs.getItemTitle(itemId), "migrated");
103 
104  // Check that we have not imported any new bookmark.
105  do_check_eq(bs.getIdForItemAt(bs.bookmarksMenuFolder, 1), -1);
106  do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1);
107 
108  do_test_finished();
109 }
const Cc
do_check_eq(typeof PlacesUtils,"object")
function create_bookmarks_html(aFilename)
getService(Ci.sbIFaceplateManager)
function continue_test()
const TOPIC_PLACES_INIT_COMPLETE
return null
Definition: FeedWriter.js:1143
const PREF_SMART_BOOKMARKS_VERSION
var os
var uri
Definition: FeedWriter.js:1135
const Ci
function run_test()
var gProfD