microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e3ae42278f91179dcf2a3c6389a8dcf0e8a76c13

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/test/resources/simulators/deviceHelper.ts

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