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: let observer = { michael@0: __proto__: NavBookmarkObserver.prototype, michael@0: michael@0: onItemAdded: function (id, folder, index) { michael@0: this._itemAddedId = id; michael@0: this._itemAddedParent = folder; michael@0: this._itemAddedIndex = index; michael@0: }, michael@0: onItemChanged: function (id, property, isAnnotationProperty, value) { michael@0: this._itemChangedId = id; michael@0: this._itemChangedProperty = property; michael@0: this._itemChanged_isAnnotationProperty = isAnnotationProperty; michael@0: this._itemChangedValue = value; michael@0: } michael@0: }; michael@0: PlacesUtils.bookmarks.addObserver(observer, false); michael@0: michael@0: do_register_cleanup(function () { michael@0: PlacesUtils.bookmarks.removeObserver(observer); michael@0: }); michael@0: michael@0: function run_test() { michael@0: // We set times in the past to workaround a timing bug due to virtual michael@0: // machines and the skew between PR_Now() and Date.now(), see bug 427142 and michael@0: // bug 858377 for details. michael@0: const PAST_PRTIME = (Date.now() - 86400000) * 1000; michael@0: michael@0: // Insert a new bookmark. michael@0: let testFolder = PlacesUtils.bookmarks.createFolder( michael@0: PlacesUtils.placesRootId, "test Folder", michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: let bookmarkId = PlacesUtils.bookmarks.insertBookmark( michael@0: testFolder, uri("http://google.com/"), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, ""); michael@0: michael@0: // Sanity check. michael@0: do_check_true(observer.itemChangedProperty === undefined); michael@0: michael@0: // Set dateAdded in the past and verify the bookmarks cache. michael@0: PlacesUtils.bookmarks.setItemDateAdded(bookmarkId, PAST_PRTIME); michael@0: do_check_eq(observer._itemChangedProperty, "dateAdded"); michael@0: do_check_eq(observer._itemChangedValue, PAST_PRTIME); michael@0: let dateAdded = PlacesUtils.bookmarks.getItemDateAdded(bookmarkId); michael@0: do_check_eq(dateAdded, PAST_PRTIME); michael@0: michael@0: // After just inserting, modified should be the same as dateAdded. michael@0: do_check_eq(PlacesUtils.bookmarks.getItemLastModified(bookmarkId), dateAdded); michael@0: michael@0: // Set lastModified in the past and verify the bookmarks cache. michael@0: PlacesUtils.bookmarks.setItemLastModified(bookmarkId, PAST_PRTIME); michael@0: do_check_eq(observer._itemChangedProperty, "lastModified"); michael@0: do_check_eq(observer._itemChangedValue, PAST_PRTIME); michael@0: do_check_eq(PlacesUtils.bookmarks.getItemLastModified(bookmarkId), michael@0: PAST_PRTIME); michael@0: michael@0: // Set bookmark title michael@0: PlacesUtils.bookmarks.setItemTitle(bookmarkId, "Google"); michael@0: michael@0: // Test notifications. michael@0: do_check_eq(observer._itemChangedId, bookmarkId); michael@0: do_check_eq(observer._itemChangedProperty, "title"); michael@0: do_check_eq(observer._itemChangedValue, "Google"); michael@0: michael@0: // Check lastModified has been updated. michael@0: is_time_ordered(PAST_PRTIME, michael@0: PlacesUtils.bookmarks.getItemLastModified(bookmarkId)); michael@0: michael@0: // Check that node properties are updated. michael@0: let root = PlacesUtils.getFolderContents(testFolder).root; michael@0: do_check_eq(root.childCount, 1); michael@0: let childNode = root.getChild(0); michael@0: michael@0: // confirm current dates match node properties michael@0: do_check_eq(PlacesUtils.bookmarks.getItemDateAdded(bookmarkId), michael@0: childNode.dateAdded); michael@0: do_check_eq(PlacesUtils.bookmarks.getItemLastModified(bookmarkId), michael@0: childNode.lastModified); michael@0: michael@0: // Test live update of lastModified when setting title. michael@0: PlacesUtils.bookmarks.setItemLastModified(bookmarkId, PAST_PRTIME); michael@0: PlacesUtils.bookmarks.setItemTitle(bookmarkId, "Google"); michael@0: michael@0: // Check lastModified has been updated. michael@0: is_time_ordered(PAST_PRTIME, childNode.lastModified); michael@0: // Test that node value matches db value. michael@0: do_check_eq(PlacesUtils.bookmarks.getItemLastModified(bookmarkId), michael@0: childNode.lastModified); michael@0: michael@0: // Test live update of the exposed date apis. michael@0: PlacesUtils.bookmarks.setItemDateAdded(bookmarkId, PAST_PRTIME); michael@0: do_check_eq(childNode.dateAdded, PAST_PRTIME); michael@0: PlacesUtils.bookmarks.setItemLastModified(bookmarkId, PAST_PRTIME); michael@0: do_check_eq(childNode.lastModified, PAST_PRTIME); michael@0: michael@0: root.containerOpen = false; michael@0: }