tor-browser

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

GeolocationTestUtils.sys.mjs (1891B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const lazy = {};
      5 
      6 ChromeUtils.defineESModuleGetters(lazy, {
      7  sinon: "resource://testing-common/Sinon.sys.mjs",
      8 });
      9 
     10 ChromeUtils.defineLazyGetter(lazy, "GeolocationUtils", () => {
     11  try {
     12    return ChromeUtils.importESModule(
     13      "moz-src:///browser/components/urlbar/private/GeolocationUtils.sys.mjs"
     14    ).GeolocationUtils;
     15  } catch {
     16    // Fallback to URI format prior to FF 144.
     17    return ChromeUtils.importESModule(
     18      "resource:///modules/urlbar/private/GeolocationUtils.sys.mjs"
     19    ).GeolocationUtils;
     20  }
     21 });
     22 
     23 /**
     24 *
     25 */
     26 class _GeolocationTestUtils {
     27  get SAN_FRANCISCO() {
     28    return {
     29      country_code: "US",
     30      city: "San Francisco",
     31      region_code: "CA",
     32    };
     33  }
     34 
     35  /**
     36   * Initializes the utils.
     37   *
     38   * @param {object} scope
     39   *   The global JS scope where tests are being run. This allows the instance
     40   *   to access test helpers like `Assert` that are available in the scope.
     41   */
     42  init(scope) {
     43    if (!scope) {
     44      throw new Error(
     45        "GeolocationTestUtils.init() must be called with a scope"
     46      );
     47    }
     48 
     49    this.#scope = scope;
     50  }
     51 
     52  /**
     53   * Setup stub for GeolocationUtils.geolocation() using given geolocation.
     54   *
     55   * @param {object} geolocation
     56   * @param {string} [geolocation.country_code]
     57   * @param {string} [geolocation.city]
     58   * @param {string} [geolocation.region_code]
     59   * @returns {Function} function to restore the stub.
     60   */
     61  stubGeolocation(geolocation) {
     62    let sandbox = lazy.sinon.createSandbox();
     63    sandbox.stub(lazy.GeolocationUtils, "geolocation").resolves(geolocation);
     64 
     65    let cleanup = () => {
     66      sandbox.restore();
     67    };
     68 
     69    this.#scope.registerCleanupFunction?.(cleanup);
     70 
     71    return cleanup;
     72  }
     73 
     74  #scope;
     75 }
     76 
     77 export let GeolocationTestUtils = new _GeolocationTestUtils();