michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Get bookmark service michael@0: try { michael@0: var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].getService(Ci.nsINavBookmarksService); michael@0: } catch(ex) { michael@0: do_throw("Could not get nav-bookmarks-service\n"); michael@0: } michael@0: michael@0: // Get annotation service michael@0: try { michael@0: var annosvc= Cc["@mozilla.org/browser/annotation-service;1"].getService(Ci.nsIAnnotationService); michael@0: } catch(ex) { michael@0: do_throw("Could not get annotation service\n"); michael@0: } michael@0: michael@0: var annoObserver = { michael@0: PAGE_lastSet_URI: "", michael@0: PAGE_lastSet_AnnoName: "", michael@0: michael@0: onPageAnnotationSet: function(aURI, aName) { michael@0: this.PAGE_lastSet_URI = aURI.spec; michael@0: this.PAGE_lastSet_AnnoName = aName; michael@0: }, michael@0: michael@0: ITEM_lastSet_Id: -1, michael@0: ITEM_lastSet_AnnoName: "", michael@0: onItemAnnotationSet: function(aItemId, aName) { michael@0: this.ITEM_lastSet_Id = aItemId; michael@0: this.ITEM_lastSet_AnnoName = aName; michael@0: }, michael@0: michael@0: PAGE_lastRemoved_URI: "", michael@0: PAGE_lastRemoved_AnnoName: "", michael@0: onPageAnnotationRemoved: function(aURI, aName) { michael@0: this.PAGE_lastRemoved_URI = aURI.spec; michael@0: this.PAGE_lastRemoved_AnnoName = aName; michael@0: }, michael@0: michael@0: ITEM_lastRemoved_Id: -1, michael@0: ITEM_lastRemoved_AnnoName: "", michael@0: onItemAnnotationRemoved: function(aItemId, aName) { michael@0: this.ITEM_lastRemoved_Id = aItemId; michael@0: this.ITEM_lastRemoved_AnnoName = aName; michael@0: } michael@0: }; michael@0: michael@0: // main michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_execute() michael@0: { michael@0: var testURI = uri("http://mozilla.com/"); michael@0: var testItemId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, testURI, -1, ""); michael@0: var testAnnoName = "moz-test-places/annotations"; michael@0: var testAnnoVal = "test"; michael@0: michael@0: annosvc.addObserver(annoObserver); michael@0: // create new string annotation michael@0: try { michael@0: annosvc.setPageAnnotation(testURI, testAnnoName, testAnnoVal, 0, 0); michael@0: } catch(ex) { michael@0: do_throw("unable to add page-annotation"); michael@0: } michael@0: do_check_eq(annoObserver.PAGE_lastSet_URI, testURI.spec); michael@0: do_check_eq(annoObserver.PAGE_lastSet_AnnoName, testAnnoName); michael@0: michael@0: // get string annotation michael@0: do_check_true(annosvc.pageHasAnnotation(testURI, testAnnoName)); michael@0: var storedAnnoVal = annosvc.getPageAnnotation(testURI, testAnnoName); michael@0: do_check_true(testAnnoVal === storedAnnoVal); michael@0: // string item-annotation michael@0: try { michael@0: var lastModified = bmsvc.getItemLastModified(testItemId); michael@0: // Verify that lastModified equals dateAdded before we set the annotation. michael@0: do_check_eq(lastModified, bmsvc.getItemDateAdded(testItemId)); michael@0: // Workaround possible VM timers issues moving last modified to the past. michael@0: bmsvc.setItemLastModified(testItemId, --lastModified); michael@0: annosvc.setItemAnnotation(testItemId, testAnnoName, testAnnoVal, 0, 0); michael@0: var lastModified2 = bmsvc.getItemLastModified(testItemId); michael@0: // verify that setting the annotation updates the last modified time michael@0: do_check_true(lastModified2 > lastModified); michael@0: } catch(ex) { michael@0: do_throw("unable to add item annotation"); michael@0: } michael@0: do_check_eq(annoObserver.ITEM_lastSet_Id, testItemId); michael@0: do_check_eq(annoObserver.ITEM_lastSet_AnnoName, testAnnoName); michael@0: michael@0: try { michael@0: var annoVal = annosvc.getItemAnnotation(testItemId, testAnnoName); michael@0: // verify the anno value michael@0: do_check_true(testAnnoVal === annoVal); michael@0: } catch(ex) { michael@0: do_throw("unable to get item annotation"); michael@0: } michael@0: michael@0: // test getPagesWithAnnotation michael@0: var uri2 = uri("http://www.tests.tld"); michael@0: yield promiseAddVisits(uri2); michael@0: annosvc.setPageAnnotation(uri2, testAnnoName, testAnnoVal, 0, 0); michael@0: var pages = annosvc.getPagesWithAnnotation(testAnnoName); michael@0: do_check_eq(pages.length, 2); michael@0: // Don't rely on the order michael@0: do_check_false(pages[0].equals(pages[1])); michael@0: do_check_true(pages[0].equals(testURI) || pages[1].equals(testURI)); michael@0: do_check_true(pages[0].equals(uri2) || pages[1].equals(uri2)); michael@0: michael@0: // test getItemsWithAnnotation michael@0: var testItemId2 = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, uri2, -1, ""); michael@0: annosvc.setItemAnnotation(testItemId2, testAnnoName, testAnnoVal, 0, 0); michael@0: var items = annosvc.getItemsWithAnnotation(testAnnoName); michael@0: do_check_eq(items.length, 2); michael@0: // Don't rely on the order michael@0: do_check_true(items[0] != items[1]); michael@0: do_check_true(items[0] == testItemId || items[1] == testItemId); michael@0: do_check_true(items[0] == testItemId2 || items[1] == testItemId2); michael@0: michael@0: // get annotation that doesn't exist michael@0: try { michael@0: annosvc.getPageAnnotation(testURI, "blah"); michael@0: do_throw("fetching page-annotation that doesn't exist, should've thrown"); michael@0: } catch(ex) {} michael@0: try { michael@0: annosvc.getItemAnnotation(testURI, "blah"); michael@0: do_throw("fetching item-annotation that doesn't exist, should've thrown"); michael@0: } catch(ex) {} michael@0: michael@0: // get annotation info michael@0: var flags = {}, exp = {}, storageType = {}; michael@0: annosvc.getPageAnnotationInfo(testURI, testAnnoName, flags, exp, storageType); michael@0: do_check_eq(flags.value, 0); michael@0: do_check_eq(exp.value, 0); michael@0: do_check_eq(storageType.value, Ci.nsIAnnotationService.TYPE_STRING); michael@0: annosvc.getItemAnnotationInfo(testItemId, testAnnoName, flags, exp, storageType); michael@0: do_check_eq(flags.value, 0); michael@0: do_check_eq(exp.value, 0); michael@0: do_check_eq(storageType.value, Ci.nsIAnnotationService.TYPE_STRING); michael@0: michael@0: // get annotation names for a uri michael@0: var annoNames = annosvc.getPageAnnotationNames(testURI); michael@0: do_check_eq(annoNames.length, 1); michael@0: do_check_eq(annoNames[0], "moz-test-places/annotations"); michael@0: michael@0: // get annotation names for an item michael@0: var annoNames = annosvc.getItemAnnotationNames(testItemId); michael@0: do_check_eq(annoNames.length, 1); michael@0: do_check_eq(annoNames[0], "moz-test-places/annotations"); michael@0: michael@0: // copy annotations to another uri michael@0: var newURI = uri("http://mozilla.org"); michael@0: yield promiseAddVisits(newURI); michael@0: annosvc.setPageAnnotation(testURI, "oldAnno", "new", 0, 0); michael@0: annosvc.setPageAnnotation(newURI, "oldAnno", "old", 0, 0); michael@0: var annoNames = annosvc.getPageAnnotationNames(newURI); michael@0: do_check_eq(annoNames.length, 1); michael@0: do_check_eq(annoNames[0], "oldAnno"); michael@0: var oldAnnoNames = annosvc.getPageAnnotationNames(testURI); michael@0: do_check_eq(oldAnnoNames.length, 2); michael@0: var copiedAnno = oldAnnoNames[0]; michael@0: annosvc.copyPageAnnotations(testURI, newURI, false); michael@0: var newAnnoNames = annosvc.getPageAnnotationNames(newURI); michael@0: do_check_eq(newAnnoNames.length, 2); michael@0: do_check_true(annosvc.pageHasAnnotation(newURI, "oldAnno")); michael@0: do_check_true(annosvc.pageHasAnnotation(newURI, copiedAnno)); michael@0: do_check_eq(annosvc.getPageAnnotation(newURI, "oldAnno"), "old"); michael@0: annosvc.setPageAnnotation(newURI, "oldAnno", "new", 0, 0); michael@0: annosvc.copyPageAnnotations(testURI, newURI, true); michael@0: newAnnoNames = annosvc.getPageAnnotationNames(newURI); michael@0: do_check_eq(newAnnoNames.length, 2); michael@0: do_check_true(annosvc.pageHasAnnotation(newURI, "oldAnno")); michael@0: do_check_true(annosvc.pageHasAnnotation(newURI, copiedAnno)); michael@0: do_check_eq(annosvc.getPageAnnotation(newURI, "oldAnno"), "new"); michael@0: michael@0: michael@0: // copy annotations to another item michael@0: var newURI = uri("http://mozilla.org"); michael@0: var newItemId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, newURI, -1, ""); michael@0: var itemId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, testURI, -1, ""); michael@0: annosvc.setItemAnnotation(itemId, "oldAnno", "new", 0, 0); michael@0: annosvc.setItemAnnotation(itemId, "testAnno", "test", 0, 0); michael@0: annosvc.setItemAnnotation(newItemId, "oldAnno", "old", 0, 0); michael@0: var annoNames = annosvc.getItemAnnotationNames(newItemId); michael@0: do_check_eq(annoNames.length, 1); michael@0: do_check_eq(annoNames[0], "oldAnno"); michael@0: var oldAnnoNames = annosvc.getItemAnnotationNames(itemId); michael@0: do_check_eq(oldAnnoNames.length, 2); michael@0: var copiedAnno = oldAnnoNames[0]; michael@0: annosvc.copyItemAnnotations(itemId, newItemId, false); michael@0: var newAnnoNames = annosvc.getItemAnnotationNames(newItemId); michael@0: do_check_eq(newAnnoNames.length, 2); michael@0: do_check_true(annosvc.itemHasAnnotation(newItemId, "oldAnno")); michael@0: do_check_true(annosvc.itemHasAnnotation(newItemId, copiedAnno)); michael@0: do_check_eq(annosvc.getItemAnnotation(newItemId, "oldAnno"), "old"); michael@0: annosvc.setItemAnnotation(newItemId, "oldAnno", "new", 0, 0); michael@0: annosvc.copyItemAnnotations(itemId, newItemId, true); michael@0: newAnnoNames = annosvc.getItemAnnotationNames(newItemId); michael@0: do_check_eq(newAnnoNames.length, 2); michael@0: do_check_true(annosvc.itemHasAnnotation(newItemId, "oldAnno")); michael@0: do_check_true(annosvc.itemHasAnnotation(newItemId, copiedAnno)); michael@0: do_check_eq(annosvc.getItemAnnotation(newItemId, "oldAnno"), "new"); michael@0: michael@0: // test int32 anno type michael@0: var int32Key = testAnnoName + "/types/Int32"; michael@0: var int32Val = 23; michael@0: annosvc.setPageAnnotation(testURI, int32Key, int32Val, 0, 0); michael@0: do_check_true(annosvc.pageHasAnnotation(testURI, int32Key)); michael@0: var flags = {}, exp = {}, storageType = {}; michael@0: annosvc.getPageAnnotationInfo(testURI, int32Key, flags, exp, storageType); michael@0: do_check_eq(flags.value, 0); michael@0: do_check_eq(exp.value, 0); michael@0: do_check_eq(storageType.value, Ci.nsIAnnotationService.TYPE_INT32); michael@0: var storedVal = annosvc.getPageAnnotation(testURI, int32Key); michael@0: do_check_true(int32Val === storedVal); michael@0: annosvc.setItemAnnotation(testItemId, int32Key, int32Val, 0, 0); michael@0: do_check_true(annosvc.itemHasAnnotation(testItemId, int32Key)); michael@0: annosvc.getItemAnnotationInfo(testItemId, int32Key, flags, exp, storageType); michael@0: do_check_eq(flags.value, 0); michael@0: do_check_eq(exp.value, 0); michael@0: storedVal = annosvc.getItemAnnotation(testItemId, int32Key); michael@0: do_check_true(int32Val === storedVal); michael@0: michael@0: // test int64 anno type michael@0: var int64Key = testAnnoName + "/types/Int64"; michael@0: var int64Val = 4294967296; michael@0: annosvc.setPageAnnotation(testURI, int64Key, int64Val, 0, 0); michael@0: annosvc.getPageAnnotationInfo(testURI, int64Key, flags, exp, storageType); michael@0: do_check_eq(flags.value, 0); michael@0: do_check_eq(exp.value, 0); michael@0: storedVal = annosvc.getPageAnnotation(testURI, int64Key); michael@0: do_check_true(int64Val === storedVal); michael@0: annosvc.setItemAnnotation(testItemId, int64Key, int64Val, 0, 0); michael@0: do_check_true(annosvc.itemHasAnnotation(testItemId, int64Key)); michael@0: annosvc.getItemAnnotationInfo(testItemId, int64Key, flags, exp, storageType); michael@0: do_check_eq(flags.value, 0); michael@0: do_check_eq(exp.value, 0); michael@0: storedVal = annosvc.getItemAnnotation(testItemId, int64Key); michael@0: do_check_true(int64Val === storedVal); michael@0: michael@0: // test double anno type michael@0: var doubleKey = testAnnoName + "/types/Double"; michael@0: var doubleVal = 0.000002342; michael@0: annosvc.setPageAnnotation(testURI, doubleKey, doubleVal, 0, 0); michael@0: annosvc.getPageAnnotationInfo(testURI, doubleKey, flags, exp, storageType); michael@0: do_check_eq(flags.value, 0); michael@0: do_check_eq(exp.value, 0); michael@0: storedVal = annosvc.getPageAnnotation(testURI, doubleKey); michael@0: do_check_true(doubleVal === storedVal); michael@0: annosvc.setItemAnnotation(testItemId, doubleKey, doubleVal, 0, 0); michael@0: do_check_true(annosvc.itemHasAnnotation(testItemId, doubleKey)); michael@0: annosvc.getItemAnnotationInfo(testItemId, doubleKey, flags, exp, storageType); michael@0: do_check_eq(flags.value, 0); michael@0: do_check_eq(exp.value, 0); michael@0: do_check_eq(storageType.value, Ci.nsIAnnotationService.TYPE_DOUBLE); michael@0: storedVal = annosvc.getItemAnnotation(testItemId, doubleKey); michael@0: do_check_true(doubleVal === storedVal); michael@0: michael@0: // test annotation removal michael@0: annosvc.removePageAnnotation(testURI, int32Key); michael@0: michael@0: annosvc.setItemAnnotation(testItemId, testAnnoName, testAnnoVal, 0, 0); michael@0: // verify that removing an annotation updates the last modified date michael@0: var lastModified3 = bmsvc.getItemLastModified(testItemId); michael@0: // Workaround possible VM timers issues moving last modified to the past. michael@0: bmsvc.setItemLastModified(testItemId, --lastModified3); michael@0: annosvc.removeItemAnnotation(testItemId, int32Key); michael@0: var lastModified4 = bmsvc.getItemLastModified(testItemId); michael@0: LOG("verify that removing an annotation updates the last modified date"); michael@0: LOG("lastModified3 = " + lastModified3); michael@0: LOG("lastModified4 = " + lastModified4); michael@0: do_check_true(lastModified4 > lastModified3); michael@0: michael@0: do_check_eq(annoObserver.PAGE_lastRemoved_URI, testURI.spec); michael@0: do_check_eq(annoObserver.PAGE_lastRemoved_AnnoName, int32Key); michael@0: do_check_eq(annoObserver.ITEM_lastRemoved_Id, testItemId); michael@0: do_check_eq(annoObserver.ITEM_lastRemoved_AnnoName, int32Key); michael@0: michael@0: // test that getItems/PagesWithAnnotation returns an empty array after michael@0: // removing all items/pages which had the annotation set, see bug 380317. michael@0: do_check_eq(annosvc.getItemsWithAnnotation(int32Key).length, 0); michael@0: do_check_eq(annosvc.getPagesWithAnnotation(int32Key).length, 0); michael@0: michael@0: // Setting item annotations on invalid item ids should throw michael@0: var invalidIds = [-1, 0, 37643]; michael@0: for each (var id in invalidIds) { michael@0: try { michael@0: annosvc.setItemAnnotation(id, "foo", "bar", 0, 0); michael@0: do_throw("setItemAnnotation* should throw for invalid item id: " + id) michael@0: } michael@0: catch(ex) { } michael@0: } michael@0: michael@0: // setting an annotation with EXPIRE_HISTORY for an item should throw michael@0: var itemId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, testURI, -1, ""); michael@0: try { michael@0: annosvc.setItemAnnotation(itemId, "foo", "bar", 0, annosvc.EXPIRE_WITH_HISTORY); michael@0: do_throw("setting an item annotation with EXPIRE_HISTORY should throw"); michael@0: } michael@0: catch(ex) { michael@0: } michael@0: michael@0: annosvc.removeObserver(annoObserver); michael@0: }); michael@0: michael@0: add_test(function test_getAnnotationsHavingName() { michael@0: let uri = NetUtil.newURI("http://cat.mozilla.org"); michael@0: let id = PlacesUtils.bookmarks.insertBookmark( michael@0: PlacesUtils.unfiledBookmarksFolderId, uri, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, "cat"); michael@0: let fid = PlacesUtils.bookmarks.createFolder( michael@0: PlacesUtils.unfiledBookmarksFolderId, "pillow", michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: michael@0: const ANNOS = { michael@0: "int": 7, michael@0: "double": 7.7, michael@0: "string": "seven" michael@0: }; michael@0: for (let name in ANNOS) { michael@0: PlacesUtils.annotations.setPageAnnotation( michael@0: uri, name, ANNOS[name], 0, michael@0: PlacesUtils.annotations.EXPIRE_SESSION); michael@0: PlacesUtils.annotations.setItemAnnotation( michael@0: id, name, ANNOS[name], 0, michael@0: PlacesUtils.annotations.EXPIRE_SESSION); michael@0: PlacesUtils.annotations.setItemAnnotation( michael@0: fid, name, ANNOS[name], 0, michael@0: PlacesUtils.annotations.EXPIRE_SESSION); michael@0: } michael@0: michael@0: for (let name in ANNOS) { michael@0: let results = PlacesUtils.annotations.getAnnotationsWithName(name); michael@0: do_check_eq(results.length, 3); michael@0: michael@0: for (let result of results) { michael@0: do_check_eq(result.annotationName, name); michael@0: do_check_eq(result.annotationValue, ANNOS[name]); michael@0: if (result.uri) michael@0: do_check_true(result.uri.equals(uri)); michael@0: else michael@0: do_check_true(result.itemId > 0); michael@0: michael@0: if (result.itemId != -1) { michael@0: if (result.uri) michael@0: do_check_eq(result.itemId, id); michael@0: else michael@0: do_check_eq(result.itemId, fid); michael@0: do_check_guid_for_bookmark(result.itemId, result.guid); michael@0: } michael@0: else { michael@0: do_check_guid_for_uri(result.uri, result.guid); michael@0: } michael@0: } michael@0: } michael@0: michael@0: run_next_test(); michael@0: });