Bug 1238142 - Add test coverage. r=gfritzsche, a=sylvestre
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Wed, 13 Jan 2016 08:46:00 +0100
changeset 310946 3ef0b9ecf2eea91b3d55e29ec2605320d74769d6
parent 310945 ea4ce436a50c918b1721acc7081b129a2c9ac7e8
child 310947 0cd0ae95d865bb1a3f8b49c3e6fff165070d3b7a
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgfritzsche, sylvestre
bugs1238142
milestone45.0a2
Bug 1238142 - Add test coverage. 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
@@ -555,31 +555,33 @@ function checkActiveGMPlugin(data) {
   // GMP plugin version defaults to null until GMPDownloader runs to update it.
   if (data.version) {
     Assert.equal(typeof data.version, "string");
   }
   Assert.equal(typeof data.userDisabled, "boolean");
   Assert.equal(typeof data.applyBackgroundUpdates, "number");
 }
 
-function checkAddonsSection(data) {
+function checkAddonsSection(data, expectBrokenAddons) {
   const EXPECTED_FIELDS = [
     "activeAddons", "theme", "activePlugins", "activeGMPlugins", "activeExperiment",
     "persona",
   ];
 
   Assert.ok("addons" in data, "There must be an addons section in Environment.");
   for (let f of EXPECTED_FIELDS) {
     Assert.ok(f in data.addons, f + " must be available.");
   }
 
   // Check the active addons, if available.
-  let activeAddons = data.addons.activeAddons;
-  for (let addon in activeAddons) {
-    checkActiveAddon(activeAddons[addon]);
+  if (!expectBrokenAddons) {
+    let activeAddons = data.addons.activeAddons;
+    for (let addon in activeAddons) {
+      checkActiveAddon(activeAddons[addon]);
+    }
   }
 
   // Check "theme" structure.
   if (Object.keys(data.addons.theme).length !== 0) {
     checkTheme(data.addons.theme);
   }
 
   // Check the active plugins.
@@ -600,23 +602,23 @@ function checkAddonsSection(data) {
     Assert.ok(checkString(experiment.id));
     Assert.ok(checkString(experiment.branch));
   }
 
   // Check persona
   Assert.ok(checkNullOrString(data.addons.persona));
 }
 
-function checkEnvironmentData(data, isInitial = false) {
+function checkEnvironmentData(data, isInitial = false, expectBrokenAddons = false) {
   checkBuildSection(data);
   checkSettingsSection(data);
   checkProfileSection(data);
   checkPartnerSection(data, isInitial);
   checkSystemSection(data);
-  checkAddonsSection(data);
+  checkAddonsSection(data, expectBrokenAddons);
 }
 
 function run_test() {
   // Load a custom manifest to provide search engine loading from JAR files.
   do_load_manifest("chrome.manifest");
   do_test_pending();
   spoofGfxAdapter();
   do_get_profile();
@@ -959,16 +961,19 @@ add_task(function* test_addonsAndPlugins
 
   // Check plugin mime types.
   Assert.ok(targetPlugin.mimeTypes.find(m => m == PLUGIN_MIME_TYPE1));
   Assert.ok(targetPlugin.mimeTypes.find(m => m == PLUGIN_MIME_TYPE2));
   Assert.ok(!targetPlugin.mimeTypes.find(m => m == "Not There."));
 
   let personaId = (gIsGonk) ? null : PERSONA_ID;
   Assert.equal(data.addons.persona, personaId, "The correct Persona Id must be reported.");
+
+  // Uninstall the addon.
+  yield AddonTestUtils.uninstallAddonByID(ADDON_ID);
 });
 
 add_task(function* test_signedAddon() {
   const ADDON_INSTALL_URL = gDataRoot + "signed.xpi";
   const ADDON_ID = "tel-signed-xpi@tests.mozilla.org";
   const ADDON_INSTALL_DATE = truncateToDays(Date.now());
   const EXPECTED_ADDON_DATA = {
     blocklisted: false,
@@ -1035,16 +1040,117 @@ add_task(function* test_addonsFieldsLimi
   Assert.lessOrEqual(targetAddon.version.length, 100,
                "The version string must have been limited");
   Assert.lessOrEqual(targetAddon.name.length, 100,
                "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",
+    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 = {
+    blocklisted: false,
+    description: "A restartless addon which gets enabled without a reboot.",
+    name: "XPI Telemetry Restartless Test",
+    userDisabled: false,
+    appDisabled: false,
+    version: "1.0",
+    scope: 1,
+    type: "extension",
+    foreignInstall: false,
+    hasBinaryComponents: false,
+    installDay: ADDON_INSTALL_DATE,
+    updateDay: ADDON_INSTALL_DATE,
+    signedState: mozinfo.addon_signing ? AddonManager.SIGNEDSTATE_MISSING :
+                                         AddonManager.SIGNEDSTATE_NOT_REQUIRED,
+  };
+
+  let deferred = PromiseUtils.defer();
+  let receivedNotifications = 0;
+
+  let registerCheckpointPromise = (aExpected) => {
+    gNow = futureDate(gNow, 10 * MILLISECONDS_PER_MINUTE);
+    fakeNow(gNow);
+    return new Promise(resolve => TelemetryEnvironment.registerChangeListener(
+      "testBrokenAddon_collection" + aExpected, (reason, data) => {
+        Assert.equal(reason, "addons-changed");
+        receivedNotifications++;
+        resolve();
+      }));
+  };
+
+  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;
+  let checkpointPromise = registerCheckpointPromise(1);
+  SocialService.installProvider(PROVIDER_DATA, () => SocialService.enableProvider(BROKEN_MANIFEST.origin),
+                                { bypassInstallPanel: true });
+  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);
+  yield checkpointPromise;
+  assertCheckpoint(2);
+
+  // Check that the new environment contains the Social addon installed with the broken
+  // manifest and the rest of the data.
+  let data = TelemetryEnvironment.currentEnvironment;
+  checkEnvironmentData(data, false, true /* expect broken addons*/);
+
+  let activeAddons = data.addons.activeAddons;
+  Assert.ok(BROKEN_ADDON_ID in activeAddons,
+            "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;
+
+  // 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([
     [PREF_TEST, {what: TelemetryEnvironment.RECORD_PREF_STATE}],
   ]);
   Preferences.reset(PREF_TEST);
 
   gNow = futureDate(gNow, 10 * MILLISECONDS_PER_MINUTE);