Bug 1515078 - Use PreferenceExperiments API to unenroll from Pocket Newtab experiment (#4677)

This commit is contained in:
Andrei Oprea 2019-01-18 15:43:49 +00:00 коммит произвёл Ed Lee
Родитель 272b2ba2bc
Коммит 2a4c65cd29
2 изменённых файлов: 26 добавлений и 7 удалений

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

@ -10,6 +10,10 @@ const {actionTypes: at} = ChromeUtils.import("resource://activity-stream/common/
XPCOMUtils.defineLazyGlobalGetters(this, ["fetch"]);
XPCOMUtils.defineLazyModuleGetters(this, {
PreferenceExperiments: "resource://normandy/lib/PreferenceExperiments.jsm",
});
const HTML_NS = "http://www.w3.org/1999/xhtml";
const PREFERENCES_LOADED_EVENT = "home-pane-loaded";
@ -279,10 +283,19 @@ this.AboutPreferences = class AboutPreferences {
contentDiscoveryButton.textContent = formatString("prefs_content_discovery_button");
createAppend("hbox", discoveryGroup)
.appendChild(contentDiscoveryButton)
.addEventListener("click", () => {
.addEventListener("click", async () => {
const activeExperiments = await PreferenceExperiments.getAllActive();
const experiment = activeExperiments.find(exp => exp.preferenceName === DISCOVERY_STREAM_CONFIG_PREF_NAME);
// Unconditionally update the UI for a fast user response and in
// order to help with testing
discoveryGroup.style.display = "none";
contentsGroup.style.visibility = "visible";
Services.prefs.clearUserPref(DISCOVERY_STREAM_CONFIG_PREF_NAME);
if (experiment) {
await PreferenceExperiments.stop(experiment.name, {
resetValue: true,
reason: "individual-opt-out",
});
}
}, {once: true});
}

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

@ -312,18 +312,24 @@ describe("AboutPreferences Feed", () => {
// Stream is enabled
assert.propertyVal(node.style, "visibility", "hidden");
});
it("should toggle the Discovery Stream pref on button click", () => {
it("should toggle the Discovery Stream pref on button click", async () => {
DiscoveryStream = {config: {enabled: true}};
const stub = sandbox.stub(Services.prefs, "clearUserPref");
const PreferenceExperimentsStub = {
getAllActive: sandbox.stub().resolves([{name: "discoverystream", preferenceName: "browser.newtabpage.activity-stream.discoverystream.config"}]),
stop: sandbox.stub().resolves(),
};
globals.set("PreferenceExperiments", PreferenceExperimentsStub);
testRender();
assert.calledOnce(node.addEventListener);
node.addEventListener.firstCall.args[1]();
// Trigger the button click listener
await node.addEventListener.firstCall.args[1]();
assert.calledOnce(stub);
assert.calledWithExactly(stub, "browser.newtabpage.activity-stream.discoverystream.config");
assert.calledOnce(PreferenceExperimentsStub.getAllActive);
assert.calledOnce(PreferenceExperimentsStub.stop);
assert.calledWithExactly(PreferenceExperimentsStub.stop, "discoverystream", {resetValue: true, reason: "individual-opt-out"});
});
});
});