microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/test/resources/reactNative022.ts
177lines · 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 * as path from "path"; |
| 6 | import * as assert from "assert"; |
| 7 | |
| 8 | import {PromiseUtil} from "../../common/node/promise"; |
| 9 | |
| 10 | import * as reactNative from "../../common/reactNative"; |
| 11 | import {ISpawnResult} from "../../common/node/childProcess"; |
| 12 | import {FileSystem} from "../../common/node/fileSystem"; |
| 13 | import {Package} from "../../common/node/package"; |
| 14 | import {Recording, Simulator} from "./processExecution/simulator"; |
| 15 | import {AdbSimulator} from "./simulators/adbSimulator"; |
| 16 | import {APKSerializer} from "./simulators/apkSerializer"; |
| 17 | |
| 18 | const resourcesPath = path.join(__dirname, "../../../src/test/resources/"); |
| 19 | const sampleRNProjectPath = path.join(resourcesPath, "sampleReactNative022Project"); |
| 20 | const processExecutionsRecordingsPath = path.join(resourcesPath, "processExecutionsRecordings"); |
| 21 | |
| 22 | export type IReactNative = reactNative.IReactNative; |
| 23 | |
| 24 | /* This class simulates calling the React-Native CLI v0.22. It currently supports react-native init |
| 25 | and react-native run-android. */ |
| 26 | export class ReactNative022 implements IReactNative { |
| 27 | private static ANDROID_APK_RELATIVE_PATH = "android/app/build/outputs/apk/app-debug.apk"; |
| 28 | |
| 29 | private simulator: Simulator = new Simulator({ |
| 30 | beforeStart: () => this.readAndroidPackageName(), // 1. We read the package.json to verify this is a RN project |
| 31 | outputBased: [ |
| 32 | { |
| 33 | eventPattern: /:app:assembleDebug/, |
| 34 | action: () => this.createAPK(), // 2. We compile the application. |
| 35 | }, |
| 36 | { |
| 37 | eventPattern: /Installed on [0-9]+ devices*\./, |
| 38 | action: () => this.installAppInAllDevices(), // 3. We install it on all available devices. |
| 39 | }, |
| 40 | ], |
| 41 | beforeSuccess: (stdout: string, stderr: string) => // 4. If we didn't had any errors after starting to launch the app, |
| 42 | this.launchApp(stdout, stderr), // it means we were succesful |
| 43 | }); |
| 44 | |
| 45 | private recording: Recording; |
| 46 | |
| 47 | private androidPackageName: string; |
| 48 | private projectRoot: string; |
| 49 | private androidAPKPath: string; |
| 50 | |
| 51 | constructor(private adb: AdbSimulator, private fileSystem: FileSystem) { |
| 52 | assert(this.adb, "adb shouldn't be null"); |
| 53 | assert(this.fileSystem, "fileSystem shouldn't be null"); |
| 54 | } |
| 55 | |
| 56 | public loadRecordingFromName(recordingName: string): Q.Promise<void> { |
| 57 | return this.loadRecordingFromFile(path.join(processExecutionsRecordingsPath, `${recordingName}.json`)); |
| 58 | } |
| 59 | |
| 60 | public loadRecordingFromFile(recordingPath: string): Q.Promise<void> { |
| 61 | return Q({}) |
| 62 | .then(() => { |
| 63 | return new FileSystem().readFile(recordingPath); |
| 64 | }).then(fileContents => { |
| 65 | this.loadRecording(JSON.parse(fileContents)); |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | public loadRecording(recording: Recording): void { |
| 70 | assert(recording, "recording shouldn't be null"); |
| 71 | this.recording = recording; |
| 72 | } |
| 73 | |
| 74 | public createProject(projectRoot: string, projectName: string): Q.Promise<void> { |
| 75 | return Q({}) |
| 76 | .then(() => { |
| 77 | this.fileSystem.makeDirectoryRecursiveSync(projectRoot); |
| 78 | return this.readDefaultProjectFile("package.json"); |
| 79 | }).then(defaultContents => { |
| 80 | const reactNativeConfiguration = JSON.parse(defaultContents); |
| 81 | reactNativeConfiguration.name = projectName; |
| 82 | const reactNativeConfigurationFormatted = JSON.stringify(reactNativeConfiguration); |
| 83 | return this.fileSystem.writeFile(this.getPackageJsonPath(projectRoot), reactNativeConfigurationFormatted); |
| 84 | }).then(() => { |
| 85 | return this.fileSystem.mkDir(this.getAndroidProjectPath(projectRoot)); |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | public runAndroid(projectRoot: string): ISpawnResult { |
| 90 | this.projectRoot = projectRoot; |
| 91 | this.simulator.simulate(this.recording).done(); |
| 92 | return this.simulator.spawn(); |
| 93 | } |
| 94 | |
| 95 | private getAndroidProjectPath(projectRoot = this.projectRoot): string { |
| 96 | return path.join(projectRoot, "android"); |
| 97 | } |
| 98 | |
| 99 | private getPackageJsonPath(projectRoot: string): string { |
| 100 | return new Package(projectRoot, { fileSystem: this.fileSystem }).informationJsonFilePath(); |
| 101 | } |
| 102 | |
| 103 | private readAndroidPackageName(): Q.Promise<void> { |
| 104 | return new Package(this.projectRoot, { fileSystem: this.fileSystem }).name().then(name => { |
| 105 | this.androidPackageName = `com.${name.toLowerCase()}`; |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | private createAPK(): Q.Promise<void> { |
| 110 | return this.isAndroidProjectPresent().then(isPresent => { |
| 111 | if (!isPresent) { |
| 112 | return Q.reject<void>(new Error("The recording expects the Android project to be present, but it's not")); |
| 113 | } |
| 114 | }).then(() => { |
| 115 | this.androidAPKPath = path.join(this.projectRoot, ReactNative022.ANDROID_APK_RELATIVE_PATH); |
| 116 | return new APKSerializer(this.fileSystem).writeApk(this.androidAPKPath, { packageName: this.androidPackageName }); |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | private isAndroidProjectPresent(): Q.Promise<boolean> { |
| 121 | // TODO: Make more checks as neccesary for the tests |
| 122 | return this.fileSystem.directoryExists(this.getAndroidProjectPath()); |
| 123 | } |
| 124 | |
| 125 | private installAppInAllDevices(): Q.Promise<void> { |
| 126 | return new PromiseUtil().reduce(this.adb.getConnectedDevices(), device => this.installAppInDevice(device.id)); |
| 127 | } |
| 128 | |
| 129 | private installAppInDevice(deviceId: string): Q.Promise<void> { |
| 130 | return this.adb.isDeviceOnline(deviceId).then(isOnline => { |
| 131 | if (isOnline) { |
| 132 | return this.adb.installApp(this.androidAPKPath, deviceId); |
| 133 | } else { |
| 134 | // TODO: Figure out what's the right thing to do here, if we ever need this for the tests |
| 135 | } |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | private launchApp(stdout: string, stderr: string): Q.Promise<void> { |
| 140 | /* |
| 141 | Sample output we want to accept: |
| 142 | BUILD SUCCESSFUL |
| 143 | |
| 144 | Total time: 9.052 secs |
| 145 | Starting the app (C:\Program Files (x86)\Android\android-sdk/platform-tools/adb shell am start -n com.sampleapplication/.MainActivity)... |
| 146 | Starting: Intent { cmp=com.sampleapplication/.MainActivity } |
| 147 | |
| 148 | |
| 149 | Sample output we don't to accept: |
| 150 | BUILD SUCCESSFUL |
| 151 | |
| 152 | Total time: 9.052 secs |
| 153 | Starting the app (C:\Program Files (x86)\Android\android-sdk/platform-tools/adb shell am start -n com.sampleapplication/.MainActivity)... |
| 154 | Starting: Intent { cmp=com.sampleapplication/.MainActivity } |
| 155 | Error: some error happened |
| 156 | **/ |
| 157 | const succesfulOutputEnd = `Starting the app \\(.*adb shell am start -n ([^ /]+)\/\\.MainActivity\\)\\.\\.\\.\\s+` |
| 158 | + `Starting: Intent { cmp=([^ /]+)\/\\.MainActivity }\\s+$`; |
| 159 | const matches = stdout.match(new RegExp(succesfulOutputEnd)); |
| 160 | if (matches) { |
| 161 | if (matches.length === 3 && matches[1] === this.androidPackageName && matches[2] === this.androidPackageName) { |
| 162 | return this.adb.launchApp(this.projectRoot, this.androidPackageName); |
| 163 | } else { |
| 164 | return Q.reject<void>(new Error("There was an error while trying to match the Starting the app messages." |
| 165 | + "Expected to match the pattern and recognize the expected android package name, but it failed." |
| 166 | + `Expected android package name: ${this.androidPackageName}. Actual matches: ${JSON.stringify(matches)}`)); |
| 167 | } |
| 168 | } else { |
| 169 | // The record doesn't indicate that the app was launched, so we don't do anything |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | private readDefaultProjectFile(relativeFilePath: string): Q.Promise<string> { |
| 174 | const realFileSystem = new FileSystem(); // We always use the real file system (not the mock one) to read the sample project |
| 175 | return realFileSystem.readFile(path.join(sampleRNProjectPath, relativeFilePath)); |
| 176 | } |
| 177 | } |
| 178 | |