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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial