test_384370.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 Bug 384370 code.
17  *
18  * The Initial Developer of the Original Code is Mozilla Corp.
19  * Portions created by the Initial Developer are Copyright (C) 2008
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 // The following components need to be initialized to perform tests without
40 // asserting in debug builds (Bug 448804).
41 Cc["@mozilla.org/browser/livemark-service;2"].getService(Ci.nsILivemarkService);
42 Cc["@mozilla.org/feed-processor;1"].createInstance(Ci.nsIFeedProcessor);
43 
44 const LOAD_IN_SIDEBAR_ANNO = "bookmarkProperties/loadInSidebar";
45 const DESCRIPTION_ANNO = "bookmarkProperties/description";
46 const POST_DATA_ANNO = "bookmarkProperties/POSTData";
47 
48 Components.utils.import("resource://gre/modules/utils.js");
49 do_check_eq(typeof PlacesUtils, "object");
50 
51 // main
52 function run_test() {
53  /*
54  HTML+FEATURES SUMMARY:
55  - import legacy bookmarks
56  - export as json, import, test (tests integrity of html > json)
57  - export as html, import, test (tests integrity of json > html)
58 
59  BACKUP/RESTORE SUMMARY:
60  - create a bookmark in each root
61  - tag multiple URIs with multiple tags
62  - export as json, import, test
63  */
64 
65  // get places import/export service
66  var importer = Cc["@mozilla.org/browser/places/import-export-service;1"].getService(Ci.nsIPlacesImportExportService);
67 
68  // avoid creating the places smart folder during tests
69  Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch).
70  setIntPref("browser.places.smartBookmarksVersion", -1);
71 
72  // file pointer to legacy bookmarks file
73  //var bookmarksFileOld = do_get_file("bookmarks.large.html");
74  var bookmarksFileOld = do_get_file("bookmarks.preplaces.html");
75  // file pointer to a new places-exported json file
76  var jsonFile = dirSvc.get("ProfD", Ci.nsILocalFile);
77  jsonFile.append("bookmarks.exported.json");
78 
79  // create bookmarks.exported.json
80  if (jsonFile.exists())
81  jsonFile.remove(false);
82  jsonFile.create(Ci.nsILocalFile.NORMAL_FILE_TYPE, 0600);
83  if (!jsonFile.exists())
84  do_throw("couldn't create file: bookmarks.exported.json");
85 
86  // Test importing a pre-Places canonical bookmarks file.
87  // 1. import bookmarks.preplaces.html
88  // 2. run the test-suite
89  // Note: we do not empty the db before this import to catch bugs like 380999
90  try {
91  importer.importHTMLFromFile(bookmarksFileOld, true);
92  } catch(ex) { do_throw("couldn't import legacy bookmarks file: " + ex); }
93  populate();
94  validate();
95 
96  // Test exporting a Places canonical json file.
97  // 1. export to bookmarks.exported.json
98  // 2. empty bookmarks db
99  // 3. import bookmarks.exported.json
100  // 4. run the test-suite
101  try {
102  PlacesUtils.backupBookmarksToFile(jsonFile);
103  } catch(ex) { do_throw("couldn't export to file: " + ex); }
104  LOG("exported json");
105  try {
106  PlacesUtils.restoreBookmarksFromJSONFile(jsonFile);
107  } catch(ex) { do_throw("couldn't import the exported file: " + ex); }
108  LOG("imported json");
109  validate();
110  LOG("validated import");
111 }
112 
113 var tagData = [
114  { uri: uri("http://slint.us"), tags: ["indie", "kentucky", "music"] },
115  { uri: uri("http://en.wikipedia.org/wiki/Diplodocus"), tags: ["dinosaur", "dj", "rad word"] }
116 ];
117 
119  { uri: uri("http://slint.us"), title: "indie, kentucky, music" },
120  { uri: uri("http://en.wikipedia.org/wiki/Diplodocus"), title: "dinosaur, dj, rad word" }
121 ];
122 
123 /*
124 populate data in each folder
125 (menu is populated via the html import)
126 */
127 function populate() {
128  // add tags
129  for each(let {uri: u, tags: t} in tagData)
130  PlacesUtils.tagging.tagURI(u, t);
131 
132  // add unfiled bookmarks
133  for each(let {uri: u, title: t} in bookmarkData) {
134  PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarks.unfiledBookmarksFolder,
135  u, PlacesUtils.bookmarks.DEFAULT_INDEX, t);
136  }
137 
138  // add to the toolbar
139  for each(let {uri: u, title: t} in bookmarkData) {
140  PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarks.toolbarFolder,
141  u, PlacesUtils.bookmarks.DEFAULT_INDEX, t);
142  }
143 }
144 
145 function validate() {
146  testCanonicalBookmarks(PlacesUtils.bookmarks.bookmarksMenuFolder);
149  testTags();
150 }
151 
152 // Tests a bookmarks datastore that has a set of bookmarks, etc
153 // that flex each supported field and feature.
155  // query to see if the deleted folder and items have been imported
156  var query = PlacesUtils.history.getNewQuery();
157  query.setFolders([PlacesUtils.bookmarks.bookmarksMenuFolder], 1);
158  var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions());
159  var rootNode = result.root;
160  rootNode.containerOpen = true;
161 
162  // 6-2: the toolbar contents are imported to the places-toolbar folder,
163  // the separator above it is removed.
164  do_check_eq(rootNode.childCount, 4);
165 
166  // get test folder
167  var testFolder = rootNode.getChild(3);
168  do_check_eq(testFolder.type, testFolder.RESULT_TYPE_FOLDER);
169  do_check_eq(testFolder.title, "test");
170 
171  /*
172  // add date
173  do_check_eq(PlacesUtils.bookmarks.getItemDateAdded(testFolder.itemId)/1000000, 1177541020);
174  // last modified
175  do_check_eq(PlacesUtils.bookmarks.getItemLastModified(testFolder.itemId)/1000000, 1177541050);
176  */
177 
178  testFolder = testFolder.QueryInterface(Ci.nsINavHistoryQueryResultNode);
179  do_check_eq(testFolder.hasChildren, true);
180  // folder description
181  do_check_true(PlacesUtils.annotations.itemHasAnnotation(testFolder.itemId,
183  do_check_eq("folder test comment",
184  PlacesUtils.annotations.getItemAnnotation(testFolder.itemId, DESCRIPTION_ANNO));
185  // open test folder, and test the children
186  testFolder.containerOpen = true;
187  var cc = testFolder.childCount;
188  // XXX Bug 380468
189  // do_check_eq(cc, 2);
190  do_check_eq(cc, 1);
191 
192  // test bookmark 1
193  var testBookmark1 = testFolder.getChild(0);
194  // url
195  do_check_eq("http://test/post", testBookmark1.uri);
196  // title
197  do_check_eq("test post keyword", testBookmark1.title);
198  // keyword
199  do_check_eq("test", PlacesUtils.bookmarks.getKeywordForBookmark(testBookmark1.itemId));
200  // sidebar
201  do_check_true(PlacesUtils.annotations.itemHasAnnotation(testBookmark1.itemId,
203  /*
204  // add date
205  do_check_eq(testBookmark1.dateAdded/1000000, 1177375336);
206 
207  // last modified
208  do_check_eq(testBookmark1.lastModified/1000000, 1177375423);
209  */
210 
211  // post data
212  do_check_true(PlacesUtils.annotations.itemHasAnnotation(testBookmark1.itemId, POST_DATA_ANNO));
213  do_check_eq("hidden1%3Dbar&text1%3D%25s",
214  PlacesUtils.annotations.getItemAnnotation(testBookmark1.itemId, POST_DATA_ANNO));
215 
216  // last charset
217  var testURI = PlacesUtils._uri(testBookmark1.uri);
218  do_check_eq("ISO-8859-1", PlacesUtils.history.getCharsetForURI(testURI));
219 
220  // description
221  do_check_true(PlacesUtils.annotations.itemHasAnnotation(testBookmark1.itemId,
223  do_check_eq("item description",
224  PlacesUtils.annotations.getItemAnnotation(testBookmark1.itemId,
226 
227  /*
228  // XXX Bug 380468
229  // test bookmark 2
230  var testBookmark2 = testFolder.getChild(1);
231  // url
232  do_check_eq("http://test/micsum", testBookmark2.uri);
233  // title
234  do_check_eq("test microsummary", testBookmark2.title);
235  // check that it's a microsummary
236  var micsum = mssvc.getMicrosummary(testBookmark2.itemId);
237  if (!micsum)
238  do_throw("Could not import microsummary");
239  // check generator uri
240  var generator = micsum.generator;
241  do_check_eq("urn:source:http://dietrich.ganx4.com/mozilla/test-microsummary.xml", generator.uri.spec);
242  // expiration and generated title can change, so don't test them
243  */
244 
245  // clean up
246  testFolder.containerOpen = false;
247  rootNode.containerOpen = false;
248 }
249 
250 function testToolbarFolder() {
251  var query = PlacesUtils.history.getNewQuery();
252  query.setFolders([PlacesUtils.bookmarks.toolbarFolder], 1);
253  var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions());
254 
255  var toolbar = result.root;
256  toolbar.containerOpen = true;
257 
258  // child count (add 2 for pre-existing items)
259  do_check_eq(toolbar.childCount, bookmarkData.length + 2);
260 
261  // livemark
262  var livemark = toolbar.getChild(1);
263  // title
264  do_check_eq("Latest Headlines", livemark.title);
265  // livemark check
266  do_check_true(PlacesUtils.livemarks.isLivemark(livemark.itemId));
267  // site url
268  do_check_eq("http://en-us.fxfeeds.mozilla.com/en-US/firefox/livebookmarks/",
269  PlacesUtils.livemarks.getSiteURI(livemark.itemId).spec);
270  // feed url
271  do_check_eq("http://en-us.fxfeeds.mozilla.com/en-US/firefox/headlines.xml",
272  PlacesUtils.livemarks.getFeedURI(livemark.itemId).spec);
273 
274  // test added bookmark data
275  var child = toolbar.getChild(2);
276  do_check_eq(child.uri, bookmarkData[0].uri.spec);
277  do_check_eq(child.title, bookmarkData[0].title);
278  child = toolbar.getChild(3);
279  do_check_eq(child.uri, bookmarkData[1].uri.spec);
280  do_check_eq(child.title, bookmarkData[1].title);
281 
282  toolbar.containerOpen = false;
283 }
284 
286  var query = PlacesUtils.history.getNewQuery();
287  query.setFolders([PlacesUtils.bookmarks.unfiledBookmarksFolder], 1);
288  var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions());
289  var rootNode = result.root;
290  rootNode.containerOpen = true;
291  // child count (add 1 for pre-existing item)
292  do_check_eq(rootNode.childCount, bookmarkData.length + 1);
293  for (var i = 1; i < rootNode.childCount; i++) {
294  var child = rootNode.getChild(i);
295  dump(bookmarkData[i - 1].uri.spec + " == " + child.uri + "?\n");
296  do_check_true(bookmarkData[i - 1].uri.equals(uri(child.uri)));
297  do_check_eq(child.title, bookmarkData[i - 1].title);
298  /* WTF
299  if (child.tags)
300  do_check_eq(child.tags, bookmarkData[i].title);
301  */
302  }
303  rootNode.containerOpen = false;
304 }
305 
306 function testTags() {
307  for each(let {uri: u, tags: t} in tagData) {
308  var i = 0;
309  dump("test tags for " + u.spec + ": " + t + "\n");
310  var tt = PlacesUtils.tagging.getTagsForURI(u, {});
311  dump("true tags for " + u.spec + ": " + tt + "\n");
312  do_check_true(t.every(function(el) {
313  i++;
314  return tt.indexOf(el) > -1;
315  }));
316  do_check_eq(i, t.length);
317  }
318 }
function testTags()
Definition: test_384370.js:306
const Cc
do_check_eq(typeof PlacesUtils,"object")
var dirSvc
#define LOG(args)
var testFolder
function testToolbarFolder()
Definition: test_384370.js:250
function testCanonicalBookmarks()
Definition: test_384370.js:154
function populate()
Definition: test_384370.js:127
const POST_DATA_ANNO
Definition: test_384370.js:46
function run_test()
Definition: test_384370.js:52
function testUnfiledBookmarks()
Definition: test_384370.js:285
var t
function validate()
Definition: test_384370.js:145
const DESCRIPTION_ANNO
Definition: test_384370.js:45
var bookmarkData
Definition: test_384370.js:118
var uri
Definition: FeedWriter.js:1135
var tagData
Definition: test_384370.js:113
const Ci
const LOAD_IN_SIDEBAR_ANNO
Definition: test_384370.js:44
_getSelectedPageStyle s i