microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b70e9d898d4c6a04dbdda659d4048f44bb20f7ee

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

276lines · 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
4import * as Q from "q";
5import * as path from "path";
6import * as mockFs from "mock-fs";
7
8import {AndroidPlatform} from "../../../common/android/androidPlatform";
9import {IRunOptions} from "../../../common/launchArgs";
10import {FileSystem} from "../../../common/node/fileSystem";
11import {ReactNative022} from "../../../test/resources/reactNative022";
12import {AdbSimulator} from "../../../test/resources/simulators/adbSimulator";
13import {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
21// TODO: Launch the extension server
22
23suite("androidPlatform", function () {
24 suite("debuggerContext", function () {
25 const projectRoot = "C:/projects/SampleApplication_21/";
26 const androidProjectPath = path.join(projectRoot, "android");
27 const applicationName = "SampleApplication";
28 const androidPackageName = "com.sampleapplication";
29 const genericRunOptions: IRunOptions = { projectRoot: projectRoot };
30
31 let fileSystem: FileSystem;
32 let adb: AdbSimulator;
33 let simulatedAVDManager: AVDManager;
34 let reactNative: ReactNative022;
35 let fakeExtensionMessageSender: FakeExtensionMessageSender;
36 let androidPlatform: AndroidPlatform;
37
38 function createAndroidPlatform(runOptions: IRunOptions): AndroidPlatform {
39 return new AndroidPlatform(runOptions, {
40 adb: adb,
41 reactNative: reactNative,
42 fileSystem: fileSystem,
43 remoteExtension: new RemoteExtension(fakeExtensionMessageSender),
44 });
45 }
46
47 function shouldHaveReceivedSingleLogCatMessage(deviceId: string): void {
48 const expectedMessage = { message: ExtensionMessage.START_MONITORING_LOGCAT, args: [deviceId] };
49
50 const messagesSent = fakeExtensionMessageSender.getAllMessagesSent();
51 const messagesWithoutUndefineds = messagesSent.map(message => {
52 return {
53 message: message.message,
54 args: message.args.filter(value => value),
55 };
56 });
57 messagesWithoutUndefineds.should.eql([expectedMessage]);
58 }
59
60 function shouldHaveReceivedNoLogCatMessages(): void {
61 fakeExtensionMessageSender.getAllMessagesSent().should.eql([]);
62 }
63
64 setup(() => {
65 // Configure all the dependencies we'll use in our tests
66 fileSystem = new FileSystem({ fs: mockFs.fs({}) });
67 adb = new AdbSimulator(fileSystem);
68 simulatedAVDManager = new AVDManager(adb);
69 reactNative = new ReactNative022(adb, fileSystem);
70 fakeExtensionMessageSender = new FakeExtensionMessageSender();
71 androidPlatform = createAndroidPlatform(genericRunOptions);
72
73 // Create a React-Native project we'll use in our tests
74 return reactNative.createProject(projectRoot, applicationName);
75 });
76
77 const testWithRecordings = new RecordingsHelper(() => reactNative).test;
78
79 testWithRecordings("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 ], () => {
85 return Q({})
86 .then(() => {
87 return simulatedAVDManager.createAndLaunch("Nexus_5");
88 }).then(() => {
89 return androidPlatform.runApp();
90 }).then(() => {
91 return adb.isAppRunning(androidPackageName);
92 }).then(isRunning => {
93 isRunning.should.be.true();
94 shouldHaveReceivedSingleLogCatMessage("Nexus_5");
95 });
96 });
97
98 testWithRecordings("runApp launches the app when two emulators are connected",
99 ["react-native/run-android/win10-rn0.21.0/succeedsWithTwoVSEmulators"], () => {
100 return Q({})
101 .then(() => {
102 return simulatedAVDManager.createAndLaunchAll(["Nexus_5", "Nexus_6"]);
103 }).then(() => {
104 return androidPlatform.runApp();
105 }).then(() => {
106 return Q.all([
107 adb.isAppRunning(androidPackageName, "Nexus_5"),
108 adb.isAppRunning(androidPackageName, "Nexus_6"),
109 ]);
110 }).spread((isRunningOnNexus5, isRunningOnNexus6) => {
111 // It should be running in exactly one of these two devices
112 isRunningOnNexus5.should.not.eql(isRunningOnNexus6);
113 const emulatorWithAppRunningId = isRunningOnNexus5 ? "Nexus_5" : "Nexus_6";
114 shouldHaveReceivedSingleLogCatMessage(emulatorWithAppRunningId);
115 });
116 });
117
118 testWithRecordings("runApp launches the app when three emulators are connected",
119 ["react-native/run-android/win10-rn0.21.0/succeedsWithThreeVSEmulators"], () => {
120 const devicesIds = ["Nexus_5", "Nexus_6", "Other_Nexus_6"];
121 return Q({})
122 .then(() => {
123 return simulatedAVDManager.createAndLaunchAll(devicesIds);
124 }).then(() => {
125 return androidPlatform.runApp();
126 }).then(() => {
127 return Q.all([
128 adb.isAppRunning(androidPackageName, "Nexus_5"),
129 adb.isAppRunning(androidPackageName, "Nexus_6"),
130 adb.isAppRunning(androidPackageName, "Other_Nexus_6"),
131 ]);
132 }).then(isRunningList => {
133 // It should be running in exactly one of these three devices
134 isRunningList.filter(v => v).should.eql([true]);
135
136 // Get index of running emulator
137 const index = isRunningList.indexOf(true);
138 const emulatorWithAppRunningId = devicesIds[index];
139 shouldHaveReceivedSingleLogCatMessage(emulatorWithAppRunningId);
140 });
141 });
142
143 testWithRecordings("runApp fails if no devices are connected",
144 ["react-native/run-android/win10-rn0.21.0/failsDueToNoDevicesConnected"], () => {
145 return Q({})
146 .then(() => {
147 return androidPlatform.runApp();
148 }).then(() => {
149 should.assert(false, "runApp should've exited with an error");
150 }, reason => {
151 reason.message.should.eql("Unknown error");
152 shouldHaveReceivedNoLogCatMessages();
153 });
154 });
155
156 testWithRecordings("runApp launches the app in an online emulator only",
157 ["react-native/run-android/win10-rn0.21.0/succeedsWithFiveVSEmulators"], () => {
158 const onlineDevicesIds = ["Nexus_11"];
159 const offineDevicesIds = ["Nexus_5", "Nexus_6", "Nexus_10", "Nexus_12"];
160 return Q({})
161 .then(() => {
162 return simulatedAVDManager.createAndLaunchAll(onlineDevicesIds.concat(offineDevicesIds));
163 }).then(() => {
164 return adb.notifyDevicesAreOffline(offineDevicesIds);
165 }).then(() => {
166 return androidPlatform.runApp();
167 }).then(() => {
168 return adb.isAppRunning(androidPackageName, "Nexus_11");
169 }).then((isRunningOnNexus11) => {
170 isRunningOnNexus11.should.be.true();
171 shouldHaveReceivedSingleLogCatMessage("Nexus_11");
172 });
173 });
174
175 testWithRecordings("runApp launches the app in the device specified as target",
176 ["react-native/run-android/win10-rn0.21.0/succeedsWithFiveVSEmulators"], () => {
177 return Q({})
178 .then(() => {
179 return simulatedAVDManager.createAndLaunchAll(["Nexus_5", "Nexus_6", "Nexus_10", "Nexus_11", "Nexus_12"]);
180 }).then(() => {
181 const runOptions: IRunOptions = { projectRoot: projectRoot, target: "Nexus_12" };
182 return createAndroidPlatform(runOptions).runApp();
183 }).then(() => {
184 return adb.isAppRunning(androidPackageName, "Nexus_12");
185 }).then((isRunningOnNexus12) => {
186 isRunningOnNexus12.should.be.true();
187 shouldHaveReceivedSingleLogCatMessage("Nexus_12");
188 });
189 });
190
191 testWithRecordings("runApp launches the app in a random online device if the target is offline",
192 ["react-native/run-android/win10-rn0.21.0/succeedsWithTenVSEmulators"], () => {
193 const onlineDevicesIds = ["Nexus_11", "Nexus_13", "Nexus_14", "Nexus_15", "Nexus_16", "Nexus_17"];
194 const offineDevicesIds = ["Nexus_5", "Nexus_6", "Nexus_10", "Nexus_12"];
195 return Q({})
196 .then(() => {
197 return simulatedAVDManager.createAndLaunchAll(onlineDevicesIds.concat(offineDevicesIds));
198 }).then(() => {
199 return adb.notifyDevicesAreOffline(offineDevicesIds);
200 }).then(() => {
201 const runOptions: IRunOptions = { projectRoot: projectRoot, target: "Nexus_12" };
202 return createAndroidPlatform(runOptions).runApp();
203 }).then(() => {
204 return adb.findDevicesRunningApp(androidPackageName);
205 }).then((devicesRunningAppId) => {
206 devicesRunningAppId.length.should.eql(1);
207 onlineDevicesIds.should.containEql(devicesRunningAppId[0]);
208 shouldHaveReceivedSingleLogCatMessage(devicesRunningAppId[0]);
209 });
210 });
211
212 testWithRecordings("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 ], () => {
218 fakeExtensionMessageSender.setMessageResponse(Q.reject<void>("Unknown error"));
219
220 return Q({})
221 .then(() => {
222 return simulatedAVDManager.createAndLaunch("Nexus_5");
223 }).then(() => {
224 return androidPlatform.runApp();
225 }).then(() => {
226 return adb.isAppRunning(androidPackageName);
227 }).then(isRunning => {
228 isRunning.should.be.true();
229 shouldHaveReceivedSingleLogCatMessage("Nexus_5");
230 });
231 });
232
233 testWithRecordings("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 ], () => {
238 return Q({})
239 .then(() => {
240 return fileSystem.rmdir(androidProjectPath);
241 }).then(() => {
242 return simulatedAVDManager.createAndLaunch("Nexus_5");
243 }).then(() => {
244 return androidPlatform.runApp();
245 }).then(() => {
246 should.assert(false, "Expected runApp to end up with an error");
247 return false;
248 }, reason => {
249 reason.message.should.eql("Android project not found.");
250 return adb.isAppRunning(androidPackageName);
251 }).then(isRunning => {
252 isRunning.should.be.false();
253 shouldHaveReceivedNoLogCatMessages();
254 });
255 });
256
257 testWithRecordings("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"], () => {
259 return Q({})
260 .then(() => {
261 return simulatedAVDManager.createAndLaunch("Nexus_5");
262 }).then(() => {
263 return androidPlatform.runApp();
264 }).then(() => {
265 should.assert(false, "Expected runApp to end up with an error");
266 return false;
267 }, reason => {
268 "An Android shell command timed-out. Please retry the operation.".should.eql(reason.message);
269 return adb.isAppRunning(androidPackageName);
270 }).then(isRunning => {
271 isRunning.should.be.false();
272 shouldHaveReceivedNoLogCatMessages();
273 });
274 });
275 });
276});
277