1// Copyright 2016 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'dart:async'; 6 7import 'package:flutter_tools/src/build_info.dart'; 8import 'package:flutter_tools/src/device.dart'; 9import 'package:flutter_tools/src/project.dart'; 10 11import '../src/common.dart'; 12import '../src/context.dart'; 13 14void main() { 15 group('DeviceManager', () { 16 testUsingContext('getDevices', () async { 17 // Test that DeviceManager.getDevices() doesn't throw. 18 final DeviceManager deviceManager = DeviceManager(); 19 final List<Device> devices = await deviceManager.getDevices().toList(); 20 expect(devices, isList); 21 }); 22 23 testUsingContext('getDeviceById', () async { 24 final _MockDevice device1 = _MockDevice('Nexus 5', '0553790d0a4e726f'); 25 final _MockDevice device2 = _MockDevice('Nexus 5X', '01abfc49119c410e'); 26 final _MockDevice device3 = _MockDevice('iPod touch', '82564b38861a9a5'); 27 final List<Device> devices = <Device>[device1, device2, device3]; 28 final DeviceManager deviceManager = TestDeviceManager(devices); 29 30 Future<void> expectDevice(String id, List<Device> expected) async { 31 expect(await deviceManager.getDevicesById(id).toList(), expected); 32 } 33 await expectDevice('01abfc49119c410e', <Device>[device2]); 34 await expectDevice('Nexus 5X', <Device>[device2]); 35 await expectDevice('0553790d0a4e726f', <Device>[device1]); 36 await expectDevice('Nexus 5', <Device>[device1]); 37 await expectDevice('0553790', <Device>[device1]); 38 await expectDevice('Nexus', <Device>[device1, device2]); 39 }); 40 }); 41 42 group('Filter devices', () { 43 _MockDevice ephemeral; 44 _MockDevice nonEphemeralOne; 45 _MockDevice nonEphemeralTwo; 46 _MockDevice unsupported; 47 _MockDevice webDevice; 48 _MockDevice fuchsiaDevice; 49 50 setUp(() { 51 ephemeral = _MockDevice('ephemeral', 'ephemeral', true); 52 nonEphemeralOne = _MockDevice('nonEphemeralOne', 'nonEphemeralOne', false); 53 nonEphemeralTwo = _MockDevice('nonEphemeralTwo', 'nonEphemeralTwo', false); 54 unsupported = _MockDevice('unsupported', 'unsupported', true, false); 55 webDevice = _MockDevice('webby', 'webby') 56 ..targetPlatform = Future<TargetPlatform>.value(TargetPlatform.web_javascript); 57 fuchsiaDevice = _MockDevice('fuchsiay', 'fuchsiay') 58 ..targetPlatform = Future<TargetPlatform>.value(TargetPlatform.fuchsia); 59 }); 60 61 testUsingContext('chooses ephemeral device', () async { 62 final List<Device> devices = <Device>[ 63 ephemeral, 64 nonEphemeralOne, 65 nonEphemeralTwo, 66 unsupported, 67 ]; 68 69 final DeviceManager deviceManager = TestDeviceManager(devices); 70 final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current()); 71 72 expect(filtered.single, ephemeral); 73 }); 74 75 testUsingContext('does not remove all non-ephemeral', () async { 76 final List<Device> devices = <Device>[ 77 nonEphemeralOne, 78 nonEphemeralTwo, 79 ]; 80 81 final DeviceManager deviceManager = TestDeviceManager(devices); 82 final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current()); 83 84 expect(filtered, <Device>[ 85 nonEphemeralOne, 86 nonEphemeralTwo, 87 ]); 88 }); 89 90 testUsingContext('Removes web and fuchsia from --all', () async { 91 final List<Device> devices = <Device>[ 92 webDevice, 93 fuchsiaDevice, 94 ]; 95 final DeviceManager deviceManager = TestDeviceManager(devices); 96 deviceManager.specifiedDeviceId = 'all'; 97 98 final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current()); 99 100 expect(filtered, <Device>[]); 101 }); 102 103 testUsingContext('Removes unsupported devices from --all', () async { 104 final List<Device> devices = <Device>[ 105 nonEphemeralOne, 106 nonEphemeralTwo, 107 unsupported, 108 ]; 109 final DeviceManager deviceManager = TestDeviceManager(devices); 110 deviceManager.specifiedDeviceId = 'all'; 111 112 final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current()); 113 114 expect(filtered, <Device>[ 115 nonEphemeralOne, 116 nonEphemeralTwo, 117 ]); 118 }); 119 120 testUsingContext('uses DeviceManager.isDeviceSupportedForProject instead of device.isSupportedForProject', () async { 121 final List<Device> devices = <Device>[ 122 unsupported, 123 ]; 124 final TestDeviceManager deviceManager = TestDeviceManager(devices); 125 deviceManager.isAlwaysSupportedOverride = true; 126 127 final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current()); 128 129 expect(filtered, <Device>[ 130 unsupported, 131 ]); 132 }); 133 }); 134} 135 136class TestDeviceManager extends DeviceManager { 137 TestDeviceManager(this.allDevices); 138 139 final List<Device> allDevices; 140 bool isAlwaysSupportedOverride; 141 142 @override 143 Stream<Device> getAllConnectedDevices() { 144 return Stream<Device>.fromIterable(allDevices); 145 } 146 147 @override 148 bool isDeviceSupportedForProject(Device device, FlutterProject flutterProject) { 149 if (isAlwaysSupportedOverride != null) { 150 return isAlwaysSupportedOverride; 151 } 152 return super.isDeviceSupportedForProject(device, flutterProject); 153 } 154} 155 156class _MockDevice extends Device { 157 _MockDevice(this.name, String id, [bool ephemeral = true, this._isSupported = true]) : super( 158 id, 159 platformType: PlatformType.web, 160 category: Category.mobile, 161 ephemeral: ephemeral, 162 ); 163 164 final bool _isSupported; 165 166 @override 167 final String name; 168 169 @override 170 Future<TargetPlatform> targetPlatform = Future<TargetPlatform>.value(TargetPlatform.android_arm); 171 172 @override 173 void noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); 174 175 @override 176 bool isSupportedForProject(FlutterProject flutterProject) => _isSupported; 177} 178