fune/browser/components/enterprisepolicies/tests/xpcshell/test_extensionsettings.js
Rob Wu 3c0c3a2676 Bug 1836482 - Replace AddonManager.jsm imports with AddonManager.sys.mjs r=Standard8,webcompat-reviewers,twisniewski
This patch was generated as follows:

Run:
`./mach esmify --imports . --prefix=toolkit/mozapps/extensions/AddonManager`
In the output there are linter/prettifier errors due to unused
XPCOMUtils or separate importESModule calls. These have been fixed
manually and verified with `./mach lint --outgoing`.

The `esmify` script also inserts many unwanted newlines around imports
that are broken on two lines due to length. Due to the number of these,
I fixed them programatically.

1. Create patch from the changes so far.
2. From the patch, delete all lines that consist of "+" (i.e. added blank line).
3. Reset the working dir and apply the revised patch.
4. Verify that the diff between step 1 and 3 looks reasonable.
5. Verify that this patch as a whole looks reasonable.

Commands:

```
git diff > rename.diff
:%g/^+$/d
git commit -va -m WIP-rename
git revert HEAD
git apply --recount rename.diff
git diff HEAD^  # and verify that the removed lines are ok.
git commit -va  # one last review to verify correctness of whole patch.
git rebase -i HEAD~3  # drop the WIP + reverted commit, pick only the last.
```

`git apply` has the `--recount` option to force it to ignore mismatches
in line counts, which happens because we deleted added lines (^+$)
without fixing up the line counts in the file headers.

Differential Revision: https://phabricator.services.mozilla.com/D179874
2023-06-04 13:44:45 +00:00

291 lines
7.8 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const { AddonTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/AddonTestUtils.sys.mjs"
);
const { AddonManager } = ChromeUtils.importESModule(
"resource://gre/modules/AddonManager.sys.mjs"
);
AddonTestUtils.init(this);
AddonTestUtils.overrideCertDB();
AddonTestUtils.appInfo = getAppInfo();
const server = AddonTestUtils.createHttpServer({ hosts: ["example.com"] });
const BASE_URL = `http://example.com/data`;
let addonID = "policytest2@mozilla.com";
let themeID = "policytheme@mozilla.com";
let fileURL;
add_task(async function setup() {
await AddonTestUtils.promiseStartupManager();
let webExtensionFile = AddonTestUtils.createTempWebExtensionFile({
manifest: {
browser_specific_settings: {
gecko: {
id: addonID,
},
},
},
});
server.registerFile("/data/policy_test.xpi", webExtensionFile);
fileURL = Services.io
.newFileURI(webExtensionFile)
.QueryInterface(Ci.nsIFileURL);
});
add_task(async function test_extensionsettings() {
await setupPolicyEngineWithJson({
policies: {
ExtensionSettings: {
"extension1@mozilla.com": {
blocked_install_message: "Extension1 error message.",
},
"*": {
blocked_install_message: "Generic error message.",
},
},
},
});
let extensionSettings = Services.policies.getExtensionSettings(
"extension1@mozilla.com"
);
equal(
extensionSettings.blocked_install_message,
"Extension1 error message.",
"Should have extension specific message."
);
extensionSettings = Services.policies.getExtensionSettings(
"extension2@mozilla.com"
);
equal(
extensionSettings.blocked_install_message,
"Generic error message.",
"Should have generic message."
);
});
add_task(async function test_addon_blocked() {
await setupPolicyEngineWithJson({
policies: {
ExtensionSettings: {
"policytest2@mozilla.com": {
installation_mode: "blocked",
},
},
},
});
let install = await AddonManager.getInstallForURL(
BASE_URL + "/policy_test.xpi"
);
await install.install();
notEqual(install.addon, null, "Addon should not be null");
equal(install.addon.appDisabled, true, "Addon should be disabled");
await install.addon.uninstall();
});
add_task(async function test_addon_allowed() {
await setupPolicyEngineWithJson({
policies: {
ExtensionSettings: {
"policytest2@mozilla.com": {
installation_mode: "allowed",
},
"*": {
installation_mode: "blocked",
},
},
},
});
let install = await AddonManager.getInstallForURL(
BASE_URL + "/policy_test.xpi"
);
await install.install();
notEqual(install.addon, null, "Addon should not be null");
equal(install.addon.appDisabled, false, "Addon should not be disabled");
await install.addon.uninstall();
});
add_task(async function test_addon_uninstalled() {
let install = await AddonManager.getInstallForURL(
BASE_URL + "/policy_test.xpi"
);
await install.install();
notEqual(install.addon, null, "Addon should not be null");
await Promise.all([
AddonTestUtils.promiseAddonEvent("onUninstalled"),
setupPolicyEngineWithJson({
policies: {
ExtensionSettings: {
"*": {
installation_mode: "blocked",
},
},
},
}),
]);
let addon = await AddonManager.getAddonByID(addonID);
equal(addon, null, "Addon should be null");
});
add_task(async function test_addon_forceinstalled() {
await Promise.all([
AddonTestUtils.promiseInstallEvent("onInstallEnded"),
setupPolicyEngineWithJson({
policies: {
ExtensionSettings: {
"policytest2@mozilla.com": {
installation_mode: "force_installed",
install_url: BASE_URL + "/policy_test.xpi",
},
},
},
}),
]);
let addon = await AddonManager.getAddonByID(addonID);
notEqual(addon, null, "Addon should not be null");
equal(addon.appDisabled, false, "Addon should not be disabled");
equal(
addon.permissions & AddonManager.PERM_CAN_UNINSTALL,
0,
"Addon should not be able to be uninstalled."
);
equal(
addon.permissions & AddonManager.PERM_CAN_DISABLE,
0,
"Addon should not be able to be disabled."
);
await addon.uninstall();
});
add_task(async function test_addon_normalinstalled() {
await Promise.all([
AddonTestUtils.promiseInstallEvent("onInstallEnded"),
setupPolicyEngineWithJson({
policies: {
ExtensionSettings: {
"policytest2@mozilla.com": {
installation_mode: "normal_installed",
install_url: BASE_URL + "/policy_test.xpi",
},
},
},
}),
]);
let addon = await AddonManager.getAddonByID(addonID);
notEqual(addon, null, "Addon should not be null");
equal(addon.appDisabled, false, "Addon should not be disabled");
equal(
addon.permissions & AddonManager.PERM_CAN_UNINSTALL,
0,
"Addon should not be able to be uninstalled."
);
notEqual(
addon.permissions & AddonManager.PERM_CAN_DISABLE,
0,
"Addon should be able to be disabled."
);
await addon.uninstall();
});
add_task(async function test_extensionsettings_string() {
await setupPolicyEngineWithJson({
policies: {
ExtensionSettings: '{"*": {"installation_mode": "blocked"}}',
},
});
let extensionSettings = Services.policies.getExtensionSettings("*");
equal(extensionSettings.installation_mode, "blocked");
});
add_task(async function test_extensionsettings_string() {
let restrictedDomains = Services.prefs.getCharPref(
"extensions.webextensions.restrictedDomains"
);
await setupPolicyEngineWithJson({
policies: {
ExtensionSettings:
'{"*": {"restricted_domains": ["example.com","example.org"]}}',
},
});
let newRestrictedDomains = Services.prefs.getCharPref(
"extensions.webextensions.restrictedDomains"
);
equal(newRestrictedDomains, restrictedDomains + ",example.com,example.org");
});
add_task(async function test_theme() {
let themeFile = AddonTestUtils.createTempWebExtensionFile({
manifest: {
browser_specific_settings: {
gecko: {
id: themeID,
},
},
theme: {},
},
});
server.registerFile("/data/policy_theme.xpi", themeFile);
await Promise.all([
AddonTestUtils.promiseInstallEvent("onInstallEnded"),
setupPolicyEngineWithJson({
policies: {
ExtensionSettings: {
"policytheme@mozilla.com": {
installation_mode: "normal_installed",
install_url: BASE_URL + "/policy_theme.xpi",
},
},
},
}),
]);
let currentTheme = Services.prefs.getCharPref("extensions.activeThemeID");
equal(currentTheme, themeID, "Theme should be active");
let addon = await AddonManager.getAddonByID(themeID);
await addon.uninstall();
});
add_task(async function test_addon_normalinstalled_file() {
await Promise.all([
AddonTestUtils.promiseInstallEvent("onInstallEnded"),
setupPolicyEngineWithJson({
policies: {
ExtensionSettings: {
"policytest2@mozilla.com": {
installation_mode: "normal_installed",
install_url: fileURL.spec,
},
},
},
}),
]);
let addon = await AddonManager.getAddonByID(addonID);
notEqual(addon, null, "Addon should not be null");
equal(addon.appDisabled, false, "Addon should not be disabled");
equal(
addon.permissions & AddonManager.PERM_CAN_UNINSTALL,
0,
"Addon should not be able to be uninstalled."
);
notEqual(
addon.permissions & AddonManager.PERM_CAN_DISABLE,
0,
"Addon should be able to be disabled."
);
await addon.uninstall();
});