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 463622 540ffec9584e
parent 463621 7be74890bf90
child 463623 61c5b7004802
push id1713
push userryanvm@gmail.com
push date2018-05-14 12:29 +0000
treeherdermozilla-release@540ffec9584e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
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);
+  });
 });