tor-browser

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

LangPackMatcherTestUtils.sys.mjs (3864B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 import { LangPackMatcher } from "resource://gre/modules/LangPackMatcher.sys.mjs";
      6 
      7 /**
      8 * LangPackMatcher.sys.mjs calls out to to the addons store, which involves network requests.
      9 * Other tests create a fake addons server, and install mock XPIs. At the time of this
     10 * writing that infrastructure is not available for mochitests.
     11 *
     12 * Instead, this test mocks out APIs that have a side-effect, so the addons of the browser
     13 * are never modified.
     14 *
     15 * The calls to get the app's locale and system's locale are also mocked so that the
     16 * different language mismatch scenarios can be run through.
     17 *
     18 * The locales are BCP 47 identifiers:
     19 *
     20 * @param {{
     21 *   sandbox: SinonSandbox,
     22 *   systemLocale: string,
     23 *   appLocale, string,
     24 * }}
     25 */
     26 export function getAddonAndLocalAPIsMocker(testScope, sandbox) {
     27  const { info } = testScope;
     28  return function mockAddonAndLocaleAPIs({ systemLocale, appLocale }) {
     29    info("Mocking LangPackMatcher.sys.mjs APIs");
     30 
     31    let resolveLangPacks;
     32    const langPackPromise = new Promise(resolve => {
     33      resolveLangPacks = availableLangpacks => {
     34        info(
     35          `Resolving which langpacks are available for download: ${JSON.stringify(
     36            availableLangpacks
     37          )}`
     38        );
     39        resolve(
     40          availableLangpacks.map(locale => ({
     41            guid: `langpack-${locale}@firefox.mozilla.org`,
     42            type: "language",
     43            hash: locale,
     44            target_locale: locale,
     45            current_compatible_version: {
     46              files: [
     47                {
     48                  platform: "all",
     49                  url: `http://example.com/${locale}.langpack.xpi`,
     50                },
     51              ],
     52            },
     53          }))
     54        );
     55      };
     56    });
     57 
     58    let resolveInstaller;
     59    const installerPromise = new Promise(resolve => {
     60      resolveInstaller = () => {
     61        info("LangPack install finished.");
     62        resolve();
     63      };
     64    });
     65 
     66    const { mockable } = LangPackMatcher;
     67    if (appLocale) {
     68      const availableLocales = [appLocale];
     69      if (appLocale !== "en-US") {
     70        // Ensure the fallback behavior is accurately simulated for Firefox.
     71        availableLocales.push("en-US");
     72      }
     73      sandbox
     74        .stub(mockable, "getAvailableLocalesIncludingFallback")
     75        .returns(availableLocales);
     76      sandbox.stub(mockable, "getDefaultLocale").returns(appLocale);
     77      sandbox.stub(mockable, "getAppLocaleAsBCP47").returns(appLocale);
     78      sandbox.stub(mockable, "getLastFallbackLocale").returns("en-US");
     79    }
     80    if (systemLocale) {
     81      sandbox.stub(mockable, "getSystemLocale").returns(systemLocale);
     82    }
     83 
     84    sandbox.stub(mockable, "getAvailableLangpacks").callsFake(() => {
     85      info("Requesting which langpacks are available for download");
     86      return langPackPromise;
     87    });
     88 
     89    sandbox.stub(mockable, "installLangPack").callsFake(langPack => {
     90      info(`LangPack install started, but pending: ${langPack.target_locale}`);
     91      return installerPromise;
     92    });
     93 
     94    sandbox.stub(mockable, "setRequestedAppLocales").callsFake(locales => {
     95      info(
     96        `Changing the browser's requested locales to: ${JSON.stringify(
     97          locales
     98        )}`
     99      );
    100    });
    101 
    102    return {
    103      /**
    104       * Resolves the addons API call with available langpacks. Call with a list
    105       * of BCP 47 identifiers.
    106       *
    107       * @type {(availableLangpacks: string[]) => {}}
    108       */
    109      resolveLangPacks,
    110 
    111      /**
    112       * Resolves the pending call to install a langpack.
    113       *
    114       * @type {() => {}}
    115       */
    116      resolveInstaller,
    117 
    118      /**
    119       * The mocked APIs.
    120       */
    121      mockable,
    122    };
    123  };
    124 }