microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/test/resources/simulators/avdManager.ts
57lines · 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 | |
| 4 | import * as Q from "q"; |
| 5 | import {DeviceType} from "../../../common/android/adb"; |
| 6 | import {AdbSimulator} from "./adbSimulator"; |
| 7 | |
| 8 | export interface IAVDManager { |
| 9 | // Intends to simulate: android create avd -n <name> -t <targetID> [-<option> <value>] ... |
| 10 | create(avdId: string): Q.Promise<void>; |
| 11 | // Intends to simulate: emulator -avd WVGA800 -scale 96dpi -dpi-device 160 |
| 12 | launch(avdId: string): Q.Promise<string>; |
| 13 | createAndLaunch(avdId: string): Q.Promise<string>; |
| 14 | createAndLaunchAll(avdIds: string[]): Q.Promise<string[]>; |
| 15 | } |
| 16 | |
| 17 | interface IDeviceStateMapping { |
| 18 | [avdId: string]: any; |
| 19 | } |
| 20 | |
| 21 | /* Simulation of AVD Manager. */ |
| 22 | export class AVDManager implements IAVDManager { |
| 23 | private nextAvailablePort = 5555; |
| 24 | |
| 25 | private devices: IDeviceStateMapping = {}; |
| 26 | |
| 27 | constructor(private adb: AdbSimulator) {} |
| 28 | |
| 29 | public createAndLaunch(avdId: string): Q.Promise<string> { |
| 30 | return this.create(avdId).then(() => |
| 31 | this.launch(avdId)); |
| 32 | } |
| 33 | |
| 34 | // Intends to simulate: android create avd -n <name> -t <targetID> [-<option> <value>] ... |
| 35 | public create(avdId: string): Q.Promise<void> { |
| 36 | if (!this.devices[avdId]) { |
| 37 | this.devices[avdId] = {}; |
| 38 | return Q.resolve<void>(void 0); |
| 39 | } else { |
| 40 | throw new Error("Implement to match AVD: Device already exists"); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Intends to simulate: emulator -avd WVGA800 -scale 96dpi -dpi-device 160 |
| 45 | public launch(avdId: string): Q.Promise<string> { |
| 46 | if (this.devices[avdId]) { |
| 47 | this.adb.notifyDeviceWasConnected(avdId, DeviceType.AndroidSdkEmulator); |
| 48 | return Q(`emulator-${this.nextAvailablePort++}`); |
| 49 | } else { |
| 50 | throw new Error("Implement to match AVD: Device doesn't exists"); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public createAndLaunchAll(avdIds: string[]): Q.Promise<string[]> { |
| 55 | return Q.all(avdIds.map(avdId => this.createAndLaunch(avdId))); |
| 56 | } |
| 57 | } |