toolkit/components/places/tests/bookmarks/test_bmindex.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 const NUM_BOOKMARKS = 20;
michael@0 8 const NUM_SEPARATORS = 5;
michael@0 9 const NUM_FOLDERS = 10;
michael@0 10 const NUM_ITEMS = NUM_BOOKMARKS + NUM_SEPARATORS + NUM_FOLDERS;
michael@0 11 const MIN_RAND = -5;
michael@0 12 const MAX_RAND = 40;
michael@0 13
michael@0 14 var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
michael@0 15 getService(Ci.nsINavBookmarksService);
michael@0 16
michael@0 17 function check_contiguous_indexes(aBookmarks) {
michael@0 18 var indexes = [];
michael@0 19 aBookmarks.forEach(function(aBookmarkId) {
michael@0 20 let bmIndex = bs.getItemIndex(aBookmarkId);
michael@0 21 dump("Index: " + bmIndex + "\n");
michael@0 22 dump("Checking duplicates\n");
michael@0 23 do_check_eq(indexes.indexOf(bmIndex), -1);
michael@0 24 dump("Checking out of range, found " + aBookmarks.length + " items\n");
michael@0 25 do_check_true(bmIndex >= 0 && bmIndex < aBookmarks.length);
michael@0 26 indexes.push(bmIndex);
michael@0 27 });
michael@0 28 dump("Checking all valid indexes have been used\n");
michael@0 29 do_check_eq(indexes.length, aBookmarks.length);
michael@0 30 }
michael@0 31
michael@0 32 // main
michael@0 33 function run_test() {
michael@0 34 var bookmarks = [];
michael@0 35 // Insert bookmarks with random indexes.
michael@0 36 for (let i = 0; bookmarks.length < NUM_BOOKMARKS; i++) {
michael@0 37 let randIndex = Math.round(MIN_RAND + (Math.random() * (MAX_RAND - MIN_RAND)));
michael@0 38 try {
michael@0 39 let id = bs.insertBookmark(bs.unfiledBookmarksFolder,
michael@0 40 uri("http://" + i + ".mozilla.org/"),
michael@0 41 randIndex, "Test bookmark " + i);
michael@0 42 if (randIndex < -1)
michael@0 43 do_throw("Creating a bookmark at an invalid index should throw");
michael@0 44 bookmarks.push(id);
michael@0 45 }
michael@0 46 catch (ex) {
michael@0 47 if (randIndex >= -1)
michael@0 48 do_throw("Creating a bookmark at a valid index should not throw");
michael@0 49 }
michael@0 50 }
michael@0 51 check_contiguous_indexes(bookmarks);
michael@0 52
michael@0 53 // Insert separators with random indexes.
michael@0 54 for (let i = 0; bookmarks.length < NUM_BOOKMARKS + NUM_SEPARATORS; i++) {
michael@0 55 let randIndex = Math.round(MIN_RAND + (Math.random() * (MAX_RAND - MIN_RAND)));
michael@0 56 try {
michael@0 57 let id = bs.insertSeparator(bs.unfiledBookmarksFolder, randIndex);
michael@0 58 if (randIndex < -1)
michael@0 59 do_throw("Creating a separator at an invalid index should throw");
michael@0 60 bookmarks.push(id);
michael@0 61 }
michael@0 62 catch (ex) {
michael@0 63 if (randIndex >= -1)
michael@0 64 do_throw("Creating a separator at a valid index should not throw");
michael@0 65 }
michael@0 66 }
michael@0 67 check_contiguous_indexes(bookmarks);
michael@0 68
michael@0 69 // Insert folders with random indexes.
michael@0 70 for (let i = 0; bookmarks.length < NUM_ITEMS; i++) {
michael@0 71 let randIndex = Math.round(MIN_RAND + (Math.random() * (MAX_RAND - MIN_RAND)));
michael@0 72 try {
michael@0 73 let id = bs.createFolder(bs.unfiledBookmarksFolder,
michael@0 74 "Test folder " + i, randIndex);
michael@0 75 if (randIndex < -1)
michael@0 76 do_throw("Creating a folder at an invalid index should throw");
michael@0 77 bookmarks.push(id);
michael@0 78 }
michael@0 79 catch (ex) {
michael@0 80 if (randIndex >= -1)
michael@0 81 do_throw("Creating a folder at a valid index should not throw");
michael@0 82 }
michael@0 83 }
michael@0 84 check_contiguous_indexes(bookmarks);
michael@0 85
michael@0 86 // Execute some random bookmark delete.
michael@0 87 for (let i = 0; i < Math.ceil(NUM_ITEMS / 4); i++) {
michael@0 88 let id = bookmarks.splice(Math.floor(Math.random() * bookmarks.length), 1);
michael@0 89 dump("Removing item with id " + id + "\n");
michael@0 90 bs.removeItem(id);
michael@0 91 }
michael@0 92 check_contiguous_indexes(bookmarks);
michael@0 93
michael@0 94 // Execute some random bookmark move. This will also try to move it to
michael@0 95 // invalid index values.
michael@0 96 for (let i = 0; i < Math.ceil(NUM_ITEMS / 4); i++) {
michael@0 97 let randIndex = Math.floor(Math.random() * bookmarks.length);
michael@0 98 let id = bookmarks[randIndex];
michael@0 99 let newIndex = Math.round(MIN_RAND + (Math.random() * (MAX_RAND - MIN_RAND)));
michael@0 100 dump("Moving item with id " + id + " to index " + newIndex + "\n");
michael@0 101 try {
michael@0 102 bs.moveItem(id, bs.unfiledBookmarksFolder, newIndex);
michael@0 103 if (newIndex < -1)
michael@0 104 do_throw("Moving an item to a negative index should throw\n");
michael@0 105 }
michael@0 106 catch (ex) {
michael@0 107 if (newIndex >= -1)
michael@0 108 do_throw("Moving an item to a valid index should not throw\n");
michael@0 109 }
michael@0 110
michael@0 111 }
michael@0 112 check_contiguous_indexes(bookmarks);
michael@0 113
michael@0 114 // Ensure setItemIndex throws if we pass it a negative index.
michael@0 115 try {
michael@0 116 bs.setItemIndex(bookmarks[0], -1);
michael@0 117 do_throw("setItemIndex should throw for a negative index");
michael@0 118 } catch (ex) {}
michael@0 119 // Ensure setItemIndex throws if we pass it a bad itemId.
michael@0 120 try {
michael@0 121 bs.setItemIndex(0, 5);
michael@0 122 do_throw("setItemIndex should throw for a bad itemId");
michael@0 123 } catch (ex) {}
michael@0 124 }

mercurial