toolkit/components/places/tests/unit/test_384370.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_384370.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,270 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +const LOAD_IN_SIDEBAR_ANNO = "bookmarkProperties/loadInSidebar";
    1.11 +const DESCRIPTION_ANNO = "bookmarkProperties/description";
    1.12 +const POST_DATA_ANNO = "bookmarkProperties/POSTData";
    1.13 +
    1.14 +do_check_eq(typeof PlacesUtils, "object");
    1.15 +
    1.16 +// main
    1.17 +function run_test() {
    1.18 +  do_test_pending();
    1.19 +
    1.20 +  /*
    1.21 +    HTML+FEATURES SUMMARY:
    1.22 +    - import legacy bookmarks
    1.23 +    - export as json, import, test (tests integrity of html > json)
    1.24 +    - export as html, import, test (tests integrity of json > html)
    1.25 +
    1.26 +    BACKUP/RESTORE SUMMARY:
    1.27 +    - create a bookmark in each root
    1.28 +    - tag multiple URIs with multiple tags
    1.29 +    - export as json, import, test
    1.30 +  */
    1.31 +
    1.32 +  // import the importer
    1.33 +  Cu.import("resource://gre/modules/BookmarkHTMLUtils.jsm");
    1.34 +
    1.35 +  // file pointer to legacy bookmarks file
    1.36 +  var bookmarksFileOld = OS.Path.join(do_get_cwd().path, "bookmarks.preplaces.html");
    1.37 +  // file pointer to a new places-exported json file
    1.38 +  var jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.exported.json");
    1.39 +  Task.spawn(function () {
    1.40 +    // create bookmarks.exported.json
    1.41 +    if ((yield OS.File.exists(jsonFile)))
    1.42 +      yield OS.File.remove(jsonFile);
    1.43 +
    1.44 +    // Test importing a pre-Places canonical bookmarks file.
    1.45 +    // 1. import bookmarks.preplaces.html
    1.46 +    // Note: we do not empty the db before this import to catch bugs like 380999
    1.47 +    try {
    1.48 +      BookmarkHTMLUtils.importFromFile(bookmarksFileOld, true)
    1.49 +                       .then(after_import, do_report_unexpected_exception);
    1.50 +    } catch(ex) { do_throw("couldn't import legacy bookmarks file: " + ex); }
    1.51 +  });
    1.52 +
    1.53 +  function after_import() {
    1.54 +    populate();
    1.55 +
    1.56 +    // 2. run the test-suite
    1.57 +    Task.spawn(function() {
    1.58 +      yield validate();
    1.59 +      yield promiseAsyncUpdates();
    1.60 +      
    1.61 +      // Test exporting a Places canonical json file.
    1.62 +      // 1. export to bookmarks.exported.json
    1.63 +      try {
    1.64 +        yield BookmarkJSONUtils.exportToFile(jsonFile);
    1.65 +      } catch(ex) { do_throw("couldn't export to file: " + ex); }
    1.66 +      LOG("exported json");
    1.67 +
    1.68 +      // 2. empty bookmarks db
    1.69 +      // 3. import bookmarks.exported.json
    1.70 +      try {
    1.71 +        yield BookmarkJSONUtils.importFromFile(jsonFile, true);
    1.72 +      } catch(ex) { do_throw("couldn't import the exported file: " + ex); }
    1.73 +      LOG("imported json");
    1.74 +
    1.75 +      // 4. run the test-suite
    1.76 +      yield validate();
    1.77 +      LOG("validated import");
    1.78 +  
    1.79 +      yield promiseAsyncUpdates();
    1.80 +      do_test_finished();
    1.81 +    });
    1.82 +  }
    1.83 +}
    1.84 +
    1.85 +var tagData = [
    1.86 +  { uri: uri("http://slint.us"), tags: ["indie", "kentucky", "music"] },
    1.87 +  { uri: uri("http://en.wikipedia.org/wiki/Diplodocus"), tags: ["dinosaur", "dj", "rad word"] }
    1.88 +];
    1.89 +
    1.90 +var bookmarkData = [
    1.91 +  { uri: uri("http://slint.us"), title: "indie, kentucky, music" },
    1.92 +  { uri: uri("http://en.wikipedia.org/wiki/Diplodocus"), title: "dinosaur, dj, rad word" }
    1.93 +];
    1.94 +
    1.95 +/*
    1.96 +populate data in each folder
    1.97 +(menu is populated via the html import)
    1.98 +*/
    1.99 +function populate() {
   1.100 +  // add tags
   1.101 +  for each(let {uri: u, tags: t} in tagData)
   1.102 +    PlacesUtils.tagging.tagURI(u, t);
   1.103 +  
   1.104 +  // add unfiled bookmarks
   1.105 +  for each(let {uri: u, title: t} in bookmarkData) {
   1.106 +    PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarks.unfiledBookmarksFolder,
   1.107 +                                         u, PlacesUtils.bookmarks.DEFAULT_INDEX, t);
   1.108 +  }
   1.109 +
   1.110 +  // add to the toolbar
   1.111 +  for each(let {uri: u, title: t} in bookmarkData) {
   1.112 +    PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarks.toolbarFolder,
   1.113 +                                         u, PlacesUtils.bookmarks.DEFAULT_INDEX, t);
   1.114 +  }
   1.115 +}
   1.116 +
   1.117 +function validate() {
   1.118 +  yield testCanonicalBookmarks();
   1.119 +  yield testToolbarFolder();
   1.120 +  testUnfiledBookmarks();
   1.121 +  testTags();
   1.122 +}
   1.123 +
   1.124 +// Tests a bookmarks datastore that has a set of bookmarks, etc
   1.125 +// that flex each supported field and feature.
   1.126 +function testCanonicalBookmarks() {
   1.127 +  // query to see if the deleted folder and items have been imported
   1.128 +  var query = PlacesUtils.history.getNewQuery();
   1.129 +  query.setFolders([PlacesUtils.bookmarksMenuFolderId], 1);
   1.130 +  var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions());
   1.131 +  var rootNode = result.root;
   1.132 +  rootNode.containerOpen = true;
   1.133 +
   1.134 +  // Count expected bookmarks in the menu root.
   1.135 +  do_check_eq(rootNode.childCount, 3);
   1.136 +
   1.137 +  // check separator
   1.138 +  var testSeparator = rootNode.getChild(1);
   1.139 +  do_check_eq(testSeparator.type, testSeparator.RESULT_TYPE_SEPARATOR);
   1.140 +
   1.141 +  // get test folder
   1.142 +  var testFolder = rootNode.getChild(2);
   1.143 +  do_check_eq(testFolder.type, testFolder.RESULT_TYPE_FOLDER);
   1.144 +  do_check_eq(testFolder.title, "test");
   1.145 +
   1.146 +  /*
   1.147 +  // add date 
   1.148 +  do_check_eq(PlacesUtils.bookmarks.getItemDateAdded(testFolder.itemId)/1000000, 1177541020);
   1.149 +  // last modified
   1.150 +  do_check_eq(PlacesUtils.bookmarks.getItemLastModified(testFolder.itemId)/1000000, 1177541050);
   1.151 +  */
   1.152 +
   1.153 +  testFolder = testFolder.QueryInterface(Ci.nsINavHistoryQueryResultNode);
   1.154 +  do_check_eq(testFolder.hasChildren, true);
   1.155 +  // folder description
   1.156 +  do_check_true(PlacesUtils.annotations.itemHasAnnotation(testFolder.itemId,
   1.157 +                                                          DESCRIPTION_ANNO));
   1.158 +  do_check_eq("folder test comment",
   1.159 +              PlacesUtils.annotations.getItemAnnotation(testFolder.itemId, DESCRIPTION_ANNO));
   1.160 +  // open test folder, and test the children
   1.161 +  testFolder.containerOpen = true;
   1.162 +  var cc = testFolder.childCount;
   1.163 +  // XXX Bug 380468
   1.164 +  // do_check_eq(cc, 2);
   1.165 +  do_check_eq(cc, 1);
   1.166 +
   1.167 +  // test bookmark 1
   1.168 +  var testBookmark1 = testFolder.getChild(0);
   1.169 +  // url
   1.170 +  do_check_eq("http://test/post", testBookmark1.uri);
   1.171 +  // title
   1.172 +  do_check_eq("test post keyword", testBookmark1.title);
   1.173 +  // keyword
   1.174 +  do_check_eq("test", PlacesUtils.bookmarks.getKeywordForBookmark(testBookmark1.itemId));
   1.175 +  // sidebar
   1.176 +  do_check_true(PlacesUtils.annotations.itemHasAnnotation(testBookmark1.itemId,
   1.177 +                                                          LOAD_IN_SIDEBAR_ANNO));
   1.178 +  /*
   1.179 +  // add date 
   1.180 +  do_check_eq(testBookmark1.dateAdded/1000000, 1177375336);
   1.181 +
   1.182 +  // last modified
   1.183 +  do_check_eq(testBookmark1.lastModified/1000000, 1177375423);
   1.184 +  */
   1.185 +
   1.186 +  // post data
   1.187 +  do_check_true(PlacesUtils.annotations.itemHasAnnotation(testBookmark1.itemId, POST_DATA_ANNO));
   1.188 +  do_check_eq("hidden1%3Dbar&text1%3D%25s",
   1.189 +              PlacesUtils.annotations.getItemAnnotation(testBookmark1.itemId, POST_DATA_ANNO));
   1.190 +
   1.191 +  // last charset
   1.192 +  var testURI = PlacesUtils._uri(testBookmark1.uri);
   1.193 +  do_check_eq("ISO-8859-1", (yield PlacesUtils.getCharsetForURI(testURI)));
   1.194 +
   1.195 +  // description
   1.196 +  do_check_true(PlacesUtils.annotations.itemHasAnnotation(testBookmark1.itemId,
   1.197 +                                                          DESCRIPTION_ANNO));
   1.198 +  do_check_eq("item description",
   1.199 +              PlacesUtils.annotations.getItemAnnotation(testBookmark1.itemId,
   1.200 +                                                        DESCRIPTION_ANNO));
   1.201 +
   1.202 +  // clean up
   1.203 +  testFolder.containerOpen = false;
   1.204 +  rootNode.containerOpen = false;
   1.205 +}
   1.206 +
   1.207 +function testToolbarFolder() {
   1.208 +  var query = PlacesUtils.history.getNewQuery();
   1.209 +  query.setFolders([PlacesUtils.toolbarFolderId], 1);
   1.210 +  var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions());
   1.211 +
   1.212 +  var toolbar = result.root;
   1.213 +  toolbar.containerOpen = true;
   1.214 +
   1.215 +  // child count (add 2 for pre-existing items)
   1.216 +  do_check_eq(toolbar.childCount, bookmarkData.length + 2);
   1.217 +  
   1.218 +  // livemark
   1.219 +  var livemark = toolbar.getChild(1);
   1.220 +  // title
   1.221 +  do_check_eq("Latest Headlines", livemark.title);
   1.222 +
   1.223 +  let foundLivemark = yield PlacesUtils.livemarks.getLivemark({ id: livemark.itemId });
   1.224 +  do_check_eq("http://en-us.fxfeeds.mozilla.com/en-US/firefox/livebookmarks/",
   1.225 +              foundLivemark.siteURI.spec);
   1.226 +  do_check_eq("http://en-us.fxfeeds.mozilla.com/en-US/firefox/headlines.xml",
   1.227 +              foundLivemark.feedURI.spec);
   1.228 +
   1.229 +  // test added bookmark data
   1.230 +  var child = toolbar.getChild(2);
   1.231 +  do_check_eq(child.uri, bookmarkData[0].uri.spec);
   1.232 +  do_check_eq(child.title, bookmarkData[0].title);
   1.233 +  child = toolbar.getChild(3);
   1.234 +  do_check_eq(child.uri, bookmarkData[1].uri.spec);
   1.235 +  do_check_eq(child.title, bookmarkData[1].title);
   1.236 +
   1.237 +  toolbar.containerOpen = false;
   1.238 +}
   1.239 +
   1.240 +function testUnfiledBookmarks() {
   1.241 +  var query = PlacesUtils.history.getNewQuery();
   1.242 +  query.setFolders([PlacesUtils.unfiledBookmarksFolderId], 1);
   1.243 +  var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions());
   1.244 +  var rootNode = result.root;
   1.245 +  rootNode.containerOpen = true;
   1.246 +  // child count (add 1 for pre-existing item)
   1.247 +  do_check_eq(rootNode.childCount, bookmarkData.length + 1);
   1.248 +  for (var i = 1; i < rootNode.childCount; i++) {
   1.249 +    var child = rootNode.getChild(i);
   1.250 +    dump(bookmarkData[i - 1].uri.spec + " == " + child.uri + "?\n");
   1.251 +    do_check_true(bookmarkData[i - 1].uri.equals(uri(child.uri)));
   1.252 +    do_check_eq(child.title, bookmarkData[i - 1].title);
   1.253 +    /* WTF
   1.254 +    if (child.tags)
   1.255 +      do_check_eq(child.tags, bookmarkData[i].title);
   1.256 +    */
   1.257 +  }
   1.258 +  rootNode.containerOpen = false;
   1.259 +}
   1.260 +
   1.261 +function testTags() {
   1.262 +  for each(let {uri: u, tags: t} in tagData) {
   1.263 +    var i = 0;
   1.264 +    dump("test tags for " + u.spec + ": " + t + "\n");
   1.265 +    var tt = PlacesUtils.tagging.getTagsForURI(u);
   1.266 +    dump("true tags for " + u.spec + ": " + tt + "\n");
   1.267 +    do_check_true(t.every(function(el) {
   1.268 +      i++;
   1.269 +      return tt.indexOf(el) > -1;
   1.270 +    }));
   1.271 +    do_check_eq(i, t.length);
   1.272 +  }
   1.273 +}

mercurial