Bug 1458856 - Handle prefs with only a user value in preference rollout r=Gijs

MozReview-Commit-ID: 5t99YT4lJED

--HG--
extra : rebase_source : 227e0368b4611de605564b1f151c9edeb91e7bf0
This commit is contained in:
Mike Cooper 2018-05-03 13:38:30 -07:00
Родитель 01951313fc
Коммит 527fd0db60
2 изменённых файлов: 52 добавлений и 14 удалений

Просмотреть файл

@ -23,24 +23,34 @@ var PrefUtils = {
getPref(branchName, pref, defaultValue = null) {
const branch = kPrefBranches[branchName];
const type = branch.getPrefType(pref);
switch (type) {
case Services.prefs.PREF_BOOL: {
return branch.getBoolPref(pref);
try {
switch (type) {
case Services.prefs.PREF_BOOL: {
return branch.getBoolPref(pref);
}
case Services.prefs.PREF_STRING: {
return branch.getStringPref(pref);
}
case Services.prefs.PREF_INT: {
return branch.getIntPref(pref);
}
case Services.prefs.PREF_INVALID: {
return defaultValue;
}
}
case Services.prefs.PREF_STRING: {
return branch.getStringPref(pref);
}
case Services.prefs.PREF_INT: {
return branch.getIntPref(pref);
}
case Services.prefs.PREF_INVALID: {
} catch (e) {
if (branchName === "default" && e.result === Cr.NS_ERROR_UNEXPECTED) {
// There is a value for the pref on the user branch but not on the default branch. This is ok.
return defaultValue;
}
default: {
// This should never happen
throw new TypeError(`Unknown preference type (${type}) for ${pref}.`);
}
// Unexpected error, re-throw it
throw e;
}
// If `type` isn't any of the above, throw an error. Don't do this in a
// default branch of switch so that error handling is easier.
throw new TypeError(`Unknown preference type (${type}) for ${pref}.`);
},
/**

Просмотреть файл

@ -357,3 +357,31 @@ decorate_task(
Services.prefs.deleteBranch("test.pref");
},
);
// Enrollment works for prefs with only a user branch value, and no default value.
decorate_task(
PreferenceRollouts.withTestMock,
async function simple_recipe_enrollment(setExperimentActiveStub, sendEventStub) {
const recipe = {
id: 1,
arguments: {
slug: "test-rollout",
preferences: [{preferenceName: "test.pref", value: 1}],
},
};
// Set a pref on the user branch only
Services.prefs.setIntPref("test.pref", 2);
const action = new PreferenceRolloutAction();
await action.runRecipe(recipe);
await action.finalize();
is(Services.prefs.getIntPref("test.pref"), 2, "original user branch value still visible");
is(Services.prefs.getDefaultBranch("").getIntPref("test.pref"), 1, "default branch was set");
is(Services.prefs.getIntPref("app.normandy.startupRolloutPrefs.test.pref"), 1, "startup pref is est");
// Cleanup
Services.prefs.getDefaultBranch("").deleteBranch("test.pref");
},
);