Bug 1240868 - Use a mock addon provider to feed broken data in Telemetry. r=gfritzsche, a=sylvestre
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Wed, 20 Jan 2016 06:58:00 +0100
changeset 298476 0cd0ae95d865bb1a3f8b49c3e6fff165070d3b7a
parent 298475 3ef0b9ecf2eea91b3d55e29ec2605320d74769d6
child 298477 99676559916e6d645dde7ab79dffcc7f2f15c793
push id8960
push usercbook@mozilla.com
push dateFri, 22 Jan 2016 15:31:24 +0000
treeherdermozilla-aurora@0cd0ae95d865 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgfritzsche, sylvestre
bugs1240868
milestone45.0a2
Bug 1240868 - Use a mock addon provider to feed broken data in Telemetry. r=gfritzsche, a=sylvestre
toolkit/components/telemetry/tests/unit/test_TelemetryEnvironment.js
--- a/toolkit/components/telemetry/tests/unit/test_TelemetryEnvironment.js
+++ b/toolkit/components/telemetry/tests/unit/test_TelemetryEnvironment.js
@@ -116,16 +116,110 @@ var PluginHost = {
     throw Components.results.NS_ERROR_NO_INTERFACE;
   }
 }
 
 function registerFakePluginHost() {
   MockRegistrar.register("@mozilla.org/plugin/host;1", PluginHost);
 }
 
+function MockAddonWrapper(aAddon) {
+  this.addon = aAddon;
+}
+MockAddonWrapper.prototype = {
+  get id() {
+    return this.addon.id;
+  },
+
+  get type() {
+    return "service";
+  },
+
+  get appDisabled() {
+    return false;
+  },
+
+  get isCompatible() {
+    return true;
+  },
+
+  get isPlatformCompatible() {
+    return true;
+  },
+
+  get scope() {
+    return AddonManager.SCOPE_PROFILE;
+  },
+
+  get foreignInstall() {
+    return false;
+  },
+
+  get providesUpdatesSecurely() {
+    return true;
+  },
+
+  get blocklistState() {
+    return 0; // Not blocked.
+  },
+
+  get pendingOperations() {
+    return AddonManager.PENDING_NONE;
+  },
+
+  get permissions() {
+    return AddonManager.PERM_CAN_UNINSTALL | AddonManager.PERM_CAN_DISABLE;
+  },
+
+  get isActive() {
+    return true;
+  },
+
+  get name() {
+    return this.addon.name;
+  },
+
+  get version() {
+    return this.addon.version;
+  },
+
+  get creator() {
+    return new AddonManagerPrivate.AddonAuthor(this.addon.author);
+  },
+
+  get userDisabled() {
+    return this.appDisabled;
+  },
+};
+
+function createMockAddonProvider(aName) {
+  let mockProvider = {
+    _addons: [],
+
+    get name() {
+      return aName;
+    },
+
+    addAddon: function(aAddon) {
+      this._addons.push(aAddon);
+      AddonManagerPrivate.callAddonListeners("onInstalled", new MockAddonWrapper(aAddon));
+    },
+
+    getAddonsByTypes: function (aTypes, aCallback) {
+      aCallback(this._addons.map(a => new MockAddonWrapper(a)));
+    },
+
+    shutdown() {
+      return Promise.resolve();
+    },
+  };
+
+  return mockProvider;
+};
+
 /**
  * Used to spoof the Persona Id.
  */
 function spoofTheme(aId, aName, aDesc) {
   return {
     id: aId,
     name: aName,
     description: aDesc,
@@ -1043,21 +1137,19 @@ add_task(function* test_addonsFieldsLimi
                "The name string must have been limited");
   Assert.lessOrEqual(targetAddon.description.length, 100,
                "The description string must have been limited");
 });
 
 add_task(function* test_collectionWithbrokenAddonData() {
   const BROKEN_ADDON_ID = "telemetry-test2.example.com@services.mozilla.org";
   const BROKEN_MANIFEST = {
-    name: "telemetry social provider",
+    id: "telemetry-test2.example.com@services.mozilla.org",
+    name: "telemetry broken addon",
     origin: "https://telemetry-test2.example.com",
-    sidebarURL: "https://telemetry-test2.example.com/browser/browser/base/content/test/social/social_sidebar.html",
-    workerURL: "https://telemetry-test2.example.com/browser/browser/base/content/test/social/social_worker.js",
-    iconURL: "https://telemetry-test2.example.com/browser/browser/base/content/test/general/moz.png",
     version: 1, // This is intentionally not a string.
     signedState: AddonManager.SIGNEDSTATE_SIGNED,
   };
 
   const ADDON_INSTALL_URL = gDataRoot + "restartless.xpi";
   const ADDON_ID = "tel-restartless-xpi@tests.mozilla.org";
   const ADDON_INSTALL_DATE = truncateToDays(Date.now());
   const EXPECTED_ADDON_DATA = {
@@ -1091,31 +1183,21 @@ add_task(function* test_collectionWithbr
       }));
   };
 
   let assertCheckpoint = (aExpected) => {
     Assert.equal(receivedNotifications, aExpected);
     TelemetryEnvironment.unregisterChangeListener("testBrokenAddon_collection" + aExpected);
   };
 
-  // Initialize Social in order to use the SocialService later.
-  let Social = Cu.import("resource:///modules/Social.jsm", {}).Social;
-  Social.init();
-  Assert.ok(Social.initialized, "Social is now initialized");
-
-  // Add the broken provider to the SocialService.
-  const PROVIDER_DATA = {
-    origin: BROKEN_MANIFEST.origin,
-    manifest: BROKEN_MANIFEST,
-  };
-
-  let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService;
+  // Register the broken provider and install the broken addon.
   let checkpointPromise = registerCheckpointPromise(1);
-  SocialService.installProvider(PROVIDER_DATA, () => SocialService.enableProvider(BROKEN_MANIFEST.origin),
-                                { bypassInstallPanel: true });
+  let brokenAddonProvider = createMockAddonProvider("Broken Extensions Provider");
+  AddonManagerPrivate.registerProvider(brokenAddonProvider);
+  brokenAddonProvider.addAddon(BROKEN_MANIFEST);
   yield checkpointPromise;
   assertCheckpoint(1);
 
   // Set the clock in the future so our changes don't get throttled.
   gNow = fakeNow(futureDate(gNow, 10 * MILLISECONDS_PER_MINUTE));
   // Now install an addon which returns the correct information.
   checkpointPromise = registerCheckpointPromise(2);
   yield AddonTestUtils.installXPIFromURL(ADDON_INSTALL_URL);
@@ -1132,20 +1214,18 @@ add_task(function* test_collectionWithbr
             "The addon with the broken manifest must be reported.");
   Assert.equal(activeAddons[BROKEN_ADDON_ID].version, null,
                "null should be reported for invalid data.");
   Assert.ok(ADDON_ID in activeAddons,
             "The valid addon must be reported.");
   Assert.equal(activeAddons[ADDON_ID].description, EXPECTED_ADDON_DATA.description,
                "The description for the valid addon should be correct.");
 
-  // Uninstall the broken addon so don't mess with other tests.
-  deferred = PromiseUtils.defer();
-  SocialService.uninstallProvider(BROKEN_MANIFEST.origin, deferred.resolve);
-  yield deferred.promise;
+  // Unregister the broken provider so we don't mess with other tests.
+  AddonManagerPrivate.unregisterProvider(brokenAddonProvider);
 
   // Uninstall the valid addon.
   yield AddonTestUtils.uninstallAddonByID(ADDON_ID);
 });
 
 add_task(function* test_changeThrottling() {
   const PREF_TEST = "toolkit.telemetry.test.pref1";
   const PREFS_TO_WATCH = new Map([