|
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 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /** |
|
8 * Tests that nsBrowserGlue correctly restores bookmarks from a JSON backup if |
|
9 * database has been created and one backup is available. |
|
10 */ |
|
11 |
|
12 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
13 |
|
14 XPCOMUtils.defineLazyServiceGetter(this, "bs", |
|
15 "@mozilla.org/browser/nav-bookmarks-service;1", |
|
16 "nsINavBookmarksService"); |
|
17 XPCOMUtils.defineLazyServiceGetter(this, "anno", |
|
18 "@mozilla.org/browser/annotation-service;1", |
|
19 "nsIAnnotationService"); |
|
20 |
|
21 let bookmarksObserver = { |
|
22 onBeginUpdateBatch: function() {}, |
|
23 onEndUpdateBatch: function() { |
|
24 let itemId = bs.getIdForItemAt(bs.toolbarFolder, 0); |
|
25 do_check_neq(itemId, -1); |
|
26 if (anno.itemHasAnnotation(itemId, "Places/SmartBookmark")) |
|
27 continue_test(); |
|
28 }, |
|
29 onItemAdded: function() {}, |
|
30 onItemRemoved: function(id, folder, index, itemType) {}, |
|
31 onItemChanged: function() {}, |
|
32 onItemVisited: function(id, visitID, time) {}, |
|
33 onItemMoved: function() {}, |
|
34 QueryInterface: XPCOMUtils.generateQI([Ci.nsINavBookmarkObserver]) |
|
35 }; |
|
36 |
|
37 function run_test() { |
|
38 do_test_pending(); |
|
39 |
|
40 // Create our bookmarks.html copying bookmarks.glue.html to the profile |
|
41 // folder. It will be ignored. |
|
42 create_bookmarks_html("bookmarks.glue.html"); |
|
43 |
|
44 // Create our JSON backup copying bookmarks.glue.json to the profile |
|
45 // folder. It will be ignored. |
|
46 remove_all_JSON_backups(); |
|
47 create_JSON_backup("bookmarks.glue.json"); |
|
48 |
|
49 // Remove current database file. |
|
50 let db = gProfD.clone(); |
|
51 db.append("places.sqlite"); |
|
52 if (db.exists()) { |
|
53 db.remove(false); |
|
54 do_check_false(db.exists()); |
|
55 } |
|
56 |
|
57 // Initialize nsBrowserGlue before Places. |
|
58 Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIBrowserGlue); |
|
59 |
|
60 // Initialize Places through the History Service. |
|
61 let hs = Cc["@mozilla.org/browser/nav-history-service;1"]. |
|
62 getService(Ci.nsINavHistoryService); |
|
63 // Check a new database has been created. |
|
64 // nsBrowserGlue uses databaseStatus to manage initialization. |
|
65 do_check_eq(hs.databaseStatus, hs.DATABASE_STATUS_CREATE); |
|
66 |
|
67 // The test will continue once restore has finished and smart bookmarks |
|
68 // have been created. |
|
69 bs.addObserver(bookmarksObserver, false); |
|
70 } |
|
71 |
|
72 function continue_test() { |
|
73 // Check that JSON backup has been restored. |
|
74 // Notice restore from JSON notification is fired before smart bookmarks creation. |
|
75 let itemId = bs.getIdForItemAt(bs.toolbarFolder, SMART_BOOKMARKS_ON_TOOLBAR); |
|
76 do_check_eq(bs.getItemTitle(itemId), "examplejson"); |
|
77 |
|
78 remove_bookmarks_html(); |
|
79 remove_all_JSON_backups(); |
|
80 |
|
81 do_test_finished(); |
|
82 } |