diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /browser/components/extensions/test/xpcshell/test_ext_settings_overrides_defaults.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-upstream.tar.xz firefox-esr-upstream.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/components/extensions/test/xpcshell/test_ext_settings_overrides_defaults.js')
-rw-r--r-- | browser/components/extensions/test/xpcshell/test_ext_settings_overrides_defaults.js | 263 |
1 files changed, 263 insertions, 0 deletions
diff --git a/browser/components/extensions/test/xpcshell/test_ext_settings_overrides_defaults.js b/browser/components/extensions/test/xpcshell/test_ext_settings_overrides_defaults.js new file mode 100644 index 0000000000..dd24be3aff --- /dev/null +++ b/browser/components/extensions/test/xpcshell/test_ext_settings_overrides_defaults.js @@ -0,0 +1,263 @@ +/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set sts=2 sw=2 et tw=80: */ + +"use strict"; + +const { AddonTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/AddonTestUtils.sys.mjs" +); + +const { SearchTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/SearchTestUtils.sys.mjs" +); + +const { SearchUtils } = ChromeUtils.importESModule( + "resource://gre/modules/SearchUtils.sys.mjs" +); + +const { RemoteSettings } = ChromeUtils.importESModule( + "resource://services-settings/remote-settings.sys.mjs" +); + +const { sinon } = ChromeUtils.importESModule( + "resource://testing-common/Sinon.sys.mjs" +); + +const URLTYPE_SUGGEST_JSON = "application/x-suggestions+json"; + +AddonTestUtils.init(this); +AddonTestUtils.createAppInfo( + "xpcshell@tests.mozilla.org", + "XPCShell", + "42", + "42" +); + +const kSearchEngineURL = "https://example.com/?q={searchTerms}&foo=myparams"; +const kSuggestURL = "https://example.com/fake/suggest/"; +const kSuggestURLParams = "q={searchTerms}&type=list2"; + +Services.prefs.setBoolPref("browser.search.log", true); + +add_task(async function setup() { + AddonTestUtils.usePrivilegedSignatures = false; + AddonTestUtils.overrideCertDB(); + await AddonTestUtils.promiseStartupManager(); + await SearchTestUtils.useTestEngines("data", null, [ + { + webExtension: { + id: "test@search.mozilla.org", + }, + appliesTo: [ + { + included: { everywhere: true }, + default: "yes", + }, + ], + }, + { + webExtension: { + id: "test2@search.mozilla.org", + }, + appliesTo: [ + { + included: { everywhere: true }, + }, + ], + }, + ]); + await Services.search.init(); + registerCleanupFunction(async () => { + await AddonTestUtils.promiseShutdownManager(); + }); +}); + +function assertEngineParameters({ + name, + searchURL, + suggestionURL, + messageSnippet, +}) { + let engine = Services.search.getEngineByName(name); + Assert.ok(engine, `Should have found ${name}`); + + Assert.equal( + engine.getSubmission("{searchTerms}").uri.spec, + encodeURI(searchURL), + `Should have ${messageSnippet} the suggestion url.` + ); + Assert.equal( + engine.getSubmission("{searchTerms}", URLTYPE_SUGGEST_JSON)?.uri.spec, + suggestionURL ? encodeURI(suggestionURL) : suggestionURL, + `Should ${messageSnippet} the submission URL.` + ); +} + +add_task(async function test_extension_changing_to_app_provided_default() { + let ext1 = ExtensionTestUtils.loadExtension({ + manifest: { + icons: { + 16: "foo.ico", + }, + chrome_settings_overrides: { + search_provider: { + is_default: true, + name: "MozParamsTest2", + keyword: "MozSearch", + search_url: kSearchEngineURL, + suggest_url: kSuggestURL, + suggest_url_get_params: kSuggestURLParams, + }, + }, + }, + useAddonManager: "temporary", + }); + + await ext1.startup(); + await AddonTestUtils.waitForSearchProviderStartup(ext1); + + Assert.equal( + Services.search.defaultEngine.name, + "MozParamsTest2", + "Should have switched the default engine." + ); + + assertEngineParameters({ + name: "MozParamsTest2", + searchURL: "https://example.com/2/?q={searchTerms}&simple2=5", + messageSnippet: "left unchanged", + }); + + let promiseDefaultBrowserChange = SearchTestUtils.promiseSearchNotification( + "engine-default", + "browser-search-engine-modified" + ); + await ext1.unload(); + await promiseDefaultBrowserChange; + + Assert.equal( + Services.search.defaultEngine.name, + "MozParamsTest", + "Should have reverted to the original default engine." + ); +}); + +add_task(async function test_extension_overriding_app_provided_default() { + const settings = await RemoteSettings(SearchUtils.SETTINGS_ALLOWLIST_KEY); + sinon.stub(settings, "get").returns([ + { + thirdPartyId: "test@thirdparty.example.com", + overridesId: "test2@search.mozilla.org", + urls: [ + { + search_url: "https://example.com/?q={searchTerms}&foo=myparams", + }, + ], + }, + ]); + + let ext1 = ExtensionTestUtils.loadExtension({ + manifest: { + browser_specific_settings: { + gecko: { + id: "test@thirdparty.example.com", + }, + }, + icons: { + 16: "foo.ico", + }, + chrome_settings_overrides: { + search_provider: { + is_default: true, + name: "MozParamsTest2", + keyword: "MozSearch", + search_url: kSearchEngineURL, + suggest_url: kSuggestURL, + suggest_url_get_params: kSuggestURLParams, + }, + }, + }, + useAddonManager: "permanent", + }); + + info("startup"); + + await ext1.startup(); + await AddonTestUtils.waitForSearchProviderStartup(ext1); + + Assert.equal( + Services.search.defaultEngine.name, + "MozParamsTest2", + "Should have switched the default engine." + ); + assertEngineParameters({ + name: "MozParamsTest2", + searchURL: kSearchEngineURL, + suggestionURL: `${kSuggestURL}?${kSuggestURLParams}`, + messageSnippet: "changed", + }); + + info("disable"); + + let promiseDefaultBrowserChange = SearchTestUtils.promiseSearchNotification( + "engine-default", + "browser-search-engine-modified" + ); + await ext1.addon.disable(); + await promiseDefaultBrowserChange; + + Assert.equal( + Services.search.defaultEngine.name, + "MozParamsTest", + "Should have reverted to the original default engine." + ); + assertEngineParameters({ + name: "MozParamsTest2", + searchURL: "https://example.com/2/?q={searchTerms}&simple2=5", + messageSnippet: "reverted", + }); + + info("enable"); + + promiseDefaultBrowserChange = SearchTestUtils.promiseSearchNotification( + "engine-default", + "browser-search-engine-modified" + ); + await ext1.addon.enable(); + await promiseDefaultBrowserChange; + + Assert.equal( + Services.search.defaultEngine.name, + "MozParamsTest2", + "Should have switched the default engine." + ); + + assertEngineParameters({ + name: "MozParamsTest2", + searchURL: kSearchEngineURL, + suggestionURL: `${kSuggestURL}?${kSuggestURLParams}`, + messageSnippet: "changed", + }); + + info("unload"); + + promiseDefaultBrowserChange = SearchTestUtils.promiseSearchNotification( + "engine-default", + "browser-search-engine-modified" + ); + await ext1.unload(); + await promiseDefaultBrowserChange; + + Assert.equal( + Services.search.defaultEngine.name, + "MozParamsTest", + "Should have reverted to the original default engine." + ); + + assertEngineParameters({ + name: "MozParamsTest2", + searchURL: "https://example.com/2/?q={searchTerms}&simple2=5", + messageSnippet: "reverted", + }); + sinon.restore(); +}); |