michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Tests that nsBrowserGlue correctly restores bookmarks from a JSON backup if michael@0: * database has been created and one backup is available. michael@0: */ michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "bs", michael@0: "@mozilla.org/browser/nav-bookmarks-service;1", michael@0: "nsINavBookmarksService"); michael@0: XPCOMUtils.defineLazyServiceGetter(this, "anno", michael@0: "@mozilla.org/browser/annotation-service;1", michael@0: "nsIAnnotationService"); michael@0: michael@0: let bookmarksObserver = { michael@0: onBeginUpdateBatch: function() {}, michael@0: onEndUpdateBatch: function() { michael@0: let itemId = bs.getIdForItemAt(bs.toolbarFolder, 0); michael@0: do_check_neq(itemId, -1); michael@0: if (anno.itemHasAnnotation(itemId, "Places/SmartBookmark")) michael@0: continue_test(); michael@0: }, michael@0: onItemAdded: function() {}, michael@0: onItemRemoved: function(id, folder, index, itemType) {}, michael@0: onItemChanged: function() {}, michael@0: onItemVisited: function(id, visitID, time) {}, michael@0: onItemMoved: function() {}, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsINavBookmarkObserver]) michael@0: }; michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: michael@0: // Create our bookmarks.html copying bookmarks.glue.html to the profile michael@0: // folder. It will be ignored. michael@0: create_bookmarks_html("bookmarks.glue.html"); michael@0: michael@0: // Create our JSON backup copying bookmarks.glue.json to the profile michael@0: // folder. It will be ignored. michael@0: remove_all_JSON_backups(); michael@0: create_JSON_backup("bookmarks.glue.json"); michael@0: michael@0: // Remove current database file. michael@0: let db = gProfD.clone(); michael@0: db.append("places.sqlite"); michael@0: if (db.exists()) { michael@0: db.remove(false); michael@0: do_check_false(db.exists()); michael@0: } michael@0: michael@0: // Initialize nsBrowserGlue before Places. michael@0: Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIBrowserGlue); michael@0: michael@0: // Initialize Places through the History Service. michael@0: let hs = Cc["@mozilla.org/browser/nav-history-service;1"]. michael@0: getService(Ci.nsINavHistoryService); michael@0: // Check a new database has been created. michael@0: // nsBrowserGlue uses databaseStatus to manage initialization. michael@0: do_check_eq(hs.databaseStatus, hs.DATABASE_STATUS_CREATE); michael@0: michael@0: // The test will continue once restore has finished and smart bookmarks michael@0: // have been created. michael@0: bs.addObserver(bookmarksObserver, false); michael@0: } michael@0: michael@0: function continue_test() { michael@0: // Check that JSON backup has been restored. michael@0: // Notice restore from JSON notification is fired before smart bookmarks creation. michael@0: let itemId = bs.getIdForItemAt(bs.toolbarFolder, SMART_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(bs.getItemTitle(itemId), "examplejson"); michael@0: michael@0: remove_bookmarks_html(); michael@0: remove_all_JSON_backups(); michael@0: michael@0: do_test_finished(); michael@0: }