microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
test/smoke/package/src/helpers/androidEmulatorHelper.ts
161lines · 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 cp from "child_process"; |
| 5 | import { SmokeTestsConstants } from "./smokeTestsConstants"; |
| 6 | import { sleep } from "./utilities"; |
| 7 | |
| 8 | export class AndroidEmulatorHelper { |
| 9 | |
| 10 | public static androidEmulatorPort = 5554; |
| 11 | public static androidEmulatorName = `emulator-${AndroidEmulatorHelper.androidEmulatorPort}`; |
| 12 | |
| 13 | public static getDevice(): string | undefined { |
| 14 | if (!process.env.ANDROID_EMULATOR) { |
| 15 | throw new Error("Environment variable 'ANDROID_EMULATOR' is not set. Exiting..."); |
| 16 | } |
| 17 | return process.env.ANDROID_EMULATOR; |
| 18 | } |
| 19 | |
| 20 | public static async runAndroidEmulator() { |
| 21 | this.terminateAndroidEmulator(); |
| 22 | // Boot options for emulator - https://developer.android.com/studio/run/emulator-commandline |
| 23 | const emulatorOpts = ["-avd", |
| 24 | <string>this.getDevice(), |
| 25 | "-gpu", "swiftshader_indirect", |
| 26 | "-wipe-data", |
| 27 | "-port", this.androidEmulatorPort.toString(), |
| 28 | "-no-snapshot-save", |
| 29 | "-no-boot-anim", |
| 30 | "-no-audio"]; |
| 31 | console.log(`*** Executing Android emulator with 'emulator ${emulatorOpts.join(" ")}' command...`); |
| 32 | const proc = cp.spawn("emulator", emulatorOpts, {stdio: "pipe"}); |
| 33 | let started = false; |
| 34 | proc.stdout.on("data", (chunk) => { |
| 35 | process.stdout.write(chunk); |
| 36 | if (/boot completed/.test(chunk.toString().trim())) { |
| 37 | started = true; |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | proc.stderr.on("data", (chunk) => { |
| 42 | process.stderr.write(chunk); |
| 43 | }); |
| 44 | |
| 45 | console.log(`*** Waiting for emulator to load (timeout is ${SmokeTestsConstants.emulatorLoadTimeout}ms)`); |
| 46 | let awaitRetries: number = SmokeTestsConstants.emulatorLoadTimeout / 1000; |
| 47 | let retry = 1; |
| 48 | await new Promise((resolve, reject) => { |
| 49 | let check = setInterval(async () => { |
| 50 | if (started) { |
| 51 | clearInterval(check); |
| 52 | console.log("*** Emulator finished loading, waiting for 2 seconds"); |
| 53 | await sleep(2000); |
| 54 | resolve(); |
| 55 | } else { |
| 56 | retry++; |
| 57 | if (retry >= awaitRetries) { |
| 58 | // When time's up just let it go - emulator should have started at this time |
| 59 | // The reason why std check didn't work is more likely that extra logging (INFO level) for emulator was disabled |
| 60 | clearInterval(check); |
| 61 | resolve(); |
| 62 | } |
| 63 | } |
| 64 | }, 1000); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | // Terminates emulator "emulator-PORT" if it exists, where PORT is 5554 by default |
| 69 | public static terminateAndroidEmulator() { |
| 70 | let devices = cp.execSync("adb devices").toString().trim(); |
| 71 | console.log("*** Checking for running android emulators..."); |
| 72 | if (devices !== "List of devices attached") { |
| 73 | // Check if we already have a running emulator, and terminate it if it so |
| 74 | console.log(`Terminating Android '${this.androidEmulatorName}'...`); |
| 75 | cp.execSync(`adb -s ${this.androidEmulatorName} emu kill`, {stdio: "inherit"}); |
| 76 | } else { |
| 77 | console.log("*** No running android emulators found"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Check if appPackage is installed on Android device for waitTime ms |
| 82 | public static async checkIfAppIsInstalled(appPackage: string, waitTime: number, waitInitTime?: number) { |
| 83 | let awaitRetries: number = waitTime / 1000; |
| 84 | let retry = 1; |
| 85 | await new Promise((resolve, reject) => { |
| 86 | let check = setInterval(async () => { |
| 87 | if (retry % 5 === 0) { |
| 88 | console.log(`*** Check if app is being installed with command 'adb shell pm list packages ${appPackage}' for ${retry} time`); |
| 89 | } |
| 90 | let result; |
| 91 | try { |
| 92 | result = cp.execSync(`adb shell pm list packages ${appPackage}`).toString().trim(); |
| 93 | } catch (e) { |
| 94 | clearInterval(check); |
| 95 | reject(`Error occured while check app is installed:\n ${e}`); |
| 96 | } |
| 97 | if (result) { |
| 98 | clearInterval(check); |
| 99 | const initTimeout = waitInitTime || 10000; |
| 100 | console.log(`*** Installed ${appPackage} app found, await ${initTimeout}ms for initializing...`); |
| 101 | await sleep(initTimeout); |
| 102 | resolve(); |
| 103 | } else { |
| 104 | retry++; |
| 105 | if (retry >= awaitRetries) { |
| 106 | clearInterval(check); |
| 107 | reject(`${appPackage} not found after ${waitTime}ms`); |
| 108 | } |
| 109 | } |
| 110 | }, 1000); |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | // Check if appPackage is installed on Android device for waitTime ms |
| 115 | public static async checkIfAndroidAppIsInstalled(appPackage: string, waitTime: number, waitInitTime?: number) { |
| 116 | let awaitRetries: number = waitTime / 1000; |
| 117 | let retry = 1; |
| 118 | await new Promise((resolve, reject) => { |
| 119 | let check = setInterval(async () => { |
| 120 | if (retry % 5 === 0) { |
| 121 | console.log(`*** Check if app is being installed with command 'adb shell pm list packages ${appPackage}' for ${retry} time`); |
| 122 | } |
| 123 | let result; |
| 124 | try { |
| 125 | result = cp.execSync(`adb shell pm list packages ${appPackage}`).toString().trim(); |
| 126 | } catch (e) { |
| 127 | clearInterval(check); |
| 128 | reject(`Error occured while check app is installed:\n ${e}`); |
| 129 | } |
| 130 | if (result) { |
| 131 | clearInterval(check); |
| 132 | const initTimeout = waitInitTime || 10000; |
| 133 | console.log(`*** Installed ${appPackage} app found, await ${initTimeout}ms for initializing...`); |
| 134 | await sleep(initTimeout); |
| 135 | resolve(); |
| 136 | } else { |
| 137 | retry++; |
| 138 | if (retry >= awaitRetries) { |
| 139 | clearInterval(check); |
| 140 | reject(`${appPackage} not found after ${waitTime}ms`); |
| 141 | } |
| 142 | } |
| 143 | }, 1000); |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | public static uninstallTestAppFromEmulator(appPackage: string) { |
| 148 | console.log(`*** Uninstalling test app ${appPackage}' from Emulator`); |
| 149 | try { |
| 150 | cp.spawnSync("adb", ["shell", "pm", "uninstall", appPackage], {stdio: "inherit"}); |
| 151 | } catch (e) { |
| 152 | console.error(`Error occured while uninstalling test app:\n ${e}`); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | public static async enableDrawPermitForApp(packageName: string) { |
| 157 | const drawPermitCommand = `adb -s ${AndroidEmulatorHelper.androidEmulatorName} shell appops set ${packageName} SYSTEM_ALERT_WINDOW allow`; |
| 158 | console.log(`*** Enabling permission for drawing over apps via: ${drawPermitCommand}`); |
| 159 | cp.execSync(drawPermitCommand, {stdio: "inherit"}); |
| 160 | } |
| 161 | } |
| 162 | |