summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/remote-debugging/adb/xpcshell/test_adb.js
blob: dfbaab5a6b8644dfe16d1a63a1016105550bc6db (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { ExtensionTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/ExtensionXPCShellUtils.sys.mjs"
);
const { NetUtil } = ChromeUtils.importESModule(
  "resource://gre/modules/NetUtil.sys.mjs"
);
const {
  getFileForBinary,
} = require("resource://devtools/client/shared/remote-debugging/adb/adb-binary.js");
const {
  check,
} = require("resource://devtools/client/shared/remote-debugging/adb/adb-running-checker.js");
const {
  adbProcess,
} = require("resource://devtools/client/shared/remote-debugging/adb/adb-process.js");
const {
  TrackDevicesCommand,
} = require("resource://devtools/client/shared/remote-debugging/adb/commands/index.js");

const ADB_JSON = {
  Linux: {
    x86: ["linux/adb"],
    x86_64: ["linux64/adb"],
  },
  Darwin: {
    x86_64: ["mac64/adb"],
  },
  WINNT: {
    x86: ["win32/adb.exe", "win32/AdbWinApi.dll", "win32/AdbWinUsbApi.dll"],
    x86_64: ["win32/adb.exe", "win32/AdbWinApi.dll", "win32/AdbWinUsbApi.dll"],
  },
};
let extension_version = 1.0;

ExtensionTestUtils.init(this);

function readAdbMockContent() {
  const adbMockFile = do_get_file("adb.py", false);
  const s = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
    Ci.nsIFileInputStream
  );
  s.init(adbMockFile, -1, -1, false);
  try {
    return NetUtil.readInputStreamToString(s, s.available());
  } finally {
    s.close();
  }
}

const adbMock = readAdbMockContent();

add_task(async function setup() {
  // Prepare the profile directory where the adb extension will be installed.
  do_get_profile();
});

add_task(async function testAdbIsNotRunningInitially() {
  const isAdbRunning = await check();
  // Assume that no adb server running.
  ok(!isAdbRunning, "adb is not running initially");
});

add_task(async function testNoAdbExtension() {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      version: (extension_version++).toString(),
      browser_specific_settings: {
        gecko: { id: "not-adb@mozilla.org" },
      },
    },
  });

  await extension.startup();

  const adbBinary = await getFileForBinary();
  equal(adbBinary, null);

  await extension.unload();
});

add_task(async function testNoAdbJSON() {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      version: (extension_version++).toString(),
      browser_specific_settings: {
        // The extension id here and in later test cases should match the
        // corresponding prefrece value.
        gecko: { id: "adb@mozilla.org" },
      },
    },
  });

  await extension.startup();

  const adbBinary = await getFileForBinary();
  equal(adbBinary, null);

  await extension.unload();
});

add_task(async function testNoTargetBinaries() {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      version: (extension_version++).toString(),
      browser_specific_settings: {
        gecko: { id: "adb@mozilla.org" },
      },
    },
    files: {
      "adb.json": JSON.stringify(ADB_JSON),
    },
  });

  await extension.startup();

  const adbBinary = await getFileForBinary();
  equal(adbBinary, null);

  await extension.unload();
});

add_task(async function testExtract() {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      version: (extension_version++).toString(),
      browser_specific_settings: {
        gecko: { id: "adb@mozilla.org" },
      },
    },
    files: {
      "adb.json": JSON.stringify(ADB_JSON),
      "linux/adb": "adb",
      "linux64/adb": "adb",
      "mac64/adb": "adb",
      "win32/adb.exe": "adb.exe",
      "win32/AdbWinApi.dll": "AdbWinApi.dll",
      "win32/AdbWinUsbApi.dll": "AdbWinUsbApi.dll",
    },
  });

  await extension.startup();

  const adbBinary = await getFileForBinary();
  ok(await adbBinary.exists());

  await extension.unload();
});

add_task(
  {
    skip_if: () => mozinfo.os == "win", // bug 1482008
  },
  async function testStartAndStop() {
    const extension = ExtensionTestUtils.loadExtension({
      manifest: {
        version: (extension_version++).toString(),
        browser_specific_settings: {
          gecko: { id: "adb@mozilla.org" },
        },
      },
      files: {
        "adb.json": JSON.stringify(ADB_JSON),
        "linux/adb": adbMock,
        "linux64/adb": adbMock,
        "mac64/adb": adbMock,
        "win32/adb.exe": adbMock,
        "win32/AdbWinApi.dll": "dummy",
        "win32/AdbWinUsbApi.dll": "dummy",
      },
    });

    await extension.startup();

    // Call start() once and call stop() afterwards.
    await adbProcess.start();
    ok(adbProcess.ready);
    ok(await check(), "adb is now running");

    await adbProcess.stop();
    ok(!adbProcess.ready);
    ok(!(await check()), "adb is no longer running");

    // Call start() twice and call stop() afterwards.
    await adbProcess.start();
    await adbProcess.start();
    ok(adbProcess.ready);
    ok(await check(), "adb is now running");

    await adbProcess.stop();
    ok(!adbProcess.ready);
    ok(!(await check()), "adb is no longer running");

    await extension.unload();
  }
);

add_task(
  {
    skip_if: () => mozinfo.os == "win", // bug 1482008
  },
  async function testTrackDevices() {
    const extension = ExtensionTestUtils.loadExtension({
      manifest: {
        version: (extension_version++).toString(),
        browser_specific_settings: {
          gecko: { id: "adb@mozilla.org" },
        },
      },
      files: {
        "adb.json": JSON.stringify(ADB_JSON),
        "linux/adb": adbMock,
        "linux64/adb": adbMock,
        "mac64/adb": adbMock,
        "win32/adb.exe": adbMock,
        "win32/AdbWinApi.dll": "dummy",
        "win32/AdbWinUsbApi.dll": "dummy",
      },
    });

    await extension.startup();

    await adbProcess.start();
    ok(adbProcess.ready);

    ok(await check(), "adb is now running");

    const receivedDeviceId = await new Promise(resolve => {
      const trackDevicesCommand = new TrackDevicesCommand();
      trackDevicesCommand.on("device-connected", deviceId => {
        resolve(deviceId);
      });
      trackDevicesCommand.run();
    });

    equal(receivedDeviceId, "1234567890");

    await adbProcess.stop();
    ok(!adbProcess.ready);

    await extension.unload();
  }
);