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/debugger/android/androidPlatform.test.ts

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