Unverified Commit 6192e420 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove mocks from device test (#82472)

parent 96320ae7
...@@ -15,10 +15,9 @@ import 'package:flutter_tools/src/build_info.dart'; ...@@ -15,10 +15,9 @@ import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/device.dart'; import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/project.dart'; import 'package:flutter_tools/src/project.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:mockito/mockito.dart'; import 'package:test/fake.dart';
import '../src/common.dart'; import '../src/common.dart';
import '../src/context.dart';
import '../src/fake_devices.dart'; import '../src/fake_devices.dart';
void main() { void main() {
...@@ -184,18 +183,13 @@ void main() { ...@@ -184,18 +183,13 @@ void main() {
nonEphemeralOne, nonEphemeralOne,
nonEphemeralTwo, nonEphemeralTwo,
]; ];
final MockTerminal mockTerminal = MockTerminal(); final FakeTerminal terminal = FakeTerminal()
when(mockTerminal.stdinHasTerminal).thenReturn(true); ..setPrompt(<String>['1', '2', 'q', 'Q'], '1');
when(mockTerminal.promptForCharInput(<String>['1', '2', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: anyNamed('logger'),
prompt: anyNamed('prompt'),
)).thenAnswer((Invocation invocation) async => '1');
final DeviceManager deviceManager = TestDeviceManager( final DeviceManager deviceManager = TestDeviceManager(
devices, devices,
logger: BufferLogger.test(), logger: BufferLogger.test(),
terminal: mockTerminal, terminal: terminal,
); );
final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject()); final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
...@@ -209,19 +203,13 @@ void main() { ...@@ -209,19 +203,13 @@ void main() {
nonEphemeralOne, nonEphemeralOne,
nonEphemeralTwo, nonEphemeralTwo,
]; ];
final MockTerminal mockTerminal = MockTerminal(); final FakeTerminal terminal = FakeTerminal()
..setPrompt(<String>['1', '2', 'q', 'Q'], '2');
when(mockTerminal.stdinHasTerminal).thenReturn(true);
when(mockTerminal.promptForCharInput(<String>['1', '2', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: anyNamed('logger'),
prompt: anyNamed('prompt'),
)).thenAnswer((Invocation invocation) async => '2');
final DeviceManager deviceManager = TestDeviceManager( final DeviceManager deviceManager = TestDeviceManager(
devices, devices,
logger: BufferLogger.test(), logger: BufferLogger.test(),
terminal: mockTerminal, terminal: terminal,
); );
final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject()); final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
...@@ -236,19 +224,13 @@ void main() { ...@@ -236,19 +224,13 @@ void main() {
ephemeralTwo, ephemeralTwo,
]; ];
final MockTerminal mockTerminal = MockTerminal(); final FakeTerminal terminal = FakeTerminal()
..setPrompt(<String>['1', '2', 'q', 'Q'], '1');
when(mockTerminal.stdinHasTerminal).thenReturn(true);
when(mockTerminal.promptForCharInput(<String>['1', '2', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: anyNamed('logger'),
prompt: anyNamed('prompt'),
)).thenAnswer((Invocation invocation) async => '1');
final DeviceManager deviceManager = TestDeviceManager( final DeviceManager deviceManager = TestDeviceManager(
devices, devices,
logger: BufferLogger.test(), logger: BufferLogger.test(),
terminal: mockTerminal, terminal: terminal,
); );
final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject()); final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
...@@ -262,19 +244,13 @@ void main() { ...@@ -262,19 +244,13 @@ void main() {
ephemeralOne, ephemeralOne,
ephemeralTwo, ephemeralTwo,
]; ];
final MockTerminal mockTerminal = MockTerminal(); final FakeTerminal terminal = FakeTerminal()
..setPrompt(<String>['1', '2', 'q', 'Q'], '2');
when(mockTerminal.stdinHasTerminal).thenReturn(true);
when(mockTerminal.promptForCharInput(<String>['1', '2', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: anyNamed('logger'),
prompt: anyNamed('prompt'),
)).thenAnswer((Invocation invocation) async => '2');
final DeviceManager deviceManager = TestDeviceManager( final DeviceManager deviceManager = TestDeviceManager(
devices, devices,
logger: BufferLogger.test(), logger: BufferLogger.test(),
terminal: mockTerminal, terminal: terminal,
); );
final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject()); final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
...@@ -291,19 +267,13 @@ void main() { ...@@ -291,19 +267,13 @@ void main() {
nonEphemeralTwo, nonEphemeralTwo,
]; ];
final MockTerminal mockTerminal = MockTerminal(); final FakeTerminal terminal = FakeTerminal()
..setPrompt(<String>['1', '2', '3', '4', 'q', 'Q'], '3');
when(mockTerminal.stdinHasTerminal).thenReturn(true);
when(mockTerminal.promptForCharInput(<String>['1', '2', '3', '4', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: anyNamed('logger'),
prompt: anyNamed('prompt'),
)).thenAnswer((Invocation invocation) async => '3');
final DeviceManager deviceManager = TestDeviceManager( final DeviceManager deviceManager = TestDeviceManager(
devices, devices,
logger: BufferLogger.test(), logger: BufferLogger.test(),
terminal: mockTerminal, terminal: terminal,
); );
final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject()); final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
...@@ -319,19 +289,13 @@ void main() { ...@@ -319,19 +289,13 @@ void main() {
ephemeralTwo, ephemeralTwo,
]; ];
final MockTerminal mockTerminal = MockTerminal(); final FakeTerminal terminal = FakeTerminal()
..setPrompt(<String>['1', '2', 'q', 'Q'], 'q');
when(mockTerminal.stdinHasTerminal).thenReturn(true);
when(mockTerminal.promptForCharInput(<String>['1', '2', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: anyNamed('logger'),
prompt: anyNamed('prompt'),
)).thenAnswer((Invocation invocation) async => 'q');
final DeviceManager deviceManager = TestDeviceManager( final DeviceManager deviceManager = TestDeviceManager(
devices, devices,
logger: BufferLogger.test(), logger: BufferLogger.test(),
terminal: mockTerminal, terminal: terminal,
); );
await expectLater( await expectLater(
() async => deviceManager.findTargetDevices(FakeFlutterProject()), () async => deviceManager.findTargetDevices(FakeFlutterProject()),
...@@ -429,14 +393,13 @@ void main() { ...@@ -429,14 +393,13 @@ void main() {
final List<Device> devices = <Device>[ final List<Device> devices = <Device>[
ephemeralOne, ephemeralOne,
]; ];
final MockDeviceDiscovery mockDeviceDiscovery = MockDeviceDiscovery(); final MockDeviceDiscovery deviceDiscovery = MockDeviceDiscovery()
when(mockDeviceDiscovery.supportsPlatform).thenReturn(true); ..deviceValues = devices;
when(mockDeviceDiscovery.devices).thenAnswer((_) async => devices);
final DeviceManager deviceManager = TestDeviceManager( final DeviceManager deviceManager = TestDeviceManager(
<Device>[], <Device>[],
deviceDiscoveryOverrides: <DeviceDiscovery>[ deviceDiscoveryOverrides: <DeviceDiscovery>[
mockDeviceDiscovery deviceDiscovery
], ],
logger: BufferLogger.test(), logger: BufferLogger.test(),
terminal: Terminal.test(), terminal: Terminal.test(),
...@@ -447,24 +410,22 @@ void main() { ...@@ -447,24 +410,22 @@ void main() {
); );
expect(filtered.single, ephemeralOne); expect(filtered.single, ephemeralOne);
verify(mockDeviceDiscovery.devices).called(1); expect(deviceDiscovery.devicesCalled, 1);
verifyNever(mockDeviceDiscovery.discoverDevices(timeout: anyNamed('timeout'))); expect(deviceDiscovery.discoverDevicesCalled, 0);
}); });
testUsingContext('refreshes device cache with a timeout', () async { testWithoutContext('refreshes device cache with a timeout', () async {
final List<Device> devices = <Device>[ final List<Device> devices = <Device>[
ephemeralOne, ephemeralOne,
]; ];
const Duration timeout = Duration(seconds: 2); const Duration timeout = Duration(seconds: 2);
final MockDeviceDiscovery mockDeviceDiscovery = MockDeviceDiscovery(); final MockDeviceDiscovery deviceDiscovery = MockDeviceDiscovery()
when(mockDeviceDiscovery.supportsPlatform).thenReturn(true); ..deviceValues = devices;
when(mockDeviceDiscovery.discoverDevices(timeout: timeout)).thenAnswer((_) async => devices);
when(mockDeviceDiscovery.devices).thenAnswer((_) async => devices);
final DeviceManager deviceManager = TestDeviceManager( final DeviceManager deviceManager = TestDeviceManager(
<Device>[], <Device>[],
deviceDiscoveryOverrides: <DeviceDiscovery>[ deviceDiscoveryOverrides: <DeviceDiscovery>[
mockDeviceDiscovery deviceDiscovery
], ],
logger: BufferLogger.test(), logger: BufferLogger.test(),
terminal: Terminal.test(), terminal: Terminal.test(),
...@@ -476,13 +437,13 @@ void main() { ...@@ -476,13 +437,13 @@ void main() {
); );
expect(filtered.single, ephemeralOne); expect(filtered.single, ephemeralOne);
verify(mockDeviceDiscovery.devices).called(1); expect(deviceDiscovery.devicesCalled, 1);
verify(mockDeviceDiscovery.discoverDevices(timeout: anyNamed('timeout'))).called(1); expect(deviceDiscovery.discoverDevicesCalled, 1);
}); });
}); });
group('JSON encode devices', () { group('JSON encode devices', () {
testUsingContext('Consistency of JSON representation', () async { testWithoutContext('Consistency of JSON representation', () async {
expect( expect(
// This tests that fakeDevices is a list of tuples where "second" is the // This tests that fakeDevices is a list of tuples where "second" is the
// correct JSON representation of the "first". Actual values are irrelevant // correct JSON representation of the "first". Actual values are irrelevant
...@@ -534,8 +495,28 @@ class TestDeviceManager extends DeviceManager { ...@@ -534,8 +495,28 @@ class TestDeviceManager extends DeviceManager {
} }
} }
class MockTerminal extends Mock implements AnsiTerminal {} class MockDeviceDiscovery extends Fake implements DeviceDiscovery {
class MockDeviceDiscovery extends Mock implements DeviceDiscovery {} int devicesCalled = 0;
int discoverDevicesCalled = 0;
@override
bool supportsPlatform = true;
List<Device> deviceValues = <Device>[];
@override
Future<List<Device>> get devices async {
devicesCalled += 1;
return deviceValues;
}
@override
Future<List<Device>> discoverDevices({Duration timeout}) async {
discoverDevicesCalled += 1;
return deviceValues;
}
}
class FakeFlutterProject extends Fake implements FlutterProject {} class FakeFlutterProject extends Fake implements FlutterProject {}
class LongPollingDeviceDiscovery extends PollingDeviceDiscovery { class LongPollingDeviceDiscovery extends PollingDeviceDiscovery {
...@@ -579,3 +560,31 @@ class ThrowingPollingDeviceDiscovery extends PollingDeviceDiscovery { ...@@ -579,3 +560,31 @@ class ThrowingPollingDeviceDiscovery extends PollingDeviceDiscovery {
@override @override
bool get canListAnything => true; bool get canListAnything => true;
} }
class FakeTerminal extends Fake implements Terminal {
@override
bool stdinHasTerminal = true;
@override
bool usesTerminalUi = true;
void setPrompt(List<String> characters, String result) {
_nextPrompt = characters;
_nextResult = result;
}
List<String> _nextPrompt;
String _nextResult;
@override
Future<String> promptForCharInput(
List<String> acceptedCharacters, {
Logger logger,
String prompt,
int defaultChoiceIndex,
bool displayAcceptedCharacters = true,
}) async {
expect(acceptedCharacters, _nextPrompt);
return _nextResult;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment