test_bookmarks_html.js
Go to the documentation of this file.
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 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is mozilla.com code.
17  *
18  * The Initial Developer of the Original Code is Mozilla Corp.
19  * Portions created by the Initial Developer are Copyright (C) 2007
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Dietrich Ayala <dietrich@mozilla.com>
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
39 // get history service
40 try {
41  var histsvc = Cc["@mozilla.org/browser/nav-history-service;1"].getService(Ci.nsINavHistoryService);
42 } catch(ex) {
43  do_throw("Could not get nav-history-service\n");
44 }
45 
46 // Get bookmark service
47 try {
48  var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].getService(Ci.nsINavBookmarksService);
49 } catch(ex) {
50  do_throw("Could not get nav-bookmarks-service\n");
51 }
52 
53 // Get annotation service
54 try {
55  var annosvc = Cc["@mozilla.org/browser/annotation-service;1"].getService(Ci.nsIAnnotationService);
56 } catch(ex) {
57  do_throw("Could not get annotation service\n");
58 }
59 
60 // Get livemark service
61 try {
62  var livemarksvc = Cc["@mozilla.org/browser/livemark-service;2"].getService(Ci.nsILivemarkService);
63 } catch(ex) {
64  do_throw("Could not get livemark service\n");
65 }
66 
67 // Get favicon service
68 try {
69  var iconsvc = Cc["@mozilla.org/browser/favicon-service;1"].getService(Ci.nsIFaviconService);
70 } catch(ex) {
71  do_throw("Could not get favicon service\n");
72 }
73 
74 // Get microsummary service
75 try {
76  var mssvc = Cc["@mozilla.org/microsummary/service;1"].getService(Ci.nsIMicrosummaryService);
77 } catch(ex) {
78  do_throw("Could not get microsummary service\n");
79 }
80 
81 // Get io service
82 try {
83  var iosvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
84 } catch (ex) {
85  do_throw("Could not get io service\n");
86 }
87 
88 const DESCRIPTION_ANNO = "bookmarkProperties/description";
89 const LOAD_IN_SIDEBAR_ANNO = "bookmarkProperties/loadInSidebar";
90 const POST_DATA_ANNO = "bookmarkProperties/POSTData";
91 
92 const TEST_FAVICON_PAGE_URL = "http://en-US.www.mozilla.com/en-US/firefox/central/";
93 const TEST_FAVICON_DATA_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dPFpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAEEAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIGTDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8vZswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJFPcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwTAR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg==";
94 
95 // main
96 function run_test() {
97  // get places import/export service
98  var importer = Cc["@mozilla.org/browser/places/import-export-service;1"].getService(Ci.nsIPlacesImportExportService);
99 
100  // avoid creating the places smart folder during tests
101  Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch).
102  setIntPref("browser.places.smartBookmarksVersion", -1);
103 
104  // file pointer to legacy bookmarks file
105  var bookmarksFileOld = do_get_file("bookmarks.preplaces.html");
106  // file pointer to a new places-exported bookmarks file
107  var bookmarksFileNew = dirSvc.get("ProfD", Ci.nsILocalFile);
108  bookmarksFileNew.append("bookmarks.exported.html");
109 
110  // create bookmarks.exported.html
111  if (bookmarksFileNew.exists())
112  bookmarksFileNew.remove(false);
113  bookmarksFileNew.create(Ci.nsILocalFile.NORMAL_FILE_TYPE, 0600);
114  if (!bookmarksFileNew.exists())
115  do_throw("couldn't create file: bookmarks.exported.html");
116 
117  // Test importing a pre-Places canonical bookmarks file.
118  // 1. import bookmarks.preplaces.html
119  // 2. run the test-suite
120  // Note: we do not empty the db before this import to catch bugs like 380999
121  try {
122  importer.importHTMLFromFile(bookmarksFileOld, true);
123  } catch(ex) { do_throw("couldn't import legacy bookmarks file: " + ex); }
124  testCanonicalBookmarks(bmsvc.bookmarksMenuFolder);
125 
126  // Test exporting a Places canonical bookmarks file.
127  // 1. export to bookmarks.exported.html
128  // 2. empty bookmarks db
129  // 3. import bookmarks.exported.html
130  // 4. run the test-suite
131  try {
132  importer.exportHTMLToFile(bookmarksFileNew);
133  } catch(ex) { do_throw("couldn't export to file: " + ex); }
134  bmsvc.removeFolderChildren(bmsvc.bookmarksMenuFolder);
135  bmsvc.removeFolderChildren(bmsvc.toolbarFolder);
136  try {
137  importer.importHTMLFromFile(bookmarksFileNew, true);
138  } catch(ex) { do_throw("couldn't import the exported file: " + ex); }
139  testCanonicalBookmarks(bmsvc.bookmarksMenuFolder);
140 
141  /*
142  // XXX import-to-folder tests disabled due to bug 363634
143  // Test importing a pre-Places canonical bookmarks file to a specific folder.
144  // 1. create a new folder
145  // 2. import bookmarks.preplaces.html to that folder
146  // 3. run the test-suite
147  var testFolder = bmsvc.createFolder(bmsvc.bookmarksMenuFolder, "test-import", bmsvc.DEFAULT_INDEX);
148  try {
149  importer.importHTMLFromFileToFolder(bookmarksFileOld, testFolder, false);
150  } catch(ex) { do_throw("couldn't import the exported file to folder: " + ex); }
151  testCanonicalBookmarks(testFolder);
152  bmsvc.removeFolder(testFolder);
153 
154  // Test importing a Places canonical bookmarks file to a specific folder.
155  // 1. create a new folder
156  // 2. import bookmarks.exported.html to that folder
157  // 3. run the test-suite
158  var testFolder = bmsvc.createFolder(bmsvc.bookmarksMenuFolder, "test-import", bmsvc.DEFAULT_INDEX);
159  try {
160  importer.importHTMLFromFileToFolder(bookmarksFileNew, testFolder, false);
161  } catch(ex) { do_throw("couldn't import the exported file to folder: " + ex); }
162  testCanonicalBookmarks(testFolder);
163  bmsvc.removeFolder(testFolder);
164 
165  // XXX Disabled due to bug 381129 - separators will be duplicated on re-import
166  // Test importing the exported bookmarks.html file *on top of* the existing
167  // bookmarks. This tests import of IDs. If we support IDs correctly, there
168  // should be no difference after the import.
169  // 1. empty bookmarks db
170  // 2. import the exported bookmarks file
171  // 3. export to file
172  // 3. import the exported bookmarks file
173  // 4. run the test-suite
174  bmsvc.removeFolderChildren(bmsvc.bookmarksMenuFolder);
175  try {
176  importer.importHTMLFromFile(bookmarksFileNew, true);
177  } catch(ex) { do_throw("couldn't import the exported file: " + ex); }
178  try {
179  importer.exportHTMLToFile(bookmarksFileNew);
180  } catch(ex) { do_throw("couldn't export to file: " + ex); }
181  try {
182  importer.importHTMLFromFile(bookmarksFileNew, true);
183  } catch(ex) { do_throw("couldn't import the exported file: " + ex); }
184  testCanonicalBookmarks(bmsvc.bookmarksMenuFolder);
185  */
186  /*
187  XXX if there are new fields we add to the bookmarks HTML format
188  then test them here
189  Test importing a Places canonical bookmarks file
190  1. empty the bookmarks datastore
191  2. import bookmarks.places.html
192  3. run the test-suite
193  4. run the additional-test-suite
194  */
195 }
196 
197 // Tests a bookmarks datastore that has a set of bookmarks, etc
198 // that flex each supported field and feature.
199 function testCanonicalBookmarks(aFolder) {
200  // query to see if the deleted folder and items have been imported
201  var query = histsvc.getNewQuery();
202  query.setFolders([aFolder], 1);
203  var result = histsvc.executeQuery(query, histsvc.getNewQueryOptions());
204  var rootNode = result.root;
205  rootNode.containerOpen = true;
206 
207  // 6-2: the toolbar folder and unfiled bookmarks folder imported to the
208  // corresponding places folders
209  do_check_eq(rootNode.childCount, 4);
210 
211  // get test folder
212  var testFolder = rootNode.getChild(3);
213  do_check_eq(testFolder.type, testFolder.RESULT_TYPE_FOLDER);
214  do_check_eq(testFolder.title, "test");
215 
216  // add date
217  do_check_eq(bmsvc.getItemDateAdded(testFolder.itemId)/1000000, 1177541020);
218  // last modified
219  do_check_eq(bmsvc.getItemLastModified(testFolder.itemId)/1000000, 1177541050);
220 
221  testFolder = testFolder.QueryInterface(Ci.nsINavHistoryQueryResultNode);
222  do_check_eq(testFolder.hasChildren, true);
223  // folder description
224  do_check_true(annosvc.itemHasAnnotation(testFolder.itemId,
226  do_check_eq("folder test comment",
227  annosvc.getItemAnnotation(testFolder.itemId, DESCRIPTION_ANNO));
228  // open test folder, and test the children
229  testFolder.containerOpen = true;
230  var cc = testFolder.childCount;
231  // XXX Bug 380468
232  // do_check_eq(cc, 2);
233  do_check_eq(cc, 1);
234 
235  // test bookmark 1
236  var testBookmark1 = testFolder.getChild(0);
237  // url
238  do_check_eq("http://test/post", testBookmark1.uri);
239  // title
240  do_check_eq("test post keyword", testBookmark1.title);
241  // keyword
242  do_check_eq("test", bmsvc.getKeywordForBookmark(testBookmark1.itemId));
243  // sidebar
244  do_check_true(annosvc.itemHasAnnotation(testBookmark1.itemId,
246  // add date
247  do_check_eq(testBookmark1.dateAdded/1000000, 1177375336);
248 
249  // last modified
250  do_check_eq(testBookmark1.lastModified/1000000, 1177375423);
251 
252  // post data
253  do_check_true(annosvc.itemHasAnnotation(testBookmark1.itemId,
254  POST_DATA_ANNO));
255  do_check_eq("hidden1%3Dbar&text1%3D%25s",
256  annosvc.getItemAnnotation(testBookmark1.itemId, POST_DATA_ANNO));
257 
258  // last charset
259  var testURI = uri(testBookmark1.uri);
260  do_check_eq("ISO-8859-1", histsvc.getCharsetForURI(testURI));
261 
262  // description
263  do_check_true(annosvc.itemHasAnnotation(testBookmark1.itemId,
265  do_check_eq("item description",
266  annosvc.getItemAnnotation(testBookmark1.itemId,
268 
269  /*
270  // XXX Bug 380468
271  // test bookmark 2
272  var testBookmark2 = testFolder.getChild(1);
273  // url
274  do_check_eq("http://test/micsum", testBookmark2.uri);
275  // title
276  do_check_eq("test microsummary", testBookmark2.title);
277  // check that it's a microsummary
278  var micsum = mssvc.getMicrosummary(testBookmark2.itemId);
279  if (!micsum)
280  do_throw("Could not import microsummary");
281  // check generator uri
282  var generator = micsum.generator;
283  do_check_eq("urn:source:http://dietrich.ganx4.com/mozilla/test-microsummary.xml", generator.uri.spec);
284  // expiration and generated title can change, so don't test them
285  */
286  // clean up
287  testFolder.containerOpen = false;
288  rootNode.containerOpen = false;
289 
290  query.setFolders([bmsvc.toolbarFolder], 1);
291  result = histsvc.executeQuery(query, histsvc.getNewQueryOptions());
292  // bookmarks toolbar
293  var toolbar = result.root;
294  toolbar.containerOpen = true;
295  do_check_eq(toolbar.childCount, 2);
296 
297  // livemark
298  var livemark = toolbar.getChild(1);
299  // title
300  do_check_eq("Latest Headlines", livemark.title);
301  // livemark check
302  do_check_true(livemarksvc.isLivemark(livemark.itemId));
303  // site url
304  do_check_eq("http://en-us.fxfeeds.mozilla.com/en-US/firefox/livebookmarks/",
305  livemarksvc.getSiteURI(livemark.itemId).spec);
306  // feed url
307  do_check_eq("http://en-us.fxfeeds.mozilla.com/en-US/firefox/headlines.xml",
308  livemarksvc.getFeedURI(livemark.itemId).spec);
309 
310  toolbar.containerOpen = false;
311 
312  // unfiled bookmarks
313  query.setFolders([bmsvc.unfiledBookmarksFolder], 1);
314  result = histsvc.executeQuery(query, histsvc.getNewQueryOptions());
315  var unfiledBookmarks = result.root;
316  unfiledBookmarks.containerOpen = true;
317  do_check_eq(unfiledBookmarks.childCount, 1);
318  unfiledBookmarks.containerOpen = false;
319 
320  // favicons
321  var faviconURI = iconsvc.getFaviconForPage(uri(TEST_FAVICON_PAGE_URL));
322  var dataURL = iconsvc.getFaviconDataAsDataURL(faviconURI);
324 }
const Cc
var faviconURI
Definition: FeedWriter.js:1371
do_check_eq(typeof PlacesUtils,"object")
var dirSvc
const TEST_FAVICON_PAGE_URL
var testFolder
const histsvc
const TEST_FAVICON_DATA_URL
const LOAD_IN_SIDEBAR_ANNO
function testCanonicalBookmarks(aFolder)
const bmsvc
function run_test()
var uri
Definition: FeedWriter.js:1135
const DESCRIPTION_ANNO
const Ci
var iosvc
const POST_DATA_ANNO