microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.3.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/test/resources/simulators/adbSimulator.ts

182lines · modeblame

a2e27a97digeff10 years ago1// 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
52f3873ddigeff10 years ago6import * as adb from "../../../common/android/adb";
a2e27a97digeff10 years ago7
8import {FileSystem} from "../../../common/node/fileSystem";
9import {APKSerializer} from "./apkSerializer";
10
52f3873ddigeff10 years ago11export type IDevice = adb.IDevice;
a2e27a97digeff10 years ago12
13interface IDeviceStateMapping {
14[deviceId: string]: IDeviceState;
15}
16
17interface IDeviceState {
18isOnline: boolean;
52f3873ddigeff10 years ago19type: adb.DeviceType;
a2e27a97digeff10 years ago20installedApplications: IInstalledApplicationStateMapping;
21runningApplications: 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 {
36isInDebugMode: boolean;
37}
38
52f3873ddigeff10 years ago39/* Simulation of adb/ADB */
c51e2d6bdigeff10 years ago40export class AdbSimulator extends adb.AdbEnhancements {
a2e27a97digeff10 years ago41private connectedDevices: IDeviceStateMapping = {};
42private fileSystem: FileSystem;
43
44constructor(fileSystem: FileSystem) {
52f3873ddigeff10 years ago45super();
a2e27a97digeff10 years ago46this.fileSystem = fileSystem;
47}
48
49// Intends to simulate: adb devices
50public getConnectedDevices(): Q.Promise<IDevice[]> {
51return Q.resolve(this.getDevicesIds().map(deviceId => {
52f3873ddigeff10 years ago52const device = this.connectedDevices[deviceId];
53return { id: deviceId, isOnline: device.isOnline, type: device.type };
a2e27a97digeff10 years ago54}));
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
59public reloadAppInDebugMode(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void> {
60const runningApplicationState = this.getRunningAppOrNull(packageName, debugTarget);
61if (runningApplicationState) {
62runningApplicationState.isInDebugMode = true;
63return Q.resolve<void>(void 0);
64} else {
65throw new Error("Implement proper adb response: Application is not running");
66}
67}
68
69// Intends to simulate: adb shell am start
70public launchApp(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void> {
71const deviceState = this.getOnlineDeviceById(debugTarget);
72const installedApplicationState = deviceState.installedApplications[packageName];
73if (installedApplicationState) {
74deviceState.runningApplications[packageName] = { isInDebugMode: false };
75return Q.resolve<void>(void 0);
76} else {
77throw new Error("Implement proper adb response: Application doesn't exist");
78}
79}
80
81// Intends to simulate: adb install
82public installApp(apkPath: string, debugTarget?: string): Q.Promise<void> {
83const deviceState = this.getOnlineDeviceById(debugTarget);
84return new APKSerializer(this.fileSystem).readPackageNameFromFile(apkPath).then(packageName => {
85deviceState.installedApplications[packageName] = {};
86});
87}
88
89// Intends to simulate: adb shell ps | grep <packageName> | awk '{print $9}'
90public isAppRunning(packageName: string, debugTarget?: string): Q.Promise<boolean> {
91return Q.resolve(this.isAppRunningSync(packageName, debugTarget));
92}
93
94// Intends to simulate: combination of this.getConnectedDevices() and this.isAppRunning()
95public findDevicesRunningApp(packageName: string): Q.Promise<string[]> {
96return Q.resolve(this.getOnlineDevicesIds().filter(deviceId =>
97this.isAppRunningSync(packageName, deviceId)));
98}
99
100// Intends to simulate: <adb devices> second column
101public isDeviceOnline(deviceId: string): Q.Promise<boolean> {
102return 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
52f3873ddigeff10 years ago106public notifyDeviceWasConnected(deviceId: string, deviceType: adb.DeviceType): void {
a2e27a97digeff10 years ago107if (this.connectedDevices[deviceId]) {
108throw new Error(`Device ${deviceId} was already connected to simulated ADB`);
109} else {
52f3873ddigeff10 years ago110this.connectedDevices[deviceId] = { isOnline: true, installedApplications: {}, runningApplications: {}, type: deviceType };
a2e27a97digeff10 years ago111}
112}
113
114// We get notified that a device went offline. TODO: Find out how can this happen for real
0766856fdigeff10 years ago115public notifyDevicesAreOffline(deviceIds: string[]): void {
a2e27a97digeff10 years ago116deviceIds.forEach(deviceId => {
117return this.notifyDeviceIsOffline(deviceId);
118});
119}
120
52f3873ddigeff10 years ago121public apiVersion(deviceId: string): Q.Promise<adb.AndroidAPILevel> {
122throw new Error("Not yet implemented: Implement if we need to use these methods in a test");
123}
124
125public reverseAdd(deviceId: string, devicePort: string, computerPort: string): Q.Promise<void> {
126throw new Error("Not yet implemented: Implement if we need to use these methods in a test");
127}
128
a2e27a97digeff10 years ago129private isAppRunningSync(packageName: string, debugTarget?: string): boolean {
130return this.getRunningAppOrNull(packageName, debugTarget) != null;
131}
132
133private notifyDeviceIsOffline(deviceId: string): void {
134this.getOnlineDeviceById(deviceId).isOnline = false;
135}
136
137private getRunningAppOrNull(packageName: string, debugTarget?: string): IRunningApplicationState {
138return this.getOnlineDeviceById(debugTarget).runningApplications[packageName];
139}
140
141private getOnlineDeviceById(deviceId?: string): IDeviceState {
142const deviceState = this.getDeviceById(deviceId);
143if (deviceState.isOnline) {
144return deviceState;
145} else {
146throw new Error("Implement proper adb response: Target device isn't online");
147}
148}
149
150private getDeviceById(deviceId?: string): IDeviceState {
151if (deviceId) { // If the deviceId is specified, we search for that device
152const deviceState = this.connectedDevices[deviceId];
153if (deviceState) { // If it exists, we return it
154return deviceState;
155} else { // If not we fail
156throw new Error("Implement proper adb response: Target device doesn't exist");
157}
158} else {
159const devicesIds = this.getDevicesIds();
160if (devicesIds.length === 1) { // If deviceId is null and we have a single device, we return that device
161return this.connectedDevices[devicesIds[0]];
162} else if (devicesIds.length > 1) {
163throw new Error("error: more than one device/emulator"); // error code = 1
164} else {
165throw new Error("error: no devices found"); // error code = 1
166}
167}
168}
169
170private getDevicesIds(): string[] {
171return Object.keys(this.connectedDevices);
172}
173
174private getOnlineDevicesIds(): string[] {
175return this.getDevicesIds().filter(deviceId =>
176this.isDeviceOnlineSync(deviceId));
177}
178
179private isDeviceOnlineSync(deviceId: string): boolean {
180return this.getDeviceById(deviceId).isOnline;
181}
182}