michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://gre/modules/PlacesUtils.jsm"); michael@0: Cu.import("resource://services-sync/constants.js"); michael@0: Cu.import("resource://services-sync/engines/bookmarks.js"); michael@0: Cu.import("resource://services-sync/engines.js"); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: Cu.import("resource://services-sync/util.js"); michael@0: michael@0: Service.engineManager.register(BookmarksEngine); michael@0: let engine = Service.engineManager.get("bookmarks"); michael@0: let store = engine._store; michael@0: let tracker = engine._tracker; michael@0: michael@0: store.wipe(); michael@0: tracker.persistChangedIDs = false; michael@0: michael@0: function test_tracking() { michael@0: _("Verify we've got an empty tracker to work with."); michael@0: let tracker = engine._tracker; michael@0: do_check_empty(tracker.changedIDs); michael@0: michael@0: let folder = PlacesUtils.bookmarks.createFolder( michael@0: PlacesUtils.bookmarks.bookmarksMenuFolder, michael@0: "Test Folder", PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: function createBmk() { michael@0: return PlacesUtils.bookmarks.insertBookmark( michael@0: folder, Utils.makeURI("http://getfirefox.com"), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!"); michael@0: } michael@0: michael@0: try { michael@0: _("Create bookmark. Won't show because we haven't started tracking yet"); michael@0: createBmk(); michael@0: do_check_empty(tracker.changedIDs); michael@0: do_check_eq(tracker.score, 0); michael@0: michael@0: _("Tell the tracker to start tracking changes."); michael@0: Svc.Obs.notify("weave:engine:start-tracking"); michael@0: createBmk(); michael@0: // We expect two changed items because the containing folder michael@0: // changed as well (new child). michael@0: do_check_attribute_count(tracker.changedIDs, 2); michael@0: do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 2); michael@0: michael@0: _("Notifying twice won't do any harm."); michael@0: Svc.Obs.notify("weave:engine:start-tracking"); michael@0: createBmk(); michael@0: do_check_attribute_count(tracker.changedIDs, 3); michael@0: do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 4); michael@0: michael@0: _("Let's stop tracking again."); michael@0: tracker.clearChangedIDs(); michael@0: tracker.resetScore(); michael@0: Svc.Obs.notify("weave:engine:stop-tracking"); michael@0: createBmk(); michael@0: do_check_empty(tracker.changedIDs); michael@0: do_check_eq(tracker.score, 0); michael@0: michael@0: _("Notifying twice won't do any harm."); michael@0: Svc.Obs.notify("weave:engine:stop-tracking"); michael@0: createBmk(); michael@0: do_check_empty(tracker.changedIDs); michael@0: do_check_eq(tracker.score, 0); michael@0: michael@0: } finally { michael@0: _("Clean up."); michael@0: store.wipe(); michael@0: tracker.clearChangedIDs(); michael@0: tracker.resetScore(); michael@0: Svc.Obs.notify("weave:engine:stop-tracking"); michael@0: } michael@0: } michael@0: michael@0: function test_onItemChanged() { michael@0: // Anno that's in ANNOS_TO_TRACK. michael@0: const DESCRIPTION_ANNO = "bookmarkProperties/description"; michael@0: michael@0: _("Verify we've got an empty tracker to work with."); michael@0: let tracker = engine._tracker; michael@0: do_check_empty(tracker.changedIDs); michael@0: do_check_eq(tracker.score, 0); michael@0: michael@0: try { michael@0: Svc.Obs.notify("weave:engine:stop-tracking"); michael@0: let folder = PlacesUtils.bookmarks.createFolder( michael@0: PlacesUtils.bookmarks.bookmarksMenuFolder, "Parent", michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: _("Track changes to annos."); michael@0: let b = PlacesUtils.bookmarks.insertBookmark( michael@0: folder, Utils.makeURI("http://getfirefox.com"), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!"); michael@0: let bGUID = engine._store.GUIDForId(b); michael@0: _("New item is " + b); michael@0: _("GUID: " + bGUID); michael@0: michael@0: Svc.Obs.notify("weave:engine:start-tracking"); michael@0: PlacesUtils.annotations.setItemAnnotation( michael@0: b, DESCRIPTION_ANNO, "A test description", 0, michael@0: PlacesUtils.annotations.EXPIRE_NEVER); michael@0: do_check_true(tracker.changedIDs[bGUID] > 0); michael@0: do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE); michael@0: michael@0: } finally { michael@0: _("Clean up."); michael@0: store.wipe(); michael@0: tracker.clearChangedIDs(); michael@0: tracker.resetScore(); michael@0: Svc.Obs.notify("weave:engine:stop-tracking"); michael@0: } michael@0: } michael@0: michael@0: function test_onItemMoved() { michael@0: _("Verify we've got an empty tracker to work with."); michael@0: let tracker = engine._tracker; michael@0: do_check_empty(tracker.changedIDs); michael@0: do_check_eq(tracker.score, 0); michael@0: michael@0: try { michael@0: let fx_id = PlacesUtils.bookmarks.insertBookmark( michael@0: PlacesUtils.bookmarks.bookmarksMenuFolder, michael@0: Utils.makeURI("http://getfirefox.com"), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "Get Firefox!"); michael@0: let fx_guid = engine._store.GUIDForId(fx_id); michael@0: let tb_id = PlacesUtils.bookmarks.insertBookmark( michael@0: PlacesUtils.bookmarks.bookmarksMenuFolder, michael@0: Utils.makeURI("http://getthunderbird.com"), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "Get Thunderbird!"); michael@0: let tb_guid = engine._store.GUIDForId(tb_id); michael@0: michael@0: Svc.Obs.notify("weave:engine:start-tracking"); michael@0: michael@0: // Moving within the folder will just track the folder. michael@0: PlacesUtils.bookmarks.moveItem( michael@0: tb_id, PlacesUtils.bookmarks.bookmarksMenuFolder, 0); michael@0: do_check_true(tracker.changedIDs['menu'] > 0); michael@0: do_check_eq(tracker.changedIDs['toolbar'], undefined); michael@0: do_check_eq(tracker.changedIDs[fx_guid], undefined); michael@0: do_check_eq(tracker.changedIDs[tb_guid], undefined); michael@0: do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE); michael@0: tracker.clearChangedIDs(); michael@0: tracker.resetScore(); michael@0: michael@0: // Moving a bookmark to a different folder will track the old michael@0: // folder, the new folder and the bookmark. michael@0: PlacesUtils.bookmarks.moveItem(tb_id, PlacesUtils.bookmarks.toolbarFolder, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: do_check_true(tracker.changedIDs['menu'] > 0); michael@0: do_check_true(tracker.changedIDs['toolbar'] > 0); michael@0: do_check_eq(tracker.changedIDs[fx_guid], undefined); michael@0: do_check_true(tracker.changedIDs[tb_guid] > 0); michael@0: do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 3); michael@0: michael@0: } finally { michael@0: _("Clean up."); michael@0: store.wipe(); michael@0: tracker.clearChangedIDs(); michael@0: tracker.resetScore(); michael@0: Svc.Obs.notify("weave:engine:stop-tracking"); michael@0: } michael@0: michael@0: } michael@0: michael@0: function run_test() { michael@0: initTestLogging("Trace"); michael@0: michael@0: Log.repository.getLogger("Sync.Engine.Bookmarks").level = Log.Level.Trace; michael@0: Log.repository.getLogger("Sync.Store.Bookmarks").level = Log.Level.Trace; michael@0: Log.repository.getLogger("Sync.Tracker.Bookmarks").level = Log.Level.Trace; michael@0: michael@0: test_tracking(); michael@0: test_onItemChanged(); michael@0: test_onItemMoved(); michael@0: } michael@0: