Bug 1488923 - When notifying listeners that a study is ending, include the reason why the study is ending r=aswan a=pascalc
authorMichael Cooper <mcooper@mozilla.com>
Wed, 19 Sep 2018 03:19:45 +0000
changeset 492574 93d45f9ae4c694e7950a02406d6ea9c511700109
parent 492573 17ab3c3b12562b85a7d9de98b7bcdd788e6b8236
child 492575 f197ee75ff38460143b3d649154b830674b44886
push id1815
push userffxbld-merge
push dateMon, 15 Oct 2018 10:40:45 +0000
treeherdermozilla-release@18d4c09e9378 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaswan, pascalc
bugs1488923
milestone63.0
Bug 1488923 - When notifying listeners that a study is ending, include the reason why the study is ending r=aswan a=pascalc Differential Revision: https://phabricator.services.mozilla.com/D6196
toolkit/components/normandy/lib/AddonStudies.jsm
toolkit/components/normandy/test/unit/test_addon_unenroll.js
--- a/toolkit/components/normandy/lib/AddonStudies.jsm
+++ b/toolkit/components/normandy/lib/AddonStudies.jsm
@@ -241,17 +241,17 @@ var AddonStudies = {
 
     Services.obs.notifyObservers(study, STUDY_ENDED_TOPIC, `${study.recipeId}`);
     TelemetryEvents.sendEvent("unenroll", "addon_study", study.name, {
       addonId: study.addonId,
       addonVersion: study.addonVersion,
       reason,
     });
 
-    await this.onUnenroll(study.addonId);
+    await this.onUnenroll(study.addonId, reason);
   },
 
   // Maps extension id -> Set(callbacks)
   _unenrollListeners: new Map(),
 
   /**
    * Register a callback to be invoked when a given study ends.
    *
@@ -270,19 +270,19 @@ var AddonStudies = {
   /**
    * Invoke the unenroll callback (if any) for the given extension
    *
    * @param {string} id The extension id
    *
    * @returns {Promise} A Promise resolved after the unenroll listener
    *                    (if any) has finished its unenroll tasks.
    */
-  onUnenroll(id) {
+  onUnenroll(id, reason) {
     let callbacks = this._unenrollListeners.get(id);
     let promises = [];
     if (callbacks) {
       for (let callback of callbacks) {
-        promises.push(callback());
+        promises.push(callback(reason));
       }
     }
     return Promise.all(promises);
   },
 };
--- a/toolkit/components/normandy/test/unit/test_addon_unenroll.js
+++ b/toolkit/components/normandy/test/unit/test_addon_unenroll.js
@@ -62,47 +62,47 @@ add_task(async function test_addon_unenr
         this.study = class extends ExtensionAPI {
           getAPI(context) {
             return {
               study: {
                 onStudyEnded: new ExtensionCommon.EventManager({
                   context,
                   name: "study.onStudyEnded",
                   register: fire => {
-                    AddonStudies.addUnenrollListener(this.extension.id,
-                                                     () => fire.sync());
+                    AddonStudies.addUnenrollListener(this.extension.id, reason => fire.sync(reason));
                     return () => {};
                   },
                 }).api(),
               },
             };
           }
         };
       },
     },
 
     background() {
-      browser.study.onStudyEnded.addListener(() => {
-        browser.test.sendMessage("got-event");
+      browser.study.onStudyEnded.addListener(reason => {
+        browser.test.sendMessage("got-event", reason);
         return new Promise(resolve => {
           browser.test.onMessage.addListener(resolve);
         });
       });
     },
   });
 
   const server = AddonTestUtils.createHttpServer({hosts: ["example.com"]});
   server.registerFile("/study.xpi", xpi);
 
   // Begin by telling Normandy to install the test extension above
   // that uses a webextension experiment to register a blocking callback
   // to be invoked when the study ends.
   let extension = ExtensionTestUtils.expectExtension(ID);
 
   const RECIPE_ID = 1;
+  const UNENROLL_REASON = "test-ending";
   let action = new AddonStudyAction();
   await action.runRecipe({
     id: RECIPE_ID,
     type: "addon-study",
     arguments: {
       name: "addon unenroll test",
       description: "testing",
       addonUrl: "http://example.com/study.xpi",
@@ -110,26 +110,27 @@ add_task(async function test_addon_unenr
   });
 
   await extension.awaitStartup();
 
   let addon = await AddonManager.getAddonByID(ID);
   ok(addon, "Extension is installed");
 
   // Tell Normandy to end the study, the extension event should be fired.
-  let unenrollPromise = action.unenroll(RECIPE_ID);
+  let unenrollPromise = action.unenroll(RECIPE_ID, UNENROLL_REASON);
 
-  await extension.awaitMessage("got-event");
+  let receivedReason = await extension.awaitMessage("got-event");
   info("Got onStudyEnded event in extension");
+  equal(receivedReason, UNENROLL_REASON, "Unenroll reason should be passed");
 
   // The extension has not yet finished its unenrollment tasks, so it
   // should not yet be uninstalled.
   addon = await AddonManager.getAddonByID(ID);
   ok(addon, "Extension has not yet been uninstalled");
 
   // Once the extension does resolve the promise returned from the
   // event listener, the uninstall can proceed.
   extension.sendMessage("resolve");
   await unenrollPromise;
 
   addon = await AddonManager.getAddonByID(ID);
-  equal(addon, null, "Afer resolving studyEnded promise, extension is uninstalled");
+  equal(addon, null, "After resolving studyEnded promise, extension is uninstalled");
 });