gecko/services/sync/tests/unit/test_addons_tracker.js
Gregory Szorc 7ece058f3d Bug 785225 - Part 9: Refactor engines to not use singletons; r=rnewman
Engines now maintain a reference to the service they belong to. This
allows them to obtain references to other engine instances belonging to
that service and that service only.

Stores and trackers now maintain a reference to the engine they belong
to.

Engine managers now maintain a reference back to a service.

The clients singleton has been removed. It now exists as an instance
variable on Service. Parts of ClientsEngine do behave as singletons
(e.g. commands). This will be addressed in future refactoring.
2012-08-29 14:43:41 -07:00

177 lines
4.5 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
Cu.import("resource://gre/modules/AddonManager.jsm");
Cu.import("resource://services-sync/engines/addons.js");
Cu.import("resource://services-sync/constants.js");
Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");
loadAddonTestFunctions();
startupManager();
Svc.Prefs.set("addons.ignoreRepositoryChecking", true);
Svc.Prefs.set("engine.addons", true);
Service.engineManager.register(AddonsEngine);
let engine = Service.engineManager.get("addons");
let reconciler = engine._reconciler;
let store = engine._store;
let tracker = engine._tracker;
const addon1ID = "addon1@tests.mozilla.org";
function cleanup_and_advance() {
Svc.Obs.notify("weave:engine:stop-tracking");
tracker.observe(null, "weave:engine:stop-tracking");
tracker.resetScore();
tracker.clearChangedIDs();
tracker._lazySave.clear();
reconciler._addons = {};
reconciler._changes = [];
let cb = Async.makeSpinningCallback();
reconciler.saveState(null, cb);
cb.wait();
run_next_test();
}
function run_test() {
initTestLogging("Trace");
Log4Moz.repository.getLogger("Sync.Engine.Addons").level = Log4Moz.Level.Trace;
Log4Moz.repository.getLogger("Sync.AddonsReconciler").level =
Log4Moz.Level.Trace;
cleanup_and_advance();
}
add_test(function test_empty() {
_("Verify the tracker is empty to start with.");
do_check_eq(0, Object.keys(tracker.changedIDs).length);
do_check_eq(0, tracker.score);
cleanup_and_advance();
});
add_test(function test_not_tracking() {
_("Ensures the tracker doesn't do anything when it isn't tracking.");
let addon = installAddon("test_bootstrap1_1");
uninstallAddon(addon);
do_check_eq(0, Object.keys(tracker.changedIDs).length);
do_check_eq(0, tracker.score);
cleanup_and_advance();
});
add_test(function test_track_install() {
_("Ensure that installing an add-on notifies tracker.");
reconciler.startListening();
Svc.Obs.notify("weave:engine:start-tracking");
do_check_eq(0, tracker.score);
let addon = installAddon("test_bootstrap1_1");
let changed = tracker.changedIDs;
do_check_eq(1, Object.keys(changed).length);
do_check_true(addon.syncGUID in changed);
do_check_eq(SCORE_INCREMENT_XLARGE, tracker.score);
uninstallAddon(addon);
cleanup_and_advance();
});
add_test(function test_track_uninstall() {
_("Ensure that uninstalling an add-on notifies tracker.");
reconciler.startListening();
let addon = installAddon("test_bootstrap1_1");
let guid = addon.syncGUID;
do_check_eq(0, tracker.score);
Svc.Obs.notify("weave:engine:start-tracking");
uninstallAddon(addon);
let changed = tracker.changedIDs;
do_check_eq(1, Object.keys(changed).length);
do_check_true(guid in changed);
do_check_eq(SCORE_INCREMENT_XLARGE, tracker.score);
cleanup_and_advance();
});
add_test(function test_track_user_disable() {
_("Ensure that tracker sees disabling of add-on");
reconciler.startListening();
let addon = installAddon("test_bootstrap1_1");
do_check_false(addon.userDisabled);
do_check_false(addon.appDisabled);
do_check_true(addon.isActive);
Svc.Obs.notify("weave:engine:start-tracking");
do_check_eq(0, tracker.score);
let cb = Async.makeSyncCallback();
let listener = {
onDisabled: function(disabled) {
_("onDisabled");
if (disabled.id == addon.id) {
AddonManager.removeAddonListener(listener);
cb();
}
},
onDisabling: function(disabling) {
_("onDisabling add-on");
}
};
AddonManager.addAddonListener(listener);
_("Disabling add-on");
addon.userDisabled = true;
_("Disabling started...");
Async.waitForSyncCallback(cb);
let changed = tracker.changedIDs;
do_check_eq(1, Object.keys(changed).length);
do_check_true(addon.syncGUID in changed);
do_check_eq(SCORE_INCREMENT_XLARGE, tracker.score);
uninstallAddon(addon);
cleanup_and_advance();
});
add_test(function test_track_enable() {
_("Ensure that enabling a disabled add-on notifies tracker.");
reconciler.startListening();
let addon = installAddon("test_bootstrap1_1");
addon.userDisabled = true;
store._sleep(0);
do_check_eq(0, tracker.score);
Svc.Obs.notify("weave:engine:start-tracking");
addon.userDisabled = false;
store._sleep(0);
let changed = tracker.changedIDs;
do_check_eq(1, Object.keys(changed).length);
do_check_true(addon.syncGUID in changed);
do_check_eq(SCORE_INCREMENT_XLARGE, tracker.score);
uninstallAddon(addon);
cleanup_and_advance();
});