1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/sync/tests/unit/test_bookmark_tracker.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,178 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Cu.import("resource://gre/modules/PlacesUtils.jsm"); 1.8 +Cu.import("resource://services-sync/constants.js"); 1.9 +Cu.import("resource://services-sync/engines/bookmarks.js"); 1.10 +Cu.import("resource://services-sync/engines.js"); 1.11 +Cu.import("resource://services-sync/service.js"); 1.12 +Cu.import("resource://services-sync/util.js"); 1.13 + 1.14 +Service.engineManager.register(BookmarksEngine); 1.15 +let engine = Service.engineManager.get("bookmarks"); 1.16 +let store = engine._store; 1.17 +let tracker = engine._tracker; 1.18 + 1.19 +store.wipe(); 1.20 +tracker.persistChangedIDs = false; 1.21 + 1.22 +function test_tracking() { 1.23 + _("Verify we've got an empty tracker to work with."); 1.24 + let tracker = engine._tracker; 1.25 + do_check_empty(tracker.changedIDs); 1.26 + 1.27 + let folder = PlacesUtils.bookmarks.createFolder( 1.28 + PlacesUtils.bookmarks.bookmarksMenuFolder, 1.29 + "Test Folder", PlacesUtils.bookmarks.DEFAULT_INDEX); 1.30 + function createBmk() { 1.31 + return PlacesUtils.bookmarks.insertBookmark( 1.32 + folder, Utils.makeURI("http://getfirefox.com"), 1.33 + PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!"); 1.34 + } 1.35 + 1.36 + try { 1.37 + _("Create bookmark. Won't show because we haven't started tracking yet"); 1.38 + createBmk(); 1.39 + do_check_empty(tracker.changedIDs); 1.40 + do_check_eq(tracker.score, 0); 1.41 + 1.42 + _("Tell the tracker to start tracking changes."); 1.43 + Svc.Obs.notify("weave:engine:start-tracking"); 1.44 + createBmk(); 1.45 + // We expect two changed items because the containing folder 1.46 + // changed as well (new child). 1.47 + do_check_attribute_count(tracker.changedIDs, 2); 1.48 + do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 2); 1.49 + 1.50 + _("Notifying twice won't do any harm."); 1.51 + Svc.Obs.notify("weave:engine:start-tracking"); 1.52 + createBmk(); 1.53 + do_check_attribute_count(tracker.changedIDs, 3); 1.54 + do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 4); 1.55 + 1.56 + _("Let's stop tracking again."); 1.57 + tracker.clearChangedIDs(); 1.58 + tracker.resetScore(); 1.59 + Svc.Obs.notify("weave:engine:stop-tracking"); 1.60 + createBmk(); 1.61 + do_check_empty(tracker.changedIDs); 1.62 + do_check_eq(tracker.score, 0); 1.63 + 1.64 + _("Notifying twice won't do any harm."); 1.65 + Svc.Obs.notify("weave:engine:stop-tracking"); 1.66 + createBmk(); 1.67 + do_check_empty(tracker.changedIDs); 1.68 + do_check_eq(tracker.score, 0); 1.69 + 1.70 + } finally { 1.71 + _("Clean up."); 1.72 + store.wipe(); 1.73 + tracker.clearChangedIDs(); 1.74 + tracker.resetScore(); 1.75 + Svc.Obs.notify("weave:engine:stop-tracking"); 1.76 + } 1.77 +} 1.78 + 1.79 +function test_onItemChanged() { 1.80 + // Anno that's in ANNOS_TO_TRACK. 1.81 + const DESCRIPTION_ANNO = "bookmarkProperties/description"; 1.82 + 1.83 + _("Verify we've got an empty tracker to work with."); 1.84 + let tracker = engine._tracker; 1.85 + do_check_empty(tracker.changedIDs); 1.86 + do_check_eq(tracker.score, 0); 1.87 + 1.88 + try { 1.89 + Svc.Obs.notify("weave:engine:stop-tracking"); 1.90 + let folder = PlacesUtils.bookmarks.createFolder( 1.91 + PlacesUtils.bookmarks.bookmarksMenuFolder, "Parent", 1.92 + PlacesUtils.bookmarks.DEFAULT_INDEX); 1.93 + _("Track changes to annos."); 1.94 + let b = PlacesUtils.bookmarks.insertBookmark( 1.95 + folder, Utils.makeURI("http://getfirefox.com"), 1.96 + PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!"); 1.97 + let bGUID = engine._store.GUIDForId(b); 1.98 + _("New item is " + b); 1.99 + _("GUID: " + bGUID); 1.100 + 1.101 + Svc.Obs.notify("weave:engine:start-tracking"); 1.102 + PlacesUtils.annotations.setItemAnnotation( 1.103 + b, DESCRIPTION_ANNO, "A test description", 0, 1.104 + PlacesUtils.annotations.EXPIRE_NEVER); 1.105 + do_check_true(tracker.changedIDs[bGUID] > 0); 1.106 + do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE); 1.107 + 1.108 + } finally { 1.109 + _("Clean up."); 1.110 + store.wipe(); 1.111 + tracker.clearChangedIDs(); 1.112 + tracker.resetScore(); 1.113 + Svc.Obs.notify("weave:engine:stop-tracking"); 1.114 + } 1.115 +} 1.116 + 1.117 +function test_onItemMoved() { 1.118 + _("Verify we've got an empty tracker to work with."); 1.119 + let tracker = engine._tracker; 1.120 + do_check_empty(tracker.changedIDs); 1.121 + do_check_eq(tracker.score, 0); 1.122 + 1.123 + try { 1.124 + let fx_id = PlacesUtils.bookmarks.insertBookmark( 1.125 + PlacesUtils.bookmarks.bookmarksMenuFolder, 1.126 + Utils.makeURI("http://getfirefox.com"), 1.127 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.128 + "Get Firefox!"); 1.129 + let fx_guid = engine._store.GUIDForId(fx_id); 1.130 + let tb_id = PlacesUtils.bookmarks.insertBookmark( 1.131 + PlacesUtils.bookmarks.bookmarksMenuFolder, 1.132 + Utils.makeURI("http://getthunderbird.com"), 1.133 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.134 + "Get Thunderbird!"); 1.135 + let tb_guid = engine._store.GUIDForId(tb_id); 1.136 + 1.137 + Svc.Obs.notify("weave:engine:start-tracking"); 1.138 + 1.139 + // Moving within the folder will just track the folder. 1.140 + PlacesUtils.bookmarks.moveItem( 1.141 + tb_id, PlacesUtils.bookmarks.bookmarksMenuFolder, 0); 1.142 + do_check_true(tracker.changedIDs['menu'] > 0); 1.143 + do_check_eq(tracker.changedIDs['toolbar'], undefined); 1.144 + do_check_eq(tracker.changedIDs[fx_guid], undefined); 1.145 + do_check_eq(tracker.changedIDs[tb_guid], undefined); 1.146 + do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE); 1.147 + tracker.clearChangedIDs(); 1.148 + tracker.resetScore(); 1.149 + 1.150 + // Moving a bookmark to a different folder will track the old 1.151 + // folder, the new folder and the bookmark. 1.152 + PlacesUtils.bookmarks.moveItem(tb_id, PlacesUtils.bookmarks.toolbarFolder, 1.153 + PlacesUtils.bookmarks.DEFAULT_INDEX); 1.154 + do_check_true(tracker.changedIDs['menu'] > 0); 1.155 + do_check_true(tracker.changedIDs['toolbar'] > 0); 1.156 + do_check_eq(tracker.changedIDs[fx_guid], undefined); 1.157 + do_check_true(tracker.changedIDs[tb_guid] > 0); 1.158 + do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 3); 1.159 + 1.160 + } finally { 1.161 + _("Clean up."); 1.162 + store.wipe(); 1.163 + tracker.clearChangedIDs(); 1.164 + tracker.resetScore(); 1.165 + Svc.Obs.notify("weave:engine:stop-tracking"); 1.166 + } 1.167 + 1.168 +} 1.169 + 1.170 +function run_test() { 1.171 + initTestLogging("Trace"); 1.172 + 1.173 + Log.repository.getLogger("Sync.Engine.Bookmarks").level = Log.Level.Trace; 1.174 + Log.repository.getLogger("Sync.Store.Bookmarks").level = Log.Level.Trace; 1.175 + Log.repository.getLogger("Sync.Tracker.Bookmarks").level = Log.Level.Trace; 1.176 + 1.177 + test_tracking(); 1.178 + test_onItemChanged(); 1.179 + test_onItemMoved(); 1.180 +} 1.181 +