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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_annotations.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,363 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// Get bookmark service
    1.11 +try {
    1.12 +  var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].getService(Ci.nsINavBookmarksService);
    1.13 +} catch(ex) {
    1.14 +  do_throw("Could not get nav-bookmarks-service\n");
    1.15 +}
    1.16 +
    1.17 +// Get annotation service
    1.18 +try {
    1.19 +  var annosvc= Cc["@mozilla.org/browser/annotation-service;1"].getService(Ci.nsIAnnotationService);
    1.20 +} catch(ex) {
    1.21 +  do_throw("Could not get annotation service\n");
    1.22 +}
    1.23 +
    1.24 +var annoObserver = {
    1.25 +  PAGE_lastSet_URI: "",
    1.26 +  PAGE_lastSet_AnnoName: "",
    1.27 +
    1.28 +  onPageAnnotationSet: function(aURI, aName) {
    1.29 +    this.PAGE_lastSet_URI = aURI.spec;
    1.30 +    this.PAGE_lastSet_AnnoName = aName;
    1.31 +  },
    1.32 +
    1.33 +  ITEM_lastSet_Id: -1,
    1.34 +  ITEM_lastSet_AnnoName: "",
    1.35 +  onItemAnnotationSet: function(aItemId, aName) {
    1.36 +    this.ITEM_lastSet_Id = aItemId;
    1.37 +    this.ITEM_lastSet_AnnoName = aName;
    1.38 +  },
    1.39 +
    1.40 +  PAGE_lastRemoved_URI: "",
    1.41 +  PAGE_lastRemoved_AnnoName: "",
    1.42 +  onPageAnnotationRemoved: function(aURI, aName) {
    1.43 +    this.PAGE_lastRemoved_URI = aURI.spec;
    1.44 +    this.PAGE_lastRemoved_AnnoName = aName;
    1.45 +  },
    1.46 +
    1.47 +  ITEM_lastRemoved_Id: -1,
    1.48 +  ITEM_lastRemoved_AnnoName: "",
    1.49 +  onItemAnnotationRemoved: function(aItemId, aName) {
    1.50 +    this.ITEM_lastRemoved_Id = aItemId;
    1.51 +    this.ITEM_lastRemoved_AnnoName = aName;
    1.52 +  }
    1.53 +};
    1.54 +
    1.55 +// main
    1.56 +function run_test()
    1.57 +{
    1.58 +  run_next_test();
    1.59 +}
    1.60 +
    1.61 +add_task(function test_execute()
    1.62 +{
    1.63 +  var testURI = uri("http://mozilla.com/");
    1.64 +  var testItemId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, testURI, -1, "");
    1.65 +  var testAnnoName = "moz-test-places/annotations";
    1.66 +  var testAnnoVal = "test";
    1.67 +
    1.68 +  annosvc.addObserver(annoObserver);
    1.69 +  // create new string annotation
    1.70 +  try {
    1.71 +    annosvc.setPageAnnotation(testURI, testAnnoName, testAnnoVal, 0, 0);
    1.72 +  } catch(ex) {
    1.73 +    do_throw("unable to add page-annotation");
    1.74 +  }
    1.75 +  do_check_eq(annoObserver.PAGE_lastSet_URI, testURI.spec);
    1.76 +  do_check_eq(annoObserver.PAGE_lastSet_AnnoName, testAnnoName);
    1.77 +
    1.78 +  // get string annotation
    1.79 +  do_check_true(annosvc.pageHasAnnotation(testURI, testAnnoName));
    1.80 +  var storedAnnoVal = annosvc.getPageAnnotation(testURI, testAnnoName);
    1.81 +  do_check_true(testAnnoVal === storedAnnoVal);
    1.82 +  // string item-annotation
    1.83 +  try {
    1.84 +    var lastModified = bmsvc.getItemLastModified(testItemId);
    1.85 +    // Verify that lastModified equals dateAdded before we set the annotation.
    1.86 +    do_check_eq(lastModified, bmsvc.getItemDateAdded(testItemId));
    1.87 +    // Workaround possible VM timers issues moving last modified to the past.
    1.88 +    bmsvc.setItemLastModified(testItemId, --lastModified);
    1.89 +    annosvc.setItemAnnotation(testItemId, testAnnoName, testAnnoVal, 0, 0);
    1.90 +    var lastModified2 = bmsvc.getItemLastModified(testItemId);
    1.91 +    // verify that setting the annotation updates the last modified time
    1.92 +    do_check_true(lastModified2 > lastModified);
    1.93 +  } catch(ex) {
    1.94 +    do_throw("unable to add item annotation");
    1.95 +  }
    1.96 +  do_check_eq(annoObserver.ITEM_lastSet_Id, testItemId);
    1.97 +  do_check_eq(annoObserver.ITEM_lastSet_AnnoName, testAnnoName);
    1.98 +
    1.99 +  try {
   1.100 +    var annoVal = annosvc.getItemAnnotation(testItemId, testAnnoName);
   1.101 +    // verify the anno value
   1.102 +    do_check_true(testAnnoVal === annoVal);
   1.103 +  } catch(ex) {
   1.104 +    do_throw("unable to get item annotation");
   1.105 +  }
   1.106 +
   1.107 +  // test getPagesWithAnnotation
   1.108 +  var uri2 = uri("http://www.tests.tld");
   1.109 +  yield promiseAddVisits(uri2);
   1.110 +  annosvc.setPageAnnotation(uri2, testAnnoName, testAnnoVal, 0, 0);
   1.111 +  var pages = annosvc.getPagesWithAnnotation(testAnnoName);
   1.112 +  do_check_eq(pages.length, 2);
   1.113 +  // Don't rely on the order
   1.114 +  do_check_false(pages[0].equals(pages[1]));
   1.115 +  do_check_true(pages[0].equals(testURI) || pages[1].equals(testURI));
   1.116 +  do_check_true(pages[0].equals(uri2) || pages[1].equals(uri2));
   1.117 +
   1.118 +  // test getItemsWithAnnotation
   1.119 +  var testItemId2 = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, uri2, -1, "");
   1.120 +  annosvc.setItemAnnotation(testItemId2, testAnnoName, testAnnoVal, 0, 0);
   1.121 +  var items = annosvc.getItemsWithAnnotation(testAnnoName);
   1.122 +  do_check_eq(items.length, 2);
   1.123 +  // Don't rely on the order
   1.124 +  do_check_true(items[0] != items[1]);
   1.125 +  do_check_true(items[0] == testItemId || items[1] == testItemId);
   1.126 +  do_check_true(items[0] == testItemId2 || items[1] == testItemId2);
   1.127 +
   1.128 +  // get annotation that doesn't exist
   1.129 +  try {
   1.130 +    annosvc.getPageAnnotation(testURI, "blah");
   1.131 +    do_throw("fetching page-annotation that doesn't exist, should've thrown");
   1.132 +  } catch(ex) {}
   1.133 +  try {
   1.134 +    annosvc.getItemAnnotation(testURI, "blah");
   1.135 +    do_throw("fetching item-annotation that doesn't exist, should've thrown");
   1.136 +  } catch(ex) {}
   1.137 +
   1.138 +  // get annotation info
   1.139 +  var flags = {}, exp = {}, storageType = {};
   1.140 +  annosvc.getPageAnnotationInfo(testURI, testAnnoName, flags, exp, storageType);
   1.141 +  do_check_eq(flags.value, 0);
   1.142 +  do_check_eq(exp.value, 0);
   1.143 +  do_check_eq(storageType.value, Ci.nsIAnnotationService.TYPE_STRING);
   1.144 +  annosvc.getItemAnnotationInfo(testItemId, testAnnoName, flags, exp, storageType);
   1.145 +  do_check_eq(flags.value, 0);
   1.146 +  do_check_eq(exp.value, 0);
   1.147 +  do_check_eq(storageType.value, Ci.nsIAnnotationService.TYPE_STRING);
   1.148 +
   1.149 +  // get annotation names for a uri
   1.150 +  var annoNames = annosvc.getPageAnnotationNames(testURI);
   1.151 +  do_check_eq(annoNames.length, 1);
   1.152 +  do_check_eq(annoNames[0], "moz-test-places/annotations");
   1.153 +
   1.154 +  // get annotation names for an item
   1.155 +  var annoNames = annosvc.getItemAnnotationNames(testItemId);
   1.156 +  do_check_eq(annoNames.length, 1);
   1.157 +  do_check_eq(annoNames[0], "moz-test-places/annotations");
   1.158 +
   1.159 +  // copy annotations to another uri
   1.160 +  var newURI = uri("http://mozilla.org");
   1.161 +  yield promiseAddVisits(newURI);
   1.162 +  annosvc.setPageAnnotation(testURI, "oldAnno", "new", 0, 0);
   1.163 +  annosvc.setPageAnnotation(newURI, "oldAnno", "old", 0, 0);
   1.164 +  var annoNames = annosvc.getPageAnnotationNames(newURI);
   1.165 +  do_check_eq(annoNames.length, 1);
   1.166 +  do_check_eq(annoNames[0], "oldAnno");
   1.167 +  var oldAnnoNames = annosvc.getPageAnnotationNames(testURI);
   1.168 +  do_check_eq(oldAnnoNames.length, 2);
   1.169 +  var copiedAnno = oldAnnoNames[0];
   1.170 +  annosvc.copyPageAnnotations(testURI, newURI, false);
   1.171 +  var newAnnoNames = annosvc.getPageAnnotationNames(newURI);
   1.172 +  do_check_eq(newAnnoNames.length, 2);
   1.173 +  do_check_true(annosvc.pageHasAnnotation(newURI, "oldAnno"));
   1.174 +  do_check_true(annosvc.pageHasAnnotation(newURI, copiedAnno));
   1.175 +  do_check_eq(annosvc.getPageAnnotation(newURI, "oldAnno"), "old");
   1.176 +  annosvc.setPageAnnotation(newURI, "oldAnno", "new", 0, 0);
   1.177 +  annosvc.copyPageAnnotations(testURI, newURI, true);
   1.178 +  newAnnoNames = annosvc.getPageAnnotationNames(newURI);
   1.179 +  do_check_eq(newAnnoNames.length, 2);
   1.180 +  do_check_true(annosvc.pageHasAnnotation(newURI, "oldAnno"));
   1.181 +  do_check_true(annosvc.pageHasAnnotation(newURI, copiedAnno));
   1.182 +  do_check_eq(annosvc.getPageAnnotation(newURI, "oldAnno"), "new");
   1.183 +
   1.184 +
   1.185 +  // copy annotations to another item
   1.186 +  var newURI = uri("http://mozilla.org");
   1.187 +  var newItemId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, newURI, -1, "");
   1.188 +  var itemId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, testURI, -1, "");
   1.189 +  annosvc.setItemAnnotation(itemId, "oldAnno", "new", 0, 0);
   1.190 +  annosvc.setItemAnnotation(itemId, "testAnno", "test", 0, 0);
   1.191 +  annosvc.setItemAnnotation(newItemId, "oldAnno", "old", 0, 0);
   1.192 +  var annoNames = annosvc.getItemAnnotationNames(newItemId);
   1.193 +  do_check_eq(annoNames.length, 1);
   1.194 +  do_check_eq(annoNames[0], "oldAnno");
   1.195 +  var oldAnnoNames = annosvc.getItemAnnotationNames(itemId);
   1.196 +  do_check_eq(oldAnnoNames.length, 2);
   1.197 +  var copiedAnno = oldAnnoNames[0];
   1.198 +  annosvc.copyItemAnnotations(itemId, newItemId, false);
   1.199 +  var newAnnoNames = annosvc.getItemAnnotationNames(newItemId);
   1.200 +  do_check_eq(newAnnoNames.length, 2);
   1.201 +  do_check_true(annosvc.itemHasAnnotation(newItemId, "oldAnno"));
   1.202 +  do_check_true(annosvc.itemHasAnnotation(newItemId, copiedAnno));
   1.203 +  do_check_eq(annosvc.getItemAnnotation(newItemId, "oldAnno"), "old");
   1.204 +  annosvc.setItemAnnotation(newItemId, "oldAnno", "new", 0, 0);
   1.205 +  annosvc.copyItemAnnotations(itemId, newItemId, true);
   1.206 +  newAnnoNames = annosvc.getItemAnnotationNames(newItemId);
   1.207 +  do_check_eq(newAnnoNames.length, 2);
   1.208 +  do_check_true(annosvc.itemHasAnnotation(newItemId, "oldAnno"));
   1.209 +  do_check_true(annosvc.itemHasAnnotation(newItemId, copiedAnno));
   1.210 +  do_check_eq(annosvc.getItemAnnotation(newItemId, "oldAnno"), "new");
   1.211 +
   1.212 +  // test int32 anno type
   1.213 +  var int32Key = testAnnoName + "/types/Int32";
   1.214 +  var int32Val = 23;
   1.215 +  annosvc.setPageAnnotation(testURI, int32Key, int32Val, 0, 0);
   1.216 +  do_check_true(annosvc.pageHasAnnotation(testURI, int32Key));
   1.217 +  var flags = {}, exp = {}, storageType = {};
   1.218 +  annosvc.getPageAnnotationInfo(testURI, int32Key, flags, exp, storageType);
   1.219 +  do_check_eq(flags.value, 0);
   1.220 +  do_check_eq(exp.value, 0);
   1.221 +  do_check_eq(storageType.value, Ci.nsIAnnotationService.TYPE_INT32);
   1.222 +  var storedVal = annosvc.getPageAnnotation(testURI, int32Key);
   1.223 +  do_check_true(int32Val === storedVal);
   1.224 +  annosvc.setItemAnnotation(testItemId, int32Key, int32Val, 0, 0);
   1.225 +  do_check_true(annosvc.itemHasAnnotation(testItemId, int32Key));
   1.226 +  annosvc.getItemAnnotationInfo(testItemId, int32Key, flags, exp, storageType);
   1.227 +  do_check_eq(flags.value, 0);
   1.228 +  do_check_eq(exp.value, 0);
   1.229 +  storedVal = annosvc.getItemAnnotation(testItemId, int32Key);
   1.230 +  do_check_true(int32Val === storedVal);
   1.231 +
   1.232 +  // test int64 anno type
   1.233 +  var int64Key = testAnnoName + "/types/Int64";
   1.234 +  var int64Val = 4294967296;
   1.235 +  annosvc.setPageAnnotation(testURI, int64Key, int64Val, 0, 0);
   1.236 +  annosvc.getPageAnnotationInfo(testURI, int64Key, flags, exp, storageType);
   1.237 +  do_check_eq(flags.value, 0);
   1.238 +  do_check_eq(exp.value, 0);
   1.239 +  storedVal = annosvc.getPageAnnotation(testURI, int64Key);
   1.240 +  do_check_true(int64Val === storedVal);
   1.241 +  annosvc.setItemAnnotation(testItemId, int64Key, int64Val, 0, 0);
   1.242 +  do_check_true(annosvc.itemHasAnnotation(testItemId, int64Key));
   1.243 +  annosvc.getItemAnnotationInfo(testItemId, int64Key, flags, exp, storageType);
   1.244 +  do_check_eq(flags.value, 0);
   1.245 +  do_check_eq(exp.value, 0);
   1.246 +  storedVal = annosvc.getItemAnnotation(testItemId, int64Key);
   1.247 +  do_check_true(int64Val === storedVal);
   1.248 +
   1.249 +  // test double anno type
   1.250 +  var doubleKey = testAnnoName + "/types/Double";
   1.251 +  var doubleVal = 0.000002342;
   1.252 +  annosvc.setPageAnnotation(testURI, doubleKey, doubleVal, 0, 0);
   1.253 +  annosvc.getPageAnnotationInfo(testURI, doubleKey, flags, exp, storageType);
   1.254 +  do_check_eq(flags.value, 0);
   1.255 +  do_check_eq(exp.value, 0);
   1.256 +  storedVal = annosvc.getPageAnnotation(testURI, doubleKey);
   1.257 +  do_check_true(doubleVal === storedVal);
   1.258 +  annosvc.setItemAnnotation(testItemId, doubleKey, doubleVal, 0, 0);
   1.259 +  do_check_true(annosvc.itemHasAnnotation(testItemId, doubleKey));
   1.260 +  annosvc.getItemAnnotationInfo(testItemId, doubleKey, flags, exp, storageType);
   1.261 +  do_check_eq(flags.value, 0);
   1.262 +  do_check_eq(exp.value, 0);
   1.263 +  do_check_eq(storageType.value, Ci.nsIAnnotationService.TYPE_DOUBLE);
   1.264 +  storedVal = annosvc.getItemAnnotation(testItemId, doubleKey);
   1.265 +  do_check_true(doubleVal === storedVal);
   1.266 +
   1.267 +  // test annotation removal
   1.268 +  annosvc.removePageAnnotation(testURI, int32Key);
   1.269 +
   1.270 +  annosvc.setItemAnnotation(testItemId, testAnnoName, testAnnoVal, 0, 0);
   1.271 +  // verify that removing an annotation updates the last modified date
   1.272 +  var lastModified3 = bmsvc.getItemLastModified(testItemId);
   1.273 +  // Workaround possible VM timers issues moving last modified to the past.
   1.274 +  bmsvc.setItemLastModified(testItemId, --lastModified3);
   1.275 +  annosvc.removeItemAnnotation(testItemId, int32Key);
   1.276 +  var lastModified4 = bmsvc.getItemLastModified(testItemId);
   1.277 +  LOG("verify that removing an annotation updates the last modified date");
   1.278 +  LOG("lastModified3 = " + lastModified3);
   1.279 +  LOG("lastModified4 = " + lastModified4);
   1.280 +  do_check_true(lastModified4 > lastModified3);
   1.281 +
   1.282 +  do_check_eq(annoObserver.PAGE_lastRemoved_URI, testURI.spec);
   1.283 +  do_check_eq(annoObserver.PAGE_lastRemoved_AnnoName, int32Key);
   1.284 +  do_check_eq(annoObserver.ITEM_lastRemoved_Id, testItemId);
   1.285 +  do_check_eq(annoObserver.ITEM_lastRemoved_AnnoName, int32Key);
   1.286 +
   1.287 +  // test that getItems/PagesWithAnnotation returns an empty array after
   1.288 +  // removing all items/pages which had the annotation set, see bug 380317.
   1.289 +  do_check_eq(annosvc.getItemsWithAnnotation(int32Key).length, 0);
   1.290 +  do_check_eq(annosvc.getPagesWithAnnotation(int32Key).length, 0);
   1.291 +
   1.292 +  // Setting item annotations on invalid item ids should throw
   1.293 +  var invalidIds = [-1, 0, 37643];
   1.294 +  for each (var id in invalidIds) {
   1.295 +    try {
   1.296 +      annosvc.setItemAnnotation(id, "foo", "bar", 0, 0);
   1.297 +      do_throw("setItemAnnotation* should throw for invalid item id: " + id)
   1.298 +    }
   1.299 +    catch(ex) { }
   1.300 +  }
   1.301 +
   1.302 +  // setting an annotation with EXPIRE_HISTORY for an item should throw
   1.303 +  var itemId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, testURI, -1, "");
   1.304 +  try {
   1.305 +    annosvc.setItemAnnotation(itemId, "foo", "bar", 0, annosvc.EXPIRE_WITH_HISTORY);
   1.306 +    do_throw("setting an item annotation with EXPIRE_HISTORY should throw");
   1.307 +  }
   1.308 +  catch(ex) {
   1.309 +  }
   1.310 +
   1.311 +  annosvc.removeObserver(annoObserver);
   1.312 +});
   1.313 +
   1.314 +add_test(function test_getAnnotationsHavingName() {
   1.315 +  let uri = NetUtil.newURI("http://cat.mozilla.org");
   1.316 +  let id = PlacesUtils.bookmarks.insertBookmark(
   1.317 +    PlacesUtils.unfiledBookmarksFolderId, uri,
   1.318 +    PlacesUtils.bookmarks.DEFAULT_INDEX, "cat");
   1.319 +  let fid = PlacesUtils.bookmarks.createFolder(
   1.320 +    PlacesUtils.unfiledBookmarksFolderId, "pillow",
   1.321 +    PlacesUtils.bookmarks.DEFAULT_INDEX);
   1.322 +
   1.323 +  const ANNOS = {
   1.324 +    "int": 7,
   1.325 +    "double": 7.7,
   1.326 +    "string": "seven"
   1.327 +  };
   1.328 +  for (let name in ANNOS) {
   1.329 +    PlacesUtils.annotations.setPageAnnotation(
   1.330 +      uri, name, ANNOS[name], 0,
   1.331 +      PlacesUtils.annotations.EXPIRE_SESSION);
   1.332 +    PlacesUtils.annotations.setItemAnnotation(
   1.333 +      id, name, ANNOS[name], 0,
   1.334 +      PlacesUtils.annotations.EXPIRE_SESSION);
   1.335 +    PlacesUtils.annotations.setItemAnnotation(
   1.336 +      fid, name, ANNOS[name], 0,
   1.337 +      PlacesUtils.annotations.EXPIRE_SESSION);
   1.338 +  }
   1.339 +
   1.340 +  for (let name in ANNOS) {
   1.341 +    let results = PlacesUtils.annotations.getAnnotationsWithName(name);
   1.342 +    do_check_eq(results.length, 3);
   1.343 +
   1.344 +    for (let result of results) {
   1.345 +      do_check_eq(result.annotationName, name);
   1.346 +      do_check_eq(result.annotationValue, ANNOS[name]);
   1.347 +      if (result.uri)
   1.348 +        do_check_true(result.uri.equals(uri));
   1.349 +      else
   1.350 +        do_check_true(result.itemId > 0);
   1.351 +
   1.352 +      if (result.itemId != -1) {
   1.353 +        if (result.uri)
   1.354 +          do_check_eq(result.itemId, id);
   1.355 +        else
   1.356 +          do_check_eq(result.itemId, fid);
   1.357 +        do_check_guid_for_bookmark(result.itemId, result.guid);
   1.358 +      }
   1.359 +      else {
   1.360 +        do_check_guid_for_uri(result.uri, result.guid);
   1.361 +      }
   1.362 +    }
   1.363 +  }
   1.364 +
   1.365 +  run_next_test();
   1.366 +});

mercurial