toolkit/components/places/tests/bookmarks/test_393498.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/bookmarks/test_393498.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,102 @@
     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 +let observer = {
    1.11 +  __proto__: NavBookmarkObserver.prototype,
    1.12 +
    1.13 +  onItemAdded: function (id, folder, index) {
    1.14 +    this._itemAddedId = id;
    1.15 +    this._itemAddedParent = folder;
    1.16 +    this._itemAddedIndex = index;
    1.17 +  },
    1.18 +  onItemChanged: function (id, property, isAnnotationProperty, value) {
    1.19 +    this._itemChangedId = id;
    1.20 +    this._itemChangedProperty = property;
    1.21 +    this._itemChanged_isAnnotationProperty = isAnnotationProperty;
    1.22 +    this._itemChangedValue = value;
    1.23 +  }
    1.24 +};
    1.25 +PlacesUtils.bookmarks.addObserver(observer, false);
    1.26 +
    1.27 +do_register_cleanup(function () {
    1.28 +  PlacesUtils.bookmarks.removeObserver(observer);
    1.29 +});
    1.30 +
    1.31 +function run_test() {
    1.32 +  // We set times in the past to workaround a timing bug due to virtual
    1.33 +  // machines and the skew between PR_Now() and Date.now(), see bug 427142 and
    1.34 +  // bug 858377 for details.
    1.35 +  const PAST_PRTIME = (Date.now() - 86400000) * 1000;
    1.36 +
    1.37 +  // Insert a new bookmark.
    1.38 +  let testFolder = PlacesUtils.bookmarks.createFolder(
    1.39 +    PlacesUtils.placesRootId, "test Folder",
    1.40 +    PlacesUtils.bookmarks.DEFAULT_INDEX);
    1.41 +  let bookmarkId = PlacesUtils.bookmarks.insertBookmark(
    1.42 +    testFolder, uri("http://google.com/"),
    1.43 +    PlacesUtils.bookmarks.DEFAULT_INDEX, "");
    1.44 +
    1.45 +  // Sanity check.
    1.46 +  do_check_true(observer.itemChangedProperty === undefined);
    1.47 +
    1.48 +  // Set dateAdded in the past and verify the bookmarks cache.
    1.49 +  PlacesUtils.bookmarks.setItemDateAdded(bookmarkId, PAST_PRTIME);
    1.50 +  do_check_eq(observer._itemChangedProperty, "dateAdded");
    1.51 +  do_check_eq(observer._itemChangedValue, PAST_PRTIME);
    1.52 +  let dateAdded = PlacesUtils.bookmarks.getItemDateAdded(bookmarkId);
    1.53 +  do_check_eq(dateAdded, PAST_PRTIME);
    1.54 +
    1.55 +  // After just inserting, modified should be the same as dateAdded.
    1.56 +  do_check_eq(PlacesUtils.bookmarks.getItemLastModified(bookmarkId), dateAdded);
    1.57 +
    1.58 +  // Set lastModified in the past and verify the bookmarks cache.
    1.59 +  PlacesUtils.bookmarks.setItemLastModified(bookmarkId, PAST_PRTIME);
    1.60 +  do_check_eq(observer._itemChangedProperty, "lastModified");
    1.61 +  do_check_eq(observer._itemChangedValue, PAST_PRTIME);
    1.62 +  do_check_eq(PlacesUtils.bookmarks.getItemLastModified(bookmarkId),
    1.63 +              PAST_PRTIME);
    1.64 +
    1.65 +  // Set bookmark title
    1.66 +  PlacesUtils.bookmarks.setItemTitle(bookmarkId, "Google");
    1.67 +
    1.68 +  // Test notifications.
    1.69 +  do_check_eq(observer._itemChangedId, bookmarkId);
    1.70 +  do_check_eq(observer._itemChangedProperty, "title");
    1.71 +  do_check_eq(observer._itemChangedValue, "Google");
    1.72 +
    1.73 +  // Check lastModified has been updated.
    1.74 +  is_time_ordered(PAST_PRTIME,
    1.75 +                  PlacesUtils.bookmarks.getItemLastModified(bookmarkId));
    1.76 +
    1.77 +  // Check that node properties are updated.
    1.78 +  let root = PlacesUtils.getFolderContents(testFolder).root;
    1.79 +  do_check_eq(root.childCount, 1);
    1.80 +  let childNode = root.getChild(0);
    1.81 +
    1.82 +  // confirm current dates match node properties
    1.83 +  do_check_eq(PlacesUtils.bookmarks.getItemDateAdded(bookmarkId),
    1.84 +              childNode.dateAdded);
    1.85 +  do_check_eq(PlacesUtils.bookmarks.getItemLastModified(bookmarkId),
    1.86 +              childNode.lastModified);
    1.87 +
    1.88 +  // Test live update of lastModified when setting title.
    1.89 +  PlacesUtils.bookmarks.setItemLastModified(bookmarkId, PAST_PRTIME);
    1.90 +  PlacesUtils.bookmarks.setItemTitle(bookmarkId, "Google");
    1.91 +
    1.92 +  // Check lastModified has been updated.
    1.93 +  is_time_ordered(PAST_PRTIME, childNode.lastModified);
    1.94 +  // Test that node value matches db value.
    1.95 +  do_check_eq(PlacesUtils.bookmarks.getItemLastModified(bookmarkId),
    1.96 +              childNode.lastModified);
    1.97 +
    1.98 +  // Test live update of the exposed date apis.
    1.99 +  PlacesUtils.bookmarks.setItemDateAdded(bookmarkId, PAST_PRTIME);
   1.100 +  do_check_eq(childNode.dateAdded, PAST_PRTIME);
   1.101 +  PlacesUtils.bookmarks.setItemLastModified(bookmarkId, PAST_PRTIME);
   1.102 +  do_check_eq(childNode.lastModified, PAST_PRTIME);
   1.103 +
   1.104 +  root.containerOpen = false;
   1.105 +}

mercurial