microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
test/extension/android/androidPlatform.test.ts
305lines · 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"; | |
396b6627Vladimir Kotikov8 years ago | 5 | import * as fs from "fs"; |
c7f1165cdigeff10 years ago | 6 | import * as path from "path"; |
| 7 | import * as mockFs from "mock-fs"; | |
| 8 | | |
0a68f8dbArtem Egorov8 years ago | 9 | import {AndroidPlatform} from "../../../src/extension/android/androidPlatform"; |
| 10 | import {IAndroidRunOptions} from "../../../src/extension/launchArgs"; | |
3c172a05Artem Egorov8 years ago | 11 | import {FileSystem} from "../../../src/common/node/fileSystem"; |
5c8365a6Artem Egorov8 years ago | 12 | import {ReactNative022} from "../../resources/reactNative022"; |
7daed3fcArtem Egorov8 years ago | 13 | import * as adb from "../../../src/extension/android/adb"; |
c7f1165cdigeff10 years ago | 14 | import {RecordingsHelper} from "../../resources/recordingsHelper"; |
8022afdfVladimir Kotikov8 years ago | 15 | import {CommandExecutor} from "../../../src/common/commandExecutor"; |
377a189cArtem Egorov8 years ago | 16 | import * as rnHelper from "../../../src/common/reactNativeProjectHelper"; |
c7f1165cdigeff10 years ago | 17 | |
| 18 | import "should"; | |
8022afdfVladimir Kotikov8 years ago | 19 | import * as sinon from "sinon"; |
e4dd9aa4Serge Svekolnikov8 years ago | 20 | import { SettingsHelper } from "../../../src/extension/settingsHelper"; |
c7f1165cdigeff10 years ago | 21 | |
f0008229digeff10 years ago | 22 | // TODO: Launch the extension server |
c7f1165cdigeff10 years ago | 23 | |
| 24 | suite("androidPlatform", function () { | |
0a68f8dbArtem Egorov8 years ago | 25 | suite("extensionContext", function () { |
c7f1165cdigeff10 years ago | 26 | const projectRoot = "C:/projects/SampleApplication_21/"; |
| 27 | const androidProjectPath = path.join(projectRoot, "android"); | |
| 28 | const applicationName = "SampleApplication"; | |
| 29 | const androidPackageName = "com.sampleapplication"; | |
2e432a9eArtem Egorov8 years ago | 30 | const genericRunOptions: IAndroidRunOptions = { platform: "android", workspaceRoot: projectRoot, projectRoot: projectRoot }; |
c7f1165cdigeff10 years ago | 31 | |
396b6627Vladimir Kotikov8 years ago | 32 | const rnProjectContent = fs.readFileSync(ReactNative022.DEFAULT_PROJECT_FILE, "utf8"); |
| 33 | | |
c7f1165cdigeff10 years ago | 34 | let fileSystem: FileSystem; |
| 35 | let reactNative: ReactNative022; | |
| 36 | let androidPlatform: AndroidPlatform; | |
8022afdfVladimir Kotikov8 years ago | 37 | let sandbox: Sinon.SinonSandbox; |
7daed3fcArtem Egorov8 years ago | 38 | let devices: any; |
c7f1165cdigeff10 years ago | 39 | |
0db0be15Artem Egorov8 years ago | 40 | function createAndroidPlatform(runOptions: IAndroidRunOptions): AndroidPlatform { |
7daed3fcArtem Egorov8 years ago | 41 | return new AndroidPlatform(runOptions); |
c7f1165cdigeff10 years ago | 42 | } |
| 43 | | |
0766856fdigeff10 years ago | 44 | setup(() => { |
396b6627Vladimir Kotikov8 years ago | 45 | mockFs(); |
8022afdfVladimir Kotikov8 years ago | 46 | sandbox = sinon.sandbox.create(); |
396b6627Vladimir Kotikov8 years ago | 47 | |
0766856fdigeff10 years ago | 48 | // Configure all the dependencies we'll use in our tests |
396b6627Vladimir Kotikov8 years ago | 49 | fileSystem = new FileSystem(); |
7daed3fcArtem Egorov8 years ago | 50 | reactNative = new ReactNative022(fileSystem); |
e4dd9aa4Serge Svekolnikov8 years ago | 51 | |
| 52 | sandbox.stub(SettingsHelper, "getReactNativeProjectRoot", () => projectRoot); | |
| 53 | | |
0766856fdigeff10 years ago | 54 | androidPlatform = createAndroidPlatform(genericRunOptions); |
| 55 | | |
8022afdfVladimir Kotikov8 years ago | 56 | sandbox.stub(CommandExecutor.prototype, "spawnReactCommand", function () { |
| 57 | return reactNative.runAndroid(genericRunOptions); | |
| 58 | }); | |
| 59 | | |
377a189cArtem Egorov8 years ago | 60 | sandbox.stub(rnHelper.ReactNativeProjectHelper, "getReactNativeVersion", function () { |
| 61 | return Q.resolve("0.0.1"); | |
| 62 | }); | |
| 63 | | |
7daed3fcArtem Egorov8 years ago | 64 | sandbox.stub(adb.AdbHelper, "launchApp", function (projectRoot_: string, packageName: string, debugTarget?: string) { |
| 65 | devices = devices.map((device: any) => { | |
| 66 | if (!debugTarget) { | |
| 67 | device.installedApplications[androidPackageName] = { isInDebugMode: false }; | |
| 68 | } | |
| 69 | | |
| 70 | if (debugTarget && debugTarget === device.id) { | |
| 71 | device.installedApplications[androidPackageName] = { isInDebugMode: false }; | |
| 72 | } | |
| 73 | | |
| 74 | return device; | |
| 75 | }); | |
| 76 | | |
| 77 | return Q.resolve(void 0); | |
| 78 | }); | |
| 79 | sandbox.stub(adb.AdbHelper, "getConnectedDevices", function () { | |
| 80 | return Q.resolve(devices); | |
| 81 | }); | |
| 82 | sandbox.stub(adb.AdbHelper, "getOnlineDevices", function () { | |
| 83 | return Q.resolve(devices.filter((device: any) => { | |
| 84 | return device.isOnline; | |
| 85 | })); | |
| 86 | }); | |
| 87 | sandbox.stub(adb.AdbHelper, "apiVersion", function () { | |
| 88 | return Q.resolve(adb.AndroidAPILevel.LOLLIPOP); | |
| 89 | }); | |
| 90 | sandbox.stub(adb.AdbHelper, "reverseAdb", function () { | |
| 91 | return Q.resolve(void 0); | |
| 92 | }); | |
| 93 | | |
| 94 | sandbox.stub(reactNative, "installAppInDevice", function (deviceId: string) { | |
| 95 | devices = devices.map((device: any) => { | |
| 96 | if (deviceId && deviceId === device.id) { | |
| 97 | device.installedApplications[androidPackageName] = {}; | |
| 98 | } | |
| 99 | | |
| 100 | return device; | |
| 101 | }); | |
| 102 | return Q.resolve(void 0); | |
| 103 | }); | |
| 104 | | |
0766856fdigeff10 years ago | 105 | // Create a React-Native project we'll use in our tests |
396b6627Vladimir Kotikov8 years ago | 106 | return reactNative |
| 107 | .fromProjectFileContent(rnProjectContent) | |
| 108 | .createProject(projectRoot, applicationName); | |
| 109 | }); | |
| 110 | | |
| 111 | teardown(() => { | |
| 112 | mockFs.restore(); | |
8022afdfVladimir Kotikov8 years ago | 113 | sandbox.restore(); |
7daed3fcArtem Egorov8 years ago | 114 | devices = []; |
0766856fdigeff10 years ago | 115 | }); |
| 116 | | |
c7f1165cdigeff10 years ago | 117 | const testWithRecordings = new RecordingsHelper(() => reactNative).test; |
| 118 | | |
| 119 | testWithRecordings("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 ago | 125 | devices = fillDevices(["Nexus_5"]); |
| 126 | | |
c7f1165cdigeff10 years ago | 127 | return Q({}) |
| 128 | .then(() => { | |
| 129 | return androidPlatform.runApp(); | |
| 130 | }).then(() => { | |
7daed3fcArtem Egorov8 years ago | 131 | return devices[0].installedApplications[androidPackageName].isInDebugMode === false; |
c7f1165cdigeff10 years ago | 132 | }).then(isRunning => { |
| 133 | isRunning.should.be.true(); | |
| 134 | }); | |
| 135 | }); | |
| 136 | | |
| 137 | testWithRecordings("runApp launches the app when two emulators are connected", | |
| 138 | ["react-native/run-android/win10-rn0.21.0/succeedsWithTwoVSEmulators"], () => { | |
7daed3fcArtem Egorov8 years ago | 139 | devices = fillDevices(["Nexus_5", "Nexus_6"]); |
| 140 | | |
c7f1165cdigeff10 years ago | 141 | return Q({}) |
| 142 | .then(() => { | |
| 143 | return androidPlatform.runApp(); | |
| 144 | }).then(() => { | |
| 145 | return Q.all([ | |
7daed3fcArtem Egorov8 years ago | 146 | Q.resolve(devices[0].installedApplications[androidPackageName].isInDebugMode === false), |
| 147 | Q.resolve(devices[1].installedApplications[androidPackageName].isInDebugMode === false), | |
c7f1165cdigeff10 years ago | 148 | ]); |
| 149 | }).spread((isRunningOnNexus5, isRunningOnNexus6) => { | |
| 150 | // It should be running in exactly one of these two devices | |
| 151 | isRunningOnNexus5.should.not.eql(isRunningOnNexus6); | |
| 152 | }); | |
| 153 | }); | |
| 154 | | |
| 155 | testWithRecordings("runApp launches the app when three emulators are connected", | |
| 156 | ["react-native/run-android/win10-rn0.21.0/succeedsWithThreeVSEmulators"], () => { | |
7daed3fcArtem Egorov8 years ago | 157 | devices = fillDevices(["Nexus_5", "Nexus_6", "Nexus_7"]); |
c7f1165cdigeff10 years ago | 158 | return Q({}) |
| 159 | .then(() => { | |
| 160 | return androidPlatform.runApp(); | |
| 161 | }).then(() => { | |
| 162 | return Q.all([ | |
7daed3fcArtem Egorov8 years ago | 163 | Q.resolve(devices[0].installedApplications[androidPackageName].isInDebugMode === false), |
| 164 | Q.resolve(devices[1].installedApplications[androidPackageName].isInDebugMode === false), | |
| 165 | Q.resolve(devices[2].installedApplications[androidPackageName].isInDebugMode === false), | |
c7f1165cdigeff10 years ago | 166 | ]); |
| 167 | }).then(isRunningList => { | |
0766856fdigeff10 years ago | 168 | // It should be running in exactly one of these three devices |
c7f1165cdigeff10 years ago | 169 | isRunningList.filter(v => v).should.eql([true]); |
| 170 | }); | |
| 171 | }); | |
| 172 | | |
| 173 | testWithRecordings("runApp fails if no devices are connected", | |
| 174 | ["react-native/run-android/win10-rn0.21.0/failsDueToNoDevicesConnected"], () => { | |
| 175 | return Q({}) | |
| 176 | .then(() => { | |
| 177 | return androidPlatform.runApp(); | |
| 178 | }).then(() => { | |
| 179 | should.assert(false, "runApp should've exited with an error"); | |
| 180 | }, reason => { | |
77a9922aRuslan Bikkinin8 years ago | 181 | reason.message.startsWith("Unknown error: not all success patterns were matched").should.be.true(); |
c7f1165cdigeff10 years ago | 182 | }); |
| 183 | }); | |
| 184 | | |
| 185 | testWithRecordings("runApp launches the app in an online emulator only", | |
| 186 | ["react-native/run-android/win10-rn0.21.0/succeedsWithFiveVSEmulators"], () => { | |
7daed3fcArtem Egorov8 years ago | 187 | devices = fillDevices(["Nexus_5", "Nexus_6", "Nexus_7", "Nexus_8", "Nexus_9"]); |
| 188 | devices[4].isOnline = false; | |
| 189 | | |
c7f1165cdigeff10 years ago | 190 | return Q({}) |
| 191 | .then(() => { | |
| 192 | return androidPlatform.runApp(); | |
| 193 | }).then(() => { | |
7daed3fcArtem Egorov8 years ago | 194 | return devices[4].installedApplications[androidPackageName].isInDebugMode === false; |
| 195 | }).then((isRunningOnOfflineDevice) => { | |
| 196 | isRunningOnOfflineDevice.should.be.false(); | |
c7f1165cdigeff10 years ago | 197 | }); |
| 198 | }); | |
| 199 | | |
| 200 | testWithRecordings("runApp launches the app in the device specified as target", | |
| 201 | ["react-native/run-android/win10-rn0.21.0/succeedsWithFiveVSEmulators"], () => { | |
7daed3fcArtem Egorov8 years ago | 202 | devices = fillDevices(["Nexus_5", "Nexus_6", "Nexus_10", "Nexus_11", "Nexus_12"]); |
| 203 | | |
c7f1165cdigeff10 years ago | 204 | return Q({}) |
| 205 | .then(() => { | |
c9c3d133Artem Egorov8 years ago | 206 | const runOptions: any = { platform: "android", workspaceRoot: projectRoot, projectRoot: projectRoot, target: "Nexus_12" }; |
c7f1165cdigeff10 years ago | 207 | return createAndroidPlatform(runOptions).runApp(); |
| 208 | }).then(() => { | |
7daed3fcArtem Egorov8 years ago | 209 | return devices[4].installedApplications[androidPackageName].isInDebugMode === false; |
c7f1165cdigeff10 years ago | 210 | }).then((isRunningOnNexus12) => { |
| 211 | isRunningOnNexus12.should.be.true(); | |
| 212 | }); | |
| 213 | }); | |
| 214 | | |
| 215 | testWithRecordings("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 ago | 217 | const onlineDevicesIds = ["Nexus_11", "Nexus_13", "Nexus_14", "Nexus_15", "Nexus_16", "Nexus_17"]; |
| 218 | const offineDevicesIds = ["Nexus_5", "Nexus_6", "Nexus_10", "Nexus_12"]; | |
7daed3fcArtem Egorov8 years ago | 219 | devices = fillDevices(offineDevicesIds.concat(onlineDevicesIds)); |
| 220 | devices[0].isOnline = false; | |
| 221 | devices[1].isOnline = false; | |
| 222 | devices[2].isOnline = false; | |
| 223 | devices[3].isOnline = false; | |
| 224 | | |
c7f1165cdigeff10 years ago | 225 | return Q({}) |
| 226 | .then(() => { | |
c9c3d133Artem Egorov8 years ago | 227 | const runOptions: any = { platform: "android", workspaceRoot: projectRoot, projectRoot: projectRoot, target: "Nexus_12" }; |
c7f1165cdigeff10 years ago | 228 | return createAndroidPlatform(runOptions).runApp(); |
| 229 | }).then(() => { | |
7daed3fcArtem Egorov8 years ago | 230 | return devices.filter((device: any) => device.installedApplications[androidPackageName].isInDebugMode === false); |
c7f1165cdigeff10 years ago | 231 | }).then((devicesRunningAppId) => { |
| 232 | devicesRunningAppId.length.should.eql(1); | |
7daed3fcArtem Egorov8 years ago | 233 | onlineDevicesIds.should.containEql(devicesRunningAppId[0].id); |
c7f1165cdigeff10 years ago | 234 | }); |
| 235 | }); | |
| 236 | | |
| 237 | testWithRecordings("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 ago | 243 | devices = fillDevices(["Nexus_5"]); |
| 244 | | |
c7f1165cdigeff10 years ago | 245 | return Q({}) |
| 246 | .then(() => { | |
| 247 | return androidPlatform.runApp(); | |
| 248 | }).then(() => { | |
7daed3fcArtem Egorov8 years ago | 249 | return devices[0].installedApplications[androidPackageName].isInDebugMode === false; |
c7f1165cdigeff10 years ago | 250 | }).then(isRunning => { |
| 251 | isRunning.should.be.true(); | |
| 252 | }); | |
| 253 | }); | |
| 254 | | |
| 255 | testWithRecordings("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 ago | 260 | devices = fillDevices(["Nexus_5"]); |
| 261 | | |
c7f1165cdigeff10 years ago | 262 | return Q({}) |
| 263 | .then(() => { | |
| 264 | return fileSystem.rmdir(androidProjectPath); | |
| 265 | }).then(() => { | |
| 266 | return androidPlatform.runApp(); | |
| 267 | }).then(() => { | |
| 268 | should.assert(false, "Expected runApp to end up with an error"); | |
| 269 | return false; | |
| 270 | }, reason => { | |
| 271 | reason.message.should.eql("Android project not found."); | |
7daed3fcArtem Egorov8 years ago | 272 | return !!devices[0].installedApplications[androidPackageName]; |
c7f1165cdigeff10 years ago | 273 | }).then(isRunning => { |
| 274 | isRunning.should.be.false(); | |
| 275 | }); | |
| 276 | }); | |
| 277 | | |
| 278 | testWithRecordings("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 ago | 280 | devices = fillDevices(["Nexus_5"]); |
| 281 | | |
c7f1165cdigeff10 years ago | 282 | return Q({}) |
| 283 | .then(() => { | |
| 284 | return androidPlatform.runApp(); | |
| 285 | }).then(() => { | |
| 286 | should.assert(false, "Expected runApp to end up with an error"); | |
| 287 | return false; | |
| 288 | }, reason => { | |
| 289 | "An Android shell command timed-out. Please retry the operation.".should.eql(reason.message); | |
7daed3fcArtem Egorov8 years ago | 290 | return !!devices[0].installedApplications[androidPackageName]; |
c7f1165cdigeff10 years ago | 291 | }).then(isRunning => { |
| 292 | isRunning.should.be.false(); | |
| 293 | }); | |
| 294 | }); | |
| 295 | }); | |
f0008229digeff10 years ago | 296 | }); |
7daed3fcArtem Egorov8 years ago | 297 | |
| 298 | function fillDevices(ids: string[]): any[] { | |
| 299 | let devices: any[] = []; | |
| 300 | ids.forEach(id => { | |
| 301 | devices.push({ isOnline: true, installedApplications: {}, runningApplications: {}, type: adb.DeviceType.AndroidSdkEmulator, id: id }); | |
| 302 | }); | |
| 303 | | |
| 304 | return devices; | |
| 305 | } |