toolkit/components/places/tests/bookmarks/test_417228-other-roots.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/bookmarks/test_417228-other-roots.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,160 @@
     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 +var tests = [];
    1.11 +
    1.12 +/*
    1.13 +
    1.14 +Backup/restore tests example:
    1.15 +
    1.16 +var myTest = {
    1.17 +  populate: function () { ... add bookmarks ... },
    1.18 +  validate: function () { ... query for your bookmarks ... }
    1.19 +}
    1.20 +
    1.21 +this.push(myTest);
    1.22 +
    1.23 +*/
    1.24 +
    1.25 +tests.push({
    1.26 +  excludeItemsFromRestore: [],
    1.27 +  populate: function populate() {
    1.28 +    // check initial size
    1.29 +    var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId,
    1.30 +                                                 false, false).root;
    1.31 +    do_check_eq(rootNode.childCount, 4);
    1.32 +
    1.33 +    // create a test root
    1.34 +    this._folderTitle = "test folder";
    1.35 +    this._folderId = 
    1.36 +      PlacesUtils.bookmarks.createFolder(PlacesUtils.placesRootId,
    1.37 +                                         this._folderTitle,
    1.38 +                                         PlacesUtils.bookmarks.DEFAULT_INDEX);
    1.39 +    do_check_eq(rootNode.childCount, 5);
    1.40 +
    1.41 +    // add a tag
    1.42 +    this._testURI = PlacesUtils._uri("http://test");
    1.43 +    this._tags = ["a", "b"];
    1.44 +    PlacesUtils.tagging.tagURI(this._testURI, this._tags);
    1.45 +
    1.46 +    // add a child to each root, including our test root
    1.47 +    this._roots = [PlacesUtils.bookmarksMenuFolderId, PlacesUtils.toolbarFolderId,
    1.48 +                   PlacesUtils.unfiledBookmarksFolderId, this._folderId];
    1.49 +    this._roots.forEach(function(aRootId) {
    1.50 +      // clean slate
    1.51 +      PlacesUtils.bookmarks.removeFolderChildren(aRootId);
    1.52 +      // add a test bookmark
    1.53 +      PlacesUtils.bookmarks.insertBookmark(aRootId, this._testURI,
    1.54 +                                           PlacesUtils.bookmarks.DEFAULT_INDEX, "test");
    1.55 +    }, this);
    1.56 +
    1.57 +    // add a folder to exclude from replacing during restore
    1.58 +    // this will still be present post-restore
    1.59 +    var excludedFolderId =
    1.60 +      PlacesUtils.bookmarks.createFolder(PlacesUtils.placesRootId,
    1.61 +                                         "excluded",
    1.62 +                                         PlacesUtils.bookmarks.DEFAULT_INDEX);
    1.63 +    do_check_eq(rootNode.childCount, 6);
    1.64 +    this.excludeItemsFromRestore.push(excludedFolderId); 
    1.65 +
    1.66 +    // add a test bookmark to it
    1.67 +    PlacesUtils.bookmarks.insertBookmark(excludedFolderId, this._testURI,
    1.68 +                                         PlacesUtils.bookmarks.DEFAULT_INDEX, "test");
    1.69 +  },
    1.70 +
    1.71 +  inbetween: function inbetween() {
    1.72 +    // add some items that should be removed by the restore
    1.73 +
    1.74 +    // add a folder
    1.75 +    this._litterTitle = "otter";
    1.76 +    PlacesUtils.bookmarks.createFolder(PlacesUtils.placesRootId,
    1.77 +                                       this._litterTitle, 0);
    1.78 +
    1.79 +    // add some tags
    1.80 +    PlacesUtils.tagging.tagURI(this._testURI, ["c", "d"]);
    1.81 +  },
    1.82 +
    1.83 +  validate: function validate() {
    1.84 +    // validate tags restored
    1.85 +    var tags = PlacesUtils.tagging.getTagsForURI(this._testURI);
    1.86 +    // also validates that litter tags are gone
    1.87 +    do_check_eq(this._tags.toString(), tags.toString());
    1.88 +
    1.89 +    var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId,
    1.90 +                                                 false, false).root;
    1.91 +
    1.92 +    // validate litter is gone
    1.93 +    do_check_neq(rootNode.getChild(0).title, this._litterTitle);
    1.94 +
    1.95 +    // test root count is the same
    1.96 +    do_check_eq(rootNode.childCount, 6);
    1.97 +
    1.98 +    var foundTestFolder = 0;
    1.99 +    for (var i = 0; i < rootNode.childCount; i++) {
   1.100 +      var node = rootNode.getChild(i);
   1.101 +
   1.102 +      LOG("validating " + node.title);
   1.103 +      if (node.itemId != PlacesUtils.tagsFolderId) {
   1.104 +        if (node.title == this._folderTitle) {
   1.105 +          // check the test folder's properties
   1.106 +          do_check_eq(node.type, node.RESULT_TYPE_FOLDER);
   1.107 +          do_check_eq(node.title, this._folderTitle);
   1.108 +          foundTestFolder++;
   1.109 +        }
   1.110 +
   1.111 +        // test contents
   1.112 +        node.QueryInterface(Ci.nsINavHistoryContainerResultNode).containerOpen = true;
   1.113 +        do_check_eq(node.childCount, 1);
   1.114 +        var child = node.getChild(0);
   1.115 +        do_check_true(PlacesUtils._uri(child.uri).equals(this._testURI));
   1.116 +
   1.117 +        // clean up
   1.118 +        node.containerOpen = false;
   1.119 +      }
   1.120 +    }
   1.121 +    do_check_eq(foundTestFolder, 1);
   1.122 +    rootNode.containerOpen = false;
   1.123 +  }
   1.124 +});
   1.125 +
   1.126 +function run_test() {
   1.127 +  run_next_test();
   1.128 +}
   1.129 +
   1.130 +add_task(function () {
   1.131 +  // make json file
   1.132 +  let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json");
   1.133 +
   1.134 +  // array of ids not to delete when restoring
   1.135 +  var excludedItemsFromRestore = [];
   1.136 +
   1.137 +  // populate db
   1.138 +  tests.forEach(function(aTest) {
   1.139 +    aTest.populate();
   1.140 +    // sanity
   1.141 +    aTest.validate();
   1.142 +
   1.143 +    if (aTest.excludedItemsFromRestore)
   1.144 +      excludedItemsFromRestore = excludedItems.concat(aTest.excludedItemsFromRestore);
   1.145 +  });
   1.146 +
   1.147 +  yield BookmarkJSONUtils.exportToFile(jsonFile);
   1.148 +
   1.149 +  tests.forEach(function(aTest) {
   1.150 +    aTest.inbetween();
   1.151 +  });
   1.152 +
   1.153 +  // restore json file
   1.154 +  yield BookmarkJSONUtils.importFromFile(jsonFile, true);
   1.155 +
   1.156 +  // validate
   1.157 +  tests.forEach(function(aTest) {
   1.158 +    aTest.validate();
   1.159 +  });
   1.160 +
   1.161 +  // clean up
   1.162 +  yield OS.File.remove(jsonFile);
   1.163 +});

mercurial