From 60755f6d9cc11215afbef220e5ad4aa9ba80003a Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Fri, 29 Apr 2016 09:52:44 -0700 Subject: [PATCH] fix an issue parsing adb output (#3629) --- .../lib/src/android/android_device.dart | 29 +++++++++++++------ .../test/android_device_test.dart | 24 +++++++++++++++ 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/packages/flutter_tools/lib/src/android/android_device.dart b/packages/flutter_tools/lib/src/android/android_device.dart index 775e3f764b..47bda53d4d 100644 --- a/packages/flutter_tools/lib/src/android/android_device.dart +++ b/packages/flutter_tools/lib/src/android/android_device.dart @@ -55,8 +55,12 @@ class AndroidDevice extends Device { bool get isLocalEmulator { if (_isLocalEmulator == null) { // http://developer.android.com/ndk/guides/abis.html (x86, armeabi-v7a, ...) - String value = runCheckedSync(adbCommandForDevice(['shell', 'getprop', 'ro.product.cpu.abi'])); - _isLocalEmulator = value.startsWith('x86'); + try { + String value = runCheckedSync(adbCommandForDevice(['shell', 'getprop', 'ro.product.cpu.abi'])); + _isLocalEmulator = value.startsWith('x86'); + } catch (error) { + _isLocalEmulator = false; + } } return _isLocalEmulator; @@ -390,14 +394,21 @@ class AndroidDevice extends Device { } } -List getAdbDevices() { - String adbPath = getAdbPath(androidSdk); - if (adbPath == null) - return []; - +/// Return the list of connected ADB devices. +/// +/// [mockAdbOutput] is public for testing. +List getAdbDevices({ String mockAdbOutput }) { List devices = []; + List output; - List output = runSync([adbPath, 'devices', '-l']).trim().split('\n'); + if (mockAdbOutput == null) { + String adbPath = getAdbPath(androidSdk); + if (adbPath == null) + return []; + output = runSync([adbPath, 'devices', '-l']).trim().split('\n'); + } else { + output = mockAdbOutput.trim().split('\n'); + } // 015d172c98400a03 device usb:340787200X product:nakasi model:Nexus_7 device:grouper RegExp deviceRegExLong = new RegExp( @@ -445,7 +456,7 @@ List getAdbDevices() { } else if (deviceState == 'offline') { printError('Device $deviceID is offline.'); } else { - devices.add(new AndroidDevice(deviceID)); + devices.add(new AndroidDevice(deviceID, modelID: deviceID)); } } else { printError( diff --git a/packages/flutter_tools/test/android_device_test.dart b/packages/flutter_tools/test/android_device_test.dart index be140721aa..5978778917 100644 --- a/packages/flutter_tools/test/android_device_test.dart +++ b/packages/flutter_tools/test/android_device_test.dart @@ -15,4 +15,28 @@ void main() { expect(device.id, equals(deviceId)); }); }); + + group('getAdbDevices', () { + testUsingContext('physical devices', () { + List devices = getAdbDevices(mockAdbOutput: ''' +List of devices attached +05a02bac device usb:336592896X product:razor model:Nexus_7 device:flo + +'''); + expect(devices, hasLength(1)); + expect(devices.first.name, 'Nexus 7'); + }); + + testUsingContext('emulators and short listings', () { + List devices = getAdbDevices(mockAdbOutput: ''' +List of devices attached +localhost:36790 device +0149947A0D01500C device usb:340787200X +emulator-5612 host features:shell_2 + +'''); + expect(devices, hasLength(3)); + expect(devices.first.name, 'localhost:36790'); + }); + }); }