microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c7f1165c059665e9baf0897641ef4f63773f36ed

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/test/resources/reactNative022.ts

178lines · modeblame

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