/* Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ */ /** * Tests that deprecated config engines that are installed by the user (e.g. via * contextual search) are installed as user search engines, and tests migration * of previous config engines to user search engines. */ "use strict"; ChromeUtils.defineESModuleGetters(this, { AppProvidedConfigEngine: "moz-src:///toolkit/components/search/ConfigSearchEngine.sys.mjs", UserInstalledConfigEngine: "moz-src:///toolkit/components/search/ConfigSearchEngine.sys.mjs", UserSearchEngine: "moz-src:///toolkit/components/search/UserSearchEngine.sys.mjs", }); const ENGINE_NAME = "Additional Engine"; const SECOND_ENGINE_NAME = "Second Additional Engine"; function createEngineConfig(identifier, name, host, deprecated) { return { identifier, base: { name, deprecated, partner: { default: { code: "pc1" } }, urls: { search: { base: `https://${host}/search`, params: [ { name: "foo", value: "bar" }, { name: "pc", value: "{partnerCode}" }, { name: "exp", experimentConfig: "testPref" }, ], searchTermParamName: "q", }, }, }, variants: [{ environment: { regions: ["de"] } }], }; } function createConfig(deprecated) { return [ { identifier: "appDefault" }, createEngineConfig("additional", ENGINE_NAME, "example.net", deprecated), createEngineConfig( "additional2", SECOND_ENGINE_NAME, "example.org", deprecated ), ]; } let notificationBoxStub; add_setup(async function () { let client = RemoteSettings("search-config-icons"); await client.db.clear(); await insertRecordIntoCollection(client, { filename: "svgIcon.svg", engineIdentifiers: ["additional", "additional2"], imageSize: 16, }); Services.prefs .getDefaultBranch("browser.search.param.") .setCharPref("testPref", "exp1"); SearchTestUtils.setRemoteSettingsConfig(createConfig(false)); await SearchService.init(); notificationBoxStub = sinon.stub( SearchService, "_showRemovalOfSearchEngineNotificationBox" ); registerCleanupFunction(() => sinon.restore()); }); async function resetState() { let engine = SearchService.getEngineByName(ENGINE_NAME); if (engine) { await SearchService.removeEngine(engine); } if (Region.home != "US") { await promiseSetHomeRegion("US"); } await SearchTestUtils.updateRemoteSettingsConfig(createConfig(false)); await SearchService.setDefault( SearchService.getEngineById("appDefault"), SearchService.CHANGE_REASON.UNKNOWN ); notificationBoxStub.resetHistory(); } async function installContextualEngine() { let engine = await SearchService.findContextualSearchEngineByHost("example.net"); Assert.ok( engine instanceof UserInstalledConfigEngine, "Should have found a user installed config engine" ); await SearchService.addSearchEngine(engine); engine.alias = "myalias"; engine.setAttr("order", 5); return engine; } async function restartSearchService(config) { await promiseAfterSettings(); SearchService.reset(); SearchTestUtils.setRemoteSettingsConfig(config); await SearchService.init(); } function assertMigrated() { Assert.ok( !SearchService.getEngineById("additional"), "Should have removed the config engine" ); let engine = SearchService.getEngineByName(ENGINE_NAME); Assert.ok( engine instanceof UserSearchEngine, "Should have replaced the engine with a user search engine" ); Assert.equal( engine.getSubmission("test").uri.spec, "https://example.net/search?foo=bar&q=test", "Should have the same search URL" ); Assert.equal(engine.alias, "myalias", "Should have kept the alias"); Assert.equal(engine.getAttr("order"), 5, "Should have kept the order"); return engine; } add_task(async function test_find_by_host_not_deprecated() { let engine = await SearchService.findContextualSearchEngineByHost("example.net"); Assert.ok( engine instanceof UserInstalledConfigEngine, "Should have returned a user installed config engine" ); Assert.ok(!engine.deprecated, "Should not be marked as deprecated"); Assert.equal( engine.getSubmission("test").uri.spec, "https://example.net/search?foo=bar&pc=pc1&exp=exp1&q=test", "Should have the partner code and experiment parameters" ); }); add_task(async function test_find_by_host_deprecated() { await resetState(); await SearchTestUtils.updateRemoteSettingsConfig(createConfig(true)); let engine = await SearchService.findContextualSearchEngineByHost("example.net"); Assert.ok( engine instanceof UserInstalledConfigEngine, "Should have returned a user installed config engine" ); Assert.ok(engine.deprecated, "Should be marked as deprecated"); }); add_task(async function test_install_prompt_tracked_per_deprecated_engine() { let first = await SearchService.findContextualSearchEngineByHost("example.net"); let second = await SearchService.findContextualSearchEngineByHost("example.org"); Assert.ok( !(await SearchService.shouldShowInstallPrompt(first)), "Should not prompt on the first use of the first engine" ); Assert.ok( !(await SearchService.shouldShowInstallPrompt(second)), "Should not prompt on the first use of the second engine" ); Assert.ok( await SearchService.shouldShowInstallPrompt(second), "Should prompt on the second use of the second engine" ); Assert.ok( await SearchService.shouldShowInstallPrompt(first), "Should prompt on the second use of the first engine" ); }); add_task(async function test_add_deprecated_engine() { let engine = await SearchService.findContextualSearchEngineByHost("example.net"); await SearchService.addSearchEngine(engine); let addedEngine = SearchService.getEngineByName(ENGINE_NAME); Assert.ok( addedEngine instanceof UserSearchEngine, "Should have added a user search engine for a deprecated engine" ); Assert.equal( addedEngine.getSubmission("test").uri.spec, "https://example.net/search?foo=bar&q=test", "Should have the same search URL without config only parameters" ); await restartSearchService(createConfig(true)); Assert.ok( SearchService.getEngineByName(ENGINE_NAME) instanceof UserSearchEngine, "Should have kept the user search engine after restart" ); }); add_task(async function test_reload_with_contextual_engine() { await resetState(); let engine = await installContextualEngine(); await SearchService.setDefault(engine, SearchService.CHANGE_REASON.UNKNOWN); await SearchTestUtils.updateRemoteSettingsConfig(createConfig(true)); let newEngine = assertMigrated(); Assert.equal( SearchService.defaultEngine, newEngine, "Should have kept the migrated engine as default" ); Assert.ok( notificationBoxStub.notCalled, "Should not have shown the removal notification box" ); }); add_task(async function test_reload_with_contextual_engine_and_not_default() { await resetState(); await installContextualEngine(); await SearchTestUtils.updateRemoteSettingsConfig(createConfig(true)); assertMigrated(); Assert.equal( SearchService.defaultEngine.id, "appDefault", "Should not have changed the default engine" ); }); add_task( async function test_reload_app_provided_and_contextual_selects_app_provided() { await resetState(); await installContextualEngine(); await promiseSetHomeRegion("de"); Assert.ok( SearchService.getEngineById("additional") instanceof AppProvidedConfigEngine, "Should be an app provided engine in the region" ); await SearchTestUtils.updateRemoteSettingsConfig(createConfig(true)); assertMigrated(); } ); add_task(async function test_startup_with_contextual_engine() { await resetState(); let engine = await installContextualEngine(); await SearchService.setDefault(engine, SearchService.CHANGE_REASON.UNKNOWN); await restartSearchService(createConfig(true)); let newEngine = assertMigrated(); Assert.equal( SearchService.defaultEngine, newEngine, "Should have kept the migrated engine as default" ); Assert.ok( notificationBoxStub.notCalled, "Should not have shown the removal notification box" ); await restartSearchService(createConfig(true)); Assert.equal( (await SearchService.getEngines()).filter(e => e.name == ENGINE_NAME) .length, 1, "Should not have duplicated the engine after restart" ); Assert.equal( SearchService.defaultEngine.name, ENGINE_NAME, "Should have kept the migrated engine as default after restart" ); }); add_task(async function test_startup_with_contextual_engine_hidden() { await resetState(); let engine = await installContextualEngine(); engine.hidden = true; await restartSearchService(createConfig(true)); Assert.ok( !SearchService.getEngineByName(ENGINE_NAME), "Should not have migrated a hidden engine" ); });