toolkit/components/places/tests/unit/test_annotations.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:7cb972a31d68
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
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 // Get bookmark service
8 try {
9 var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].getService(Ci.nsINavBookmarksService);
10 } catch(ex) {
11 do_throw("Could not get nav-bookmarks-service\n");
12 }
13
14 // Get annotation service
15 try {
16 var annosvc= Cc["@mozilla.org/browser/annotation-service;1"].getService(Ci.nsIAnnotationService);
17 } catch(ex) {
18 do_throw("Could not get annotation service\n");
19 }
20
21 var annoObserver = {
22 PAGE_lastSet_URI: "",
23 PAGE_lastSet_AnnoName: "",
24
25 onPageAnnotationSet: function(aURI, aName) {
26 this.PAGE_lastSet_URI = aURI.spec;
27 this.PAGE_lastSet_AnnoName = aName;
28 },
29
30 ITEM_lastSet_Id: -1,
31 ITEM_lastSet_AnnoName: "",
32 onItemAnnotationSet: function(aItemId, aName) {
33 this.ITEM_lastSet_Id = aItemId;
34 this.ITEM_lastSet_AnnoName = aName;
35 },
36
37 PAGE_lastRemoved_URI: "",
38 PAGE_lastRemoved_AnnoName: "",
39 onPageAnnotationRemoved: function(aURI, aName) {
40 this.PAGE_lastRemoved_URI = aURI.spec;
41 this.PAGE_lastRemoved_AnnoName = aName;
42 },
43
44 ITEM_lastRemoved_Id: -1,
45 ITEM_lastRemoved_AnnoName: "",
46 onItemAnnotationRemoved: function(aItemId, aName) {
47 this.ITEM_lastRemoved_Id = aItemId;
48 this.ITEM_lastRemoved_AnnoName = aName;
49 }
50 };
51
52 // main
53 function run_test()
54 {
55 run_next_test();
56 }
57
58 add_task(function test_execute()
59 {
60 var testURI = uri("http://mozilla.com/");
61 var testItemId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, testURI, -1, "");
62 var testAnnoName = "moz-test-places/annotations";
63 var testAnnoVal = "test";
64
65 annosvc.addObserver(annoObserver);
66 // create new string annotation
67 try {
68 annosvc.setPageAnnotation(testURI, testAnnoName, testAnnoVal, 0, 0);
69 } catch(ex) {
70 do_throw("unable to add page-annotation");
71 }
72 do_check_eq(annoObserver.PAGE_lastSet_URI, testURI.spec);
73 do_check_eq(annoObserver.PAGE_lastSet_AnnoName, testAnnoName);
74
75 // get string annotation
76 do_check_true(annosvc.pageHasAnnotation(testURI, testAnnoName));
77 var storedAnnoVal = annosvc.getPageAnnotation(testURI, testAnnoName);
78 do_check_true(testAnnoVal === storedAnnoVal);
79 // string item-annotation
80 try {
81 var lastModified = bmsvc.getItemLastModified(testItemId);
82 // Verify that lastModified equals dateAdded before we set the annotation.
83 do_check_eq(lastModified, bmsvc.getItemDateAdded(testItemId));
84 // Workaround possible VM timers issues moving last modified to the past.
85 bmsvc.setItemLastModified(testItemId, --lastModified);
86 annosvc.setItemAnnotation(testItemId, testAnnoName, testAnnoVal, 0, 0);
87 var lastModified2 = bmsvc.getItemLastModified(testItemId);
88 // verify that setting the annotation updates the last modified time
89 do_check_true(lastModified2 > lastModified);
90 } catch(ex) {
91 do_throw("unable to add item annotation");
92 }
93 do_check_eq(annoObserver.ITEM_lastSet_Id, testItemId);
94 do_check_eq(annoObserver.ITEM_lastSet_AnnoName, testAnnoName);
95
96 try {
97 var annoVal = annosvc.getItemAnnotation(testItemId, testAnnoName);
98 // verify the anno value
99 do_check_true(testAnnoVal === annoVal);
100 } catch(ex) {
101 do_throw("unable to get item annotation");
102 }
103
104 // test getPagesWithAnnotation
105 var uri2 = uri("http://www.tests.tld");
106 yield promiseAddVisits(uri2);
107 annosvc.setPageAnnotation(uri2, testAnnoName, testAnnoVal, 0, 0);
108 var pages = annosvc.getPagesWithAnnotation(testAnnoName);
109 do_check_eq(pages.length, 2);
110 // Don't rely on the order
111 do_check_false(pages[0].equals(pages[1]));
112 do_check_true(pages[0].equals(testURI) || pages[1].equals(testURI));
113 do_check_true(pages[0].equals(uri2) || pages[1].equals(uri2));
114
115 // test getItemsWithAnnotation
116 var testItemId2 = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, uri2, -1, "");
117 annosvc.setItemAnnotation(testItemId2, testAnnoName, testAnnoVal, 0, 0);
118 var items = annosvc.getItemsWithAnnotation(testAnnoName);
119 do_check_eq(items.length, 2);
120 // Don't rely on the order
121 do_check_true(items[0] != items[1]);
122 do_check_true(items[0] == testItemId || items[1] == testItemId);
123 do_check_true(items[0] == testItemId2 || items[1] == testItemId2);
124
125 // get annotation that doesn't exist
126 try {
127 annosvc.getPageAnnotation(testURI, "blah");
128 do_throw("fetching page-annotation that doesn't exist, should've thrown");
129 } catch(ex) {}
130 try {
131 annosvc.getItemAnnotation(testURI, "blah");
132 do_throw("fetching item-annotation that doesn't exist, should've thrown");
133 } catch(ex) {}
134
135 // get annotation info
136 var flags = {}, exp = {}, storageType = {};
137 annosvc.getPageAnnotationInfo(testURI, testAnnoName, flags, exp, storageType);
138 do_check_eq(flags.value, 0);
139 do_check_eq(exp.value, 0);
140 do_check_eq(storageType.value, Ci.nsIAnnotationService.TYPE_STRING);
141 annosvc.getItemAnnotationInfo(testItemId, testAnnoName, flags, exp, storageType);
142 do_check_eq(flags.value, 0);
143 do_check_eq(exp.value, 0);
144 do_check_eq(storageType.value, Ci.nsIAnnotationService.TYPE_STRING);
145
146 // get annotation names for a uri
147 var annoNames = annosvc.getPageAnnotationNames(testURI);
148 do_check_eq(annoNames.length, 1);
149 do_check_eq(annoNames[0], "moz-test-places/annotations");
150
151 // get annotation names for an item
152 var annoNames = annosvc.getItemAnnotationNames(testItemId);
153 do_check_eq(annoNames.length, 1);
154 do_check_eq(annoNames[0], "moz-test-places/annotations");
155
156 // copy annotations to another uri
157 var newURI = uri("http://mozilla.org");
158 yield promiseAddVisits(newURI);
159 annosvc.setPageAnnotation(testURI, "oldAnno", "new", 0, 0);
160 annosvc.setPageAnnotation(newURI, "oldAnno", "old", 0, 0);
161 var annoNames = annosvc.getPageAnnotationNames(newURI);
162 do_check_eq(annoNames.length, 1);
163 do_check_eq(annoNames[0], "oldAnno");
164 var oldAnnoNames = annosvc.getPageAnnotationNames(testURI);
165 do_check_eq(oldAnnoNames.length, 2);
166 var copiedAnno = oldAnnoNames[0];
167 annosvc.copyPageAnnotations(testURI, newURI, false);
168 var newAnnoNames = annosvc.getPageAnnotationNames(newURI);
169 do_check_eq(newAnnoNames.length, 2);
170 do_check_true(annosvc.pageHasAnnotation(newURI, "oldAnno"));
171 do_check_true(annosvc.pageHasAnnotation(newURI, copiedAnno));
172 do_check_eq(annosvc.getPageAnnotation(newURI, "oldAnno"), "old");
173 annosvc.setPageAnnotation(newURI, "oldAnno", "new", 0, 0);
174 annosvc.copyPageAnnotations(testURI, newURI, true);
175 newAnnoNames = annosvc.getPageAnnotationNames(newURI);
176 do_check_eq(newAnnoNames.length, 2);
177 do_check_true(annosvc.pageHasAnnotation(newURI, "oldAnno"));
178 do_check_true(annosvc.pageHasAnnotation(newURI, copiedAnno));
179 do_check_eq(annosvc.getPageAnnotation(newURI, "oldAnno"), "new");
180
181
182 // copy annotations to another item
183 var newURI = uri("http://mozilla.org");
184 var newItemId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, newURI, -1, "");
185 var itemId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, testURI, -1, "");
186 annosvc.setItemAnnotation(itemId, "oldAnno", "new", 0, 0);
187 annosvc.setItemAnnotation(itemId, "testAnno", "test", 0, 0);
188 annosvc.setItemAnnotation(newItemId, "oldAnno", "old", 0, 0);
189 var annoNames = annosvc.getItemAnnotationNames(newItemId);
190 do_check_eq(annoNames.length, 1);
191 do_check_eq(annoNames[0], "oldAnno");
192 var oldAnnoNames = annosvc.getItemAnnotationNames(itemId);
193 do_check_eq(oldAnnoNames.length, 2);
194 var copiedAnno = oldAnnoNames[0];
195 annosvc.copyItemAnnotations(itemId, newItemId, false);
196 var newAnnoNames = annosvc.getItemAnnotationNames(newItemId);
197 do_check_eq(newAnnoNames.length, 2);
198 do_check_true(annosvc.itemHasAnnotation(newItemId, "oldAnno"));
199 do_check_true(annosvc.itemHasAnnotation(newItemId, copiedAnno));
200 do_check_eq(annosvc.getItemAnnotation(newItemId, "oldAnno"), "old");
201 annosvc.setItemAnnotation(newItemId, "oldAnno", "new", 0, 0);
202 annosvc.copyItemAnnotations(itemId, newItemId, true);
203 newAnnoNames = annosvc.getItemAnnotationNames(newItemId);
204 do_check_eq(newAnnoNames.length, 2);
205 do_check_true(annosvc.itemHasAnnotation(newItemId, "oldAnno"));
206 do_check_true(annosvc.itemHasAnnotation(newItemId, copiedAnno));
207 do_check_eq(annosvc.getItemAnnotation(newItemId, "oldAnno"), "new");
208
209 // test int32 anno type
210 var int32Key = testAnnoName + "/types/Int32";
211 var int32Val = 23;
212 annosvc.setPageAnnotation(testURI, int32Key, int32Val, 0, 0);
213 do_check_true(annosvc.pageHasAnnotation(testURI, int32Key));
214 var flags = {}, exp = {}, storageType = {};
215 annosvc.getPageAnnotationInfo(testURI, int32Key, flags, exp, storageType);
216 do_check_eq(flags.value, 0);
217 do_check_eq(exp.value, 0);
218 do_check_eq(storageType.value, Ci.nsIAnnotationService.TYPE_INT32);
219 var storedVal = annosvc.getPageAnnotation(testURI, int32Key);
220 do_check_true(int32Val === storedVal);
221 annosvc.setItemAnnotation(testItemId, int32Key, int32Val, 0, 0);
222 do_check_true(annosvc.itemHasAnnotation(testItemId, int32Key));
223 annosvc.getItemAnnotationInfo(testItemId, int32Key, flags, exp, storageType);
224 do_check_eq(flags.value, 0);
225 do_check_eq(exp.value, 0);
226 storedVal = annosvc.getItemAnnotation(testItemId, int32Key);
227 do_check_true(int32Val === storedVal);
228
229 // test int64 anno type
230 var int64Key = testAnnoName + "/types/Int64";
231 var int64Val = 4294967296;
232 annosvc.setPageAnnotation(testURI, int64Key, int64Val, 0, 0);
233 annosvc.getPageAnnotationInfo(testURI, int64Key, flags, exp, storageType);
234 do_check_eq(flags.value, 0);
235 do_check_eq(exp.value, 0);
236 storedVal = annosvc.getPageAnnotation(testURI, int64Key);
237 do_check_true(int64Val === storedVal);
238 annosvc.setItemAnnotation(testItemId, int64Key, int64Val, 0, 0);
239 do_check_true(annosvc.itemHasAnnotation(testItemId, int64Key));
240 annosvc.getItemAnnotationInfo(testItemId, int64Key, flags, exp, storageType);
241 do_check_eq(flags.value, 0);
242 do_check_eq(exp.value, 0);
243 storedVal = annosvc.getItemAnnotation(testItemId, int64Key);
244 do_check_true(int64Val === storedVal);
245
246 // test double anno type
247 var doubleKey = testAnnoName + "/types/Double";
248 var doubleVal = 0.000002342;
249 annosvc.setPageAnnotation(testURI, doubleKey, doubleVal, 0, 0);
250 annosvc.getPageAnnotationInfo(testURI, doubleKey, flags, exp, storageType);
251 do_check_eq(flags.value, 0);
252 do_check_eq(exp.value, 0);
253 storedVal = annosvc.getPageAnnotation(testURI, doubleKey);
254 do_check_true(doubleVal === storedVal);
255 annosvc.setItemAnnotation(testItemId, doubleKey, doubleVal, 0, 0);
256 do_check_true(annosvc.itemHasAnnotation(testItemId, doubleKey));
257 annosvc.getItemAnnotationInfo(testItemId, doubleKey, flags, exp, storageType);
258 do_check_eq(flags.value, 0);
259 do_check_eq(exp.value, 0);
260 do_check_eq(storageType.value, Ci.nsIAnnotationService.TYPE_DOUBLE);
261 storedVal = annosvc.getItemAnnotation(testItemId, doubleKey);
262 do_check_true(doubleVal === storedVal);
263
264 // test annotation removal
265 annosvc.removePageAnnotation(testURI, int32Key);
266
267 annosvc.setItemAnnotation(testItemId, testAnnoName, testAnnoVal, 0, 0);
268 // verify that removing an annotation updates the last modified date
269 var lastModified3 = bmsvc.getItemLastModified(testItemId);
270 // Workaround possible VM timers issues moving last modified to the past.
271 bmsvc.setItemLastModified(testItemId, --lastModified3);
272 annosvc.removeItemAnnotation(testItemId, int32Key);
273 var lastModified4 = bmsvc.getItemLastModified(testItemId);
274 LOG("verify that removing an annotation updates the last modified date");
275 LOG("lastModified3 = " + lastModified3);
276 LOG("lastModified4 = " + lastModified4);
277 do_check_true(lastModified4 > lastModified3);
278
279 do_check_eq(annoObserver.PAGE_lastRemoved_URI, testURI.spec);
280 do_check_eq(annoObserver.PAGE_lastRemoved_AnnoName, int32Key);
281 do_check_eq(annoObserver.ITEM_lastRemoved_Id, testItemId);
282 do_check_eq(annoObserver.ITEM_lastRemoved_AnnoName, int32Key);
283
284 // test that getItems/PagesWithAnnotation returns an empty array after
285 // removing all items/pages which had the annotation set, see bug 380317.
286 do_check_eq(annosvc.getItemsWithAnnotation(int32Key).length, 0);
287 do_check_eq(annosvc.getPagesWithAnnotation(int32Key).length, 0);
288
289 // Setting item annotations on invalid item ids should throw
290 var invalidIds = [-1, 0, 37643];
291 for each (var id in invalidIds) {
292 try {
293 annosvc.setItemAnnotation(id, "foo", "bar", 0, 0);
294 do_throw("setItemAnnotation* should throw for invalid item id: " + id)
295 }
296 catch(ex) { }
297 }
298
299 // setting an annotation with EXPIRE_HISTORY for an item should throw
300 var itemId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, testURI, -1, "");
301 try {
302 annosvc.setItemAnnotation(itemId, "foo", "bar", 0, annosvc.EXPIRE_WITH_HISTORY);
303 do_throw("setting an item annotation with EXPIRE_HISTORY should throw");
304 }
305 catch(ex) {
306 }
307
308 annosvc.removeObserver(annoObserver);
309 });
310
311 add_test(function test_getAnnotationsHavingName() {
312 let uri = NetUtil.newURI("http://cat.mozilla.org");
313 let id = PlacesUtils.bookmarks.insertBookmark(
314 PlacesUtils.unfiledBookmarksFolderId, uri,
315 PlacesUtils.bookmarks.DEFAULT_INDEX, "cat");
316 let fid = PlacesUtils.bookmarks.createFolder(
317 PlacesUtils.unfiledBookmarksFolderId, "pillow",
318 PlacesUtils.bookmarks.DEFAULT_INDEX);
319
320 const ANNOS = {
321 "int": 7,
322 "double": 7.7,
323 "string": "seven"
324 };
325 for (let name in ANNOS) {
326 PlacesUtils.annotations.setPageAnnotation(
327 uri, name, ANNOS[name], 0,
328 PlacesUtils.annotations.EXPIRE_SESSION);
329 PlacesUtils.annotations.setItemAnnotation(
330 id, name, ANNOS[name], 0,
331 PlacesUtils.annotations.EXPIRE_SESSION);
332 PlacesUtils.annotations.setItemAnnotation(
333 fid, name, ANNOS[name], 0,
334 PlacesUtils.annotations.EXPIRE_SESSION);
335 }
336
337 for (let name in ANNOS) {
338 let results = PlacesUtils.annotations.getAnnotationsWithName(name);
339 do_check_eq(results.length, 3);
340
341 for (let result of results) {
342 do_check_eq(result.annotationName, name);
343 do_check_eq(result.annotationValue, ANNOS[name]);
344 if (result.uri)
345 do_check_true(result.uri.equals(uri));
346 else
347 do_check_true(result.itemId > 0);
348
349 if (result.itemId != -1) {
350 if (result.uri)
351 do_check_eq(result.itemId, id);
352 else
353 do_check_eq(result.itemId, fid);
354 do_check_guid_for_bookmark(result.itemId, result.guid);
355 }
356 else {
357 do_check_guid_for_uri(result.uri, result.guid);
358 }
359 }
360 }
361
362 run_next_test();
363 });

mercurial