Bug 1458906 - A "Sponsored content" card is wrongly displayed on the first new tab opened, after the "Sponsored Stories" option was unchecked. r=k88hudson, a=jcristau
authorRicky Rosario <rickyrosario@gmail.com>
Thu, 03 May 2018 17:29:35 -0400
changeset 795884 423e30150b9a962f5ad50da561b4ab45e4cbd4a0
parent 795883 d5595a55f4c1d19ebbcf5a0206d5937b92137e7e
child 795885 0ff2170567cb3e45bb2f82e21cbc9d0cdfb20939
push id110108
push userbmo:tom@mozilla.com
push dateWed, 16 May 2018 18:43:56 +0000
reviewersk88hudson, jcristau
bugs1458906
milestone60.0.1
Bug 1458906 - A "Sponsored content" card is wrongly displayed on the first new tab opened, after the "Sponsored Stories" option was unchecked. r=k88hudson, a=jcristau MozReview-Commit-ID: 40XhoB5t7IG
browser/extensions/activity-stream/lib/TopStoriesFeed.jsm
browser/extensions/activity-stream/test/unit/lib/TopStoriesFeed.test.js
--- 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);
+  });
 });