summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/test/browser/helper-mocks.js
blob: 76fc414ae8e650087ed8394d372d2daf1f8e6902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/* import-globals-from ../../../shared/test/shared-head.js */

const MOCKS_ROOT = CHROME_URL_ROOT + "mocks/";
/* import-globals-from mocks/helper-adb-mock.js */
Services.scriptloader.loadSubScript(MOCKS_ROOT + "helper-adb-mock.js", this);
/* import-globals-from mocks/helper-client-wrapper-mock.js */
Services.scriptloader.loadSubScript(
  MOCKS_ROOT + "helper-client-wrapper-mock.js",
  this
);
/* import-globals-from mocks/helper-runtime-client-factory-mock.js */
Services.scriptloader.loadSubScript(
  MOCKS_ROOT + "helper-runtime-client-factory-mock.js",
  this
);

const {
  RUNTIMES,
} = require("resource://devtools/client/aboutdebugging/src/constants.js");

/**
 * This wrapper around the mocks used in about:debugging tests provides helpers to
 * quickly setup mocks for runtime tests involving USB, network or wifi runtimes that can
 * are difficult to setup in a test environment.
 */
class Mocks {
  constructor() {
    // Setup the adb mock to rely on internal arrays.
    this.adbMock = createAdbMock();
    this.adbProcessMock = createAdbProcessMock();
    this._usbRuntimes = [];
    this._usbDevices = [];
    this.adbMock.adb.getRuntimes = () => {
      return this._usbRuntimes;
    };
    this.adbMock.adb.getDevices = () => {
      const runtimeDevices = this._usbRuntimes.map(r => {
        return { id: r.deviceId, name: r.deviceName };
      });
      return runtimeDevices.concat(this._usbDevices);
    };

    // adb.updateRuntimes should ultimately fire the "runtime-list-updated" event.
    this.adbMock.adb.updateRuntimes = () => {
      this.emitUSBUpdate();
    };

    this.adbMock.adb.isProcessStarted = () => true;

    // Prepare a fake observer to be able to emit events from this mock.
    this._observerMock = addObserverMock(this.adbMock.adb);

    // Setup the runtime-client-factory mock to rely on the internal _clients map.
    this.runtimeClientFactoryMock = createRuntimeClientFactoryMock();
    this._clients = {
      [RUNTIMES.NETWORK]: {},
      [RUNTIMES.THIS_FIREFOX]: {},
      [RUNTIMES.USB]: {},
    };
    this.runtimeClientFactoryMock.createClientForRuntime = runtime => {
      return this._clients[runtime.type][runtime.id];
    };

    // Add a client for THIS_FIREFOX, since about:debugging will start on the This Firefox
    // page.
    this._thisFirefoxClient = createThisFirefoxClientMock();
    this._clients[RUNTIMES.THIS_FIREFOX][RUNTIMES.THIS_FIREFOX] =
      this._thisFirefoxClient;

    // Enable mocks and remove them after the test.
    this.enableMocks();
    registerCleanupFunction(() => this.disableMocks());
  }

  get thisFirefoxClient() {
    return this._thisFirefoxClient;
  }

  enableMocks() {
    enableAdbMock(this.adbMock);
    enableAdbProcessMock(this.adbProcessMock);
    enableRuntimeClientFactoryMock(this.runtimeClientFactoryMock);
  }

  disableMocks() {
    disableAdbMock();
    disableAdbProcessMock();
    disableRuntimeClientFactoryMock();

    for (const host of Object.keys(this._clients[RUNTIMES.NETWORK])) {
      this.removeNetworkRuntime(host);
    }
  }

  createNetworkRuntime(host, runtimeInfo) {
    const {
      addNetworkLocation,
    } = require("resource://devtools/client/aboutdebugging/src/modules/network-locations.js");
    addNetworkLocation(host);

    // Add a valid client that can be returned for this particular runtime id.
    const mockNetworkClient = createClientMock();
    mockNetworkClient.getDeviceDescription = () => {
      return {
        name: runtimeInfo.name || "TestBrand",
        channel: runtimeInfo.channel || "release",
        version: runtimeInfo.version || "1.0",
      };
    };
    this._clients[RUNTIMES.NETWORK][host] = mockNetworkClient;

    return mockNetworkClient;
  }

  removeNetworkRuntime(host) {
    const {
      removeNetworkLocation,
    } = require("resource://devtools/client/aboutdebugging/src/modules/network-locations.js");
    removeNetworkLocation(host);

    delete this._clients[RUNTIMES.NETWORK][host];
  }

  emitUSBUpdate() {
    this._observerMock.emit("runtime-list-updated");
  }

  /**
   * Creates a USB runtime for which a client conenction can be established.
   * @param {String} id
   *        The id of the runtime.
   * @param {Object} optional object used to create the fake runtime & device
   *        - channel: {String} Release channel, for instance "release", "nightly"
   *        - clientWrapper: {ClientWrapper} optional ClientWrapper for this runtime
   *        - deviceId: {String} Device id
   *        - deviceName: {String} Device name
   *        - isFenix: {Boolean} set by ADB if the package name matches a Fenix package
   *        - name: {String} Application name, for instance "Firefox"
   *        - shortName: {String} Short name for the device
   *        - socketPath: {String} (should only be used for connecting, so not here)
   *        - version: {String} Version, for instance "63.0a"
   *        - versionName: {String} Version return by ADB "63.0a"
   * @return {Object} Returns the mock client created for this runtime so that methods
   * can be overridden on it.
   */
  createUSBRuntime(id, runtimeInfo = {}) {
    // Add a new runtime to the list of scanned runtimes.
    this._usbRuntimes.push({
      deviceId: runtimeInfo.deviceId || "test device id",
      deviceName: runtimeInfo.deviceName || "test device name",
      id,
      isFenix: runtimeInfo.isFenix,
      shortName: runtimeInfo.shortName || "testshort",
      socketPath: runtimeInfo.socketPath || "test/path",
      versionName: runtimeInfo.versionName || "1.0",
    });

    // Add a valid client that can be returned for this particular runtime id.
    let mockUsbClient = runtimeInfo.clientWrapper;
    if (mockUsbClient) {
      const originalGetDeviceDescription =
        mockUsbClient.getDeviceDescription.bind(mockUsbClient);
      mockUsbClient.getDeviceDescription = async () => {
        const deviceDescription = await originalGetDeviceDescription();
        return {
          channel: runtimeInfo.channel || deviceDescription.channel,
          name: runtimeInfo.name || deviceDescription.name,
          version: runtimeInfo.version || deviceDescription.version,
        };
      };
    } else {
      // If no clientWrapper was provided, create a mock client here.
      mockUsbClient = createClientMock();
      mockUsbClient.getDeviceDescription = () => {
        return {
          channel: runtimeInfo.channel || "release",
          name: runtimeInfo.name || "TestBrand",
          version: runtimeInfo.version || "1.0",
        };
      };
    }

    this._clients[RUNTIMES.USB][id] = mockUsbClient;

    return mockUsbClient;
  }

  removeUSBRuntime(id) {
    this._usbRuntimes = this._usbRuntimes.filter(runtime => runtime.id !== id);
    delete this._clients[RUNTIMES.USB][id];
  }

  addDevice(deviceId, deviceName) {
    this._usbDevices.push({
      id: deviceId,
      name: deviceName,
    });
  }

  removeDevice(deviceId) {
    this._usbDevices = this._usbDevices.filter(d => {
      return d.id !== deviceId;
    });
  }

  removeRuntime(id) {
    if (this._clients[RUNTIMES.USB][id]) {
      this.removeUSBRuntime(id);
    } else if (this._clients[RUNTIMES.NETWORK][id]) {
      this.removeNetworkRuntime(id);
    }
  }
}
/* exported Mocks */

const silenceWorkerUpdates = function () {
  const {
    removeMockedModule,
    setMockedModule,
  } = require("resource://devtools/shared/loader/browser-loader-mocks.js");

  const mock = {
    WorkersListener: () => {
      return {
        addListener: () => {},
        removeListener: () => {},
      };
    },
  };
  setMockedModule(mock, "devtools/client/shared/workers-listener");

  registerCleanupFunction(() => {
    removeMockedModule("devtools/client/shared/workers-listener");
  });
};
/* exported silenceWorkerUpdates */

async function createLocalClientWrapper() {
  info("Create a local DevToolsClient");
  const {
    DevToolsServer,
  } = require("resource://devtools/server/devtools-server.js");
  const {
    DevToolsClient,
  } = require("resource://devtools/client/devtools-client.js");
  const {
    ClientWrapper,
  } = require("resource://devtools/client/aboutdebugging/src/modules/client-wrapper.js");

  DevToolsServer.init();
  DevToolsServer.registerAllActors();
  const client = new DevToolsClient(DevToolsServer.connectPipe());

  await client.connect();
  return new ClientWrapper(client);
}
/* exported createLocalClientWrapper */