tor-browser

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

browser_browserloader_mocks.js (4775B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { BrowserLoader } = ChromeUtils.importESModule(
      7  "resource://devtools/shared/loader/browser-loader.sys.mjs"
      8 );
      9 
     10 const {
     11  getMockedModule,
     12  setMockedModule,
     13  removeMockedModule,
     14 } = require("resource://devtools/shared/loader/browser-loader-mocks.js");
     15 const { require: browserRequire } = BrowserLoader({
     16  baseURI: "resource://devtools/client/shared/",
     17  window,
     18 });
     19 
     20 // Check that modules can be mocked in the browser loader.
     21 // Test with a custom test module under the chrome:// scheme.
     22 function testWithChromeScheme() {
     23  // Full chrome URI for the test module.
     24  const CHROME_URI = CHROME_URL_ROOT + "test-mocked-module.js";
     25  const ORIGINAL_VALUE = "Original value";
     26  const MOCKED_VALUE_1 = "Mocked value 1";
     27  const MOCKED_VALUE_2 = "Mocked value 2";
     28 
     29  const m1 = browserRequire(CHROME_URI);
     30  ok(m1, "Regular module can be required");
     31  is(m1.methodToMock(), ORIGINAL_VALUE, "Method returns the expected value");
     32  is(m1.someProperty, "someProperty", "Property has the expected value");
     33 
     34  info("Create a simple mocked version of the test module");
     35  const mockedModule = {
     36    methodToMock: () => MOCKED_VALUE_1,
     37    someProperty: "somePropertyMocked",
     38  };
     39  setMockedModule(mockedModule, CHROME_URI);
     40  ok(!!getMockedModule(CHROME_URI), "Has an entry for the chrome URI.");
     41 
     42  const m2 = browserRequire(CHROME_URI);
     43  ok(m2, "Mocked module can be required via chrome URI");
     44  is(
     45    m2.methodToMock(),
     46    MOCKED_VALUE_1,
     47    "Mocked method returns the expected value"
     48  );
     49  is(
     50    m2.someProperty,
     51    "somePropertyMocked",
     52    "Mocked property has the expected value"
     53  );
     54 
     55  const { methodToMock: requiredMethod } = browserRequire(CHROME_URI);
     56  Assert.strictEqual(
     57    requiredMethod(),
     58    MOCKED_VALUE_1,
     59    "Mocked method returns the expected value when imported with destructuring"
     60  );
     61 
     62  info("Update the mocked method to return a different value");
     63  mockedModule.methodToMock = () => MOCKED_VALUE_2;
     64  is(
     65    requiredMethod(),
     66    MOCKED_VALUE_2,
     67    "Mocked method returns the updated value  when imported with destructuring"
     68  );
     69 
     70  info("Remove the mock for the test module");
     71  removeMockedModule(CHROME_URI);
     72  ok(!getMockedModule(CHROME_URI), "Has no entry for the chrome URI.");
     73 
     74  const m3 = browserRequire(CHROME_URI);
     75  ok(m3, "Regular module can be required after removing the mock");
     76  is(
     77    m3.methodToMock(),
     78    ORIGINAL_VALUE,
     79    "Method on module returns the expected value"
     80  );
     81 }
     82 
     83 // Similar tests as in testWithChromeScheme, but this time with a devtools module
     84 // available under the resource:// scheme.
     85 function testWithRegularDevtoolsModule() {
     86  // Testing with devtools/shared/path because it is a simple module, that can be imported
     87  // with destructuring. Any other module would do.
     88  const DEVTOOLS_MODULE_PATH = "devtools/shared/path";
     89  const DEVTOOLS_MODULE_URI = "resource://devtools/shared/path.js";
     90 
     91  const m1 = browserRequire(DEVTOOLS_MODULE_PATH);
     92  is(
     93    m1.joinURI("https://a", "b"),
     94    "https://a/b",
     95    "Original module was required"
     96  );
     97 
     98  info(
     99    "Set a mock for a sub-part of the path, which should not match require calls"
    100  );
    101  setMockedModule({ joinURI: () => "WRONG_PATH" }, "shared/path");
    102 
    103  ok(
    104    !getMockedModule(DEVTOOLS_MODULE_URI),
    105    "Has no mock entry for the full URI"
    106  );
    107  const m2 = browserRequire(DEVTOOLS_MODULE_PATH);
    108  is(
    109    m2.joinURI("https://a", "b"),
    110    "https://a/b",
    111    "Original module is still required"
    112  );
    113 
    114  info(
    115    "Set a mock for the complete path, which should now match require calls"
    116  );
    117  const mockedModule = {
    118    joinURI: () => "MOCKED VALUE",
    119  };
    120  setMockedModule(mockedModule, DEVTOOLS_MODULE_PATH);
    121  ok(
    122    !!getMockedModule(DEVTOOLS_MODULE_URI),
    123    "Has a mock entry for the full URI."
    124  );
    125 
    126  const m3 = browserRequire(DEVTOOLS_MODULE_PATH);
    127  is(
    128    m3.joinURI("https://a", "b"),
    129    "MOCKED VALUE",
    130    "The mocked module has been returned"
    131  );
    132 
    133  info(
    134    "Check that the mocked methods can be updated after a destructuring import"
    135  );
    136  const { joinURI } = browserRequire(DEVTOOLS_MODULE_PATH);
    137  mockedModule.joinURI = () => "UPDATED VALUE";
    138  is(
    139    joinURI("https://a", "b"),
    140    "UPDATED VALUE",
    141    "Mocked method was correctly updated"
    142  );
    143 
    144  removeMockedModule(DEVTOOLS_MODULE_PATH);
    145  ok(
    146    !getMockedModule(DEVTOOLS_MODULE_URI),
    147    "Has no mock entry for the full URI"
    148  );
    149  const m4 = browserRequire(DEVTOOLS_MODULE_PATH);
    150  is(
    151    m4.joinURI("https://a", "b"),
    152    "https://a/b",
    153    "Original module can be required again"
    154  );
    155 }
    156 
    157 function test() {
    158  testWithChromeScheme();
    159  testWithRegularDevtoolsModule();
    160  delete window.getBrowserLoaderForWindow;
    161  finish();
    162 }