tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 3772f90b8ab1cd689dbea4c2bbdae315a8a97984
parent 1ee58eaa7d17fc114f849464fef2eee9f953bed9
Author: James Teow <jteow@mozilla.com>
Date:   Thu,  4 Dec 2025 14:44:08 +0000

Bug 1883392 - Refactor test_override_allowlist_switch.js - r=Standard8

Create more helpers for reusability and to make test code a bit more
declarative, and use runTestScenario for test setup and teardown.

Differential Revision: https://phabricator.services.mozilla.com/D274814

Diffstat:
Mtoolkit/components/search/tests/xpcshell/test_override_allowlist_switch.js | 608+++++++++++++++++++++++++++++++++++++------------------------------------------
1 file changed, 283 insertions(+), 325 deletions(-)

diff --git a/toolkit/components/search/tests/xpcshell/test_override_allowlist_switch.js b/toolkit/components/search/tests/xpcshell/test_override_allowlist_switch.js @@ -12,6 +12,7 @@ const SEARCH_URL_BASE = "https://example.com/"; const SEARCH_URL_PARAMS = `?sourceId=enterprise&q={searchTerms}`; const ENGINE_NAME = "Simple Engine"; +const ALIAS = "star"; const ALLOWLIST = [ { @@ -31,127 +32,73 @@ const ALLOWLIST = [ }, ]; -const CONFIG_SIMPLE_LOCALE_DE_V2 = [ - { - recordType: "engine", - identifier: "basic", - base: { - name: "basic", - urls: { - search: { - base: "https://ar.wikipedia.org/wiki/%D8%AE%D8%A7%D8%B5:%D8%A8%D8%AD%D8%AB", - params: [ - { - name: "sourceId", - value: "Mozilla-search", - }, - ], - searchTermParamName: "search", +function createConfig(simpleEngineEnvironment) { + return [ + { + recordType: "engine", + identifier: "basic", + base: { + name: "basic", + urls: { + search: { + base: "https://ar.wikipedia.org/wiki/%D8%AE%D8%A7%D8%B5:%D8%A8%D8%AD%D8%AB", + params: [ + { + name: "sourceId", + value: "Mozilla-search", + }, + ], + searchTermParamName: "search", + }, }, }, - }, - variants: [ - { - environment: { allRegionsAndLocales: true }, - }, - ], - }, - { - recordType: "engine", - identifier: "simple", - base: { - name: "Simple Engine", - urls: { - search: { - base: "https://example.com", - params: [ - { - name: "sourceId", - value: "Mozilla-search", - }, - ], - searchTermParamName: "search", + variants: [ + { + environment: { allRegionsAndLocales: true }, }, - }, + ], }, - variants: [ - { - environment: { locales: ["de"] }, - }, - ], - }, - { - recordType: "defaultEngines", - globalDefault: "basic", - specificDefaults: [], - }, - { - recordType: "engineOrders", - orders: [], - }, -]; - -const CONFIG_SIMPLE_EVERYWHERE_V2 = [ - { - recordType: "engine", - identifier: "basic", - base: { - name: "basic", - urls: { - search: { - base: "https://ar.wikipedia.org/wiki/%D8%AE%D8%A7%D8%B5:%D8%A8%D8%AD%D8%AB", - params: [ - { - name: "sourceId", - value: "Mozilla-search", - }, - ], - searchTermParamName: "search", + { + recordType: "engine", + identifier: "simple", + base: { + name: "Simple Engine", + urls: { + search: { + base: "https://example.com", + params: [ + { + name: "sourceId", + value: "Mozilla-search", + }, + ], + searchTermParamName: "search", + }, }, }, - }, - variants: [ - { - environment: { allRegionsAndLocales: true }, - }, - ], - }, - { - recordType: "engine", - identifier: "simple", - base: { - name: "Simple Engine", - urls: { - search: { - base: "https://example.com", - params: [ - { - name: "sourceId", - value: "Mozilla-search", - }, - ], - searchTermParamName: "search", + variants: [ + { + environment: simpleEngineEnvironment, }, - }, + ], }, - variants: [ - { - environment: { allRegionsAndLocales: true }, - }, - ], - }, - { - recordType: "defaultEngines", - globalDefault: "basic", - specificDefaults: [], - }, - { - recordType: "engineOrders", - orders: [], - }, -]; + { + recordType: "defaultEngines", + globalDefault: "basic", + specificDefaults: [], + }, + { + recordType: "engineOrders", + orders: [], + }, + ]; +} + +const CONFIG_SIMPLE_LOCALE_DE_V2 = createConfig({ locales: ["de"] }); +const CONFIG_SIMPLE_EVERYWHERE_V2 = createConfig({ + allRegionsAndLocales: true, +}); -let lastEngineId; let extension; let notificationBoxStub; @@ -188,20 +135,22 @@ add_setup(async function () { * user has the WebExtension installed and set as default. */ add_task(async function test_app_provided_engine_deployment_extended() { - await assertCorrectlySwitchedWhenExtended(async () => { - info("Change configuration to include engine in user's environment"); - - await SearchTestUtils.updateRemoteSettingsConfig( - CONFIG_SIMPLE_EVERYWHERE_V2 - ); - }); + await runTestScenario({ + testOpenSearch: false, + extend: async () => { + info("Change configuration to include engine in user's environment"); - await assertCorrectlySwitchedWhenRemoved(async () => { - info("Change configuration to remove engine from user's environment"); + await SearchTestUtils.updateRemoteSettingsConfig( + CONFIG_SIMPLE_EVERYWHERE_V2 + ); + }, + remove: async () => { + info("Change configuration to remove engine from user's environment"); - await SearchTestUtils.updateRemoteSettingsConfig( - CONFIG_SIMPLE_LOCALE_DE_V2 - ); + await SearchTestUtils.updateRemoteSettingsConfig( + CONFIG_SIMPLE_LOCALE_DE_V2 + ); + }, }); }); @@ -212,139 +161,113 @@ add_task(async function test_app_provided_engine_deployment_extended() { */ add_task( async function test_app_provided_engine_deployment_extended_opensearch() { - await assertCorrectlySwitchedWhenExtended(async () => { - info("Change configuration to include engine in user's environment"); - - await SearchTestUtils.updateRemoteSettingsConfig( - CONFIG_SIMPLE_EVERYWHERE_V2 - ); - }, true); - - await assertCorrectlySwitchedWhenRemoved(async () => { - info("Change configuration to remove engine from user's environment"); + await runTestScenario({ + testOpenSearch: true, + extend: async () => { + info("Change configuration to include engine in user's environment"); + + await SearchTestUtils.updateRemoteSettingsConfig( + CONFIG_SIMPLE_EVERYWHERE_V2 + ); + }, + remove: async () => { + info("Change configuration to remove engine from user's environment"); - await SearchTestUtils.updateRemoteSettingsConfig( - CONFIG_SIMPLE_LOCALE_DE_V2 - ); - }, true); + await SearchTestUtils.updateRemoteSettingsConfig( + CONFIG_SIMPLE_LOCALE_DE_V2 + ); + }, + }); } ); add_task( async function test_app_provided_engine_deployment_extended_restart_only() { - await assertCorrectlySwitchedWhenExtended(async () => { - info( - "Change configuration with restart to include engine in user's environment" - ); - - SearchTestUtils.setRemoteSettingsConfig(CONFIG_SIMPLE_EVERYWHERE_V2); - await promiseAfterSettings(); - Services.search.wrappedJSObject.reset(); - await Services.search.init(); - }); - - await assertCorrectlySwitchedWhenRemoved(async () => { - info( - "Change configuration with restart to remove engine from user's environment" - ); - SearchTestUtils.setRemoteSettingsConfig(CONFIG_SIMPLE_LOCALE_DE_V2); - await promiseAfterSettings(); - Services.search.wrappedJSObject.reset(); - await Services.search.init(); - // Ensure settings have been saved before the engines are added, so that - // we know we won't have race conditions when `addEnginesFromExtension` - // loads the settings itself. - await promiseAfterSettings(); - - // Simulate the add-on manager starting up and telling the - // search service about the add-on again. - let extensionData = { - ...extension.extension, - startupReason: "APP_STARTUP", - }; - await Services.search.addEnginesFromExtension(extensionData); + await runTestScenario({ + testOpenSearch: false, + extend: async () => { + info( + "Change configuration with restart to include engine in user's environment" + ); + + await SearchTestUtils.setRemoteSettingsConfig( + CONFIG_SIMPLE_EVERYWHERE_V2 + ); + await restartSearchService(true); + }, + remove: async () => { + info( + "Change configuration with restart to remove engine from user's environment" + ); + + await SearchTestUtils.setRemoteSettingsConfig( + CONFIG_SIMPLE_LOCALE_DE_V2 + ); + await restartSearchService(true); + // Ensure settings have been saved before the engines are added, so that + // we know we won't have race conditions when `addEnginesFromExtension` + // loads the settings itself. + await promiseAfterSettings(); + + // Simulate the add-on manager starting up and telling the + // search service about the add-on again. + await simulateExtensionStartup(false); + }, }); - - let settingsData = await promiseSettingsData(); - Assert.ok( - settingsData.engines.every(e => !e._metaData.overriddenBy), - "Should have cleared the overridden by flag after removal" - ); } ); add_task( async function test_app_provided_engine_deployment_extended_restart_only_startup_extension() { - await assertCorrectlySwitchedWhenExtended(async () => { - info( - "Change configuration with restart to include engine in user's environment" - ); - - SearchTestUtils.setRemoteSettingsConfig(CONFIG_SIMPLE_EVERYWHERE_V2); - await promiseAfterSettings(); - Services.search.wrappedJSObject.reset(); - await Services.search.init(); - }); - - await assertCorrectlySwitchedWhenRemoved(async () => { - info( - "Change configuration with restart to remove engine from user's environment" - ); - - SearchTestUtils.setRemoteSettingsConfig(CONFIG_SIMPLE_LOCALE_DE_V2); - await promiseAfterSettings(); - Services.search.wrappedJSObject.reset(); - // Simulate the add-on manager starting up and telling the - // search service about the add-on again. - // - // In this test, it does this before init() is called, to - // simulate this being a startup extension. - let extensionData = { - ...extension.extension, - startupReason: "APP_STARTUP", - }; - await Services.search.addEnginesFromExtension(extensionData); - - await Services.search.init(); + await runTestScenario({ + testOpenSearch: false, + extend: async () => { + info( + "Change configuration with restart to include engine in user's environment" + ); + + SearchTestUtils.setRemoteSettingsConfig(CONFIG_SIMPLE_EVERYWHERE_V2); + await restartSearchService(true); + }, + remove: async () => { + info( + "Change configuration with restart to remove engine from user's environment" + ); + + SearchTestUtils.setRemoteSettingsConfig(CONFIG_SIMPLE_LOCALE_DE_V2); + await restartSearchService(false); + // Simulate the add-on manager starting up and telling the + // search service about the add-on again. + // + // In this test, it does this before init() is called, to + // simulate this being a startup extension. + await simulateExtensionStartup(false); + + await Services.search.init(); + }, }); - - let settingsData = await promiseSettingsData(); - Assert.ok( - settingsData.engines.every(e => !e._metaData.overriddenBy), - "Should have cleared the overridden by flag after removal" - ); } ); add_task( async function test_app_provided_engine_deployment_extended_opensearch_restart_only() { - await assertCorrectlySwitchedWhenExtended(async () => { - info( - "Change configuration with restart to include engine in user's environment" - ); - - SearchTestUtils.setRemoteSettingsConfig(CONFIG_SIMPLE_EVERYWHERE_V2); - await promiseAfterSettings(); - Services.search.wrappedJSObject.reset(); - await Services.search.init(); - }, true); - - await assertCorrectlySwitchedWhenRemoved(async () => { - info( - "Change configuration with restart to remove engine from user's environment" - ); - - SearchTestUtils.setRemoteSettingsConfig(CONFIG_SIMPLE_LOCALE_DE_V2); - await promiseAfterSettings(); - Services.search.wrappedJSObject.reset(); - await Services.search.init(); - }, true); + await runTestScenario({ + testOpenSearch: true, + extend: async () => { + info( + "Change configuration with restart to include engine in user's enonment" + ); + + SearchTestUtils.setRemoteSettingsConfig(CONFIG_SIMPLE_EVERYWHERE_V2); + await restartSearchService(true); + }, + remove: async () => { + info("Change configuration with restart to remove engine from user's"); - let settingsData = await promiseSettingsData(); - Assert.ok( - settingsData.engines.every(e => !e._metaData.overriddenBy), - "Should have cleared the overridden by flag after removal" - ); + SearchTestUtils.setRemoteSettingsConfig(CONFIG_SIMPLE_LOCALE_DE_V2); + await restartSearchService(true); + }, + }); } ); @@ -354,35 +277,23 @@ add_task( * application provided engine is (or is not) available. */ add_task(async function test_user_environment_changes() { - await assertCorrectlySwitchedWhenExtended(async () => { - info("Change locale to de"); + await runTestScenario({ + testOpenSearch: false, + extend: async () => { + info("Change locale to de"); - await promiseSetLocale("de"); - }); - - await assertCorrectlySwitchedWhenRemoved(async () => { - info("Change locale to en"); + await promiseSetLocale("de"); + }, + remove: async () => { + info("Change locale to en"); - await promiseSetLocale("en"); + await promiseSetLocale("en"); + }, }); }); -/** - * Asserts that overrides are handled correctly when a WebExtension is - * installed, and an application provided engine is added for the user. - * - * This is designed to be used prior to assertCorrectlySwitchedWhenRemoved. - * - * @param {Function} changeFn - * A function that applies the change to cause the application provided - * engine to be added for the user. - * @param {boolean} testOpenSearch - * Set to true to test OpenSearch based engines. - */ -async function assertCorrectlySwitchedWhenExtended( - changeFn, - testOpenSearch = false -) { +async function runTestScenario({ extend, remove, testOpenSearch = false }) { + // Setup the test. await SearchTestUtils.updateRemoteSettingsConfig(CONFIG_SIMPLE_LOCALE_DE_V2); notificationBoxStub.resetHistory(); @@ -392,51 +303,78 @@ async function assertCorrectlySwitchedWhenExtended( } based engine and set as default` ); - let engine; - if (testOpenSearch) { - engine = await SearchTestUtils.installOpenSearchEngine({ - url: `${gHttpURL}/sjs/engineMaker.sjs?${JSON.stringify({ - baseURL: SEARCH_URL_BASE, - queryString: SEARCH_URL_PARAMS, - name: ENGINE_NAME, - method: "GET", - })}`, - }); - } else { - extension = await SearchTestUtils.installSearchExtension( - { - name: ENGINE_NAME, - search_url: SEARCH_URL_BASE, - search_url_get_params: SEARCH_URL_PARAMS, - }, - { skipUnload: true } - ); - await extension.awaitStartup(); - - engine = Services.search.getEngineById( - "simpleengine@tests.mozilla.orgdefault" - ); - } - - await Services.search.setDefault( - engine, - Ci.nsISearchService.CHANGE_REASON_UNKNOWN - ); + let result = await installThirdPartyEngineAsDefault(testOpenSearch); + let engine = result.engine; + // Overwrite the global value of extension. + extension = result.extension; // Set a user defined alias. - engine.alias = "star"; + engine.alias = ALIAS; + // Assert the pre-conditions. await assertEngineCorrectlySet({ expectedId: engine.id, - expectedAlias: "star", + expectedAlias: ALIAS, appEngineOverriden: false, }); + // Make changes and assert them. + await assertCorrectlySwitchedWhenExtended(extend, testOpenSearch); + + // Prepare for removing engines. + notificationBoxStub.resetHistory(); + + await assertCorrectlySwitchedWhenRemoved(remove, engine.id, testOpenSearch); + + let settingsData = await promiseSettingsData(); + Assert.ok( + settingsData.engines.every(e => !e._metaData.overriddenBy), + "Should have cleared the overridden by flag after removal" + ); +} + +async function restartSearchService(initSearchService = false) { + info("Restarting search service."); + await promiseAfterSettings(); + Services.search.wrappedJSObject.reset(); + if (initSearchService) { + await Services.search.init(); + } +} + +async function simulateExtensionStartup(trySetDefault) { + let extensionData = { + ...extension.extension, + startupReason: "APP_STARTUP", + }; + if (trySetDefault) { + await Services.search.maybeSetAndOverrideDefault(extensionData); + } else { + await Services.search.addEnginesFromExtension(extensionData); + } +} + +/** + * Asserts that overrides are handled correctly when a WebExtension is + * installed, and an application provided engine is added for the user. + * + * This is designed to be used prior to assertCorrectlySwitchedWhenRemoved. + * + * @param {Function} changeFn + * A function that applies the change to cause the application provided + * engine to be added for the user. + * @param {boolean} testOpenSearch + * Set to true to test OpenSearch based engines. + */ +async function assertCorrectlySwitchedWhenExtended( + changeFn, + testOpenSearch = false +) { await changeFn(); await assertEngineCorrectlySet({ expectedId: "simple", - expectedAlias: "star", + expectedAlias: ALIAS, appEngineOverriden: true, }); Assert.ok( @@ -445,17 +383,9 @@ async function assertCorrectlySwitchedWhenExtended( ); info("Test restarting search service ensure settings are kept."); - - await promiseAfterSettings(); - Services.search.wrappedJSObject.reset(); - await Services.search.init(); - + await restartSearchService(true); if (!testOpenSearch) { - let extensionData = { - ...extension.extension, - startupReason: "APP_STARTUP", - }; - await Services.search.maybeSetAndOverrideDefault(extensionData); + await simulateExtensionStartup(true); } Assert.ok( @@ -464,12 +394,9 @@ async function assertCorrectlySwitchedWhenExtended( ); await assertEngineCorrectlySet({ expectedId: "simple", - expectedAlias: "star", + expectedAlias: ALIAS, appEngineOverriden: true, }); - - // Save lastEngineId for use in assertCorrectlySwitchedWhenRemoved. - lastEngineId = engine.id; } /** @@ -482,48 +409,42 @@ async function assertCorrectlySwitchedWhenExtended( * @param {Function} changeFn * A function that applies the change to cause the application provided * engine to be removed for the user. + * @param {string} engineId + * The id of the engine. * @param {boolean} testOpenSearch * Set to true to test OpenSearch based engines. */ async function assertCorrectlySwitchedWhenRemoved( changeFn, + engineId, testOpenSearch = false ) { - notificationBoxStub.resetHistory(); - await changeFn(); await assertEngineCorrectlySet({ - expectedId: lastEngineId, - expectedAlias: "star", + expectedId: engineId, + expectedAlias: ALIAS, appEngineOverriden: false, }); info("Test restarting search service to remove application provided engine"); - await promiseAfterSettings(); - Services.search.wrappedJSObject.reset(); + await restartSearchService(false); if (!testOpenSearch) { - let extensionData = { - ...extension.extension, - startupReason: "APP_STARTUP", - }; - await Services.search.addEnginesFromExtension(extensionData); + await simulateExtensionStartup(false); } await Services.search.init(); await assertEngineCorrectlySet({ - expectedId: lastEngineId, - expectedAlias: "star", + expectedId: engineId, + expectedAlias: ALIAS, appEngineOverriden: false, }); if (testOpenSearch) { - await Services.search.removeEngine( - Services.search.getEngineById(lastEngineId) - ); + await Services.search.removeEngine(Services.search.getEngineById(engineId)); } else { await extension.unload(); } @@ -570,3 +491,40 @@ async function assertEngineCorrectlySet({ "Should have the correct alias" ); } + +async function installThirdPartyEngineAsDefault(useOpenSearch) { + let engine; + let extension = null; + + if (useOpenSearch) { + engine = await SearchTestUtils.installOpenSearchEngine({ + url: `${gHttpURL}/sjs/engineMaker.sjs?${JSON.stringify({ + baseURL: SEARCH_URL_BASE, + queryString: SEARCH_URL_PARAMS, + name: ENGINE_NAME, + method: "GET", + })}`, + }); + } else { + extension = await SearchTestUtils.installSearchExtension( + { + name: ENGINE_NAME, + search_url: SEARCH_URL_BASE, + search_url_get_params: SEARCH_URL_PARAMS, + }, + { skipUnload: true } + ); + await extension.awaitStartup(); + + engine = Services.search.getEngineById( + "simpleengine@tests.mozilla.orgdefault" + ); + } + + await Services.search.setDefault( + engine, + Ci.nsISearchService.CHANGE_REASON_UNKNOWN + ); + + return { engine, extension }; +}