|
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 LOAD_IN_SIDEBAR_ANNO = "bookmarkProperties/loadInSidebar"; |
|
8 const DESCRIPTION_ANNO = "bookmarkProperties/description"; |
|
9 const POST_DATA_ANNO = "bookmarkProperties/POSTData"; |
|
10 |
|
11 do_check_eq(typeof PlacesUtils, "object"); |
|
12 |
|
13 // main |
|
14 function run_test() { |
|
15 do_test_pending(); |
|
16 |
|
17 /* |
|
18 HTML+FEATURES SUMMARY: |
|
19 - import legacy bookmarks |
|
20 - export as json, import, test (tests integrity of html > json) |
|
21 - export as html, import, test (tests integrity of json > html) |
|
22 |
|
23 BACKUP/RESTORE SUMMARY: |
|
24 - create a bookmark in each root |
|
25 - tag multiple URIs with multiple tags |
|
26 - export as json, import, test |
|
27 */ |
|
28 |
|
29 // import the importer |
|
30 Cu.import("resource://gre/modules/BookmarkHTMLUtils.jsm"); |
|
31 |
|
32 // file pointer to legacy bookmarks file |
|
33 var bookmarksFileOld = OS.Path.join(do_get_cwd().path, "bookmarks.preplaces.html"); |
|
34 // file pointer to a new places-exported json file |
|
35 var jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.exported.json"); |
|
36 Task.spawn(function () { |
|
37 // create bookmarks.exported.json |
|
38 if ((yield OS.File.exists(jsonFile))) |
|
39 yield OS.File.remove(jsonFile); |
|
40 |
|
41 // Test importing a pre-Places canonical bookmarks file. |
|
42 // 1. import bookmarks.preplaces.html |
|
43 // Note: we do not empty the db before this import to catch bugs like 380999 |
|
44 try { |
|
45 BookmarkHTMLUtils.importFromFile(bookmarksFileOld, true) |
|
46 .then(after_import, do_report_unexpected_exception); |
|
47 } catch(ex) { do_throw("couldn't import legacy bookmarks file: " + ex); } |
|
48 }); |
|
49 |
|
50 function after_import() { |
|
51 populate(); |
|
52 |
|
53 // 2. run the test-suite |
|
54 Task.spawn(function() { |
|
55 yield validate(); |
|
56 yield promiseAsyncUpdates(); |
|
57 |
|
58 // Test exporting a Places canonical json file. |
|
59 // 1. export to bookmarks.exported.json |
|
60 try { |
|
61 yield BookmarkJSONUtils.exportToFile(jsonFile); |
|
62 } catch(ex) { do_throw("couldn't export to file: " + ex); } |
|
63 LOG("exported json"); |
|
64 |
|
65 // 2. empty bookmarks db |
|
66 // 3. import bookmarks.exported.json |
|
67 try { |
|
68 yield BookmarkJSONUtils.importFromFile(jsonFile, true); |
|
69 } catch(ex) { do_throw("couldn't import the exported file: " + ex); } |
|
70 LOG("imported json"); |
|
71 |
|
72 // 4. run the test-suite |
|
73 yield validate(); |
|
74 LOG("validated import"); |
|
75 |
|
76 yield promiseAsyncUpdates(); |
|
77 do_test_finished(); |
|
78 }); |
|
79 } |
|
80 } |
|
81 |
|
82 var tagData = [ |
|
83 { uri: uri("http://slint.us"), tags: ["indie", "kentucky", "music"] }, |
|
84 { uri: uri("http://en.wikipedia.org/wiki/Diplodocus"), tags: ["dinosaur", "dj", "rad word"] } |
|
85 ]; |
|
86 |
|
87 var bookmarkData = [ |
|
88 { uri: uri("http://slint.us"), title: "indie, kentucky, music" }, |
|
89 { uri: uri("http://en.wikipedia.org/wiki/Diplodocus"), title: "dinosaur, dj, rad word" } |
|
90 ]; |
|
91 |
|
92 /* |
|
93 populate data in each folder |
|
94 (menu is populated via the html import) |
|
95 */ |
|
96 function populate() { |
|
97 // add tags |
|
98 for each(let {uri: u, tags: t} in tagData) |
|
99 PlacesUtils.tagging.tagURI(u, t); |
|
100 |
|
101 // add unfiled bookmarks |
|
102 for each(let {uri: u, title: t} in bookmarkData) { |
|
103 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarks.unfiledBookmarksFolder, |
|
104 u, PlacesUtils.bookmarks.DEFAULT_INDEX, t); |
|
105 } |
|
106 |
|
107 // add to the toolbar |
|
108 for each(let {uri: u, title: t} in bookmarkData) { |
|
109 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarks.toolbarFolder, |
|
110 u, PlacesUtils.bookmarks.DEFAULT_INDEX, t); |
|
111 } |
|
112 } |
|
113 |
|
114 function validate() { |
|
115 yield testCanonicalBookmarks(); |
|
116 yield testToolbarFolder(); |
|
117 testUnfiledBookmarks(); |
|
118 testTags(); |
|
119 } |
|
120 |
|
121 // Tests a bookmarks datastore that has a set of bookmarks, etc |
|
122 // that flex each supported field and feature. |
|
123 function testCanonicalBookmarks() { |
|
124 // query to see if the deleted folder and items have been imported |
|
125 var query = PlacesUtils.history.getNewQuery(); |
|
126 query.setFolders([PlacesUtils.bookmarksMenuFolderId], 1); |
|
127 var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions()); |
|
128 var rootNode = result.root; |
|
129 rootNode.containerOpen = true; |
|
130 |
|
131 // Count expected bookmarks in the menu root. |
|
132 do_check_eq(rootNode.childCount, 3); |
|
133 |
|
134 // check separator |
|
135 var testSeparator = rootNode.getChild(1); |
|
136 do_check_eq(testSeparator.type, testSeparator.RESULT_TYPE_SEPARATOR); |
|
137 |
|
138 // get test folder |
|
139 var testFolder = rootNode.getChild(2); |
|
140 do_check_eq(testFolder.type, testFolder.RESULT_TYPE_FOLDER); |
|
141 do_check_eq(testFolder.title, "test"); |
|
142 |
|
143 /* |
|
144 // add date |
|
145 do_check_eq(PlacesUtils.bookmarks.getItemDateAdded(testFolder.itemId)/1000000, 1177541020); |
|
146 // last modified |
|
147 do_check_eq(PlacesUtils.bookmarks.getItemLastModified(testFolder.itemId)/1000000, 1177541050); |
|
148 */ |
|
149 |
|
150 testFolder = testFolder.QueryInterface(Ci.nsINavHistoryQueryResultNode); |
|
151 do_check_eq(testFolder.hasChildren, true); |
|
152 // folder description |
|
153 do_check_true(PlacesUtils.annotations.itemHasAnnotation(testFolder.itemId, |
|
154 DESCRIPTION_ANNO)); |
|
155 do_check_eq("folder test comment", |
|
156 PlacesUtils.annotations.getItemAnnotation(testFolder.itemId, DESCRIPTION_ANNO)); |
|
157 // open test folder, and test the children |
|
158 testFolder.containerOpen = true; |
|
159 var cc = testFolder.childCount; |
|
160 // XXX Bug 380468 |
|
161 // do_check_eq(cc, 2); |
|
162 do_check_eq(cc, 1); |
|
163 |
|
164 // test bookmark 1 |
|
165 var testBookmark1 = testFolder.getChild(0); |
|
166 // url |
|
167 do_check_eq("http://test/post", testBookmark1.uri); |
|
168 // title |
|
169 do_check_eq("test post keyword", testBookmark1.title); |
|
170 // keyword |
|
171 do_check_eq("test", PlacesUtils.bookmarks.getKeywordForBookmark(testBookmark1.itemId)); |
|
172 // sidebar |
|
173 do_check_true(PlacesUtils.annotations.itemHasAnnotation(testBookmark1.itemId, |
|
174 LOAD_IN_SIDEBAR_ANNO)); |
|
175 /* |
|
176 // add date |
|
177 do_check_eq(testBookmark1.dateAdded/1000000, 1177375336); |
|
178 |
|
179 // last modified |
|
180 do_check_eq(testBookmark1.lastModified/1000000, 1177375423); |
|
181 */ |
|
182 |
|
183 // post data |
|
184 do_check_true(PlacesUtils.annotations.itemHasAnnotation(testBookmark1.itemId, POST_DATA_ANNO)); |
|
185 do_check_eq("hidden1%3Dbar&text1%3D%25s", |
|
186 PlacesUtils.annotations.getItemAnnotation(testBookmark1.itemId, POST_DATA_ANNO)); |
|
187 |
|
188 // last charset |
|
189 var testURI = PlacesUtils._uri(testBookmark1.uri); |
|
190 do_check_eq("ISO-8859-1", (yield PlacesUtils.getCharsetForURI(testURI))); |
|
191 |
|
192 // description |
|
193 do_check_true(PlacesUtils.annotations.itemHasAnnotation(testBookmark1.itemId, |
|
194 DESCRIPTION_ANNO)); |
|
195 do_check_eq("item description", |
|
196 PlacesUtils.annotations.getItemAnnotation(testBookmark1.itemId, |
|
197 DESCRIPTION_ANNO)); |
|
198 |
|
199 // clean up |
|
200 testFolder.containerOpen = false; |
|
201 rootNode.containerOpen = false; |
|
202 } |
|
203 |
|
204 function testToolbarFolder() { |
|
205 var query = PlacesUtils.history.getNewQuery(); |
|
206 query.setFolders([PlacesUtils.toolbarFolderId], 1); |
|
207 var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions()); |
|
208 |
|
209 var toolbar = result.root; |
|
210 toolbar.containerOpen = true; |
|
211 |
|
212 // child count (add 2 for pre-existing items) |
|
213 do_check_eq(toolbar.childCount, bookmarkData.length + 2); |
|
214 |
|
215 // livemark |
|
216 var livemark = toolbar.getChild(1); |
|
217 // title |
|
218 do_check_eq("Latest Headlines", livemark.title); |
|
219 |
|
220 let foundLivemark = yield PlacesUtils.livemarks.getLivemark({ id: livemark.itemId }); |
|
221 do_check_eq("http://en-us.fxfeeds.mozilla.com/en-US/firefox/livebookmarks/", |
|
222 foundLivemark.siteURI.spec); |
|
223 do_check_eq("http://en-us.fxfeeds.mozilla.com/en-US/firefox/headlines.xml", |
|
224 foundLivemark.feedURI.spec); |
|
225 |
|
226 // test added bookmark data |
|
227 var child = toolbar.getChild(2); |
|
228 do_check_eq(child.uri, bookmarkData[0].uri.spec); |
|
229 do_check_eq(child.title, bookmarkData[0].title); |
|
230 child = toolbar.getChild(3); |
|
231 do_check_eq(child.uri, bookmarkData[1].uri.spec); |
|
232 do_check_eq(child.title, bookmarkData[1].title); |
|
233 |
|
234 toolbar.containerOpen = false; |
|
235 } |
|
236 |
|
237 function testUnfiledBookmarks() { |
|
238 var query = PlacesUtils.history.getNewQuery(); |
|
239 query.setFolders([PlacesUtils.unfiledBookmarksFolderId], 1); |
|
240 var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions()); |
|
241 var rootNode = result.root; |
|
242 rootNode.containerOpen = true; |
|
243 // child count (add 1 for pre-existing item) |
|
244 do_check_eq(rootNode.childCount, bookmarkData.length + 1); |
|
245 for (var i = 1; i < rootNode.childCount; i++) { |
|
246 var child = rootNode.getChild(i); |
|
247 dump(bookmarkData[i - 1].uri.spec + " == " + child.uri + "?\n"); |
|
248 do_check_true(bookmarkData[i - 1].uri.equals(uri(child.uri))); |
|
249 do_check_eq(child.title, bookmarkData[i - 1].title); |
|
250 /* WTF |
|
251 if (child.tags) |
|
252 do_check_eq(child.tags, bookmarkData[i].title); |
|
253 */ |
|
254 } |
|
255 rootNode.containerOpen = false; |
|
256 } |
|
257 |
|
258 function testTags() { |
|
259 for each(let {uri: u, tags: t} in tagData) { |
|
260 var i = 0; |
|
261 dump("test tags for " + u.spec + ": " + t + "\n"); |
|
262 var tt = PlacesUtils.tagging.getTagsForURI(u); |
|
263 dump("true tags for " + u.spec + ": " + tt + "\n"); |
|
264 do_check_true(t.every(function(el) { |
|
265 i++; |
|
266 return tt.indexOf(el) > -1; |
|
267 })); |
|
268 do_check_eq(i, t.length); |
|
269 } |
|
270 } |