Bug 1458906 - A "Sponsored content" card is wrongly displayed on the first new tab opened, after the "Sponsored Stories" option was unchecked r?k88hudson draft
authorRicky Rosario <rickyrosario@gmail.com>
Thu, 03 May 2018 17:29:35 -0400
changeset 791264 f45047c972d164b7ff3347d1995dd40750b70179
parent 791136 35165f43d0b97f500dafb4f0f7b9ca8d91416812
push id108765
push userbmo:rrosario@mozilla.com
push dateThu, 03 May 2018 21:38:06 +0000
reviewersk88hudson
bugs1458906
milestone61.0a1
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
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);
+  });
 });