mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
fix getDeviceById to match exact name (#5657)
* fix getDeviceById to match exact name fixes https://github.com/flutter/flutter/issues/5508
This commit is contained in:
parent
f6353b686d
commit
83bf5d10c0
@ -39,13 +39,14 @@ class DeviceManager {
|
|||||||
/// `null`.
|
/// `null`.
|
||||||
///
|
///
|
||||||
/// This does a case insentitive compare with `deviceId`.
|
/// This does a case insentitive compare with `deviceId`.
|
||||||
Future<Device> getDeviceById(String deviceId) async {
|
Future<Device> getDeviceById(String deviceId, [List<Device> devices]) async {
|
||||||
deviceId = deviceId.toLowerCase();
|
deviceId = deviceId.toLowerCase();
|
||||||
List<Device> devices = await getAllConnectedDevices();
|
devices ??= await getAllConnectedDevices();
|
||||||
Device device = devices.firstWhere(
|
Device device = devices.firstWhere(
|
||||||
(Device device) => device.id.toLowerCase() == deviceId,
|
(Device device) =>
|
||||||
orElse: () => null
|
device.id.toLowerCase() == deviceId ||
|
||||||
);
|
device.name.toLowerCase() == deviceId,
|
||||||
|
orElse: () => null);
|
||||||
|
|
||||||
if (device != null)
|
if (device != null)
|
||||||
return device;
|
return device;
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter_tools/src/device.dart';
|
import 'package:flutter_tools/src/device.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
@ -15,5 +17,33 @@ void main() {
|
|||||||
List<Device> devices = await deviceManager.getDevices();
|
List<Device> devices = await deviceManager.getDevices();
|
||||||
expect(devices, isList);
|
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<Device> devices = <Device>[device1, device2, device3];
|
||||||
|
|
||||||
|
Future<Null> 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);
|
||||||
|
}
|
||||||
|
@ -82,7 +82,7 @@ class MockDeviceManager implements DeviceManager {
|
|||||||
Future<List<Device>> getAllConnectedDevices() => new Future<List<Device>>.value(devices);
|
Future<List<Device>> getAllConnectedDevices() => new Future<List<Device>>.value(devices);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Device> getDeviceById(String deviceId) {
|
Future<Device> getDeviceById(String deviceId, [List<Device> _]) {
|
||||||
Device device = devices.firstWhere((Device device) => device.id == deviceId, orElse: () => null);
|
Device device = devices.firstWhere((Device device) => device.id == deviceId, orElse: () => null);
|
||||||
return new Future<Device>.value(device);
|
return new Future<Device>.value(device);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user