michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const NUM_BOOKMARKS = 20; michael@0: const NUM_SEPARATORS = 5; michael@0: const NUM_FOLDERS = 10; michael@0: const NUM_ITEMS = NUM_BOOKMARKS + NUM_SEPARATORS + NUM_FOLDERS; michael@0: const MIN_RAND = -5; michael@0: const MAX_RAND = 40; michael@0: michael@0: var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. michael@0: getService(Ci.nsINavBookmarksService); michael@0: michael@0: function check_contiguous_indexes(aBookmarks) { michael@0: var indexes = []; michael@0: aBookmarks.forEach(function(aBookmarkId) { michael@0: let bmIndex = bs.getItemIndex(aBookmarkId); michael@0: dump("Index: " + bmIndex + "\n"); michael@0: dump("Checking duplicates\n"); michael@0: do_check_eq(indexes.indexOf(bmIndex), -1); michael@0: dump("Checking out of range, found " + aBookmarks.length + " items\n"); michael@0: do_check_true(bmIndex >= 0 && bmIndex < aBookmarks.length); michael@0: indexes.push(bmIndex); michael@0: }); michael@0: dump("Checking all valid indexes have been used\n"); michael@0: do_check_eq(indexes.length, aBookmarks.length); michael@0: } michael@0: michael@0: // main michael@0: function run_test() { michael@0: var bookmarks = []; michael@0: // Insert bookmarks with random indexes. michael@0: for (let i = 0; bookmarks.length < NUM_BOOKMARKS; i++) { michael@0: let randIndex = Math.round(MIN_RAND + (Math.random() * (MAX_RAND - MIN_RAND))); michael@0: try { michael@0: let id = bs.insertBookmark(bs.unfiledBookmarksFolder, michael@0: uri("http://" + i + ".mozilla.org/"), michael@0: randIndex, "Test bookmark " + i); michael@0: if (randIndex < -1) michael@0: do_throw("Creating a bookmark at an invalid index should throw"); michael@0: bookmarks.push(id); michael@0: } michael@0: catch (ex) { michael@0: if (randIndex >= -1) michael@0: do_throw("Creating a bookmark at a valid index should not throw"); michael@0: } michael@0: } michael@0: check_contiguous_indexes(bookmarks); michael@0: michael@0: // Insert separators with random indexes. michael@0: for (let i = 0; bookmarks.length < NUM_BOOKMARKS + NUM_SEPARATORS; i++) { michael@0: let randIndex = Math.round(MIN_RAND + (Math.random() * (MAX_RAND - MIN_RAND))); michael@0: try { michael@0: let id = bs.insertSeparator(bs.unfiledBookmarksFolder, randIndex); michael@0: if (randIndex < -1) michael@0: do_throw("Creating a separator at an invalid index should throw"); michael@0: bookmarks.push(id); michael@0: } michael@0: catch (ex) { michael@0: if (randIndex >= -1) michael@0: do_throw("Creating a separator at a valid index should not throw"); michael@0: } michael@0: } michael@0: check_contiguous_indexes(bookmarks); michael@0: michael@0: // Insert folders with random indexes. michael@0: for (let i = 0; bookmarks.length < NUM_ITEMS; i++) { michael@0: let randIndex = Math.round(MIN_RAND + (Math.random() * (MAX_RAND - MIN_RAND))); michael@0: try { michael@0: let id = bs.createFolder(bs.unfiledBookmarksFolder, michael@0: "Test folder " + i, randIndex); michael@0: if (randIndex < -1) michael@0: do_throw("Creating a folder at an invalid index should throw"); michael@0: bookmarks.push(id); michael@0: } michael@0: catch (ex) { michael@0: if (randIndex >= -1) michael@0: do_throw("Creating a folder at a valid index should not throw"); michael@0: } michael@0: } michael@0: check_contiguous_indexes(bookmarks); michael@0: michael@0: // Execute some random bookmark delete. michael@0: for (let i = 0; i < Math.ceil(NUM_ITEMS / 4); i++) { michael@0: let id = bookmarks.splice(Math.floor(Math.random() * bookmarks.length), 1); michael@0: dump("Removing item with id " + id + "\n"); michael@0: bs.removeItem(id); michael@0: } michael@0: check_contiguous_indexes(bookmarks); michael@0: michael@0: // Execute some random bookmark move. This will also try to move it to michael@0: // invalid index values. michael@0: for (let i = 0; i < Math.ceil(NUM_ITEMS / 4); i++) { michael@0: let randIndex = Math.floor(Math.random() * bookmarks.length); michael@0: let id = bookmarks[randIndex]; michael@0: let newIndex = Math.round(MIN_RAND + (Math.random() * (MAX_RAND - MIN_RAND))); michael@0: dump("Moving item with id " + id + " to index " + newIndex + "\n"); michael@0: try { michael@0: bs.moveItem(id, bs.unfiledBookmarksFolder, newIndex); michael@0: if (newIndex < -1) michael@0: do_throw("Moving an item to a negative index should throw\n"); michael@0: } michael@0: catch (ex) { michael@0: if (newIndex >= -1) michael@0: do_throw("Moving an item to a valid index should not throw\n"); michael@0: } michael@0: michael@0: } michael@0: check_contiguous_indexes(bookmarks); michael@0: michael@0: // Ensure setItemIndex throws if we pass it a negative index. michael@0: try { michael@0: bs.setItemIndex(bookmarks[0], -1); michael@0: do_throw("setItemIndex should throw for a negative index"); michael@0: } catch (ex) {} michael@0: // Ensure setItemIndex throws if we pass it a bad itemId. michael@0: try { michael@0: bs.setItemIndex(0, 5); michael@0: do_throw("setItemIndex should throw for a bad itemId"); michael@0: } catch (ex) {} michael@0: }