Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4224 from AdamHillier/bz1471695
Browse files Browse the repository at this point in the history
Bug 1471695 - add Addon targeting to ASRouter
  • Loading branch information
k88hudson committed Jul 4, 2018
2 parents 061c05f + 94a080b commit ba90fe5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/ASRouterTargeting.jsm
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
ChromeUtils.import("resource://gre/modules/components-utils/FilterExpressions.jsm");
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.defineModuleGetter(this, "AddonManager",
"resource://gre/modules/AddonManager.jsm");
ChromeUtils.defineModuleGetter(this, "ProfileAge",
"resource://gre/modules/ProfileAge.jsm");
ChromeUtils.import("resource://gre/modules/Console.jsm");
Expand Down Expand Up @@ -27,6 +29,28 @@ const TargetingGetters = {
get hasFxAccount() {
return Services.prefs.prefHasUserValue(FXA_USERNAME_PREF);
},
get addonsInfo() {
return AddonManager.getActiveAddons(["extension", "service"])
.then(({addons, fullData}) => {
const info = {};
for (const addon of addons) {
info[addon.id] = {
version: addon.version,
type: addon.type,
isSystem: addon.isSystem,
isWebExtension: addon.isWebExtension
};
if (fullData) {
Object.assign(info[addon.id], {
name: addon.name,
userDisabled: addon.userDisabled,
installDate: addon.installDate
});
}
}
return {addons: info, isFullData: fullData};
});
},
// Temporary targeting function for the purposes of running the simplified onboarding experience
get isInExperimentCohort() {
return Services.prefs.getIntPref(ONBOARDING_EXPERIMENT_PREF, 0);
Expand Down
63 changes: 63 additions & 0 deletions test/functional/mochitest/browser_asrouter_targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ ChromeUtils.defineModuleGetter(this, "ASRouterTargeting",
"resource://activity-stream/lib/ASRouterTargeting.jsm");
ChromeUtils.defineModuleGetter(this, "ProfileAge",
"resource://gre/modules/ProfileAge.jsm");
ChromeUtils.defineModuleGetter(this, "AddonManager",
"resource://gre/modules/AddonManager.jsm");

const {AddonTestUtils} = ChromeUtils.import("resource://testing-common/AddonTestUtils.jsm", {});

// ASRouterTargeting.isMatch
add_task(async function should_do_correct_targeting() {
Expand Down Expand Up @@ -66,3 +70,62 @@ add_task(async function checkhasFxAccount() {
is(await ASRouterTargeting.findMatchingMessage([message], {}), message,
"should select correct item by hasFxAccount");
});

AddonTestUtils.initMochitest(this);

add_task(async function checkAddonsInfo() {
const FAKE_ID = "testaddon@tests.mozilla.org";
const FAKE_NAME = "Test Addon";
const FAKE_VERSION = "0.5.7";

const xpi = AddonTestUtils.createTempWebExtensionFile({
manifest: {
applications: {gecko: {id: FAKE_ID}},
name: FAKE_NAME,
version: FAKE_VERSION
}
});

await Promise.all([
AddonTestUtils.promiseWebExtensionStartup(FAKE_ID),
AddonManager.installTemporaryAddon(xpi)
]);

const {addons} = await AddonManager.getActiveAddons(["extension", "service"]);

const {addons: asRouterAddons, isFullData} = await ASRouterTargeting.Environment.addonsInfo;

ok(addons.every(({id}) => asRouterAddons[id]), "should contain every addon");

ok(Object.getOwnPropertyNames(asRouterAddons).every(id => addons.some(addon => addon.id === id)),
"should contain no incorrect addons");

const testAddon = asRouterAddons[FAKE_ID];

ok(Object.prototype.hasOwnProperty.call(testAddon, "version") && testAddon.version === FAKE_VERSION,
"should correctly provide `version` property");

ok(Object.prototype.hasOwnProperty.call(testAddon, "type") && testAddon.type === "extension",
"should correctly provide `type` property");

ok(Object.prototype.hasOwnProperty.call(testAddon, "isSystem") && testAddon.isSystem === false,
"should correctly provide `isSystem` property");

ok(Object.prototype.hasOwnProperty.call(testAddon, "isWebExtension") && testAddon.isWebExtension === true,
"should correctly provide `isWebExtension` property");

// As we installed our test addon the addons database must be initialised, so
// (in this test environment) we expect to receive "full" data

ok(isFullData, "should receive full data");

ok(Object.prototype.hasOwnProperty.call(testAddon, "name") && testAddon.name === FAKE_NAME,
"should correctly provide `name` property from full data");

ok(Object.prototype.hasOwnProperty.call(testAddon, "userDisabled") && testAddon.userDisabled === false,
"should correctly provide `userDisabled` property from full data");

ok(Object.prototype.hasOwnProperty.call(testAddon, "installDate") &&
(Math.abs(new Date() - new Date(testAddon.installDate)) < 60 * 1000),
"should correctly provide `installDate` property from full data");
});

0 comments on commit ba90fe5

Please sign in to comment.