1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/places/tests/unit/test_browserGlue_restore.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,82 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/** 1.11 + * Tests that nsBrowserGlue correctly restores bookmarks from a JSON backup if 1.12 + * database has been created and one backup is available. 1.13 + */ 1.14 + 1.15 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); 1.16 + 1.17 +XPCOMUtils.defineLazyServiceGetter(this, "bs", 1.18 + "@mozilla.org/browser/nav-bookmarks-service;1", 1.19 + "nsINavBookmarksService"); 1.20 +XPCOMUtils.defineLazyServiceGetter(this, "anno", 1.21 + "@mozilla.org/browser/annotation-service;1", 1.22 + "nsIAnnotationService"); 1.23 + 1.24 +let bookmarksObserver = { 1.25 + onBeginUpdateBatch: function() {}, 1.26 + onEndUpdateBatch: function() { 1.27 + let itemId = bs.getIdForItemAt(bs.toolbarFolder, 0); 1.28 + do_check_neq(itemId, -1); 1.29 + if (anno.itemHasAnnotation(itemId, "Places/SmartBookmark")) 1.30 + continue_test(); 1.31 + }, 1.32 + onItemAdded: function() {}, 1.33 + onItemRemoved: function(id, folder, index, itemType) {}, 1.34 + onItemChanged: function() {}, 1.35 + onItemVisited: function(id, visitID, time) {}, 1.36 + onItemMoved: function() {}, 1.37 + QueryInterface: XPCOMUtils.generateQI([Ci.nsINavBookmarkObserver]) 1.38 +}; 1.39 + 1.40 +function run_test() { 1.41 + do_test_pending(); 1.42 + 1.43 + // Create our bookmarks.html copying bookmarks.glue.html to the profile 1.44 + // folder. It will be ignored. 1.45 + create_bookmarks_html("bookmarks.glue.html"); 1.46 + 1.47 + // Create our JSON backup copying bookmarks.glue.json to the profile 1.48 + // folder. It will be ignored. 1.49 + remove_all_JSON_backups(); 1.50 + create_JSON_backup("bookmarks.glue.json"); 1.51 + 1.52 + // Remove current database file. 1.53 + let db = gProfD.clone(); 1.54 + db.append("places.sqlite"); 1.55 + if (db.exists()) { 1.56 + db.remove(false); 1.57 + do_check_false(db.exists()); 1.58 + } 1.59 + 1.60 + // Initialize nsBrowserGlue before Places. 1.61 + Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIBrowserGlue); 1.62 + 1.63 + // Initialize Places through the History Service. 1.64 + let hs = Cc["@mozilla.org/browser/nav-history-service;1"]. 1.65 + getService(Ci.nsINavHistoryService); 1.66 + // Check a new database has been created. 1.67 + // nsBrowserGlue uses databaseStatus to manage initialization. 1.68 + do_check_eq(hs.databaseStatus, hs.DATABASE_STATUS_CREATE); 1.69 + 1.70 + // The test will continue once restore has finished and smart bookmarks 1.71 + // have been created. 1.72 + bs.addObserver(bookmarksObserver, false); 1.73 +} 1.74 + 1.75 +function continue_test() { 1.76 + // Check that JSON backup has been restored. 1.77 + // Notice restore from JSON notification is fired before smart bookmarks creation. 1.78 + let itemId = bs.getIdForItemAt(bs.toolbarFolder, SMART_BOOKMARKS_ON_TOOLBAR); 1.79 + do_check_eq(bs.getItemTitle(itemId), "examplejson"); 1.80 + 1.81 + remove_bookmarks_html(); 1.82 + remove_all_JSON_backups(); 1.83 + 1.84 + do_test_finished(); 1.85 +}