microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.8

Branches

Tags

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

Clone

HTTPS

Download ZIP

test/resources/reactNative022.ts

183lines · 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
3c172a05Artem Egorov8 years ago8import {PromiseUtil} from "../../src/common/node/promise";
c7f1165cdigeff10 years ago9
0a68f8dbArtem Egorov8 years ago10import {IAndroidRunOptions} from "../../src/extension/launchArgs";
3c172a05Artem Egorov8 years ago11import {ISpawnResult} from "../../src/common/node/childProcess";
12import {FileSystem} from "../../src/common/node/fileSystem";
13import {Package} from "../../src/common/node/package";
c7f1165cdigeff10 years ago14import {Recording, Simulator} from "./processExecution/simulator";
7daed3fcArtem Egorov8 years ago15import {AdbHelper} from "../../src/extension/android/adb";
c7f1165cdigeff10 years ago16import {APKSerializer} from "./simulators/apkSerializer";
17
678db279Artem Egorov8 years ago18const sampleRNProjectPath = path.join(__dirname, "sampleReactNative022Project");
19const processExecutionsRecordingsPath = path.join(__dirname, "processExecutionsRecordings");
c7f1165cdigeff10 years ago20
21/* This class simulates calling the React-Native CLI v0.22. It currently supports react-native init
f0008229digeff10 years ago22and react-native run-android. */
8022afdfVladimir Kotikov8 years ago23export class ReactNative022 {
396b6627Vladimir Kotikov8 years ago24
25public static DEFAULT_PROJECT_FILE = path.join(sampleRNProjectPath, "package.json");
26
c7f1165cdigeff10 years ago27private static ANDROID_APK_RELATIVE_PATH = "android/app/build/outputs/apk/app-debug.apk";
28
396b6627Vladimir Kotikov8 years ago29private projectFileContent: string;
30
c7f1165cdigeff10 years ago31private simulator: Simulator = new Simulator({
32beforeStart: () => this.readAndroidPackageName(), // 1. We read the package.json to verify this is a RN project
33outputBased: [
34{
35eventPattern: /:app:assembleDebug/,
36action: () => this.createAPK(), // 2. We compile the application.
37},
38{
39eventPattern: /Installed on [0-9]+ devices*\./,
40action: () => this.installAppInAllDevices(), // 3. We install it on all available devices.
41},
42],
43beforeSuccess: (stdout: string, stderr: string) => // 4. If we didn't had any errors after starting to launch the app,
44this.launchApp(stdout, stderr), // it means we were succesful
45});
46
47private recording: Recording;
48
49private androidPackageName: string;
50private projectRoot: string;
51private androidAPKPath: string;
52
7daed3fcArtem Egorov8 years ago53constructor(private fileSystem: FileSystem) {
c7f1165cdigeff10 years ago54assert(this.fileSystem, "fileSystem shouldn't be null");
55}
56
396b6627Vladimir Kotikov8 years ago57public fromProjectFileContent(content: string): this {
58this.projectFileContent = content;
59return this;
60}
61
c7f1165cdigeff10 years ago62public loadRecordingFromName(recordingName: string): Q.Promise<void> {
63return this.loadRecordingFromFile(path.join(processExecutionsRecordingsPath, `${recordingName}.json`));
64}
65
396b6627Vladimir Kotikov8 years ago66public loadRecordingFromString(recordingContent: string): Q.Promise<void> {
67return Q.when(this.loadRecording(JSON.parse(recordingContent)));
68}
69
c7f1165cdigeff10 years ago70public loadRecordingFromFile(recordingPath: string): Q.Promise<void> {
71return Q({})
72.then(() => {
73return new FileSystem().readFile(recordingPath);
74}).then(fileContents => {
75this.loadRecording(JSON.parse(fileContents));
76});
77}
78
79public loadRecording(recording: Recording): void {
80assert(recording, "recording shouldn't be null");
81this.recording = recording;
82}
83
84public createProject(projectRoot: string, projectName: string): Q.Promise<void> {
85return Q({})
86.then(() => {
87this.fileSystem.makeDirectoryRecursiveSync(projectRoot);
396b6627Vladimir Kotikov8 years ago88return this.projectFileContent !== undefined ?
89this.projectFileContent :
90this.readDefaultProjectFile();
c7f1165cdigeff10 years ago91}).then(defaultContents => {
92const reactNativeConfiguration = JSON.parse(defaultContents);
93reactNativeConfiguration.name = projectName;
94const reactNativeConfigurationFormatted = JSON.stringify(reactNativeConfiguration);
95return this.fileSystem.writeFile(this.getPackageJsonPath(projectRoot), reactNativeConfigurationFormatted);
96}).then(() => {
97return this.fileSystem.mkDir(this.getAndroidProjectPath(projectRoot));
98});
99}
100
0db0be15Artem Egorov8 years ago101public runAndroid(runOptions: IAndroidRunOptions): ISpawnResult {
102this.projectRoot = runOptions.projectRoot;
c7f1165cdigeff10 years ago103this.simulator.simulate(this.recording).done();
104return this.simulator.spawn();
105}
106
107private getAndroidProjectPath(projectRoot = this.projectRoot): string {
108return path.join(projectRoot, "android");
109}
110
111private getPackageJsonPath(projectRoot: string): string {
112return new Package(projectRoot, { fileSystem: this.fileSystem }).informationJsonFilePath();
113}
114
115private readAndroidPackageName(): Q.Promise<void> {
116return new Package(this.projectRoot, { fileSystem: this.fileSystem }).name().then(name => {
117this.androidPackageName = `com.${name.toLowerCase()}`;
118});
119}
120
121private createAPK(): Q.Promise<void> {
122return this.isAndroidProjectPresent().then(isPresent => {
5c8365a6Artem Egorov8 years ago123return isPresent ? void 0 : Q.reject<void>(new Error("The recording expects the Android project to be present, but it's not"));
c7f1165cdigeff10 years ago124}).then(() => {
125this.androidAPKPath = path.join(this.projectRoot, ReactNative022.ANDROID_APK_RELATIVE_PATH);
126return new APKSerializer(this.fileSystem).writeApk(this.androidAPKPath, { packageName: this.androidPackageName });
127});
128}
129
130private isAndroidProjectPresent(): Q.Promise<boolean> {
131// TODO: Make more checks as neccesary for the tests
132return this.fileSystem.directoryExists(this.getAndroidProjectPath());
133}
134
135private installAppInAllDevices(): Q.Promise<void> {
7daed3fcArtem Egorov8 years ago136let devices = AdbHelper.getConnectedDevices();
137return new PromiseUtil().reduce(devices, device => this.installAppInDevice(device.id));
c7f1165cdigeff10 years ago138}
139
140private installAppInDevice(deviceId: string): Q.Promise<void> {
7daed3fcArtem Egorov8 years ago141throw Error("Mock not implemented");
c7f1165cdigeff10 years ago142}
143
144private launchApp(stdout: string, stderr: string): Q.Promise<void> {
145/*
146Sample output we want to accept:
147BUILD SUCCESSFUL
148
149Total time: 9.052 secs
150Starting the app (C:\Program Files (x86)\Android\android-sdk/platform-tools/adb shell am start -n com.sampleapplication/.MainActivity)...
151Starting: Intent { cmp=com.sampleapplication/.MainActivity }
152
153
154Sample output we don't to accept:
155BUILD SUCCESSFUL
156
157Total time: 9.052 secs
158Starting the app (C:\Program Files (x86)\Android\android-sdk/platform-tools/adb shell am start -n com.sampleapplication/.MainActivity)...
159Starting: Intent { cmp=com.sampleapplication/.MainActivity }
160Error: some error happened
161**/
162const succesfulOutputEnd = `Starting the app \\(.*adb shell am start -n ([^ /]+)\/\\.MainActivity\\)\\.\\.\\.\\s+`
163+ `Starting: Intent { cmp=([^ /]+)\/\\.MainActivity }\\s+$`;
164const matches = stdout.match(new RegExp(succesfulOutputEnd));
165if (matches) {
166if (matches.length === 3 && matches[1] === this.androidPackageName && matches[2] === this.androidPackageName) {
7daed3fcArtem Egorov8 years ago167return AdbHelper.launchApp(this.projectRoot, this.androidPackageName);
c7f1165cdigeff10 years ago168} else {
169return Q.reject<void>(new Error("There was an error while trying to match the Starting the app messages."
170+ "Expected to match the pattern and recognize the expected android package name, but it failed."
171+ `Expected android package name: ${this.androidPackageName}. Actual matches: ${JSON.stringify(matches)}`));
172}
173} else {
174// The record doesn't indicate that the app was launched, so we don't do anything
5c8365a6Artem Egorov8 years ago175return Q.resolve(void 0);
c7f1165cdigeff10 years ago176}
177}
178
396b6627Vladimir Kotikov8 years ago179private readDefaultProjectFile(): Q.Promise<string> {
c7f1165cdigeff10 years ago180const realFileSystem = new FileSystem(); // We always use the real file system (not the mock one) to read the sample project
396b6627Vladimir Kotikov8 years ago181return realFileSystem.readFile(ReactNative022.DEFAULT_PROJECT_FILE);
c7f1165cdigeff10 years ago182}
183}