tor-browser

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

helper-client-wrapper-mock.js (4132B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict"; // defined in head.js
      5 
      6 /* global CHROME_URL_ROOT */
      7 
      8 // This head file contains helpers to create mock versions of the ClientWrapper class
      9 // defined at devtools/client/aboutdebugging/src/modules/client-wrapper.js .
     10 
     11 const {
     12  RUNTIME_PREFERENCE,
     13 } = require("resource://devtools/client/aboutdebugging/src/constants.js");
     14 
     15 // Sensible default values for runtime preferences that should be usable in most
     16 // situations
     17 const DEFAULT_PREFERENCES = {
     18  [RUNTIME_PREFERENCE.CONNECTION_PROMPT]: true,
     19  [RUNTIME_PREFERENCE.PERMANENT_PRIVATE_BROWSING]: false,
     20  [RUNTIME_PREFERENCE.SERVICE_WORKERS_ENABLED]: true,
     21 };
     22 
     23 // Creates a simple mock ClientWrapper.
     24 function createClientMock() {
     25  const EventEmitter = require("resource://devtools/shared/event-emitter.js");
     26  const eventEmitter = {};
     27  EventEmitter.decorate(eventEmitter);
     28 
     29  return {
     30    // add a reference to the internal event emitter so that consumers can fire client
     31    // events.
     32    _eventEmitter: eventEmitter,
     33    _preferences: {},
     34    contentProcessFronts: [],
     35    serviceWorkerRegistrationFronts: [],
     36    once: (evt, listener) => {
     37      eventEmitter.once(evt, listener);
     38    },
     39    on: (evt, listener) => {
     40      eventEmitter.on(evt, listener);
     41    },
     42    off: (evt, listener) => {
     43      eventEmitter.off(evt, listener);
     44    },
     45    client: {
     46      once: (evt, listener) => {
     47        eventEmitter.once(evt, listener);
     48      },
     49      on: (evt, listener) => {
     50        eventEmitter.on(evt, listener);
     51      },
     52      off: (evt, listener) => {
     53        eventEmitter.off(evt, listener);
     54      },
     55    },
     56    // no-op
     57    close: () => {},
     58    // client is not closed
     59    isClosed: () => false,
     60    // no-op
     61    connect: () => {},
     62    // no-op
     63    getDeviceDescription: () => {},
     64    // Return default preference value or null if no match.
     65    getPreference(prefName) {
     66      if (prefName in this._preferences) {
     67        return this._preferences[prefName];
     68      }
     69      if (prefName in DEFAULT_PREFERENCES) {
     70        return DEFAULT_PREFERENCES[prefName];
     71      }
     72      return null;
     73    },
     74    // no-op
     75    createRootResourceCommand: () => {
     76      return {
     77        watchResources: () => new Promise(r => r()),
     78        unwatchResources: () => {},
     79      };
     80    },
     81    // Empty array of addons
     82    listAddons: () => [],
     83    // Empty array of processes
     84    listProcesses: () => [],
     85    // Empty array of tabs
     86    listTabs: () => [],
     87    // Empty arrays of workers
     88    listWorkers: () => ({
     89      otherWorkers: [],
     90      serviceWorkers: [],
     91      sharedWorkers: [],
     92    }),
     93    // no-op
     94    getMainProcess: () => {},
     95    // no-op
     96    getFront: () => {},
     97    // stores the preference locally (doesn't update about:config)
     98    setPreference(prefName, value) {
     99      this._preferences[prefName] = value;
    100    },
    101    getPerformancePanelUrl: () => CHROME_URL_ROOT + "empty.html",
    102    loadPerformanceProfiler: () => {},
    103    // Valid compatibility report
    104    checkVersionCompatibility: () => {
    105      const {
    106        COMPATIBILITY_STATUS,
    107      } = require("resource://devtools/client/shared/remote-debugging/version-checker.js");
    108      return { status: COMPATIBILITY_STATUS.COMPATIBLE };
    109    },
    110    // No traits by default but allow updates.
    111    traits: {},
    112  };
    113 }
    114 
    115 // Create a ClientWrapper mock that can be used to replace the this-firefox runtime.
    116 function createThisFirefoxClientMock() {
    117  const mockThisFirefoxDescription = {
    118    name: "Firefox",
    119    channel: "nightly",
    120    version: "63.0",
    121  };
    122 
    123  // Create a fake about:debugging tab because our test helper openAboutDebugging
    124  // waits until about:debugging is displayed in the list of tabs.
    125  const mockAboutDebuggingTab = {
    126    retrieveFavicon: () => {},
    127    outerWindowID: 0,
    128    traits: {},
    129    url: "about:debugging",
    130  };
    131 
    132  const mockThisFirefoxClient = createClientMock();
    133  mockThisFirefoxClient.listTabs = () => [mockAboutDebuggingTab];
    134  mockThisFirefoxClient.getDeviceDescription = () => mockThisFirefoxDescription;
    135 
    136  return mockThisFirefoxClient;
    137 }
    138 /* exported createThisFirefoxClientMock */