|
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 var tests = []; |
|
8 |
|
9 /* |
|
10 |
|
11 Backup/restore tests example: |
|
12 |
|
13 var myTest = { |
|
14 populate: function () { ... add bookmarks ... }, |
|
15 validate: function () { ... query for your bookmarks ... } |
|
16 } |
|
17 |
|
18 this.push(myTest); |
|
19 |
|
20 */ |
|
21 |
|
22 /* |
|
23 |
|
24 test summary: |
|
25 - create folders with content |
|
26 - create a query bookmark for those folders |
|
27 - backs up bookmarks |
|
28 - restores bookmarks |
|
29 - confirms that the query has the new ids for the same folders |
|
30 |
|
31 scenarios: |
|
32 - 1 folder (folder shortcut) |
|
33 - n folders (single query) |
|
34 - n folders (multiple queries) |
|
35 |
|
36 */ |
|
37 |
|
38 const DEFAULT_INDEX = PlacesUtils.bookmarks.DEFAULT_INDEX; |
|
39 |
|
40 var test = { |
|
41 _testRootId: null, |
|
42 _testRootTitle: "test root", |
|
43 _folderIds: [], |
|
44 _bookmarkURIs: [], |
|
45 _count: 3, |
|
46 |
|
47 populate: function populate() { |
|
48 // folder to hold this test |
|
49 PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.toolbarFolderId); |
|
50 this._testRootId = |
|
51 PlacesUtils.bookmarks.createFolder(PlacesUtils.toolbarFolderId, |
|
52 this._testRootTitle, DEFAULT_INDEX); |
|
53 |
|
54 // create test folders each with a bookmark |
|
55 for (var i = 0; i < this._count; i++) { |
|
56 var folderId = |
|
57 PlacesUtils.bookmarks.createFolder(this._testRootId, "folder" + i, DEFAULT_INDEX); |
|
58 this._folderIds.push(folderId) |
|
59 |
|
60 var bookmarkURI = uri("http://" + i); |
|
61 PlacesUtils.bookmarks.insertBookmark(folderId, bookmarkURI, |
|
62 DEFAULT_INDEX, "bookmark" + i); |
|
63 this._bookmarkURIs.push(bookmarkURI); |
|
64 } |
|
65 |
|
66 // create a query URI with 1 folder (ie: folder shortcut) |
|
67 this._queryURI1 = uri("place:folder=" + this._folderIds[0] + "&queryType=1"); |
|
68 this._queryTitle1 = "query1"; |
|
69 PlacesUtils.bookmarks.insertBookmark(this._testRootId, this._queryURI1, |
|
70 DEFAULT_INDEX, this._queryTitle1); |
|
71 |
|
72 // create a query URI with _count folders |
|
73 this._queryURI2 = uri("place:folder=" + this._folderIds.join("&folder=") + "&queryType=1"); |
|
74 this._queryTitle2 = "query2"; |
|
75 PlacesUtils.bookmarks.insertBookmark(this._testRootId, this._queryURI2, |
|
76 DEFAULT_INDEX, this._queryTitle2); |
|
77 |
|
78 // create a query URI with _count queries (each with a folder) |
|
79 // first get a query object for each folder |
|
80 var queries = this._folderIds.map(function(aFolderId) { |
|
81 var query = PlacesUtils.history.getNewQuery(); |
|
82 query.setFolders([aFolderId], 1); |
|
83 return query; |
|
84 }); |
|
85 var options = PlacesUtils.history.getNewQueryOptions(); |
|
86 options.queryType = options.QUERY_TYPE_BOOKMARKS; |
|
87 this._queryURI3 = |
|
88 uri(PlacesUtils.history.queriesToQueryString(queries, queries.length, options)); |
|
89 this._queryTitle3 = "query3"; |
|
90 PlacesUtils.bookmarks.insertBookmark(this._testRootId, this._queryURI3, |
|
91 DEFAULT_INDEX, this._queryTitle3); |
|
92 }, |
|
93 |
|
94 clean: function () {}, |
|
95 |
|
96 validate: function validate() { |
|
97 // Throw a wrench in the works by inserting some new bookmarks, |
|
98 // ensuring folder ids won't be the same, when restoring. |
|
99 for (var i = 0; i < 10; i++) { |
|
100 PlacesUtils.bookmarks. |
|
101 insertBookmark(PlacesUtils.bookmarksMenuFolderId, uri("http://aaaa"+i), DEFAULT_INDEX, ""); |
|
102 } |
|
103 |
|
104 var toolbar = |
|
105 PlacesUtils.getFolderContents(PlacesUtils.toolbarFolderId, |
|
106 false, true).root; |
|
107 do_check_true(toolbar.childCount, 1); |
|
108 |
|
109 var folderNode = toolbar.getChild(0); |
|
110 do_check_eq(folderNode.type, folderNode.RESULT_TYPE_FOLDER); |
|
111 do_check_eq(folderNode.title, this._testRootTitle); |
|
112 folderNode.QueryInterface(Ci.nsINavHistoryQueryResultNode); |
|
113 folderNode.containerOpen = true; |
|
114 |
|
115 // |_count| folders + the query node |
|
116 do_check_eq(folderNode.childCount, this._count+3); |
|
117 |
|
118 for (var i = 0; i < this._count; i++) { |
|
119 var subFolder = folderNode.getChild(i); |
|
120 do_check_eq(subFolder.title, "folder"+i); |
|
121 subFolder.QueryInterface(Ci.nsINavHistoryContainerResultNode); |
|
122 subFolder.containerOpen = true; |
|
123 do_check_eq(subFolder.childCount, 1); |
|
124 var child = subFolder.getChild(0); |
|
125 do_check_eq(child.title, "bookmark"+i); |
|
126 do_check_true(uri(child.uri).equals(uri("http://" + i))) |
|
127 } |
|
128 |
|
129 // validate folder shortcut |
|
130 this.validateQueryNode1(folderNode.getChild(this._count)); |
|
131 |
|
132 // validate folders query |
|
133 this.validateQueryNode2(folderNode.getChild(this._count + 1)); |
|
134 |
|
135 // validate multiple queries query |
|
136 this.validateQueryNode3(folderNode.getChild(this._count + 2)); |
|
137 |
|
138 // clean up |
|
139 folderNode.containerOpen = false; |
|
140 toolbar.containerOpen = false; |
|
141 }, |
|
142 |
|
143 validateQueryNode1: function validateQueryNode1(aNode) { |
|
144 do_check_eq(aNode.title, this._queryTitle1); |
|
145 do_check_true(PlacesUtils.nodeIsFolder(aNode)); |
|
146 |
|
147 aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); |
|
148 aNode.containerOpen = true; |
|
149 do_check_eq(aNode.childCount, 1); |
|
150 var child = aNode.getChild(0); |
|
151 do_check_true(uri(child.uri).equals(uri("http://0"))) |
|
152 do_check_eq(child.title, "bookmark0") |
|
153 aNode.containerOpen = false; |
|
154 }, |
|
155 |
|
156 validateQueryNode2: function validateQueryNode2(aNode) { |
|
157 do_check_eq(aNode.title, this._queryTitle2); |
|
158 do_check_true(PlacesUtils.nodeIsQuery(aNode)); |
|
159 |
|
160 aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); |
|
161 aNode.containerOpen = true; |
|
162 do_check_eq(aNode.childCount, this._count); |
|
163 for (var i = 0; i < aNode.childCount; i++) { |
|
164 var child = aNode.getChild(i); |
|
165 do_check_true(uri(child.uri).equals(uri("http://" + i))) |
|
166 do_check_eq(child.title, "bookmark" + i) |
|
167 } |
|
168 aNode.containerOpen = false; |
|
169 }, |
|
170 |
|
171 validateQueryNode3: function validateQueryNode3(aNode) { |
|
172 do_check_eq(aNode.title, this._queryTitle3); |
|
173 do_check_true(PlacesUtils.nodeIsQuery(aNode)); |
|
174 |
|
175 aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); |
|
176 aNode.containerOpen = true; |
|
177 do_check_eq(aNode.childCount, this._count); |
|
178 for (var i = 0; i < aNode.childCount; i++) { |
|
179 var child = aNode.getChild(i); |
|
180 do_check_true(uri(child.uri).equals(uri("http://" + i))) |
|
181 do_check_eq(child.title, "bookmark" + i) |
|
182 } |
|
183 aNode.containerOpen = false; |
|
184 } |
|
185 } |
|
186 tests.push(test); |
|
187 |
|
188 function run_test() { |
|
189 run_next_test(); |
|
190 } |
|
191 |
|
192 add_task(function () { |
|
193 // make json file |
|
194 let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json"); |
|
195 |
|
196 // populate db |
|
197 tests.forEach(function(aTest) { |
|
198 aTest.populate(); |
|
199 // sanity |
|
200 aTest.validate(); |
|
201 }); |
|
202 |
|
203 // export json to file |
|
204 yield BookmarkJSONUtils.exportToFile(jsonFile); |
|
205 |
|
206 // clean |
|
207 tests.forEach(function(aTest) { |
|
208 aTest.clean(); |
|
209 }); |
|
210 |
|
211 // restore json file |
|
212 yield BookmarkJSONUtils.importFromFile(jsonFile, true); |
|
213 |
|
214 // validate |
|
215 tests.forEach(function(aTest) { |
|
216 aTest.validate(); |
|
217 }); |
|
218 |
|
219 // clean up |
|
220 yield OS.File.remove(jsonFile); |
|
221 }); |