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