fune/browser/components/enterprisepolicies/tests/xpcshell/test_addon_update.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

156 lines
4.2 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"
);
const { ExtensionTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/ExtensionXPCShellUtils.sys.mjs"
);
AddonTestUtils.init(this);
AddonTestUtils.overrideCertDB();
AddonTestUtils.appInfo = getAppInfo();
ExtensionTestUtils.init(this);
const server = AddonTestUtils.createHttpServer({ hosts: ["example.com"] });
const BASE_URL = `http://example.com/data`;
let TEST_NAME = "updatable.xpi";
/* Test that when a local file addon is updated,
the new version gets installed. */
add_task(async function test_local_addon_update() {
await AddonTestUtils.promiseStartupManager();
let tmpDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
let id = "updatable1@test";
let xpi1 = AddonTestUtils.createTempWebExtensionFile({
manifest: {
version: "1.0",
browser_specific_settings: {
gecko: { id },
},
},
});
xpi1.copyTo(tmpDir, TEST_NAME);
let extension = ExtensionTestUtils.expectExtension(id);
await Promise.all([
extension.awaitStartup(),
setupPolicyEngineWithJson({
policies: {
ExtensionSettings: {
"updatable1@test": {
installation_mode: "force_installed",
install_url: Services.io.newFileURI(tmpDir).spec + "/" + TEST_NAME,
},
},
},
}),
]);
let addon = await AddonManager.getAddonByID(id);
notEqual(addon, null, "Addon should not be null");
equal(addon.version, "1.0", "Addon 1.0 installed");
let xpi2 = AddonTestUtils.createTempWebExtensionFile({
manifest: {
version: "2.0",
browser_specific_settings: {
gecko: { id },
},
},
});
// overwrite the test file
xpi2.copyTo(tmpDir, TEST_NAME);
extension = ExtensionTestUtils.expectExtension(id);
await Promise.all([
extension.awaitStartup(),
setupPolicyEngineWithJson({
policies: {
ExtensionSettings: {
"updatable1@test": {
installation_mode: "force_installed",
install_url: Services.io.newFileURI(tmpDir).spec + "/" + TEST_NAME,
},
},
},
}),
]);
addon = await AddonManager.getAddonByID(id);
equal(addon.version, "2.0", "Addon 2.0 installed");
let xpifile = tmpDir.clone();
xpifile.append(TEST_NAME);
xpifile.remove(false);
});
/* Test that when the url changes,
the new version gets installed. */
add_task(async function test_newurl_addon_update() {
let id = "updatable2@test";
let xpi1 = AddonTestUtils.createTempWebExtensionFile({
manifest: {
version: "1.0",
browser_specific_settings: {
gecko: { id },
},
},
});
server.registerFile("/data/policy_test1.xpi", xpi1);
let xpi2 = AddonTestUtils.createTempWebExtensionFile({
manifest: {
version: "2.0",
browser_specific_settings: {
gecko: { id },
},
},
});
server.registerFile("/data/policy_test2.xpi", xpi2);
let extension = ExtensionTestUtils.expectExtension(id);
await Promise.all([
extension.awaitStartup(),
setupPolicyEngineWithJson({
policies: {
ExtensionSettings: {
"updatable2@test": {
installation_mode: "force_installed",
install_url: `${BASE_URL}/policy_test1.xpi`,
},
},
},
}),
]);
let addon = await AddonManager.getAddonByID(id);
notEqual(addon, null, "Addon should not be null");
equal(addon.version, "1.0", "Addon 1.0 installed");
extension = ExtensionTestUtils.expectExtension(id);
await Promise.all([
extension.awaitStartup(),
setupPolicyEngineWithJson({
policies: {
ExtensionSettings: {
"updatable2@test": {
installation_mode: "force_installed",
install_url: `${BASE_URL}/policy_test2.xpi`,
},
},
},
}),
]);
addon = await AddonManager.getAddonByID(id);
equal(addon.version, "2.0", "Addon 2.0 installed");
await AddonTestUtils.promiseShutdownManager();
});