Bug 1746751 - Use ChromeUtils.import with one parameter in blocklist test code. r=robwu DONTBUILD

Export new `BlocklistPrivate` to allow tests to reach implementation
objects.

Differential Revision: https://phabricator.services.mozilla.com/D134234
This commit is contained in:
Mathew Hodson
2022-01-07 10:06:51 +00:00
parent 1b201cfb84
commit c65c2cf238
9 changed files with 47 additions and 50 deletions

View File

@@ -560,11 +560,6 @@ module.exports = {
"toolkit/mozapps/extensions/internal/AddonTestUtils.jsm",
"toolkit/mozapps/extensions/test/browser/browser_gmpProvider.js",
"toolkit/mozapps/extensions/test/xpcshell/head_addons.js",
"toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklist_clients.js",
"toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklist_regexp_split.js",
"toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklist_targetapp_filter.js",
"toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklist_telemetry.js",
"toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklistchange.js",
"toolkit/mozapps/extensions/test/xpcshell/test_gmpProvider.js",
"toolkit/mozapps/extensions/test/xpcshell/test_no_addons.js",
"toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js",

View File

@@ -8,7 +8,7 @@
/* eslint "valid-jsdoc": [2, {requireReturn: false}] */
var EXPORTED_SYMBOLS = ["Blocklist"];
var EXPORTED_SYMBOLS = ["Blocklist", "BlocklistPrivate"];
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
@@ -263,8 +263,6 @@ const BlocklistTelemetry = {
},
};
this.BlocklistTelemetry = BlocklistTelemetry;
const Utils = {
/**
* Checks whether this entry is valid for the current OS and ABI.
@@ -509,10 +507,8 @@ async function targetAppFilter(entry, environment) {
* The RemoteSetttings client takes care of filtering out versions that don't apply.
* The code here stores entries in memory and sends them to the gfx component in
* serialized text form, using ',', '\t' and '\n' as separators.
*
* Note: we assign to the global to allow tests to reach the object directly.
*/
this.GfxBlocklistRS = {
const GfxBlocklistRS = {
_ensureInitialized() {
if (this._initialized || !gBlocklistEnabled) {
return;
@@ -686,10 +682,8 @@ this.GfxBlocklistRS = {
*
* This is a legacy format, and implements deprecated operations (bug 1620580).
* ExtensionBlocklistMLBF supersedes this implementation.
*
* Note: we assign to the global to allow tests to reach the object directly.
*/
this.ExtensionBlocklistRS = {
const ExtensionBlocklistRS = {
async _ensureEntries() {
this.ensureInitialized();
if (!this._entries && gBlocklistEnabled) {
@@ -956,10 +950,8 @@ this.ExtensionBlocklistRS = {
*
* Stashes can be used to update the blocklist without forcing the whole MLBF
* to be downloaded again. These stashes are applied on top of the base MLBF.
*
* Note: we assign to the global to allow tests to reach the object directly.
*/
this.ExtensionBlocklistMLBF = {
const ExtensionBlocklistMLBF = {
RS_ATTACHMENT_ID: "addons-mlbf.bin",
async _fetchMLBF(record) {
@@ -1484,3 +1476,11 @@ let Blocklist = {
};
Blocklist._init();
// Allow tests to reach implementation objects.
const BlocklistPrivate = {
BlocklistTelemetry,
ExtensionBlocklistMLBF,
ExtensionBlocklistRS,
GfxBlocklistRS,
};

View File

@@ -774,13 +774,12 @@ var AddonTestUtils = {
* The data to load.
*/
async loadBlocklistRawData(data) {
const bsPass = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm",
null
const { BlocklistPrivate } = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm"
);
const blocklistMapping = {
extensions: bsPass.ExtensionBlocklistRS,
extensionsMLBF: bsPass.ExtensionBlocklistMLBF,
extensions: BlocklistPrivate.ExtensionBlocklistRS,
extensionsMLBF: BlocklistPrivate.ExtensionBlocklistMLBF,
};
// Since we load the specified test data, we shouldn't let the

View File

@@ -1200,7 +1200,9 @@ async function mockGfxBlocklistItemsFromDisk(path) {
async function mockGfxBlocklistItems(items) {
const { generateUUID } = Services.uuid;
let bsPass = ChromeUtils.import("resource://gre/modules/Blocklist.jsm", null);
const { BlocklistPrivate } = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm"
);
const client = RemoteSettings(
Services.prefs.getCharPref("services.blocklist.gfx.collection"),
{ bucketNamePref: "services.blocklist.bucket" }
@@ -1221,7 +1223,7 @@ async function mockGfxBlocklistItems(items) {
await client.db.importChanges({}, collectionTimestamp, records, {
clear: true,
});
let rv = await bsPass.GfxBlocklistRS.checkForEntries();
let rv = await BlocklistPrivate.GfxBlocklistRS.checkForEntries();
return rv;
}

View File

@@ -22,8 +22,12 @@ function enable_blocklist_v2_instead_of_useMLBF() {
Blocklist.allowDeprecatedBlocklistV2 = true;
Services.prefs.setBoolPref("extensions.blocklist.useMLBF", false);
// Sanity check: blocklist v2 has been enabled.
Assert.ok(
!!Blocklist.ExtensionBlocklist._updateEntries,
const { BlocklistPrivate } = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm"
);
Assert.equal(
Blocklist.ExtensionBlocklist,
BlocklistPrivate.ExtensionBlocklistRS,
"ExtensionBlocklistRS should have been enabled"
);
}
@@ -39,11 +43,9 @@ async function load_mlbf_record_as_blob() {
function getExtensionBlocklistMLBF() {
// ExtensionBlocklist.Blocklist is an ExtensionBlocklistMLBF if the useMLBF
// pref is set to true.
// An alternative way to obtain ExtensionBlocklistMLBF is by importing the
// global of Blocklist.jsm and reading ExtensionBlocklistMLBF off it, but
// to avoid using the deprecated ChromeUtils.import(.., null), bug 1524027
// needs to be fixed first. So let's use Blocklist.ExtensionBlocklist.
const ExtensionBlocklistMLBF = Blocklist.ExtensionBlocklist;
const {
BlocklistPrivate: { ExtensionBlocklistMLBF },
} = ChromeUtils.import("resource://gre/modules/Blocklist.jsm");
Assert.ok(
Services.prefs.getBoolPref("extensions.blocklist.useMLBF", false),
"blocklist.useMLBF should be true"

View File

@@ -1,6 +1,5 @@
const BlocklistGlobal = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm",
null
const { BlocklistPrivate } = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm"
);
const { Utils: RemoteSettingsUtils } = ChromeUtils.import(
"resource://services-settings/Utils.jsm"
@@ -29,17 +28,17 @@ add_task(async function setup() {
);
// This will initialize the remote settings clients for blocklists.
BlocklistGlobal.ExtensionBlocklistRS.ensureInitialized();
BlocklistGlobal.GfxBlocklistRS._ensureInitialized();
BlocklistPrivate.ExtensionBlocklistRS.ensureInitialized();
BlocklistPrivate.GfxBlocklistRS._ensureInitialized();
// ExtensionBlocklistMLBF is covered by test_blocklist_mlbf_dump.js.
gBlocklistClients = [
{
client: BlocklistGlobal.ExtensionBlocklistRS._client,
client: BlocklistPrivate.ExtensionBlocklistRS._client,
expectHasDump: IS_ANDROID,
},
{
client: BlocklistGlobal.GfxBlocklistRS._client,
client: BlocklistPrivate.GfxBlocklistRS._client,
expectHasDump: !IS_ANDROID,
},
];

View File

@@ -57,11 +57,10 @@ add_task(async function test_check_matching_works() {
extensions: BLOCKLIST_DATA,
});
let blocklistGlobal = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm",
null
const { BlocklistPrivate } = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm"
);
let parsedEntries = blocklistGlobal.ExtensionBlocklistRS._entries;
let parsedEntries = BlocklistPrivate.ExtensionBlocklistRS._entries;
// Unfortunately, the parsed entries aren't in the same order as the original.
function strForTypeOf(val) {

View File

@@ -1,6 +1,5 @@
const BlocklistGlobal = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm",
null
const { BlocklistPrivate } = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm"
);
const { RemoteSettings } = ChromeUtils.import(
"resource://services-settings/remote-settings.js"
@@ -33,7 +32,7 @@ function run_test() {
);
// This will initialize the remote settings clients for blocklists,
// with their specific options etc.
BlocklistGlobal.ExtensionBlocklistRS.ensureInitialized();
BlocklistPrivate.ExtensionBlocklistRS.ensureInitialized();
// Obtain one of the instantiated client for our tests.
client = RemoteSettings("addons", { bucketName: "blocklists" });

View File

@@ -52,10 +52,12 @@ add_task(async function test_blocklist_lastModified_rs_scalars() {
}
const {
BlocklistPrivate: {
BlocklistTelemetry,
ExtensionBlocklistRS,
ExtensionBlocklistMLBF,
} = ChromeUtils.import("resource://gre/modules/Blocklist.jsm", null);
ExtensionBlocklistRS,
},
} = ChromeUtils.import("resource://gre/modules/Blocklist.jsm");
// Return a promise resolved when the recordRSBlocklistLastModified method
// has been called (by temporarily replacing the method with a function that