microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
488dfb95e5dc78ff6ac49af7223d606b8ca319d2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/test/debugger/android/androidPlatform.test.ts

276lines · 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 mockFs from "mock-fs";
7
52f3873ddigeff10 years ago8import {AndroidPlatform} from "../../../common/android/androidPlatform";
c7f1165cdigeff10 years ago9import {IRunOptions} from "../../../common/launchArgs";
10import {FileSystem} from "../../../common/node/fileSystem";
11import {ReactNative022} from "../../../test/resources/reactNative022";
c51e2d6bdigeff10 years ago12import {AdbSimulator} from "../../../test/resources/simulators/adbSimulator";
c7f1165cdigeff10 years ago13import {AVDManager} from "../../../test/resources/simulators/avdManager";
14import {FakeExtensionMessageSender} from "../../../test/resources/fakeExtensionMessageSender";
15import {ExtensionMessage} from "../../../common/extensionMessaging";
16import {RecordingsHelper} from "../../resources/recordingsHelper";
17import {RemoteExtension} from "../../../common/remoteExtension";
18
19import "should";
20
f0008229digeff10 years ago21// TODO: Launch the extension server
c7f1165cdigeff10 years ago22
23suite("androidPlatform", function () {
24suite("debuggerContext", function () {
25const projectRoot = "C:/projects/SampleApplication_21/";
26const androidProjectPath = path.join(projectRoot, "android");
27const applicationName = "SampleApplication";
28const androidPackageName = "com.sampleapplication";
29const genericRunOptions: IRunOptions = { projectRoot: projectRoot };
30
31let fileSystem: FileSystem;
c51e2d6bdigeff10 years ago32let adb: AdbSimulator;
c7f1165cdigeff10 years ago33let simulatedAVDManager: AVDManager;
34let reactNative: ReactNative022;
35let fakeExtensionMessageSender: FakeExtensionMessageSender;
36let androidPlatform: AndroidPlatform;
37
38function createAndroidPlatform(runOptions: IRunOptions): AndroidPlatform {
39return new AndroidPlatform(runOptions, {
52f3873ddigeff10 years ago40adb: adb,
c7f1165cdigeff10 years ago41reactNative: reactNative,
42fileSystem: fileSystem,
43remoteExtension: new RemoteExtension(fakeExtensionMessageSender),
44});
45}
46
47function shouldHaveReceivedSingleLogCatMessage(deviceId: string): void {
48const expectedMessage = { message: ExtensionMessage.START_MONITORING_LOGCAT, args: [deviceId] };
49
50const messagesSent = fakeExtensionMessageSender.getAllMessagesSent();
51const messagesWithoutUndefineds = messagesSent.map(message => {
52return {
53message: message.message,
54args: message.args.filter(value => value),
55};
56});
57messagesWithoutUndefineds.should.eql([expectedMessage]);
58}
59
60function shouldHaveReceivedNoLogCatMessages(): void {
61fakeExtensionMessageSender.getAllMessagesSent().should.eql([]);
62}
63
0766856fdigeff10 years ago64setup(() => {
65// Configure all the dependencies we'll use in our tests
66fileSystem = new FileSystem({ fs: mockFs.fs({}) });
c51e2d6bdigeff10 years ago67adb = new AdbSimulator(fileSystem);
52f3873ddigeff10 years ago68simulatedAVDManager = new AVDManager(adb);
69reactNative = new ReactNative022(adb, fileSystem);
0766856fdigeff10 years ago70fakeExtensionMessageSender = new FakeExtensionMessageSender();
71androidPlatform = createAndroidPlatform(genericRunOptions);
72
73// Create a React-Native project we'll use in our tests
74return reactNative.createProject(projectRoot, applicationName);
75});
76
c7f1165cdigeff10 years ago77const testWithRecordings = new RecordingsHelper(() => reactNative).test;
78
79testWithRecordings("runApp launches the app when a single emulator is connected",
80[
81"react-native/run-android/win10-rn0.21.0/succeedsWithOneVSEmulator",
82"react-native/run-android/win10-rn0.22.2/succeedsWithOneVSEmulator",
83"react-native/run-android/osx10.10-rn0.21.0/succeedsWithOneVSEmulator",
84], () => {
85return Q({})
86.then(() => {
87return simulatedAVDManager.createAndLaunch("Nexus_5");
88}).then(() => {
89return androidPlatform.runApp();
90}).then(() => {
52f3873ddigeff10 years ago91return adb.isAppRunning(androidPackageName);
c7f1165cdigeff10 years ago92}).then(isRunning => {
93isRunning.should.be.true();
94shouldHaveReceivedSingleLogCatMessage("Nexus_5");
95});
96});
97
98testWithRecordings("runApp launches the app when two emulators are connected",
99["react-native/run-android/win10-rn0.21.0/succeedsWithTwoVSEmulators"], () => {
100return Q({})
101.then(() => {
0766856fdigeff10 years ago102return simulatedAVDManager.createAndLaunchAll(["Nexus_5", "Nexus_6"]);
c7f1165cdigeff10 years ago103}).then(() => {
104return androidPlatform.runApp();
105}).then(() => {
106return Q.all([
52f3873ddigeff10 years ago107adb.isAppRunning(androidPackageName, "Nexus_5"),
108adb.isAppRunning(androidPackageName, "Nexus_6"),
c7f1165cdigeff10 years ago109]);
110}).spread((isRunningOnNexus5, isRunningOnNexus6) => {
111// It should be running in exactly one of these two devices
112isRunningOnNexus5.should.not.eql(isRunningOnNexus6);
113const emulatorWithAppRunningId = isRunningOnNexus5 ? "Nexus_5" : "Nexus_6";
114shouldHaveReceivedSingleLogCatMessage(emulatorWithAppRunningId);
115});
116});
117
118testWithRecordings("runApp launches the app when three emulators are connected",
119["react-native/run-android/win10-rn0.21.0/succeedsWithThreeVSEmulators"], () => {
0766856fdigeff10 years ago120const devicesIds = ["Nexus_5", "Nexus_6", "Other_Nexus_6"];
c7f1165cdigeff10 years ago121return Q({})
122.then(() => {
0766856fdigeff10 years ago123return simulatedAVDManager.createAndLaunchAll(devicesIds);
c7f1165cdigeff10 years ago124}).then(() => {
125return androidPlatform.runApp();
126}).then(() => {
127return Q.all([
52f3873ddigeff10 years ago128adb.isAppRunning(androidPackageName, "Nexus_5"),
129adb.isAppRunning(androidPackageName, "Nexus_6"),
130adb.isAppRunning(androidPackageName, "Other_Nexus_6"),
c7f1165cdigeff10 years ago131]);
132}).then(isRunningList => {
0766856fdigeff10 years ago133// It should be running in exactly one of these three devices
c7f1165cdigeff10 years ago134isRunningList.filter(v => v).should.eql([true]);
135
136// Get index of running emulator
137const index = isRunningList.indexOf(true);
0766856fdigeff10 years ago138const emulatorWithAppRunningId = devicesIds[index];
c7f1165cdigeff10 years ago139shouldHaveReceivedSingleLogCatMessage(emulatorWithAppRunningId);
140});
141});
142
143testWithRecordings("runApp fails if no devices are connected",
144["react-native/run-android/win10-rn0.21.0/failsDueToNoDevicesConnected"], () => {
145return Q({})
146.then(() => {
147return androidPlatform.runApp();
148}).then(() => {
149should.assert(false, "runApp should've exited with an error");
150}, reason => {
151reason.message.should.eql("Unknown error");
152shouldHaveReceivedNoLogCatMessages();
153});
154});
155
156testWithRecordings("runApp launches the app in an online emulator only",
157["react-native/run-android/win10-rn0.21.0/succeedsWithFiveVSEmulators"], () => {
0766856fdigeff10 years ago158const onlineDevicesIds = ["Nexus_11"];
159const offineDevicesIds = ["Nexus_5", "Nexus_6", "Nexus_10", "Nexus_12"];
c7f1165cdigeff10 years ago160return Q({})
161.then(() => {
0766856fdigeff10 years ago162return simulatedAVDManager.createAndLaunchAll(onlineDevicesIds.concat(offineDevicesIds));
c7f1165cdigeff10 years ago163}).then(() => {
52f3873ddigeff10 years ago164return adb.notifyDevicesAreOffline(offineDevicesIds);
c7f1165cdigeff10 years ago165}).then(() => {
166return androidPlatform.runApp();
167}).then(() => {
52f3873ddigeff10 years ago168return adb.isAppRunning(androidPackageName, "Nexus_11");
c7f1165cdigeff10 years ago169}).then((isRunningOnNexus11) => {
170isRunningOnNexus11.should.be.true();
171shouldHaveReceivedSingleLogCatMessage("Nexus_11");
172});
173});
174
175testWithRecordings("runApp launches the app in the device specified as target",
176["react-native/run-android/win10-rn0.21.0/succeedsWithFiveVSEmulators"], () => {
177return Q({})
178.then(() => {
0766856fdigeff10 years ago179return simulatedAVDManager.createAndLaunchAll(["Nexus_5", "Nexus_6", "Nexus_10", "Nexus_11", "Nexus_12"]);
c7f1165cdigeff10 years ago180}).then(() => {
181const runOptions: IRunOptions = { projectRoot: projectRoot, target: "Nexus_12" };
182return createAndroidPlatform(runOptions).runApp();
183}).then(() => {
52f3873ddigeff10 years ago184return adb.isAppRunning(androidPackageName, "Nexus_12");
c7f1165cdigeff10 years ago185}).then((isRunningOnNexus12) => {
186isRunningOnNexus12.should.be.true();
187shouldHaveReceivedSingleLogCatMessage("Nexus_12");
188});
189});
190
191testWithRecordings("runApp launches the app in a random online device if the target is offline",
192["react-native/run-android/win10-rn0.21.0/succeedsWithTenVSEmulators"], () => {
0766856fdigeff10 years ago193const onlineDevicesIds = ["Nexus_11", "Nexus_13", "Nexus_14", "Nexus_15", "Nexus_16", "Nexus_17"];
194const offineDevicesIds = ["Nexus_5", "Nexus_6", "Nexus_10", "Nexus_12"];
c7f1165cdigeff10 years ago195return Q({})
196.then(() => {
0766856fdigeff10 years ago197return simulatedAVDManager.createAndLaunchAll(onlineDevicesIds.concat(offineDevicesIds));
c7f1165cdigeff10 years ago198}).then(() => {
52f3873ddigeff10 years ago199return adb.notifyDevicesAreOffline(offineDevicesIds);
c7f1165cdigeff10 years ago200}).then(() => {
201const runOptions: IRunOptions = { projectRoot: projectRoot, target: "Nexus_12" };
202return createAndroidPlatform(runOptions).runApp();
203}).then(() => {
52f3873ddigeff10 years ago204return adb.findDevicesRunningApp(androidPackageName);
c7f1165cdigeff10 years ago205}).then((devicesRunningAppId) => {
206devicesRunningAppId.length.should.eql(1);
e4ac653edigeff10 years ago207onlineDevicesIds.should.containEql(devicesRunningAppId[0]);
c7f1165cdigeff10 years ago208shouldHaveReceivedSingleLogCatMessage(devicesRunningAppId[0]);
209});
210});
211
212testWithRecordings("runApp doesn't fail even if the call to start the LogCat does fail",
213[
214"react-native/run-android/win10-rn0.21.0/succeedsWithOneVSEmulator",
215"react-native/run-android/win10-rn0.22.2/succeedsWithOneVSEmulator",
216"react-native/run-android/osx10.10-rn0.21.0/succeedsWithOneVSEmulator",
217], () => {
218fakeExtensionMessageSender.setMessageResponse(Q.reject<void>("Unknown error"));
219
220return Q({})
221.then(() => {
222return simulatedAVDManager.createAndLaunch("Nexus_5");
223}).then(() => {
224return androidPlatform.runApp();
225}).then(() => {
52f3873ddigeff10 years ago226return adb.isAppRunning(androidPackageName);
c7f1165cdigeff10 years ago227}).then(isRunning => {
228isRunning.should.be.true();
229shouldHaveReceivedSingleLogCatMessage("Nexus_5");
230});
231});
232
233testWithRecordings("runApp fails when the android project doesn't exist, and shows a nice error message",
234[
235"react-native/run-android/win10-rn0.21.0/failsDueToAndroidFolderMissing",
236"react-native/run-android/win10-rn0.22.2/failsDueToAndroidFolderMissing",
237], () => {
238return Q({})
239.then(() => {
240return fileSystem.rmdir(androidProjectPath);
241}).then(() => {
242return simulatedAVDManager.createAndLaunch("Nexus_5");
243}).then(() => {
244return androidPlatform.runApp();
245}).then(() => {
246should.assert(false, "Expected runApp to end up with an error");
247return false;
248}, reason => {
249reason.message.should.eql("Android project not found.");
52f3873ddigeff10 years ago250return adb.isAppRunning(androidPackageName);
c7f1165cdigeff10 years ago251}).then(isRunning => {
252isRunning.should.be.false();
253shouldHaveReceivedNoLogCatMessages();
254});
255});
256
257testWithRecordings("runApp fails when the android emulator shell is unresponsive, and shows a nice error message",
258["react-native/run-android/osx10.10-rn0.21.0/failsDueToAdbCommandTimeout"], () => {
259return Q({})
260.then(() => {
261return simulatedAVDManager.createAndLaunch("Nexus_5");
262}).then(() => {
263return androidPlatform.runApp();
264}).then(() => {
265should.assert(false, "Expected runApp to end up with an error");
266return false;
267}, reason => {
268"An Android shell command timed-out. Please retry the operation.".should.eql(reason.message);
52f3873ddigeff10 years ago269return adb.isAppRunning(androidPackageName);
c7f1165cdigeff10 years ago270}).then(isRunning => {
271isRunning.should.be.false();
272shouldHaveReceivedNoLogCatMessages();
273});
274});
275});
f0008229digeff10 years ago276});