Bug 1220911: Switch test_experiment.js to task style. r=rhelmer

Before changing the handling of experiments make the tests a bit more readable
and use BootstrapMonitor to verify things.

--HG--
extra : commitid : LnQTmpOqRgj
extra : rebase_source : be63740ca7613bf685c9d69722e9fb2e1bb0d5e3
This commit is contained in:
Dave Townsend 2015-11-09 15:02:05 -08:00
parent 95d4a64276
commit 0ca2ae256e
3 changed files with 105 additions and 85 deletions

View File

@ -0,0 +1 @@
Components.utils.import("resource://xpcshell-data/BootstrapMonitor.jsm").monitor(this);

View File

@ -1404,6 +1404,23 @@ function ensure_test_completed() {
do_check_eq(gExpectedInstalls.length, 0);
}
/**
* Returns a promise that resolves when the given add-on event is fired. The
* resolved value is an array of arguments passed for the event.
*/
function promiseAddonEvent(event) {
return new Promise(resolve => {
let listener = {
[event]: function(...args) {
AddonManager.removeAddonListener(listener);
resolve(args);
}
}
AddonManager.addAddonListener(listener);
});
}
/**
* A helper method to install an array of AddonInstall to completion and then
* call a provided callback.

View File

@ -3,10 +3,12 @@
var scope = Components.utils.import("resource://gre/modules/addons/XPIProvider.jsm");
const XPIProvider = scope.XPIProvider;
const ID = "experiment1@tests.mozilla.org";
var gIsNightly = false;
function run_test() {
BootstrapMonitor.init();
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
startupManager();
@ -15,10 +17,16 @@ function run_test() {
run_next_test();
}
add_test(function test_experiment() {
AddonManager.getInstallForFile(do_get_addon("test_experiment1"), (install) => {
completeAllInstalls([install], () => {
AddonManager.getAddonByID("experiment1@tests.mozilla.org", (addon) => {
add_task(function* test_experiment() {
BootstrapMonitor.checkAddonNotInstalled(ID);
BootstrapMonitor.checkAddonNotStarted(ID);
yield promiseInstallAllFiles([do_get_addon("test_experiment1")]);
BootstrapMonitor.checkAddonInstalled(ID, "1.0");
BootstrapMonitor.checkAddonNotStarted(ID);
let addon = yield promiseAddonByID(ID);
Assert.ok(addon, "Addon is found.");
Assert.ok(addon.userDisabled, "Experiments are userDisabled by default.");
@ -53,31 +61,29 @@ add_test(function test_experiment() {
Assert.ok(noCompatibleCalled, "Listener called.");
Assert.ok(noUpdateCalled, "Listener called.");
Assert.ok(finishedCalled, "Listener called.");
run_next_test();
});
});
});
});
// Changes to userDisabled should not be persisted to the database.
add_test(function test_userDisabledNotPersisted() {
AddonManager.getAddonByID("experiment1@tests.mozilla.org", (addon) => {
add_task(function* test_userDisabledNotPersisted() {
let addon = yield promiseAddonByID(ID);
Assert.ok(addon, "Add-on is found.");
Assert.ok(addon.userDisabled, "Add-on is user disabled.");
let listener = {
onEnabled: (addon2) => {
AddonManager.removeAddonListener(listener);
let promise = promiseAddonEvent("onEnabled");
addon.userDisabled = false;
let [addon2] = yield promise;
BootstrapMonitor.checkAddonInstalled(ID, "1.0");
BootstrapMonitor.checkAddonStarted(ID, "1.0");
Assert.equal(addon2.id, addon.id, "Changed add-on matches expected.");
Assert.equal(addon2.userDisabled, false, "Add-on is no longer user disabled.");
Assert.ok(addon2.isActive, "Add-on is active.");
Assert.ok("experiment1@tests.mozilla.org" in XPIProvider.bootstrappedAddons,
Assert.ok(ID in XPIProvider.bootstrappedAddons,
"Experiment add-on listed in XPIProvider bootstrapped list.");
AddonManager.getAddonByID("experiment1@tests.mozilla.org", (addon) => {
addon = yield promiseAddonByID(ID);
Assert.ok(addon, "Add-on retrieved.");
Assert.equal(addon.userDisabled, false, "Add-on is still enabled after API retrieve.");
Assert.ok(addon.isActive, "Add-on is still active.");
@ -87,43 +93,39 @@ add_test(function test_userDisabledNotPersisted() {
"Should not be pending disable");
// Now when we restart the manager the add-on should revert state.
restartManager();
yield promiseRestartManager();
let persisted = JSON.parse(Services.prefs.getCharPref("extensions.bootstrappedAddons"));
Assert.ok(!("experiment1@tests.mozilla.org" in persisted),
Assert.ok(!(ID in persisted),
"Experiment add-on not persisted to bootstrappedAddons.");
AddonManager.getAddonByID("experiment1@tests.mozilla.org", (addon) => {
BootstrapMonitor.checkAddonInstalled(ID, "1.0");
BootstrapMonitor.checkAddonNotStarted(ID);
addon = yield promiseAddonByID(ID);
Assert.ok(addon, "Add-on retrieved.");
Assert.ok(addon.userDisabled, "Add-on is disabled after restart.");
Assert.equal(addon.isActive, false, "Add-on is not active after restart.");
addon.uninstall();
run_next_test();
});
});
},
};
AddonManager.addAddonListener(listener);
addon.userDisabled = false;
});
BootstrapMonitor.checkAddonNotInstalled(ID);
BootstrapMonitor.checkAddonNotStarted(ID);
});
add_test(function test_checkCompatibility() {
add_task(function test_checkCompatibility() {
if (gIsNightly)
Services.prefs.setBoolPref("extensions.checkCompatibility.nightly", false);
else
Services.prefs.setBoolPref("extensions.checkCompatibility.1", false);
restartManager();
yield promiseRestartManager();
yield promiseInstallAllFiles([do_get_addon("test_experiment1")]);
let addon = yield promiseAddonByID(ID);
BootstrapMonitor.checkAddonInstalled(ID, "1.0");
BootstrapMonitor.checkAddonNotStarted(ID);
installAllFiles([do_get_addon("test_experiment1")], () => {
AddonManager.getAddonByID("experiment1@tests.mozilla.org", (addon) => {
Assert.ok(addon, "Add-on is found.");
Assert.ok(addon.userDisabled, "Add-on is user disabled.");
Assert.ok(!addon.appDisabled, "Add-on is not app disabled.");
run_next_test();
});
});
});