Bug 1458906 - A "Sponsored content" card is wrongly displayed on the first new tab opened, after the "Sponsored Stories" option was unchecked r?k88hudson
MozReview-Commit-ID: 40XhoB5t7IG
--- a/browser/extensions/activity-stream/lib/TopStoriesFeed.jsm
+++ b/browser/extensions/activity-stream/lib/TopStoriesFeed.jsm
@@ -435,16 +435,23 @@ this.TopStoriesFeed = class TopStoriesFe
const prefVal = this._prefs.get(pref);
return prefVal ? JSON.parse(prefVal) : {};
}
writeImpressionsPref(pref, impressions) {
this._prefs.set(pref, JSON.stringify(impressions));
}
+ removeSpocs() {
+ // Uninit+re-init so that spocs are removed from all open and preloaded tabs when
+ // they are disabled.
+ this.uninit();
+ this.init();
+ }
+
onAction(action) {
switch (action.type) {
case at.INIT:
this.init();
break;
case at.SYSTEM_TICK:
if (Date.now() - this.storiesLastUpdated >= STORIES_UPDATE_TIME) {
this.fetchStories();
@@ -490,16 +497,22 @@ this.TopStoriesFeed = class TopStoriesFe
const topRecs = payload.tiles
.filter(t => !this.spocCampaignMap.has(t.id))
.map(t => t.id);
this.recordTopRecImpressions(topRecs);
}
}
break;
}
+ case at.PREF_CHANGED:
+ // Check if spocs was disabled. Remove them if they were.
+ if (action.data.name === "showSponsored" && !action.data.value) {
+ this.removeSpocs();
+ }
+ break;
}
}
};
this.STORIES_UPDATE_TIME = STORIES_UPDATE_TIME;
this.TOPICS_UPDATE_TIME = TOPICS_UPDATE_TIME;
this.SECTION_ID = SECTION_ID;
this.SPOC_IMPRESSION_TRACKING_PREF = SPOC_IMPRESSION_TRACKING_PREF;
--- a/browser/extensions/activity-stream/test/unit/lib/TopStoriesFeed.test.js
+++ b/browser/extensions/activity-stream/test/unit/lib/TopStoriesFeed.test.js
@@ -969,9 +969,16 @@ describe("Top Stories Feed", () => {
instance.affinityProvider = {};
instance.personalized = true;
instance.onAction({type: at.PLACES_HISTORY_CLEARED});
assert.calledWith(instance.cache.set, "domainAffinities", {});
assert.isUndefined(instance.affinityProvider);
});
});
+ it("should call uninit and init on disabling of showSponsored pref", () => {
+ sinon.stub(instance, "uninit");
+ sinon.stub(instance, "init");
+ instance.onAction({type: at.PREF_CHANGED, data: {name: "showSponsored", value: false}});
+ assert.calledOnce(instance.uninit);
+ assert.calledOnce(instance.init);
+ });
});