summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/test/browser/mocks/helper-adb-mock.js
blob: f02ca02ee33ad760eb24d86fb741c7a775f6d063 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Setup the loader to return the provided mock object instead of the regular adb module.
 *
 * @param {Object}
 *        mock should implement the following methods:
 *        - registerListener(listener)
 *        - getRuntimes()
 *        - getDevices()
 *        - updateRuntimes()
 *        - unregisterListener(listener)
 */
function enableAdbMock(mock) {
  const {
    setMockedModule,
  } = require("resource://devtools/shared/loader/browser-loader-mocks.js");
  setMockedModule(mock, "devtools/client/shared/remote-debugging/adb/adb");
}
/* exported enableAdbMock */

/**
 * Update the loader to clear the mock entry for the adb module.
 */
function disableAdbMock() {
  const {
    removeMockedModule,
  } = require("resource://devtools/shared/loader/browser-loader-mocks.js");
  removeMockedModule("devtools/client/shared/remote-debugging/adb/adb");
}
/* exported disableAdbMock */

/**
 * Creates a simple mock version for adb, implementing all the expected methods
 * with empty placeholders.
 */
function createAdbMock() {
  const adbMock = {};
  adbMock.registerListener = function (listener) {
    console.log("MOCKED METHOD registerListener");
  };

  adbMock.getRuntimes = function () {
    console.log("MOCKED METHOD getRuntimes");
  };

  adbMock.getDevices = function () {
    console.log("MOCKED METHOD getDevices");
  };

  adbMock.updateRuntimes = function () {
    console.log("MOCKED METHOD updateRuntimes");
  };

  adbMock.unregisterListener = function (listener) {
    console.log("MOCKED METHOD unregisterListener");
  };

  adbMock.once = function () {
    console.log("MOCKED METHOD once");
  };

  adbMock.isProcessStarted = function () {
    console.log("MOCKED METHOD isProcessStarted");
  };

  return { adb: adbMock };
}
/* exported createAdbMock */

/**
 * The adb module allows to observe runtime updates. To simulate this behaviour
 * the easiest is to use an EventEmitter-decorated object that can accept listeners and
 * can emit events from the test.
 *
 * This method will update the registerListener method of the provided
 * usbRuntimesMock in order to add listeners to a mockObserver, and returns said observer
 * so that the test can emit "runtime-list-updated" when needed.
 */
function addObserverMock(adbMock) {
  const EventEmitter = require("resource://devtools/shared/event-emitter.js");

  const observerMock = {};
  EventEmitter.decorate(observerMock);
  adbMock.registerListener = function (listener) {
    console.log("MOCKED METHOD registerListener with mock scanner");
    observerMock.on("runtime-list-updated", listener);
  };

  // NOTE FOR REVIEW: Instead of emitting "runtime-list-updated" events in the test,
  // this mock could have a emitObservedEvent method, that would just emit the correct
  // event. This way if the event name changes, everything remains contained in this
  // method.

  return observerMock;
}
/* exported addObserverMock */

function createAdbProcessMock() {
  const EventEmitter = require("resource://devtools/shared/event-emitter.js");

  const mock = {};
  EventEmitter.decorate(mock);

  mock.ready = false;

  mock.start = async () => {
    console.log("MOCKED METHOD start");
    mock.ready = true;
    mock.emit("adb-ready");
  };

  return { adbProcess: mock };
}
/* exported createAdbProcessMock */

function enableAdbProcessMock(mock) {
  const {
    setMockedModule,
  } = require("resource://devtools/shared/loader/browser-loader-mocks.js");
  setMockedModule(
    mock,
    "devtools/client/shared/remote-debugging/adb/adb-process"
  );
}
/* exported enableAdbProcessMock */

function disableAdbProcessMock() {
  const {
    removeMockedModule,
  } = require("resource://devtools/shared/loader/browser-loader-mocks.js");
  removeMockedModule("devtools/client/shared/remote-debugging/adb/adb-process");
}
/* exported disableAdbProcessMock */