2019-11-27 23:04:02 +00:00
|
|
|
// Copyright 2014 The Flutter Authors. All rights reserved.
|
2016-01-27 22:03:41 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2016-08-30 18:11:54 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2019-07-08 22:58:38 +00:00
|
|
|
import 'package:flutter_tools/src/build_info.dart';
|
2016-01-27 22:03:41 +00:00
|
|
|
import 'package:flutter_tools/src/device.dart';
|
2019-10-09 23:30:27 +00:00
|
|
|
import 'package:flutter_tools/src/base/io.dart';
|
2019-06-26 17:09:14 +00:00
|
|
|
import 'package:flutter_tools/src/project.dart';
|
2019-10-09 23:30:27 +00:00
|
|
|
import 'package:mockito/mockito.dart';
|
2016-01-27 22:03:41 +00:00
|
|
|
|
2019-07-13 18:51:44 +00:00
|
|
|
import '../src/common.dart';
|
|
|
|
import '../src/context.dart';
|
2020-03-16 21:15:00 +00:00
|
|
|
import '../src/mocks.dart';
|
2016-02-07 03:19:50 +00:00
|
|
|
|
2016-03-10 01:43:14 +00:00
|
|
|
void main() {
|
2016-01-27 22:03:41 +00:00
|
|
|
group('DeviceManager', () {
|
2016-02-07 03:19:50 +00:00
|
|
|
testUsingContext('getDevices', () async {
|
2016-01-27 22:03:41 +00:00
|
|
|
// Test that DeviceManager.getDevices() doesn't throw.
|
2018-09-12 06:29:29 +00:00
|
|
|
final DeviceManager deviceManager = DeviceManager();
|
2020-02-20 01:40:24 +00:00
|
|
|
final List<Device> devices = await deviceManager.getDevices();
|
2016-01-27 22:03:41 +00:00
|
|
|
expect(devices, isList);
|
|
|
|
});
|
2016-08-30 18:11:54 +00:00
|
|
|
|
|
|
|
testUsingContext('getDeviceById', () async {
|
2018-09-12 06:29:29 +00:00
|
|
|
final _MockDevice device1 = _MockDevice('Nexus 5', '0553790d0a4e726f');
|
|
|
|
final _MockDevice device2 = _MockDevice('Nexus 5X', '01abfc49119c410e');
|
|
|
|
final _MockDevice device3 = _MockDevice('iPod touch', '82564b38861a9a5');
|
2017-03-04 01:50:46 +00:00
|
|
|
final List<Device> devices = <Device>[device1, device2, device3];
|
2018-09-12 06:29:29 +00:00
|
|
|
final DeviceManager deviceManager = TestDeviceManager(devices);
|
2016-08-30 18:11:54 +00:00
|
|
|
|
2018-10-05 05:54:56 +00:00
|
|
|
Future<void> expectDevice(String id, List<Device> expected) async {
|
2020-02-20 01:40:24 +00:00
|
|
|
expect(await deviceManager.getDevicesById(id), expected);
|
2016-08-30 18:11:54 +00:00
|
|
|
}
|
2018-08-31 03:57:44 +00:00
|
|
|
await expectDevice('01abfc49119c410e', <Device>[device2]);
|
|
|
|
await expectDevice('Nexus 5X', <Device>[device2]);
|
|
|
|
await expectDevice('0553790d0a4e726f', <Device>[device1]);
|
|
|
|
await expectDevice('Nexus 5', <Device>[device1]);
|
|
|
|
await expectDevice('0553790', <Device>[device1]);
|
|
|
|
await expectDevice('Nexus', <Device>[device1, device2]);
|
2016-08-30 18:11:54 +00:00
|
|
|
});
|
2020-03-16 21:15:00 +00:00
|
|
|
|
|
|
|
testUsingContext('getAllConnectedDevices caches', () async {
|
|
|
|
final _MockDevice device1 = _MockDevice('Nexus 5', '0553790d0a4e726f');
|
|
|
|
final TestDeviceManager deviceManager = TestDeviceManager(<Device>[device1]);
|
|
|
|
expect(await deviceManager.getAllConnectedDevices(), <Device>[device1]);
|
|
|
|
|
|
|
|
final _MockDevice device2 = _MockDevice('Nexus 5X', '01abfc49119c410e');
|
|
|
|
deviceManager.resetDevices(<Device>[device2]);
|
|
|
|
expect(await deviceManager.getAllConnectedDevices(), <Device>[device1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('refreshAllConnectedDevices does not cache', () async {
|
|
|
|
final _MockDevice device1 = _MockDevice('Nexus 5', '0553790d0a4e726f');
|
|
|
|
final TestDeviceManager deviceManager = TestDeviceManager(<Device>[device1]);
|
|
|
|
expect(await deviceManager.refreshAllConnectedDevices(), <Device>[device1]);
|
|
|
|
|
|
|
|
final _MockDevice device2 = _MockDevice('Nexus 5X', '01abfc49119c410e');
|
|
|
|
deviceManager.resetDevices(<Device>[device2]);
|
|
|
|
expect(await deviceManager.refreshAllConnectedDevices(), <Device>[device2]);
|
|
|
|
});
|
2016-01-27 22:03:41 +00:00
|
|
|
});
|
2019-06-26 17:09:14 +00:00
|
|
|
|
|
|
|
group('Filter devices', () {
|
|
|
|
_MockDevice ephemeral;
|
|
|
|
_MockDevice nonEphemeralOne;
|
|
|
|
_MockDevice nonEphemeralTwo;
|
|
|
|
_MockDevice unsupported;
|
2019-07-08 22:58:38 +00:00
|
|
|
_MockDevice webDevice;
|
|
|
|
_MockDevice fuchsiaDevice;
|
2019-06-26 17:09:14 +00:00
|
|
|
|
|
|
|
setUp(() {
|
|
|
|
ephemeral = _MockDevice('ephemeral', 'ephemeral', true);
|
|
|
|
nonEphemeralOne = _MockDevice('nonEphemeralOne', 'nonEphemeralOne', false);
|
|
|
|
nonEphemeralTwo = _MockDevice('nonEphemeralTwo', 'nonEphemeralTwo', false);
|
|
|
|
unsupported = _MockDevice('unsupported', 'unsupported', true, false);
|
2019-07-08 22:58:38 +00:00
|
|
|
webDevice = _MockDevice('webby', 'webby')
|
|
|
|
..targetPlatform = Future<TargetPlatform>.value(TargetPlatform.web_javascript);
|
|
|
|
fuchsiaDevice = _MockDevice('fuchsiay', 'fuchsiay')
|
2019-10-28 16:38:08 +00:00
|
|
|
..targetPlatform = Future<TargetPlatform>.value(TargetPlatform.fuchsia_x64);
|
2019-06-26 17:09:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('chooses ephemeral device', () async {
|
|
|
|
final List<Device> devices = <Device>[
|
|
|
|
ephemeral,
|
|
|
|
nonEphemeralOne,
|
|
|
|
nonEphemeralTwo,
|
|
|
|
unsupported,
|
|
|
|
];
|
|
|
|
|
|
|
|
final DeviceManager deviceManager = TestDeviceManager(devices);
|
|
|
|
final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current());
|
|
|
|
|
|
|
|
expect(filtered.single, ephemeral);
|
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('does not remove all non-ephemeral', () async {
|
|
|
|
final List<Device> devices = <Device>[
|
|
|
|
nonEphemeralOne,
|
|
|
|
nonEphemeralTwo,
|
|
|
|
];
|
|
|
|
|
|
|
|
final DeviceManager deviceManager = TestDeviceManager(devices);
|
|
|
|
final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current());
|
|
|
|
|
|
|
|
expect(filtered, <Device>[
|
|
|
|
nonEphemeralOne,
|
|
|
|
nonEphemeralTwo,
|
|
|
|
]);
|
|
|
|
});
|
2019-07-08 22:58:38 +00:00
|
|
|
|
2019-08-30 04:39:38 +00:00
|
|
|
testUsingContext('Removes a single unsupported device', () async {
|
|
|
|
final List<Device> devices = <Device>[
|
|
|
|
unsupported,
|
|
|
|
];
|
|
|
|
|
|
|
|
final DeviceManager deviceManager = TestDeviceManager(devices);
|
|
|
|
final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current());
|
|
|
|
|
|
|
|
expect(filtered, <Device>[]);
|
|
|
|
});
|
|
|
|
|
2019-07-08 22:58:38 +00:00
|
|
|
testUsingContext('Removes web and fuchsia from --all', () async {
|
|
|
|
final List<Device> devices = <Device>[
|
|
|
|
webDevice,
|
|
|
|
fuchsiaDevice,
|
|
|
|
];
|
|
|
|
final DeviceManager deviceManager = TestDeviceManager(devices);
|
|
|
|
deviceManager.specifiedDeviceId = 'all';
|
|
|
|
|
|
|
|
final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current());
|
|
|
|
|
|
|
|
expect(filtered, <Device>[]);
|
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('Removes unsupported devices from --all', () async {
|
|
|
|
final List<Device> devices = <Device>[
|
|
|
|
nonEphemeralOne,
|
|
|
|
nonEphemeralTwo,
|
|
|
|
unsupported,
|
|
|
|
];
|
|
|
|
final DeviceManager deviceManager = TestDeviceManager(devices);
|
|
|
|
deviceManager.specifiedDeviceId = 'all';
|
|
|
|
|
|
|
|
final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current());
|
|
|
|
|
|
|
|
expect(filtered, <Device>[
|
|
|
|
nonEphemeralOne,
|
|
|
|
nonEphemeralTwo,
|
|
|
|
]);
|
|
|
|
});
|
2019-07-15 23:10:39 +00:00
|
|
|
|
|
|
|
testUsingContext('uses DeviceManager.isDeviceSupportedForProject instead of device.isSupportedForProject', () async {
|
|
|
|
final List<Device> devices = <Device>[
|
|
|
|
unsupported,
|
|
|
|
];
|
|
|
|
final TestDeviceManager deviceManager = TestDeviceManager(devices);
|
|
|
|
deviceManager.isAlwaysSupportedOverride = true;
|
|
|
|
|
|
|
|
final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current());
|
|
|
|
|
|
|
|
expect(filtered, <Device>[
|
|
|
|
unsupported,
|
|
|
|
]);
|
|
|
|
});
|
2019-06-26 17:09:14 +00:00
|
|
|
});
|
2019-10-09 23:30:27 +00:00
|
|
|
group('ForwardedPort', () {
|
|
|
|
group('dispose()', () {
|
|
|
|
testUsingContext('does not throw exception if no process is present', () {
|
|
|
|
final ForwardedPort forwardedPort = ForwardedPort(123, 456);
|
|
|
|
expect(forwardedPort.context, isNull);
|
|
|
|
forwardedPort.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('kills process if process was available', () {
|
|
|
|
final MockProcess mockProcess = MockProcess();
|
|
|
|
final ForwardedPort forwardedPort = ForwardedPort.withContext(123, 456, mockProcess);
|
|
|
|
forwardedPort.dispose();
|
|
|
|
expect(forwardedPort.context, isNotNull);
|
|
|
|
verify(mockProcess.kill());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-01-27 22:03:41 +00:00
|
|
|
}
|
2016-08-30 18:11:54 +00:00
|
|
|
|
2016-09-01 16:37:58 +00:00
|
|
|
class TestDeviceManager extends DeviceManager {
|
2020-03-16 21:15:00 +00:00
|
|
|
TestDeviceManager(List<Device> allDevices) {
|
|
|
|
_deviceDiscoverer = MockPollingDeviceDiscovery();
|
|
|
|
resetDevices(allDevices);
|
|
|
|
}
|
|
|
|
@override
|
|
|
|
List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[_deviceDiscoverer];
|
|
|
|
MockPollingDeviceDiscovery _deviceDiscoverer;
|
2016-09-01 16:37:58 +00:00
|
|
|
|
2020-03-16 21:15:00 +00:00
|
|
|
void resetDevices(List<Device> allDevices) {
|
|
|
|
_deviceDiscoverer.setDevices(allDevices);
|
|
|
|
}
|
2018-10-04 05:28:07 +00:00
|
|
|
|
2020-03-16 21:15:00 +00:00
|
|
|
bool isAlwaysSupportedOverride;
|
2019-07-15 23:10:39 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool isDeviceSupportedForProject(Device device, FlutterProject flutterProject) {
|
|
|
|
if (isAlwaysSupportedOverride != null) {
|
|
|
|
return isAlwaysSupportedOverride;
|
|
|
|
}
|
|
|
|
return super.isDeviceSupportedForProject(device, flutterProject);
|
|
|
|
}
|
2016-09-01 16:37:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 18:11:54 +00:00
|
|
|
class _MockDevice extends Device {
|
2019-06-26 17:09:14 +00:00
|
|
|
_MockDevice(this.name, String id, [bool ephemeral = true, this._isSupported = true]) : super(
|
2019-06-18 22:23:14 +00:00
|
|
|
id,
|
|
|
|
platformType: PlatformType.web,
|
|
|
|
category: Category.mobile,
|
2019-06-26 17:09:14 +00:00
|
|
|
ephemeral: ephemeral,
|
2019-06-18 22:23:14 +00:00
|
|
|
);
|
2018-10-04 05:28:07 +00:00
|
|
|
|
2019-06-26 17:09:14 +00:00
|
|
|
final bool _isSupported;
|
|
|
|
|
2016-08-30 18:11:54 +00:00
|
|
|
@override
|
|
|
|
final String name;
|
|
|
|
|
2019-07-08 22:58:38 +00:00
|
|
|
@override
|
|
|
|
Future<TargetPlatform> targetPlatform = Future<TargetPlatform>.value(TargetPlatform.android_arm);
|
|
|
|
|
2016-08-30 18:11:54 +00:00
|
|
|
@override
|
|
|
|
void noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
2019-06-26 17:09:14 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool isSupportedForProject(FlutterProject flutterProject) => _isSupported;
|
2016-08-30 18:11:54 +00:00
|
|
|
}
|
2019-10-09 23:30:27 +00:00
|
|
|
|
|
|
|
class MockProcess extends Mock implements Process {}
|