|
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 const EXCLUDE_FROM_BACKUP_ANNO = "places/excludeFromBackup"; |
|
8 // Menu, Toolbar, Unsorted, Tags |
|
9 const PLACES_ROOTS_COUNT = 4; |
|
10 var tests = []; |
|
11 |
|
12 /* |
|
13 |
|
14 Backup/restore tests example: |
|
15 |
|
16 var myTest = { |
|
17 populate: function () { ... add bookmarks ... }, |
|
18 validate: function () { ... query for your bookmarks ... } |
|
19 } |
|
20 |
|
21 this.push(myTest); |
|
22 |
|
23 */ |
|
24 |
|
25 var test = { |
|
26 populate: function populate() { |
|
27 // check initial size |
|
28 var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId, |
|
29 false, false).root; |
|
30 do_check_eq(rootNode.childCount, PLACES_ROOTS_COUNT ); |
|
31 rootNode.containerOpen = false; |
|
32 |
|
33 var idx = PlacesUtils.bookmarks.DEFAULT_INDEX; |
|
34 |
|
35 // create a root to be restore |
|
36 this._restoreRootTitle = "restore root"; |
|
37 var restoreRootId = PlacesUtils.bookmarks |
|
38 .createFolder(PlacesUtils.placesRootId, |
|
39 this._restoreRootTitle, idx); |
|
40 // add a test bookmark |
|
41 this._restoreRootURI = uri("http://restore.uri"); |
|
42 PlacesUtils.bookmarks.insertBookmark(restoreRootId, this._restoreRootURI, |
|
43 idx, "restore uri"); |
|
44 // add a test bookmark to be exclude |
|
45 this._restoreRootExcludeURI = uri("http://exclude.uri"); |
|
46 var exItemId = PlacesUtils.bookmarks |
|
47 .insertBookmark(restoreRootId, |
|
48 this._restoreRootExcludeURI, |
|
49 idx, "exclude uri"); |
|
50 // Annotate the bookmark for exclusion. |
|
51 PlacesUtils.annotations.setItemAnnotation(exItemId, |
|
52 EXCLUDE_FROM_BACKUP_ANNO, 1, 0, |
|
53 PlacesUtils.annotations.EXPIRE_NEVER); |
|
54 |
|
55 // create a root to be exclude |
|
56 this._excludeRootTitle = "exclude root"; |
|
57 this._excludeRootId = PlacesUtils.bookmarks |
|
58 .createFolder(PlacesUtils.placesRootId, |
|
59 this._excludeRootTitle, idx); |
|
60 // Annotate the root for exclusion. |
|
61 PlacesUtils.annotations.setItemAnnotation(this._excludeRootId, |
|
62 EXCLUDE_FROM_BACKUP_ANNO, 1, 0, |
|
63 PlacesUtils.annotations.EXPIRE_NEVER); |
|
64 // add a test bookmark exclude by exclusion of its parent |
|
65 PlacesUtils.bookmarks.insertBookmark(this._excludeRootId, |
|
66 this._restoreRootExcludeURI, |
|
67 idx, "exclude uri"); |
|
68 }, |
|
69 |
|
70 validate: function validate(aEmptyBookmarks) { |
|
71 var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId, |
|
72 false, false).root; |
|
73 |
|
74 if (!aEmptyBookmarks) { |
|
75 // since restore does not remove backup exclude items both |
|
76 // roots should still exist. |
|
77 do_check_eq(rootNode.childCount, PLACES_ROOTS_COUNT + 2); |
|
78 // open exclude root and check it still contains one item |
|
79 var restoreRootIndex = PLACES_ROOTS_COUNT; |
|
80 var excludeRootIndex = PLACES_ROOTS_COUNT+1; |
|
81 var excludeRootNode = rootNode.getChild(excludeRootIndex); |
|
82 do_check_eq(this._excludeRootTitle, excludeRootNode.title); |
|
83 excludeRootNode.QueryInterface(Ci.nsINavHistoryQueryResultNode); |
|
84 excludeRootNode.containerOpen = true; |
|
85 do_check_eq(excludeRootNode.childCount, 1); |
|
86 var excludeRootChildNode = excludeRootNode.getChild(0); |
|
87 do_check_eq(excludeRootChildNode.uri, this._restoreRootExcludeURI.spec); |
|
88 excludeRootNode.containerOpen = false; |
|
89 } |
|
90 else { |
|
91 // exclude root should not exist anymore |
|
92 do_check_eq(rootNode.childCount, PLACES_ROOTS_COUNT + 1); |
|
93 var restoreRootIndex = PLACES_ROOTS_COUNT; |
|
94 } |
|
95 |
|
96 var restoreRootNode = rootNode.getChild(restoreRootIndex); |
|
97 do_check_eq(this._restoreRootTitle, restoreRootNode.title); |
|
98 restoreRootNode.QueryInterface(Ci.nsINavHistoryQueryResultNode); |
|
99 restoreRootNode.containerOpen = true; |
|
100 do_check_eq(restoreRootNode.childCount, 1); |
|
101 var restoreRootChildNode = restoreRootNode.getChild(0); |
|
102 do_check_eq(restoreRootChildNode.uri, this._restoreRootURI.spec); |
|
103 restoreRootNode.containerOpen = false; |
|
104 |
|
105 rootNode.containerOpen = false; |
|
106 } |
|
107 } |
|
108 |
|
109 function run_test() { |
|
110 run_next_test(); |
|
111 } |
|
112 |
|
113 add_task(function() { |
|
114 // make json file |
|
115 let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json"); |
|
116 |
|
117 // populate db |
|
118 test.populate(); |
|
119 |
|
120 yield BookmarkJSONUtils.exportToFile(jsonFile); |
|
121 |
|
122 // restore json file |
|
123 yield BookmarkJSONUtils.importFromFile(jsonFile, true); |
|
124 |
|
125 // validate without removing all bookmarks |
|
126 // restore do not remove backup exclude entries |
|
127 test.validate(false); |
|
128 |
|
129 // cleanup |
|
130 remove_all_bookmarks(); |
|
131 // manually remove the excluded root |
|
132 PlacesUtils.bookmarks.removeItem(test._excludeRootId); |
|
133 // restore json file |
|
134 yield BookmarkJSONUtils.importFromFile(jsonFile, true); |
|
135 |
|
136 // validate after a complete bookmarks cleanup |
|
137 test.validate(true); |
|
138 |
|
139 // clean up |
|
140 yield OS.File.remove(jsonFile); |
|
141 }); |