From 83bf5d10c04c7a68b5028e95c38d83d63912167a Mon Sep 17 00:00:00 2001 From: Dan Rubel Date: Tue, 30 Aug 2016 14:11:54 -0400 Subject: [PATCH] fix getDeviceById to match exact name (#5657) * fix getDeviceById to match exact name fixes https://github.com/flutter/flutter/issues/5508 --- packages/flutter_tools/lib/src/device.dart | 11 +++---- packages/flutter_tools/test/device_test.dart | 30 ++++++++++++++++++++ packages/flutter_tools/test/src/context.dart | 2 +- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 62a6ba5533a..0c8ce94fa86 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -39,13 +39,14 @@ class DeviceManager { /// `null`. /// /// This does a case insentitive compare with `deviceId`. - Future getDeviceById(String deviceId) async { + Future getDeviceById(String deviceId, [List devices]) async { deviceId = deviceId.toLowerCase(); - List devices = await getAllConnectedDevices(); + devices ??= await getAllConnectedDevices(); Device device = devices.firstWhere( - (Device device) => device.id.toLowerCase() == deviceId, - orElse: () => null - ); + (Device device) => + device.id.toLowerCase() == deviceId || + device.name.toLowerCase() == deviceId, + orElse: () => null); if (device != null) return device; diff --git a/packages/flutter_tools/test/device_test.dart b/packages/flutter_tools/test/device_test.dart index 2bb2fb75f75..8c06692c490 100644 --- a/packages/flutter_tools/test/device_test.dart +++ b/packages/flutter_tools/test/device_test.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; + import 'package:flutter_tools/src/device.dart'; import 'package:test/test.dart'; @@ -15,5 +17,33 @@ void main() { List devices = await deviceManager.getDevices(); expect(devices, isList); }); + + testUsingContext('getDeviceById', () async { + DeviceManager deviceManager = new DeviceManager(); + _MockDevice device1 = new _MockDevice('Nexus 5', '0553790d0a4e726f'); + _MockDevice device2 = new _MockDevice('Nexus 5X', '01abfc49119c410e'); + _MockDevice device3 = new _MockDevice('iPod touch', '82564b38861a9a5'); + List devices = [device1, device2, device3]; + + Future expectDevice(String id, Device expected) async { + expect(await deviceManager.getDeviceById(id, devices), expected); + } + expectDevice('01abfc49119c410e', device2); + expectDevice('Nexus 5X', device2); + expectDevice('0553790d0a4e726f', device1); + expectDevice('Nexus 5', device1); + expectDevice('0553790', device1); + expectDevice('Nexus', null); + }); }); } + +class _MockDevice extends Device { + @override + final String name; + + _MockDevice(this.name, String id) : super(id); + + @override + void noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); +} diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart index c75d837b057..6f4932504d4 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -82,7 +82,7 @@ class MockDeviceManager implements DeviceManager { Future> getAllConnectedDevices() => new Future>.value(devices); @override - Future getDeviceById(String deviceId) { + Future getDeviceById(String deviceId, [List _]) { Device device = devices.firstWhere((Device device) => device.id == deviceId, orElse: () => null); return new Future.value(device); }