microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.12

Branches

Tags

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

Clone

HTTPS

Download ZIP

test/extension/android/androidPlatform.test.ts

305lines · 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";
396b6627Vladimir Kotikov8 years ago5import * as fs from "fs";
c7f1165cdigeff10 years ago6import * as path from "path";
7import * as mockFs from "mock-fs";
8
0a68f8dbArtem Egorov8 years ago9import {AndroidPlatform} from "../../../src/extension/android/androidPlatform";
10import {IAndroidRunOptions} from "../../../src/extension/launchArgs";
3c172a05Artem Egorov8 years ago11import {FileSystem} from "../../../src/common/node/fileSystem";
5c8365a6Artem Egorov8 years ago12import {ReactNative022} from "../../resources/reactNative022";
7daed3fcArtem Egorov8 years ago13import * as adb from "../../../src/extension/android/adb";
c7f1165cdigeff10 years ago14import {RecordingsHelper} from "../../resources/recordingsHelper";
8022afdfVladimir Kotikov8 years ago15import {CommandExecutor} from "../../../src/common/commandExecutor";
377a189cArtem Egorov8 years ago16import * as rnHelper from "../../../src/common/reactNativeProjectHelper";
c7f1165cdigeff10 years ago17
18import "should";
8022afdfVladimir Kotikov8 years ago19import * as sinon from "sinon";
e4dd9aa4Serge Svekolnikov8 years ago20import { SettingsHelper } from "../../../src/extension/settingsHelper";
c7f1165cdigeff10 years ago21
f0008229digeff10 years ago22// TODO: Launch the extension server
c7f1165cdigeff10 years ago23
24suite("androidPlatform", function () {
0a68f8dbArtem Egorov8 years ago25suite("extensionContext", function () {
c7f1165cdigeff10 years ago26const projectRoot = "C:/projects/SampleApplication_21/";
27const androidProjectPath = path.join(projectRoot, "android");
28const applicationName = "SampleApplication";
29const androidPackageName = "com.sampleapplication";
2e432a9eArtem Egorov8 years ago30const genericRunOptions: IAndroidRunOptions = { platform: "android", workspaceRoot: projectRoot, projectRoot: projectRoot };
c7f1165cdigeff10 years ago31
396b6627Vladimir Kotikov8 years ago32const rnProjectContent = fs.readFileSync(ReactNative022.DEFAULT_PROJECT_FILE, "utf8");
33
c7f1165cdigeff10 years ago34let fileSystem: FileSystem;
35let reactNative: ReactNative022;
36let androidPlatform: AndroidPlatform;
8022afdfVladimir Kotikov8 years ago37let sandbox: Sinon.SinonSandbox;
7daed3fcArtem Egorov8 years ago38let devices: any;
c7f1165cdigeff10 years ago39
0db0be15Artem Egorov8 years ago40function createAndroidPlatform(runOptions: IAndroidRunOptions): AndroidPlatform {
7daed3fcArtem Egorov8 years ago41return new AndroidPlatform(runOptions);
c7f1165cdigeff10 years ago42}
43
0766856fdigeff10 years ago44setup(() => {
396b6627Vladimir Kotikov8 years ago45mockFs();
8022afdfVladimir Kotikov8 years ago46sandbox = sinon.sandbox.create();
396b6627Vladimir Kotikov8 years ago47
0766856fdigeff10 years ago48// Configure all the dependencies we'll use in our tests
396b6627Vladimir Kotikov8 years ago49fileSystem = new FileSystem();
7daed3fcArtem Egorov8 years ago50reactNative = new ReactNative022(fileSystem);
e4dd9aa4Serge Svekolnikov8 years ago51
52sandbox.stub(SettingsHelper, "getReactNativeProjectRoot", () => projectRoot);
53
0766856fdigeff10 years ago54androidPlatform = createAndroidPlatform(genericRunOptions);
55
8022afdfVladimir Kotikov8 years ago56sandbox.stub(CommandExecutor.prototype, "spawnReactCommand", function () {
57return reactNative.runAndroid(genericRunOptions);
58});
59
377a189cArtem Egorov8 years ago60sandbox.stub(rnHelper.ReactNativeProjectHelper, "getReactNativeVersion", function () {
61return Q.resolve("0.0.1");
62});
63
7daed3fcArtem Egorov8 years ago64sandbox.stub(adb.AdbHelper, "launchApp", function (projectRoot_: string, packageName: string, debugTarget?: string) {
65devices = devices.map((device: any) => {
66if (!debugTarget) {
67device.installedApplications[androidPackageName] = { isInDebugMode: false };
68}
69
70if (debugTarget && debugTarget === device.id) {
71device.installedApplications[androidPackageName] = { isInDebugMode: false };
72}
73
74return device;
75});
76
77return Q.resolve(void 0);
78});
79sandbox.stub(adb.AdbHelper, "getConnectedDevices", function () {
80return Q.resolve(devices);
81});
82sandbox.stub(adb.AdbHelper, "getOnlineDevices", function () {
83return Q.resolve(devices.filter((device: any) => {
84return device.isOnline;
85}));
86});
87sandbox.stub(adb.AdbHelper, "apiVersion", function () {
88return Q.resolve(adb.AndroidAPILevel.LOLLIPOP);
89});
90sandbox.stub(adb.AdbHelper, "reverseAdb", function () {
91return Q.resolve(void 0);
92});
93
94sandbox.stub(reactNative, "installAppInDevice", function (deviceId: string) {
95devices = devices.map((device: any) => {
96if (deviceId && deviceId === device.id) {
97device.installedApplications[androidPackageName] = {};
98}
99
100return device;
101});
102return Q.resolve(void 0);
103});
104
0766856fdigeff10 years ago105// Create a React-Native project we'll use in our tests
396b6627Vladimir Kotikov8 years ago106return reactNative
107.fromProjectFileContent(rnProjectContent)
108.createProject(projectRoot, applicationName);
109});
110
111teardown(() => {
112mockFs.restore();
8022afdfVladimir Kotikov8 years ago113sandbox.restore();
7daed3fcArtem Egorov8 years ago114devices = [];
0766856fdigeff10 years ago115});
116
c7f1165cdigeff10 years ago117const testWithRecordings = new RecordingsHelper(() => reactNative).test;
118
119testWithRecordings("runApp launches the app when a single emulator is connected",
120[
121"react-native/run-android/win10-rn0.21.0/succeedsWithOneVSEmulator",
122"react-native/run-android/win10-rn0.22.2/succeedsWithOneVSEmulator",
123"react-native/run-android/osx10.10-rn0.21.0/succeedsWithOneVSEmulator",
124], () => {
7daed3fcArtem Egorov8 years ago125devices = fillDevices(["Nexus_5"]);
126
c7f1165cdigeff10 years ago127return Q({})
128.then(() => {
129return androidPlatform.runApp();
130}).then(() => {
7daed3fcArtem Egorov8 years ago131return devices[0].installedApplications[androidPackageName].isInDebugMode === false;
c7f1165cdigeff10 years ago132}).then(isRunning => {
133isRunning.should.be.true();
134});
135});
136
137testWithRecordings("runApp launches the app when two emulators are connected",
138["react-native/run-android/win10-rn0.21.0/succeedsWithTwoVSEmulators"], () => {
7daed3fcArtem Egorov8 years ago139devices = fillDevices(["Nexus_5", "Nexus_6"]);
140
c7f1165cdigeff10 years ago141return Q({})
142.then(() => {
143return androidPlatform.runApp();
144}).then(() => {
145return Q.all([
7daed3fcArtem Egorov8 years ago146Q.resolve(devices[0].installedApplications[androidPackageName].isInDebugMode === false),
147Q.resolve(devices[1].installedApplications[androidPackageName].isInDebugMode === false),
c7f1165cdigeff10 years ago148]);
149}).spread((isRunningOnNexus5, isRunningOnNexus6) => {
150// It should be running in exactly one of these two devices
151isRunningOnNexus5.should.not.eql(isRunningOnNexus6);
152});
153});
154
155testWithRecordings("runApp launches the app when three emulators are connected",
156["react-native/run-android/win10-rn0.21.0/succeedsWithThreeVSEmulators"], () => {
7daed3fcArtem Egorov8 years ago157devices = fillDevices(["Nexus_5", "Nexus_6", "Nexus_7"]);
c7f1165cdigeff10 years ago158return Q({})
159.then(() => {
160return androidPlatform.runApp();
161}).then(() => {
162return Q.all([
7daed3fcArtem Egorov8 years ago163Q.resolve(devices[0].installedApplications[androidPackageName].isInDebugMode === false),
164Q.resolve(devices[1].installedApplications[androidPackageName].isInDebugMode === false),
165Q.resolve(devices[2].installedApplications[androidPackageName].isInDebugMode === false),
c7f1165cdigeff10 years ago166]);
167}).then(isRunningList => {
0766856fdigeff10 years ago168// It should be running in exactly one of these three devices
c7f1165cdigeff10 years ago169isRunningList.filter(v => v).should.eql([true]);
170});
171});
172
173testWithRecordings("runApp fails if no devices are connected",
174["react-native/run-android/win10-rn0.21.0/failsDueToNoDevicesConnected"], () => {
175return Q({})
176.then(() => {
177return androidPlatform.runApp();
178}).then(() => {
179should.assert(false, "runApp should've exited with an error");
180}, reason => {
77a9922aRuslan Bikkinin8 years ago181reason.message.startsWith("Unknown error: not all success patterns were matched").should.be.true();
c7f1165cdigeff10 years ago182});
183});
184
185testWithRecordings("runApp launches the app in an online emulator only",
186["react-native/run-android/win10-rn0.21.0/succeedsWithFiveVSEmulators"], () => {
7daed3fcArtem Egorov8 years ago187devices = fillDevices(["Nexus_5", "Nexus_6", "Nexus_7", "Nexus_8", "Nexus_9"]);
188devices[4].isOnline = false;
189
c7f1165cdigeff10 years ago190return Q({})
191.then(() => {
192return androidPlatform.runApp();
193}).then(() => {
7daed3fcArtem Egorov8 years ago194return devices[4].installedApplications[androidPackageName].isInDebugMode === false;
195}).then((isRunningOnOfflineDevice) => {
196isRunningOnOfflineDevice.should.be.false();
c7f1165cdigeff10 years ago197});
198});
199
200testWithRecordings("runApp launches the app in the device specified as target",
201["react-native/run-android/win10-rn0.21.0/succeedsWithFiveVSEmulators"], () => {
7daed3fcArtem Egorov8 years ago202devices = fillDevices(["Nexus_5", "Nexus_6", "Nexus_10", "Nexus_11", "Nexus_12"]);
203
c7f1165cdigeff10 years ago204return Q({})
205.then(() => {
c9c3d133Artem Egorov8 years ago206const runOptions: any = { platform: "android", workspaceRoot: projectRoot, projectRoot: projectRoot, target: "Nexus_12" };
c7f1165cdigeff10 years ago207return createAndroidPlatform(runOptions).runApp();
208}).then(() => {
7daed3fcArtem Egorov8 years ago209return devices[4].installedApplications[androidPackageName].isInDebugMode === false;
c7f1165cdigeff10 years ago210}).then((isRunningOnNexus12) => {
211isRunningOnNexus12.should.be.true();
212});
213});
214
215testWithRecordings("runApp launches the app in a random online device if the target is offline",
216["react-native/run-android/win10-rn0.21.0/succeedsWithTenVSEmulators"], () => {
0766856fdigeff10 years ago217const onlineDevicesIds = ["Nexus_11", "Nexus_13", "Nexus_14", "Nexus_15", "Nexus_16", "Nexus_17"];
218const offineDevicesIds = ["Nexus_5", "Nexus_6", "Nexus_10", "Nexus_12"];
7daed3fcArtem Egorov8 years ago219devices = fillDevices(offineDevicesIds.concat(onlineDevicesIds));
220devices[0].isOnline = false;
221devices[1].isOnline = false;
222devices[2].isOnline = false;
223devices[3].isOnline = false;
224
c7f1165cdigeff10 years ago225return Q({})
226.then(() => {
c9c3d133Artem Egorov8 years ago227const runOptions: any = { platform: "android", workspaceRoot: projectRoot, projectRoot: projectRoot, target: "Nexus_12" };
c7f1165cdigeff10 years ago228return createAndroidPlatform(runOptions).runApp();
229}).then(() => {
7daed3fcArtem Egorov8 years ago230return devices.filter((device: any) => device.installedApplications[androidPackageName].isInDebugMode === false);
c7f1165cdigeff10 years ago231}).then((devicesRunningAppId) => {
232devicesRunningAppId.length.should.eql(1);
7daed3fcArtem Egorov8 years ago233onlineDevicesIds.should.containEql(devicesRunningAppId[0].id);
c7f1165cdigeff10 years ago234});
235});
236
237testWithRecordings("runApp doesn't fail even if the call to start the LogCat does fail",
238[
239"react-native/run-android/win10-rn0.21.0/succeedsWithOneVSEmulator",
240"react-native/run-android/win10-rn0.22.2/succeedsWithOneVSEmulator",
241"react-native/run-android/osx10.10-rn0.21.0/succeedsWithOneVSEmulator",
242], () => {
7daed3fcArtem Egorov8 years ago243devices = fillDevices(["Nexus_5"]);
244
c7f1165cdigeff10 years ago245return Q({})
246.then(() => {
247return androidPlatform.runApp();
248}).then(() => {
7daed3fcArtem Egorov8 years ago249return devices[0].installedApplications[androidPackageName].isInDebugMode === false;
c7f1165cdigeff10 years ago250}).then(isRunning => {
251isRunning.should.be.true();
252});
253});
254
255testWithRecordings("runApp fails when the android project doesn't exist, and shows a nice error message",
256[
257"react-native/run-android/win10-rn0.21.0/failsDueToAndroidFolderMissing",
258"react-native/run-android/win10-rn0.22.2/failsDueToAndroidFolderMissing",
259], () => {
7daed3fcArtem Egorov8 years ago260devices = fillDevices(["Nexus_5"]);
261
c7f1165cdigeff10 years ago262return Q({})
263.then(() => {
264return fileSystem.rmdir(androidProjectPath);
265}).then(() => {
266return androidPlatform.runApp();
267}).then(() => {
268should.assert(false, "Expected runApp to end up with an error");
269return false;
270}, reason => {
271reason.message.should.eql("Android project not found.");
7daed3fcArtem Egorov8 years ago272return !!devices[0].installedApplications[androidPackageName];
c7f1165cdigeff10 years ago273}).then(isRunning => {
274isRunning.should.be.false();
275});
276});
277
278testWithRecordings("runApp fails when the android emulator shell is unresponsive, and shows a nice error message",
279["react-native/run-android/osx10.10-rn0.21.0/failsDueToAdbCommandTimeout"], () => {
7daed3fcArtem Egorov8 years ago280devices = fillDevices(["Nexus_5"]);
281
c7f1165cdigeff10 years ago282return Q({})
283.then(() => {
284return androidPlatform.runApp();
285}).then(() => {
286should.assert(false, "Expected runApp to end up with an error");
287return false;
288}, reason => {
289"An Android shell command timed-out. Please retry the operation.".should.eql(reason.message);
7daed3fcArtem Egorov8 years ago290return !!devices[0].installedApplications[androidPackageName];
c7f1165cdigeff10 years ago291}).then(isRunning => {
292isRunning.should.be.false();
293});
294});
295});
f0008229digeff10 years ago296});
7daed3fcArtem Egorov8 years ago297
298function fillDevices(ids: string[]): any[] {
299let devices: any[] = [];
300ids.forEach(id => {
301devices.push({ isOnline: true, installedApplications: {}, runningApplications: {}, type: adb.DeviceType.AndroidSdkEmulator, id: id });
302});
303
304return devices;
305}