toolkit/components/places/tests/bookmarks/test_bmindex.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_bmindex.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     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 NUM_BOOKMARKS = 20;
    1.11 +const NUM_SEPARATORS = 5;
    1.12 +const NUM_FOLDERS = 10;
    1.13 +const NUM_ITEMS = NUM_BOOKMARKS + NUM_SEPARATORS + NUM_FOLDERS;
    1.14 +const MIN_RAND = -5;
    1.15 +const MAX_RAND = 40;
    1.16 +
    1.17 +var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
    1.18 +         getService(Ci.nsINavBookmarksService);
    1.19 +
    1.20 +function check_contiguous_indexes(aBookmarks) {
    1.21 +  var indexes = [];
    1.22 +  aBookmarks.forEach(function(aBookmarkId) {
    1.23 +    let bmIndex = bs.getItemIndex(aBookmarkId);
    1.24 +    dump("Index: " + bmIndex + "\n");
    1.25 +    dump("Checking duplicates\n");
    1.26 +    do_check_eq(indexes.indexOf(bmIndex), -1);
    1.27 +    dump("Checking out of range, found " + aBookmarks.length + " items\n");
    1.28 +    do_check_true(bmIndex >= 0 && bmIndex < aBookmarks.length);
    1.29 +    indexes.push(bmIndex);
    1.30 +  });
    1.31 +  dump("Checking all valid indexes have been used\n");
    1.32 +  do_check_eq(indexes.length, aBookmarks.length);
    1.33 +}
    1.34 +
    1.35 +// main
    1.36 +function run_test() {
    1.37 +  var bookmarks = [];
    1.38 +  // Insert bookmarks with random indexes.
    1.39 +  for (let i = 0; bookmarks.length < NUM_BOOKMARKS; i++) {
    1.40 +    let randIndex = Math.round(MIN_RAND + (Math.random() * (MAX_RAND - MIN_RAND)));
    1.41 +    try {
    1.42 +      let id = bs.insertBookmark(bs.unfiledBookmarksFolder,
    1.43 +                                 uri("http://" + i + ".mozilla.org/"),
    1.44 +                                 randIndex, "Test bookmark " + i);
    1.45 +      if (randIndex < -1)
    1.46 +        do_throw("Creating a bookmark at an invalid index should throw");
    1.47 +      bookmarks.push(id);
    1.48 +    }
    1.49 +    catch (ex) {
    1.50 +      if (randIndex >= -1)
    1.51 +        do_throw("Creating a bookmark at a valid index should not throw");
    1.52 +    }
    1.53 +  }
    1.54 +  check_contiguous_indexes(bookmarks);
    1.55 +
    1.56 +  // Insert separators with random indexes.
    1.57 +  for (let i = 0; bookmarks.length < NUM_BOOKMARKS + NUM_SEPARATORS; i++) {
    1.58 +    let randIndex = Math.round(MIN_RAND + (Math.random() * (MAX_RAND - MIN_RAND)));
    1.59 +    try {
    1.60 +      let id = bs.insertSeparator(bs.unfiledBookmarksFolder, randIndex);
    1.61 +      if (randIndex < -1)
    1.62 +        do_throw("Creating a separator at an invalid index should throw");
    1.63 +      bookmarks.push(id);
    1.64 +    }
    1.65 +    catch (ex) {
    1.66 +      if (randIndex >= -1)
    1.67 +        do_throw("Creating a separator at a valid index should not throw");
    1.68 +    }
    1.69 +  }
    1.70 +  check_contiguous_indexes(bookmarks);
    1.71 +
    1.72 +  // Insert folders with random indexes.
    1.73 +  for (let i = 0; bookmarks.length < NUM_ITEMS; i++) {
    1.74 +    let randIndex = Math.round(MIN_RAND + (Math.random() * (MAX_RAND - MIN_RAND)));
    1.75 +    try {
    1.76 +      let id = bs.createFolder(bs.unfiledBookmarksFolder,
    1.77 +                               "Test folder " + i, randIndex);
    1.78 +      if (randIndex < -1)
    1.79 +        do_throw("Creating a folder at an invalid index should throw");
    1.80 +      bookmarks.push(id);
    1.81 +    }
    1.82 +    catch (ex) {
    1.83 +      if (randIndex >= -1)
    1.84 +        do_throw("Creating a folder at a valid index should not throw");
    1.85 +    }
    1.86 +  }
    1.87 +  check_contiguous_indexes(bookmarks);
    1.88 +
    1.89 +  // Execute some random bookmark delete.
    1.90 +  for (let i = 0; i < Math.ceil(NUM_ITEMS / 4); i++) {
    1.91 +    let id = bookmarks.splice(Math.floor(Math.random() * bookmarks.length), 1);
    1.92 +    dump("Removing item with id " + id + "\n");
    1.93 +    bs.removeItem(id);
    1.94 +  }
    1.95 +  check_contiguous_indexes(bookmarks);
    1.96 +
    1.97 +  // Execute some random bookmark move.  This will also try to move it to
    1.98 +  // invalid index values.
    1.99 +  for (let i = 0; i < Math.ceil(NUM_ITEMS / 4); i++) {
   1.100 +    let randIndex = Math.floor(Math.random() * bookmarks.length);
   1.101 +    let id = bookmarks[randIndex];
   1.102 +    let newIndex = Math.round(MIN_RAND + (Math.random() * (MAX_RAND - MIN_RAND)));
   1.103 +    dump("Moving item with id " + id + " to index " + newIndex + "\n");
   1.104 +    try {
   1.105 +      bs.moveItem(id, bs.unfiledBookmarksFolder, newIndex);
   1.106 +      if (newIndex < -1)
   1.107 +        do_throw("Moving an item to a negative index should throw\n");
   1.108 +    }
   1.109 +    catch (ex) {
   1.110 +      if (newIndex >= -1)
   1.111 +        do_throw("Moving an item to a valid index should not throw\n");
   1.112 +    }
   1.113 +    
   1.114 +  }
   1.115 +  check_contiguous_indexes(bookmarks);
   1.116 +
   1.117 +  // Ensure setItemIndex throws if we pass it a negative index.
   1.118 +  try {
   1.119 +    bs.setItemIndex(bookmarks[0], -1);
   1.120 +    do_throw("setItemIndex should throw for a negative index");
   1.121 +  } catch (ex) {}
   1.122 +  // Ensure setItemIndex throws if we pass it a bad itemId.
   1.123 +  try {
   1.124 +    bs.setItemIndex(0, 5);
   1.125 +    do_throw("setItemIndex should throw for a bad itemId");
   1.126 +  } catch (ex) {}
   1.127 +}

mercurial