-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathandroid-application-manager.ts
333 lines (264 loc) · 12.8 KB
/
android-application-manager.ts
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { AndroidApplicationManager } from "../../mobile/android/android-application-manager";
import { Yok } from "../../yok";
import { assert } from "chai";
import { AndroidBundleToolServiceStub, CommonLoggerStub, HooksServiceStub, LogcatHelperStub, AndroidProcessServiceStub, DeviceLogProviderStub, ErrorsStub } from "./stubs";
import { FileSystemStub } from "../../../../test/stubs";
const invalidIdentifier = "invalid.identifier";
const validDeviceIdentifier = "device.identifier";
const validIdentifier = "org.nativescript.testApp";
const validStartOptions = { appId: validIdentifier, projectName: "", projectDir: "" };
const validSigning: any = { keyStoreAlias: "string", keyStorePath: "string", keyStoreAliasPassword: "string", keyStorePassword: "string" };
class AndroidDebugBridgeStub {
public calledInstallApplication = false;
public calledStopApplication = false;
public startedWithActivityManager = false;
public validIdentifierPassed = false;
public static methodCallCount = 0;
private expectedValidTestInput: string[] = [
"org.nativescript.testApp/com.tns.TestClass",
"org.nativescript.testApp/org.MyCoolApp.MyCoolActivity",
"org.nativescript.testApp/org.myCoolApp.MyCoolActivity",
"org.nativescript.testApp/com.tns.$TestClass",
"org.nativescript.testApp/com.tns._TestClass",
"org.nativescript.testApp/com.tns.$_TestClass",
"org.nativescript.testApp/com.tns._$TestClass",
"org.nativescript.testApp/com.tns.NativeScriptActivity"
];
private validTestInput: string[] = [
"other.stuff/ org.nativescript.testApp/com.tns.TestClass asdaas.dasdh2",
"other.stuff/ org.nativescript.testApp/org.MyCoolApp.MyCoolActivity asdaas.dasdh2",
"other.stuff/ org.nativescript.testApp/org.myCoolApp.MyCoolActivity asdaas.dasdh2",
"other.stuff.the.regex.might.fail.on org.nativescript.testApp/com.tns.$TestClass other.stuff.the.regex.might.fail.on",
"/might.fail.on org.nativescript.testApp/com.tns._TestClass /might.fail.on",
"might.fail.on/ org.nativescript.testApp/com.tns.$_TestClass might.fail.on//",
"/might.fail org.nativescript.testApp/com.tns._$TestClass something/might.fail.on/",
"android.intent.action.MAIN: \
3b2df03 org.nativescript.testApp/com.tns.NativeScriptActivity filter 50dd82e \
Action: \"android.intent.action.MAIN\" \
Category: \"android.intent.category.LAUNCHER\" \
-- \
intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.nativescript.testApp/com.tns.NativeScriptActivity} \
realActivity=org.nativescript.testApp/com.tns.NativeScriptActivity \
-- \
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.nativescript.testApp/com.tns.NativeScriptActivity } \
frontOfTask=true task=TaskRecord{fe592ac #449 A=org.nativescript.testApp U=0 StackId=1 sz=1}"
];
public async executeCommand(args: string[], options?: any): Promise<any> {
if (args[0] === "install") {
this.calledInstallApplication = true;
}
}
public async executeShellCommand(args: string[]): Promise<any> {
if (args && args.length > 0) {
if (args[0].startsWith("cat")) {
return;
} else if (args[0] === "pm") {
const passedIdentifier = args[2];
if (passedIdentifier === invalidIdentifier) {
return "invalid output string";
} else {
const testString = this.validTestInput[AndroidDebugBridgeStub.methodCallCount];
return testString;
}
} else {
this.startedWithActivityManager = this.checkIfStartedWithActivityManager(args);
if (this.startedWithActivityManager) {
this.validIdentifierPassed = this.checkIfValidIdentifierPassed(args);
}
}
if (this.startedWithActivityManager && args[1] === "force-stop") {
this.calledStopApplication = true;
}
}
AndroidDebugBridgeStub.methodCallCount++;
}
public async pushFile(localFilePath: string, deviceFilePath: string): Promise<void> {
await this.executeShellCommand(["push", localFilePath, deviceFilePath]);
}
public getInputLength(): number {
return this.validTestInput.length;
}
private checkIfStartedWithActivityManager(args: string[]): boolean {
const firstArgument = args[0].trim();
return firstArgument === "am";
}
private checkIfValidIdentifierPassed(args: string[]): boolean {
if (args && args.length) {
const possibleIdentifier = args[args.length - 1];
const validTestString = this.expectedValidTestInput[AndroidDebugBridgeStub.methodCallCount];
return possibleIdentifier === validTestString;
}
return false;
}
}
function createTestInjector(options?: {
justLaunch?: boolean
}): IInjector {
const testInjector = new Yok();
testInjector.register("androidApplicationManager", AndroidApplicationManager);
testInjector.register("adb", AndroidDebugBridgeStub);
testInjector.register('childProcess', {});
testInjector.register("logger", CommonLoggerStub);
testInjector.register("errors", ErrorsStub);
testInjector.register("config", {});
testInjector.register("staticConfig", {});
testInjector.register("androidDebugBridgeResultHandler", {});
testInjector.register("options", { justlaunch: options && options.justLaunch || false });
testInjector.register("identifier", validDeviceIdentifier);
testInjector.register("logcatHelper", LogcatHelperStub);
testInjector.register("androidProcessService", AndroidProcessServiceStub);
testInjector.register("androidBundleToolService", AndroidBundleToolServiceStub);
testInjector.register("fs", FileSystemStub);
testInjector.register("httpClient", {});
testInjector.register("deviceLogProvider", DeviceLogProviderStub);
testInjector.register("hooksService", HooksServiceStub);
return testInjector;
}
describe("android-application-manager", () => {
let testInjector: IInjector;
let androidApplicationManager: AndroidApplicationManager;
let androidDebugBridge: AndroidDebugBridgeStub;
let logcatHelper: LogcatHelperStub;
let androidProcessService: AndroidProcessServiceStub;
let deviceLogProvider: DeviceLogProviderStub;
let logger: CommonLoggerStub;
function setup(options?: {
justLaunch?: boolean
}) {
testInjector = createTestInjector(options);
androidApplicationManager = testInjector.resolve("androidApplicationManager");
androidDebugBridge = testInjector.resolve("adb");
logcatHelper = testInjector.resolve("logcatHelper");
androidProcessService = testInjector.resolve("androidProcessService");
deviceLogProvider = testInjector.resolve("deviceLogProvider");
logger = testInjector.resolve("logger");
}
describe("startApplication", () => {
it("fires up the right application", async () => {
setup();
for (let i = 0; i < androidDebugBridge.getInputLength(); i++) {
androidDebugBridge.validIdentifierPassed = false;
await androidApplicationManager.startApplication(validStartOptions);
assert.isTrue(androidDebugBridge.validIdentifierPassed);
assert.isTrue(androidDebugBridge.startedWithActivityManager);
}
});
it("is calling monkey to start the application when invalid identifier is passed", async () => {
setup();
await androidApplicationManager.startApplication({ appId: invalidIdentifier, projectName: "", projectDir: "" });
assert.isFalse(androidDebugBridge.startedWithActivityManager);
});
it("starts the logcat helper", async () => {
setup();
await androidApplicationManager.startApplication(validStartOptions);
assert.equal(logcatHelper.StartCallCount, 1);
});
it("do not start the logcat helper with justLaunch param", async () => {
setup();
await androidApplicationManager.startApplication(_.extend({}, validStartOptions, { justLaunch: true }));
assert.equal(logcatHelper.StartCallCount, 0);
});
it("do not start the logcat helper with justLaunch user option", async () => {
setup({ justLaunch: true });
await androidApplicationManager.startApplication(_.extend({}, validStartOptions, { justLaunch: false }));
assert.equal(logcatHelper.StartCallCount, 0);
});
it("do not start the logcat helper with both justLaunch argument and user option", async () => {
setup({ justLaunch: true });
await androidApplicationManager.startApplication(_.extend({}, validStartOptions, { justLaunch: true }));
assert.equal(logcatHelper.StartCallCount, 0);
});
it("passes the pid to the logcat helper", async () => {
setup();
const expectedPid = "pid";
androidProcessService.GetAppProcessIdResult = expectedPid;
await androidApplicationManager.startApplication(validStartOptions);
assert.equal(logcatHelper.LastStartCallOptions.pid, expectedPid);
});
it("sets the current device pid", async () => {
setup();
const expectedPid = "pid";
androidProcessService.GetAppProcessIdResult = expectedPid;
await androidApplicationManager.startApplication(validStartOptions);
assert.equal(deviceLogProvider.currentDevicePids[validDeviceIdentifier], expectedPid);
});
it("polls for pid when not available initially and passes it to the logcat helper", async () => {
setup();
const expectedPid = "pid";
androidProcessService.GetAppProcessIdResult = expectedPid;
androidProcessService.GetAppProcessIdFailAttempts = 1;
androidApplicationManager.PID_CHECK_INTERVAL = 10;
await androidApplicationManager.startApplication(validStartOptions);
assert.equal(logcatHelper.LastStartCallOptions.pid, expectedPid);
assert.isTrue(logger.traceOutput.indexOf("Wasn't able to get pid") > -1);
assert.isTrue(logger.output.indexOf(`Unable to find running "${validIdentifier}" application on device `) === -1);
});
it("starts the logcat helper without pid after a timeout, when pid not available", () => {
setup();
const expectedPidTimeout = 100;
androidApplicationManager.PID_CHECK_INTERVAL = 10;
androidApplicationManager.PID_CHECK_TIMEOUT = expectedPidTimeout;
androidProcessService.GetAppProcessIdResult = null;
const startApplicationPromise = androidApplicationManager.startApplication(validStartOptions);
startApplicationPromise.catch(() => {
assert.isTrue(logcatHelper.DumpCallCount > 0);
assert.isTrue(logger.traceOutput.indexOf("Wasn't able to get pid") > -1);
});
return assert.isRejected(startApplicationPromise, `Unable to find running "${validIdentifier}" application on device `);
});
});
describe("installApplication", () => {
afterEach(function () {
androidDebugBridge.calledInstallApplication = false;
const bundleToolService = testInjector.resolve<AndroidBundleToolServiceStub>("androidBundleToolService");
bundleToolService.isBuildApksCalled = false;
bundleToolService.isInstallApksCalled = false;
});
it("should install apk using adb", async () => {
const bundleToolService = testInjector.resolve<AndroidBundleToolServiceStub>("androidBundleToolService");
await androidApplicationManager.installApplication("myApp.apk");
assert.isFalse(bundleToolService.isBuildApksCalled);
assert.isFalse(bundleToolService.isInstallApksCalled);
assert.isTrue(androidDebugBridge.calledInstallApplication);
});
it("should install aab using bundletool", async () => {
const bundleToolService = testInjector.resolve<AndroidBundleToolServiceStub>("androidBundleToolService");
await androidApplicationManager.installApplication("myApp.aab");
assert.isTrue(bundleToolService.isBuildApksCalled);
assert.isTrue(bundleToolService.isInstallApksCalled);
assert.isFalse(androidDebugBridge.calledInstallApplication);
});
it("should skip aab build when already built", async () => {
const fsStub = testInjector.resolve<FileSystemStub>("fs");
const bundleToolService = testInjector.resolve<AndroidBundleToolServiceStub>("androidBundleToolService");
await androidApplicationManager.installApplication("myApp.aab", "my.app", validSigning);
assert.isTrue(bundleToolService.isBuildApksCalled);
assert.isTrue(bundleToolService.isInstallApksCalled);
assert.isFalse(androidDebugBridge.calledInstallApplication);
fsStub.fsStatCache["myApp.aab"] = fsStub.fsStatCache["myApp.apks"];
bundleToolService.isBuildApksCalled = false;
bundleToolService.isInstallApksCalled = false;
await androidApplicationManager.installApplication("myApp.aab", "my.app", validSigning);
assert.isFalse(bundleToolService.isBuildApksCalled);
assert.isTrue(bundleToolService.isInstallApksCalled);
assert.isFalse(androidDebugBridge.calledInstallApplication);
});
});
describe("stopApplication", () => {
it("should stop the logcat helper", async () => {
setup();
await androidApplicationManager.stopApplication(validStartOptions);
assert.equal(logcatHelper.StopCallCount, 1);
});
it("should stop the application", async () => {
setup();
await androidApplicationManager.stopApplication(validStartOptions);
assert.isTrue(androidDebugBridge.calledStopApplication);
});
it("should reset the current pid", async () => {
setup();
await androidApplicationManager.stopApplication(validStartOptions);
assert.equal(deviceLogProvider.currentDevicePids[validDeviceIdentifier], null);
});
});
});