|
1 var gLastFolderAction = ""; |
|
2 var gLastBookmarkAction = ""; |
|
3 var gLastRootAction = ""; |
|
4 |
|
5 function url(spec) { |
|
6 return Services.io.newURI(spec, null, null); |
|
7 } |
|
8 |
|
9 function test() { |
|
10 // Some very basic tests on the tags root |
|
11 var tags = Application.bookmarks.tags; |
|
12 ok(tags, "Check access to bookmark tags root"); |
|
13 ok(!tags.parent, "Check tags parent (should be null)"); |
|
14 |
|
15 //---------------------------------------------- |
|
16 |
|
17 // Some very basic tests on the unfiled root |
|
18 var unfiled = Application.bookmarks.unfiled; |
|
19 ok(unfiled, "Check access to bookmark unfiled root"); |
|
20 ok(!unfiled.parent, "Check unfiled parent (should be null)"); |
|
21 |
|
22 //---------------------------------------------- |
|
23 |
|
24 // Some basic tests on the toolbar root |
|
25 var toolbar = Application.bookmarks.toolbar; |
|
26 ok(toolbar, "Check access to bookmark toolbar root"); |
|
27 ok(!toolbar.parent, "Check toolbar parent (should be null)"); |
|
28 |
|
29 var toolbarKidCount = toolbar.children.length; |
|
30 |
|
31 // test adding folders |
|
32 var testFolderToolbar = toolbar.addFolder("FUEL in Toolbar"); |
|
33 ok(testFolderToolbar, "Check folder creation"); |
|
34 is(testFolderToolbar.type, "folder", "Check 'folder.type' after creation"); |
|
35 ok(testFolderToolbar.parent, "Check parent after folder creation"); |
|
36 |
|
37 toolbarKidCount++; |
|
38 is(toolbar.children.length, toolbarKidCount, "Check toolbar folder child count after adding a child folder"); |
|
39 |
|
40 //---------------------------------------------- |
|
41 |
|
42 // Main testing is done on the bookmarks menu root |
|
43 var root = Application.bookmarks.menu; |
|
44 ok(root, "Check access to bookmark root"); |
|
45 ok(!root.parent, "Check root parent (should be null)"); |
|
46 |
|
47 var rootKidCount = root.children.length; |
|
48 |
|
49 // test adding folders |
|
50 var testFolder = root.addFolder("FUEL"); |
|
51 ok(testFolder, "Check folder creation"); |
|
52 is(testFolder.type, "folder", "Check 'folder.type' after creation"); |
|
53 ok(testFolder.parent, "Check parent after folder creation"); |
|
54 |
|
55 rootKidCount++; |
|
56 is(root.children.length, rootKidCount, "Check root folder child count after adding a child folder"); |
|
57 |
|
58 // test modifying a folder |
|
59 testFolder.events.addListener("change", onFolderChange); |
|
60 testFolder.description = "FUEL folder"; |
|
61 is(testFolder.description, "FUEL folder", "Check setting 'folder.description'"); |
|
62 is(gLastFolderAction, "bookmarkProperties/description", "Check event handler for setting 'folder.description'"); |
|
63 |
|
64 testFolder.title = "fuel-is-cool"; |
|
65 is(testFolder.title, "fuel-is-cool", "Check setting 'folder.title'"); |
|
66 is(gLastFolderAction, "title", "Check event handler for setting 'folder.title'"); |
|
67 |
|
68 testFolder.annotations.set("testing/folder", "annotate-this", 0); |
|
69 ok(testFolder.annotations.has("testing/folder"), "Checking existence of added annotation"); |
|
70 is(gLastFolderAction, "testing/folder", "Check event handler for setting annotation"); |
|
71 gLastFolderAction = ""; |
|
72 is(testFolder.annotations.get("testing/folder"), "annotate-this", "Checking existence of added annotation"); |
|
73 testFolder.annotations.remove("testing/folder"); |
|
74 ok(!testFolder.annotations.has("testing/folder"), "Checking existence of removed annotation"); |
|
75 is(gLastFolderAction, "testing/folder", "Check event handler for removing annotation"); |
|
76 |
|
77 testFolder.events.addListener("addchild", onFolderAddChild); |
|
78 testFolder.events.addListener("removechild", onFolderRemoveChild); |
|
79 |
|
80 // test adding a bookmark |
|
81 var testBookmark = testFolder.addBookmark("Mozilla", url("https://www.mozilla.org/")); |
|
82 ok(testBookmark, "Check bookmark creation"); |
|
83 ok(testBookmark.parent, "Check parent after bookmark creation"); |
|
84 is(gLastFolderAction, "addchild", "Check event handler for adding a child to a folder"); |
|
85 is(testBookmark.type, "bookmark", "Check 'bookmark.type' after creation"); |
|
86 is(testBookmark.title, "Mozilla", "Check 'bookmark.title' after creation"); |
|
87 is(testBookmark.uri.spec, "https://www.mozilla.org/", "Check 'bookmark.uri' after creation"); |
|
88 |
|
89 is(testFolder.children.length, 1, "Check test folder child count after adding a child bookmark"); |
|
90 |
|
91 // test modifying a bookmark |
|
92 testBookmark.events.addListener("change", onBookmarkChange); |
|
93 testBookmark.description = "mozcorp"; |
|
94 is(testBookmark.description, "mozcorp", "Check setting 'bookmark.description'"); |
|
95 is(gLastBookmarkAction, "bookmarkProperties/description", "Check event handler for setting 'bookmark.description'"); |
|
96 |
|
97 testBookmark.keyword = "moz" |
|
98 is(testBookmark.keyword, "moz", "Check setting 'bookmark.keyword'"); |
|
99 is(gLastBookmarkAction, "keyword", "Check event handler for setting 'bookmark.keyword'"); |
|
100 |
|
101 testBookmark.title = "MozCorp" |
|
102 is(testBookmark.title, "MozCorp", "Check setting 'bookmark.title'"); |
|
103 is(gLastBookmarkAction, "title", "Check event handler for setting 'bookmark.title'"); |
|
104 |
|
105 testBookmark.uri = url("http://www.mozilla.org/"); |
|
106 is(testBookmark.uri.spec, "http://www.mozilla.org/", "Check setting 'bookmark.uri'"); |
|
107 is(gLastBookmarkAction, "uri", "Check event handler for setting 'bookmark.uri'"); |
|
108 |
|
109 // test adding and removing a bookmark annotation |
|
110 testBookmark.annotations.set("testing/bookmark", "annotate-this", 0); |
|
111 ok(testBookmark.annotations.has("testing/bookmark"), "Checking existence of added annotation"); |
|
112 is(gLastBookmarkAction, "testing/bookmark", "Check event handler for setting annotation"); |
|
113 gLastBookmarkAction = ""; |
|
114 is(testBookmark.annotations.get("testing/bookmark"), "annotate-this", "Checking existence of added annotation"); |
|
115 testBookmark.annotations.remove("testing/bookmark"); |
|
116 ok(!testBookmark.annotations.has("testing/bookmark"), "Checking existence of removed annotation"); |
|
117 is(gLastBookmarkAction, "testing/bookmark", "Check event handler for removing annotation"); |
|
118 is(testBookmark.annotations.get("testing/bookmark"), null, "Check existence of a missing annotation"); |
|
119 |
|
120 // quick annotation type tests |
|
121 testBookmark.annotations.set("testing/bookmark/string", "annotate-this", 0); |
|
122 ok(testBookmark.annotations.has("testing/bookmark/string"), "Checking existence of added string annotation"); |
|
123 is(testBookmark.annotations.get("testing/bookmark/string"), "annotate-this", "Checking value of added string annotation"); |
|
124 is(gLastBookmarkAction, "testing/bookmark/string", "Check event handler for setting annotation"); |
|
125 gLastBookmarkAction = ""; |
|
126 testBookmark.annotations.set("testing/bookmark/int", 100, 0); |
|
127 ok(testBookmark.annotations.has("testing/bookmark/int"), "Checking existence of added integer annotation"); |
|
128 is(testBookmark.annotations.get("testing/bookmark/int"), 100, "Checking value of added integer annotation"); |
|
129 is(gLastBookmarkAction, "testing/bookmark/int", "Check event handler for setting annotation"); |
|
130 gLastBookmarkAction = ""; |
|
131 testBookmark.annotations.set("testing/bookmark/double", 3.333, 0); |
|
132 ok(testBookmark.annotations.has("testing/bookmark/double"), "Checking existence of added double annotation"); |
|
133 is(testBookmark.annotations.get("testing/bookmark/double"), 3.333, "Checking value of added double annotation"); |
|
134 is(gLastBookmarkAction, "testing/bookmark/double", "Check event handler for setting annotation"); |
|
135 gLastBookmarkAction = ""; |
|
136 |
|
137 // test names array - NOTE: "bookmarkProperties/description" is an annotation too |
|
138 var names = testBookmark.annotations.names; |
|
139 ok(names.some(function (f) f == "bookmarkProperties/description"), "Checking for description annotation"); |
|
140 ok(names.some(function (f) f == "testing/bookmark/string"), "Checking for string test annotation"); |
|
141 ok(names.some(function (f) f == "testing/bookmark/int"), "Checking for int test annotation"); |
|
142 ok(names.some(function (f) f == "testing/bookmark/double"), "Checking for double test annotation"); |
|
143 |
|
144 // test adding a separator |
|
145 var testSeparator = testFolder.addSeparator(); |
|
146 ok(testSeparator, "Check bookmark creation"); |
|
147 ok(testSeparator.parent, "Check parent after separator creation"); |
|
148 is(gLastFolderAction, "addchild", "Check event handler for adding a child separator to a folder"); |
|
149 is(testSeparator.type, "separator", "Check 'bookmark.type' after separator creation"); |
|
150 |
|
151 is(testFolder.children.length, 2, "Check test folder child count after adding a child separator"); |
|
152 |
|
153 // test removing separator |
|
154 testSeparator.events.addListener("remove", onBookmarkRemove); |
|
155 testSeparator.remove(); |
|
156 is(gLastBookmarkAction, "remove", "Check event handler for removing separator"); |
|
157 is(gLastFolderAction, "removechild", "Check event handler for removing a child separator from a folder"); |
|
158 is(testFolder.children.length, 1, "Check test folder child count after removing a child separator"); |
|
159 |
|
160 // test removing bookmark |
|
161 testBookmark.events.addListener("remove", onBookmarkRemove); |
|
162 testBookmark.remove(); |
|
163 is(gLastBookmarkAction, "remove", "Check event handler for removing bookmark"); |
|
164 is(gLastFolderAction, "removechild", "Check event handler for removing a child from a folder"); |
|
165 is(testFolder.children.length, 0, "Check test folder child count after removing a child bookmark"); |
|
166 |
|
167 // test removing a folder |
|
168 testFolder.events.addListener("remove", onFolderRemove); |
|
169 testFolder.remove(); |
|
170 is(gLastFolderAction, "remove", "Check event handler for removing child folder"); |
|
171 rootKidCount--; |
|
172 is(root.children.length, rootKidCount, "Check root folder child count after removing a child folder"); |
|
173 |
|
174 // test moving between folders |
|
175 var testFolderA = root.addFolder("folder-a"); |
|
176 var testFolderB = root.addFolder("folder-b"); |
|
177 |
|
178 var testMove = testFolderA.addBookmark("Mozilla", url("https://www.mozilla.org/")); |
|
179 testMove.events.addListener("move", onBookmarkMove); |
|
180 is(testMove.parent.title, "folder-a", "Checking for new parent before moving bookmark"); |
|
181 |
|
182 testMove.parent = testFolderB; |
|
183 is(testMove.parent.title, "folder-b", "Checking for new parent after moving bookmark"); |
|
184 is(gLastBookmarkAction, "move", "Checking for event handler after moving bookmark"); |
|
185 |
|
186 // test moving a folder |
|
187 testFolderA.events.addListener("move", onFolderMove); |
|
188 testFolderA.parent = testFolderB; |
|
189 is(testFolderA.parent.title, "folder-b", "Checking for new parent after moving folder"); |
|
190 is(gLastFolderAction, "move", "Checking for event handler after moving folder"); |
|
191 |
|
192 // test events on the root |
|
193 root.events.addListener("add", onRootAdd); |
|
194 root.events.addListener("remove", onRootRemove); |
|
195 root.events.addListener("change", onRootChange); |
|
196 var testFolderC = root.addFolder("folder-c"); |
|
197 is(gLastRootAction, "add"); |
|
198 |
|
199 root.events.removeListener("add", onRootAdd); |
|
200 gLastRootAction = ""; |
|
201 var testFolderD = root.addFolder("folder-d"); |
|
202 is(gLastRootAction, ""); |
|
203 |
|
204 testFolderC.remove(); |
|
205 is(gLastRootAction, "remove"); |
|
206 |
|
207 testFolderD.description = "Foo"; |
|
208 is(gLastRootAction, "bookmarkProperties/description"); |
|
209 } |
|
210 |
|
211 function onFolderChange(evt) { |
|
212 gLastFolderAction = evt.data; |
|
213 } |
|
214 |
|
215 function onFolderRemove(evt) { |
|
216 gLastFolderAction = evt.type; |
|
217 } |
|
218 |
|
219 function onFolderAddChild(evt) { |
|
220 gLastFolderAction = evt.type; |
|
221 } |
|
222 |
|
223 function onFolderRemoveChild(evt) { |
|
224 gLastFolderAction = evt.type; |
|
225 } |
|
226 |
|
227 function onFolderMove(evt) { |
|
228 gLastFolderAction = evt.type; |
|
229 } |
|
230 |
|
231 function onBookmarkChange(evt) { |
|
232 gLastBookmarkAction = evt.data; |
|
233 } |
|
234 |
|
235 function onBookmarkRemove(evt) { |
|
236 gLastBookmarkAction = evt.type; |
|
237 } |
|
238 |
|
239 function onBookmarkMove(evt) { |
|
240 gLastBookmarkAction = evt.type; |
|
241 } |
|
242 |
|
243 function onRootAdd(evt) { |
|
244 gLastRootAction = evt.type; |
|
245 } |
|
246 |
|
247 function onRootRemove(evt) { |
|
248 gLastRootAction = evt.type; |
|
249 } |
|
250 |
|
251 function onRootChange(evt) { |
|
252 gLastRootAction = evt.data; |
|
253 } |