|
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 /** |
|
8 * Check for correct functionality of PlacesUtils.setAnnotationsForItem/URI |
|
9 */ |
|
10 |
|
11 var hs = PlacesUtils.history; |
|
12 var bs = PlacesUtils.bookmarks; |
|
13 var as = PlacesUtils.annotations; |
|
14 |
|
15 const TEST_URL = "http://test.mozilla.org/"; |
|
16 |
|
17 function run_test() { |
|
18 var testURI = uri(TEST_URL); |
|
19 // add a bookmark |
|
20 var itemId = bs.insertBookmark(bs.unfiledBookmarksFolder, testURI, |
|
21 bs.DEFAULT_INDEX, "test"); |
|
22 |
|
23 // create annotations array |
|
24 var testAnnos = [{ name: "testAnno/test0", |
|
25 flags: 0, |
|
26 value: "test0", |
|
27 expires: Ci.nsIAnnotationService.EXPIRE_NEVER }, |
|
28 { name: "testAnno/test1", |
|
29 flags: 0, |
|
30 value: "test1", |
|
31 expires: Ci.nsIAnnotationService.EXPIRE_NEVER }, |
|
32 { name: "testAnno/test2", |
|
33 flags: 0, |
|
34 value: "test2", |
|
35 expires: Ci.nsIAnnotationService.EXPIRE_NEVER }, |
|
36 { name: "testAnno/test3", |
|
37 flags: 0, |
|
38 value: 0, |
|
39 expires: Ci.nsIAnnotationService.EXPIRE_NEVER }]; |
|
40 |
|
41 // Add item annotations |
|
42 PlacesUtils.setAnnotationsForItem(itemId, testAnnos); |
|
43 // Check for correct addition |
|
44 testAnnos.forEach(function(anno) { |
|
45 do_check_true(as.itemHasAnnotation(itemId, anno.name)); |
|
46 do_check_eq(as.getItemAnnotation(itemId, anno.name), anno.value); |
|
47 }); |
|
48 |
|
49 // Add page annotations |
|
50 PlacesUtils.setAnnotationsForURI(testURI, testAnnos); |
|
51 // Check for correct addition |
|
52 testAnnos.forEach(function(anno) { |
|
53 do_check_true(as.pageHasAnnotation(testURI, anno.name)); |
|
54 do_check_eq(as.getPageAnnotation(testURI, anno.name), anno.value); |
|
55 }); |
|
56 |
|
57 // To unset annotations we unset their values or set them to |
|
58 //null/undefined |
|
59 testAnnos[0].value = null; |
|
60 testAnnos[1].value = undefined; |
|
61 delete testAnnos[2].value; |
|
62 delete testAnnos[3].value; |
|
63 |
|
64 // Unset all item annotations |
|
65 PlacesUtils.setAnnotationsForItem(itemId, testAnnos); |
|
66 // Check for correct removal |
|
67 testAnnos.forEach(function(anno) { |
|
68 do_check_false(as.itemHasAnnotation(itemId, anno.name)); |
|
69 // sanity: page annotations should not be removed here |
|
70 do_check_true(as.pageHasAnnotation(testURI, anno.name)); |
|
71 }); |
|
72 |
|
73 // Unset all page annotations |
|
74 PlacesUtils.setAnnotationsForURI(testURI, testAnnos); |
|
75 // Check for correct removal |
|
76 testAnnos.forEach(function(anno) { |
|
77 do_check_false(as.pageHasAnnotation(testURI, anno.name)); |
|
78 }); |
|
79 } |