diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /intl/locale/tests/LangPackMatcherTestUtils.sys.mjs | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | intl/locale/tests/LangPackMatcherTestUtils.sys.mjs | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/intl/locale/tests/LangPackMatcherTestUtils.sys.mjs b/intl/locale/tests/LangPackMatcherTestUtils.sys.mjs new file mode 100644 index 0000000000..4b18f1be13 --- /dev/null +++ b/intl/locale/tests/LangPackMatcherTestUtils.sys.mjs @@ -0,0 +1,124 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { LangPackMatcher } from "resource://gre/modules/LangPackMatcher.sys.mjs"; + +/** + * LangPackMatcher.jsm calls out to to the addons store, which involves network requests. + * Other tests create a fake addons server, and install mock XPIs. At the time of this + * writing that infrastructure is not available for mochitests. + * + * Instead, this test mocks out APIs that have a side-effect, so the addons of the browser + * are never modified. + * + * The calls to get the app's locale and system's locale are also mocked so that the + * different language mismatch scenarios can be run through. + * + * The locales are BCP 47 identifiers: + * + * @param {{ + * sandbox: SinonSandbox, + * systemLocale: string, + * appLocale, string, + * }} + */ +export function getAddonAndLocalAPIsMocker(testScope, sandbox) { + const { info } = testScope; + return function mockAddonAndLocaleAPIs({ systemLocale, appLocale }) { + info("Mocking LangPackMatcher.jsm APIs"); + + let resolveLangPacks; + const langPackPromise = new Promise(resolve => { + resolveLangPacks = availableLangpacks => { + info( + `Resolving which langpacks are available for download: ${JSON.stringify( + availableLangpacks + )}` + ); + resolve( + availableLangpacks.map(locale => ({ + guid: `langpack-${locale}@firefox.mozilla.org`, + type: "language", + hash: locale, + target_locale: locale, + current_compatible_version: { + files: [ + { + platform: "all", + url: `http://example.com/${locale}.langpack.xpi`, + }, + ], + }, + })) + ); + }; + }); + + let resolveInstaller; + const installerPromise = new Promise(resolve => { + resolveInstaller = () => { + info("LangPack install finished."); + resolve(); + }; + }); + + const { mockable } = LangPackMatcher; + if (appLocale) { + const availableLocales = [appLocale]; + if (appLocale !== "en-US") { + // Ensure the fallback behavior is accurately simulated for Firefox. + availableLocales.push("en-US"); + } + sandbox + .stub(mockable, "getAvailableLocalesIncludingFallback") + .returns(availableLocales); + sandbox.stub(mockable, "getDefaultLocale").returns(appLocale); + sandbox.stub(mockable, "getAppLocaleAsBCP47").returns(appLocale); + sandbox.stub(mockable, "getLastFallbackLocale").returns("en-US"); + } + if (systemLocale) { + sandbox.stub(mockable, "getSystemLocale").returns(systemLocale); + } + + sandbox.stub(mockable, "getAvailableLangpacks").callsFake(() => { + info("Requesting which langpacks are available for download"); + return langPackPromise; + }); + + sandbox.stub(mockable, "installLangPack").callsFake(langPack => { + info(`LangPack install started, but pending: ${langPack.target_locale}`); + return installerPromise; + }); + + sandbox.stub(mockable, "setRequestedAppLocales").callsFake(locales => { + info( + `Changing the browser's requested locales to: ${JSON.stringify( + locales + )}` + ); + }); + + return { + /** + * Resolves the addons API call with available langpacks. Call with a list + * of BCP 47 identifiers. + * + * @type {(availableLangpacks: string[]) => {}} + */ + resolveLangPacks, + + /** + * Resolves the pending call to install a langpack. + * + * @type {() => {}} + */ + resolveInstaller, + + /** + * The mocked APIs. + */ + mockable, + }; + }; +} |