From a1ed3bd737dd53e0f5b8b4eb5738ea7939a0db2c Mon Sep 17 00:00:00 2001 From: Mark Banner Date: Wed, 3 Jan 2024 16:29:53 +0000 Subject: [PATCH] Bug 1872757 - Update SearchTestUtils.useTestEngines to be able to load search-config-v2 configurations. r=mcheang This also adds data1/search-config-v2.json which uses it and starts to get some tests passing when search-config-v2 is enabled. Differential Revision: https://phabricator.services.mozilla.com/D197547 --- .../search/tests/SearchTestUtils.sys.mjs | 51 ++++++--- .../xpcshell/data1/search-config-v2.json | 101 ++++++++++++++++++ .../search/tests/xpcshell/xpcshell.toml | 1 + 3 files changed, 140 insertions(+), 13 deletions(-) create mode 100644 toolkit/components/search/tests/xpcshell/data1/search-config-v2.json diff --git a/toolkit/components/search/tests/SearchTestUtils.sys.mjs b/toolkit/components/search/tests/SearchTestUtils.sys.mjs index a2dc9b9e01ad..d415737fa7c0 100644 --- a/toolkit/components/search/tests/SearchTestUtils.sys.mjs +++ b/toolkit/components/search/tests/SearchTestUtils.sys.mjs @@ -120,37 +120,62 @@ export var SearchTestUtils = { }, /** - * Load engines from test data located in particular folders. + * For xpcshell tests, configures loading engines from test data located in + * particular folders. * * @param {string} [folder] * The folder name to use. * @param {string} [subFolder] * The subfolder to use, if any. - * @param {Array} [config] + * @param {Array} [configData] * An array which contains the configuration to set. * @returns {object} * An object that is a sinon stub for the configuration getter. */ - async useTestEngines(folder = "data", subFolder = null, config = null) { - let url = `resource://test/${folder}/`; - if (subFolder) { - url += `${subFolder}/`; + async useTestEngines(folder = "data", subFolder = null, configData = null) { + if (!lazy.SearchUtils.newSearchConfigEnabled) { + let url = `resource://test/${folder}/`; + if (subFolder) { + url += `${subFolder}/`; + } + let resProt = Services.io + .getProtocolHandler("resource") + .QueryInterface(Ci.nsIResProtocolHandler); + resProt.setSubstitution("search-extensions", Services.io.newURI(url)); } - let resProt = Services.io - .getProtocolHandler("resource") - .QueryInterface(Ci.nsIResProtocolHandler); - resProt.setSubstitution("search-extensions", Services.io.newURI(url)); const settings = await lazy.RemoteSettings(lazy.SearchUtils.SETTINGS_KEY); - if (config) { - return lazy.sinon.stub(settings, "get").returns(config); + if (configData) { + return lazy.sinon.stub(settings, "get").returns(configData); } - let response = await fetch(`resource://search-extensions/engines.json`); + let workDir = Services.dirsvc.get("CurWorkD", Ci.nsIFile); + let configFileName = + "file://" + + PathUtils.join( + workDir.path, + folder, + subFolder ?? "", + lazy.SearchUtils.newSearchConfigEnabled + ? "search-config-v2.json" + : "engines.json" + ); + + let response = await fetch(configFileName); let json = await response.json(); return lazy.sinon.stub(settings, "get").returns(json.data); }, + /** + * For mochitests, configures loading engines from test data located in + * particular folders. This will cleanup at the end of the test. + * + * This will be removed when the old configuration is removed + * (newSearchConfigEnabled = false). + * + * @param {nsIFile} testDir + * The test directory to use. + */ async useMochitestEngines(testDir) { // Replace the path we load search engines from with // the path to our test data. diff --git a/toolkit/components/search/tests/xpcshell/data1/search-config-v2.json b/toolkit/components/search/tests/xpcshell/data1/search-config-v2.json new file mode 100644 index 000000000000..98bdfa26fffd --- /dev/null +++ b/toolkit/components/search/tests/xpcshell/data1/search-config-v2.json @@ -0,0 +1,101 @@ +{ + "data": [ + { + "base": { + "name": "engine1", + "urls": { + "search": { + "base": "https://1.example.com/search", + "searchTermParamName": "q" + } + }, + "classification": "unknown" + }, + "variants": [{ "environment": { "allRegionsAndLocales": true } }], + "identifier": "engine1", + "recordType": "engine" + }, + { + "base": { + "name": "engine2", + "urls": { + "search": { + "base": "https://2.example.com/search", + "searchTermParamName": "q" + } + }, + "classification": "unknown" + }, + "variants": [{ "environment": { "allRegionsAndLocales": true } }], + "identifier": "engine2", + "recordType": "engine" + }, + { + "base": { + "name": "exp2", + "urls": { + "search": { + "base": "https://2.example.com/searchexp", + "searchTermParamName": "q" + } + }, + "classification": "unknown" + }, + "variants": [ + { + "environment": { "allRegionsAndLocales": true, "experiment": "exp2" } + } + ], + "identifier": "exp2", + "recordType": "engine" + }, + { + "base": { + "name": "exp3", + "urls": { + "search": { + "base": "https://3.example.com/searchexp", + "searchTermParamName": "q" + } + }, + "classification": "unknown" + }, + "variants": [ + { + "environment": { "allRegionsAndLocales": true, "experiment": "exp3" } + } + ], + "identifier": "exp3", + "recordType": "engine" + }, + { + "recordType": "defaultEngines", + "globalDefault": "engine1", + "globalDefaultPrivate": "engine1", + "specificDefaults": [ + { + "environment": { "experiment": "exp1" }, + "default": "engine2" + }, + { + "environment": { "experiment": "exp2" }, + "defaultPrivate": "exp2" + }, + { + "environment": { "experiment": "exp3" }, + "default": "exp3" + } + ] + }, + { + "recordType": "engineOrders", + "orders": [ + { + "environment": { "allRegionsAndLocales": true }, + "order": ["exp3", "engine1", "engine2", "exp2"] + } + ] + } + ], + "timestamp": 1704229342821 +} diff --git a/toolkit/components/search/tests/xpcshell/xpcshell.toml b/toolkit/components/search/tests/xpcshell/xpcshell.toml index 7ccae48af85b..c73255ddad37 100644 --- a/toolkit/components/search/tests/xpcshell/xpcshell.toml +++ b/toolkit/components/search/tests/xpcshell/xpcshell.toml @@ -54,6 +54,7 @@ support-files = [ "data1/exp2/manifest.json", "data1/exp3/manifest.json", "data1/engines.json", + "data1/search-config-v2.json", "simple-engines/engines.json", "simple-engines/basic/manifest.json", "simple-engines/hidden/manifest.json",