microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/test/debugger/android/androidPlatform.test.ts
276lines · modeblame
c7f1165cdigeff10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
| 4 | import * as Q from "q"; | |
| 5 | import * as path from "path"; | |
| 6 | import * as mockFs from "mock-fs"; | |
| 7 | | |
52f3873ddigeff10 years ago | 8 | import {AndroidPlatform} from "../../../common/android/androidPlatform"; |
c7f1165cdigeff10 years ago | 9 | import {IRunOptions} from "../../../common/launchArgs"; |
| 10 | import {FileSystem} from "../../../common/node/fileSystem"; | |
| 11 | import {ReactNative022} from "../../../test/resources/reactNative022"; | |
c51e2d6bdigeff10 years ago | 12 | import {AdbSimulator} from "../../../test/resources/simulators/adbSimulator"; |
c7f1165cdigeff10 years ago | 13 | import {AVDManager} from "../../../test/resources/simulators/avdManager"; |
| 14 | import {FakeExtensionMessageSender} from "../../../test/resources/fakeExtensionMessageSender"; | |
| 15 | import {ExtensionMessage} from "../../../common/extensionMessaging"; | |
| 16 | import {RecordingsHelper} from "../../resources/recordingsHelper"; | |
| 17 | import {RemoteExtension} from "../../../common/remoteExtension"; | |
| 18 | | |
| 19 | import "should"; | |
| 20 | | |
f0008229digeff10 years ago | 21 | // TODO: Launch the extension server |
c7f1165cdigeff10 years ago | 22 | |
| 23 | suite("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; | |
c51e2d6bdigeff10 years ago | 32 | let adb: AdbSimulator; |
c7f1165cdigeff10 years ago | 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, { | |
52f3873ddigeff10 years ago | 40 | adb: adb, |
c7f1165cdigeff10 years ago | 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 | | |
0766856fdigeff10 years ago | 64 | setup(() => { |
| 65 | // Configure all the dependencies we'll use in our tests | |
| 66 | fileSystem = new FileSystem({ fs: mockFs.fs({}) }); | |
c51e2d6bdigeff10 years ago | 67 | adb = new AdbSimulator(fileSystem); |
52f3873ddigeff10 years ago | 68 | simulatedAVDManager = new AVDManager(adb); |
| 69 | reactNative = new ReactNative022(adb, fileSystem); | |
0766856fdigeff10 years ago | 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 | | |
c7f1165cdigeff10 years ago | 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(() => { | |
52f3873ddigeff10 years ago | 91 | return adb.isAppRunning(androidPackageName); |
c7f1165cdigeff10 years ago | 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(() => { | |
0766856fdigeff10 years ago | 102 | return simulatedAVDManager.createAndLaunchAll(["Nexus_5", "Nexus_6"]); |
c7f1165cdigeff10 years ago | 103 | }).then(() => { |
| 104 | return androidPlatform.runApp(); | |
| 105 | }).then(() => { | |
| 106 | return Q.all([ | |
52f3873ddigeff10 years ago | 107 | adb.isAppRunning(androidPackageName, "Nexus_5"), |
| 108 | adb.isAppRunning(androidPackageName, "Nexus_6"), | |
c7f1165cdigeff10 years ago | 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"], () => { | |
0766856fdigeff10 years ago | 120 | const devicesIds = ["Nexus_5", "Nexus_6", "Other_Nexus_6"]; |
c7f1165cdigeff10 years ago | 121 | return Q({}) |
| 122 | .then(() => { | |
0766856fdigeff10 years ago | 123 | return simulatedAVDManager.createAndLaunchAll(devicesIds); |
c7f1165cdigeff10 years ago | 124 | }).then(() => { |
| 125 | return androidPlatform.runApp(); | |
| 126 | }).then(() => { | |
| 127 | return Q.all([ | |
52f3873ddigeff10 years ago | 128 | adb.isAppRunning(androidPackageName, "Nexus_5"), |
| 129 | adb.isAppRunning(androidPackageName, "Nexus_6"), | |
| 130 | adb.isAppRunning(androidPackageName, "Other_Nexus_6"), | |
c7f1165cdigeff10 years ago | 131 | ]); |
| 132 | }).then(isRunningList => { | |
0766856fdigeff10 years ago | 133 | // It should be running in exactly one of these three devices |
c7f1165cdigeff10 years ago | 134 | isRunningList.filter(v => v).should.eql([true]); |
| 135 | | |
| 136 | // Get index of running emulator | |
| 137 | const index = isRunningList.indexOf(true); | |
0766856fdigeff10 years ago | 138 | const emulatorWithAppRunningId = devicesIds[index]; |
c7f1165cdigeff10 years ago | 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"], () => { | |
0766856fdigeff10 years ago | 158 | const onlineDevicesIds = ["Nexus_11"]; |
| 159 | const offineDevicesIds = ["Nexus_5", "Nexus_6", "Nexus_10", "Nexus_12"]; | |
c7f1165cdigeff10 years ago | 160 | return Q({}) |
| 161 | .then(() => { | |
0766856fdigeff10 years ago | 162 | return simulatedAVDManager.createAndLaunchAll(onlineDevicesIds.concat(offineDevicesIds)); |
c7f1165cdigeff10 years ago | 163 | }).then(() => { |
52f3873ddigeff10 years ago | 164 | return adb.notifyDevicesAreOffline(offineDevicesIds); |
c7f1165cdigeff10 years ago | 165 | }).then(() => { |
| 166 | return androidPlatform.runApp(); | |
| 167 | }).then(() => { | |
52f3873ddigeff10 years ago | 168 | return adb.isAppRunning(androidPackageName, "Nexus_11"); |
c7f1165cdigeff10 years ago | 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(() => { | |
0766856fdigeff10 years ago | 179 | return simulatedAVDManager.createAndLaunchAll(["Nexus_5", "Nexus_6", "Nexus_10", "Nexus_11", "Nexus_12"]); |
c7f1165cdigeff10 years ago | 180 | }).then(() => { |
| 181 | const runOptions: IRunOptions = { projectRoot: projectRoot, target: "Nexus_12" }; | |
| 182 | return createAndroidPlatform(runOptions).runApp(); | |
| 183 | }).then(() => { | |
52f3873ddigeff10 years ago | 184 | return adb.isAppRunning(androidPackageName, "Nexus_12"); |
c7f1165cdigeff10 years ago | 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"], () => { | |
0766856fdigeff10 years ago | 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"]; | |
c7f1165cdigeff10 years ago | 195 | return Q({}) |
| 196 | .then(() => { | |
0766856fdigeff10 years ago | 197 | return simulatedAVDManager.createAndLaunchAll(onlineDevicesIds.concat(offineDevicesIds)); |
c7f1165cdigeff10 years ago | 198 | }).then(() => { |
52f3873ddigeff10 years ago | 199 | return adb.notifyDevicesAreOffline(offineDevicesIds); |
c7f1165cdigeff10 years ago | 200 | }).then(() => { |
| 201 | const runOptions: IRunOptions = { projectRoot: projectRoot, target: "Nexus_12" }; | |
| 202 | return createAndroidPlatform(runOptions).runApp(); | |
| 203 | }).then(() => { | |
52f3873ddigeff10 years ago | 204 | return adb.findDevicesRunningApp(androidPackageName); |
c7f1165cdigeff10 years ago | 205 | }).then((devicesRunningAppId) => { |
| 206 | devicesRunningAppId.length.should.eql(1); | |
e4ac653edigeff10 years ago | 207 | onlineDevicesIds.should.containEql(devicesRunningAppId[0]); |
c7f1165cdigeff10 years ago | 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(() => { | |
52f3873ddigeff10 years ago | 226 | return adb.isAppRunning(androidPackageName); |
c7f1165cdigeff10 years ago | 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."); | |
52f3873ddigeff10 years ago | 250 | return adb.isAppRunning(androidPackageName); |
c7f1165cdigeff10 years ago | 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); | |
52f3873ddigeff10 years ago | 269 | return adb.isAppRunning(androidPackageName); |
c7f1165cdigeff10 years ago | 270 | }).then(isRunning => { |
| 271 | isRunning.should.be.false(); | |
| 272 | shouldHaveReceivedNoLogCatMessages(); | |
| 273 | }); | |
| 274 | }); | |
| 275 | }); | |
f0008229digeff10 years ago | 276 | }); |