microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4b124a08cf4726fc4b6b1f843dcd24e31f33db5e

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/test/resources/simulators/adbSimulator.ts

182lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as Q from "q";
5
6import * as adb from "../../../common/android/adb";
7
8import {FileSystem} from "../../../common/node/fileSystem";
9import {APKSerializer} from "./apkSerializer";
10
11export type IDevice = adb.IDevice;
12
13interface IDeviceStateMapping {
14 [deviceId: string]: IDeviceState;
15}
16
17interface IDeviceState {
18 isOnline: boolean;
19 type: adb.DeviceType;
20 installedApplications: IInstalledApplicationStateMapping;
21 runningApplications: IRunningApplicationStateMapping;
22}
23
24interface IInstalledApplicationStateMapping {
25 [applicationName: string]: IInstalledApplicationState;
26}
27
28interface IInstalledApplicationState {
29}
30
31interface IRunningApplicationStateMapping {
32 [applicationName: string]: IRunningApplicationState;
33}
34
35interface IRunningApplicationState {
36 isInDebugMode: boolean;
37}
38
39/* Simulation of adb/ADB */
40export class AdbSimulator extends adb.AdbEnhancements {
41 private connectedDevices: IDeviceStateMapping = {};
42 private fileSystem: FileSystem;
43
44 constructor(fileSystem: FileSystem) {
45 super();
46 this.fileSystem = fileSystem;
47 }
48
49 // Intends to simulate: adb devices
50 public getConnectedDevices(): Q.Promise<IDevice[]> {
51 return Q.resolve(this.getDevicesIds().map(deviceId => {
52 const device = this.connectedDevices[deviceId];
53 return { id: deviceId, isOnline: device.isOnline, type: device.type };
54 }));
55 }
56
57 // Intends to simulate: the react-native application running
58 // TODO: We should move the react-native specific part of this method to another class
59 public reloadAppInDebugMode(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void> {
60 const runningApplicationState = this.getRunningAppOrNull(packageName, debugTarget);
61 if (runningApplicationState) {
62 runningApplicationState.isInDebugMode = true;
63 return Q.resolve<void>(void 0);
64 } else {
65 throw new Error("Implement proper adb response: Application is not running");
66 }
67 }
68
69 // Intends to simulate: adb shell am start
70 public launchApp(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void> {
71 const deviceState = this.getOnlineDeviceById(debugTarget);
72 const installedApplicationState = deviceState.installedApplications[packageName];
73 if (installedApplicationState) {
74 deviceState.runningApplications[packageName] = { isInDebugMode: false };
75 return Q.resolve<void>(void 0);
76 } else {
77 throw new Error("Implement proper adb response: Application doesn't exist");
78 }
79 }
80
81 // Intends to simulate: adb install
82 public installApp(apkPath: string, debugTarget?: string): Q.Promise<void> {
83 const deviceState = this.getOnlineDeviceById(debugTarget);
84 return new APKSerializer(this.fileSystem).readPackageNameFromFile(apkPath).then(packageName => {
85 deviceState.installedApplications[packageName] = {};
86 });
87 }
88
89 // Intends to simulate: adb shell ps | grep <packageName> | awk '{print $9}'
90 public isAppRunning(packageName: string, debugTarget?: string): Q.Promise<boolean> {
91 return Q.resolve(this.isAppRunningSync(packageName, debugTarget));
92 }
93
94 // Intends to simulate: combination of this.getConnectedDevices() and this.isAppRunning()
95 public findDevicesRunningApp(packageName: string): Q.Promise<string[]> {
96 return Q.resolve(this.getOnlineDevicesIds().filter(deviceId =>
97 this.isAppRunningSync(packageName, deviceId)));
98 }
99
100 // Intends to simulate: <adb devices> second column
101 public isDeviceOnline(deviceId: string): Q.Promise<boolean> {
102 return Q.resolve(this.isDeviceOnlineSync(deviceId));
103 }
104
105 // We get notified that a device was connected. Intends to simulate connecting a device to the Computer
106 public notifyDeviceWasConnected(deviceId: string, deviceType: adb.DeviceType): void {
107 if (this.connectedDevices[deviceId]) {
108 throw new Error(`Device ${deviceId} was already connected to simulated ADB`);
109 } else {
110 this.connectedDevices[deviceId] = { isOnline: true, installedApplications: {}, runningApplications: {}, type: deviceType };
111 }
112 }
113
114 // We get notified that a device went offline. TODO: Find out how can this happen for real
115 public notifyDevicesAreOffline(deviceIds: string[]): void {
116 deviceIds.forEach(deviceId => {
117 return this.notifyDeviceIsOffline(deviceId);
118 });
119 }
120
121 public apiVersion(deviceId: string): Q.Promise<adb.AndroidAPILevel> {
122 throw new Error("Not yet implemented: Implement if we need to use these methods in a test");
123 }
124
125 public reverseAdd(deviceId: string, devicePort: string, computerPort: string): Q.Promise<void> {
126 throw new Error("Not yet implemented: Implement if we need to use these methods in a test");
127 }
128
129 private isAppRunningSync(packageName: string, debugTarget?: string): boolean {
130 return this.getRunningAppOrNull(packageName, debugTarget) != null;
131 }
132
133 private notifyDeviceIsOffline(deviceId: string): void {
134 this.getOnlineDeviceById(deviceId).isOnline = false;
135 }
136
137 private getRunningAppOrNull(packageName: string, debugTarget?: string): IRunningApplicationState {
138 return this.getOnlineDeviceById(debugTarget).runningApplications[packageName];
139 }
140
141 private getOnlineDeviceById(deviceId?: string): IDeviceState {
142 const deviceState = this.getDeviceById(deviceId);
143 if (deviceState.isOnline) {
144 return deviceState;
145 } else {
146 throw new Error("Implement proper adb response: Target device isn't online");
147 }
148 }
149
150 private getDeviceById(deviceId?: string): IDeviceState {
151 if (deviceId) { // If the deviceId is specified, we search for that device
152 const deviceState = this.connectedDevices[deviceId];
153 if (deviceState) { // If it exists, we return it
154 return deviceState;
155 } else { // If not we fail
156 throw new Error("Implement proper adb response: Target device doesn't exist");
157 }
158 } else {
159 const devicesIds = this.getDevicesIds();
160 if (devicesIds.length === 1) { // If deviceId is null and we have a single device, we return that device
161 return this.connectedDevices[devicesIds[0]];
162 } else if (devicesIds.length > 1) {
163 throw new Error("error: more than one device/emulator"); // error code = 1
164 } else {
165 throw new Error("error: no devices found"); // error code = 1
166 }
167 }
168 }
169
170 private getDevicesIds(): string[] {
171 return Object.keys(this.connectedDevices);
172 }
173
174 private getOnlineDevicesIds(): string[] {
175 return this.getDevicesIds().filter(deviceId =>
176 this.isDeviceOnlineSync(deviceId));
177 }
178
179 private isDeviceOnlineSync(deviceId: string): boolean {
180 return this.getDeviceById(deviceId).isOnline;
181 }
182}
183