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';
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:meta/meta.dart';
import 'package:mockito/mockito.dart';
import 'package:test/fake.dart';
import '../src/common.dart';
import '../src/context.dart';
import '../src/fake_devices.dart';
void main() {
......@@ -184,18 +183,13 @@ void main() {
nonEphemeralOne,
nonEphemeralTwo,
];
final MockTerminal mockTerminal = MockTerminal();
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 FakeTerminal terminal = FakeTerminal()
..setPrompt(<String>['1', '2', 'q', 'Q'], '1');
final DeviceManager deviceManager = TestDeviceManager(
devices,
logger: BufferLogger.test(),
terminal: mockTerminal,
terminal: terminal,
);
final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
......@@ -209,19 +203,13 @@ void main() {
nonEphemeralOne,
nonEphemeralTwo,
];
final MockTerminal mockTerminal = MockTerminal();
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 FakeTerminal terminal = FakeTerminal()
..setPrompt(<String>['1', '2', 'q', 'Q'], '2');
final DeviceManager deviceManager = TestDeviceManager(
devices,
logger: BufferLogger.test(),
terminal: mockTerminal,
terminal: terminal,
);
final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
......@@ -236,19 +224,13 @@ void main() {
ephemeralTwo,
];
final MockTerminal mockTerminal = MockTerminal();
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 FakeTerminal terminal = FakeTerminal()
..setPrompt(<String>['1', '2', 'q', 'Q'], '1');
final DeviceManager deviceManager = TestDeviceManager(
devices,
logger: BufferLogger.test(),
terminal: mockTerminal,
terminal: terminal,
);
final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
......@@ -262,19 +244,13 @@ void main() {
ephemeralOne,
ephemeralTwo,
];
final MockTerminal mockTerminal = MockTerminal();
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 FakeTerminal terminal = FakeTerminal()
..setPrompt(<String>['1', '2', 'q', 'Q'], '2');
final DeviceManager deviceManager = TestDeviceManager(
devices,
logger: BufferLogger.test(),
terminal: mockTerminal,
terminal: terminal,
);
final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
......@@ -291,19 +267,13 @@ void main() {
nonEphemeralTwo,
];
final MockTerminal mockTerminal = MockTerminal();
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 FakeTerminal terminal = FakeTerminal()
..setPrompt(<String>['1', '2', '3', '4', 'q', 'Q'], '3');
final DeviceManager deviceManager = TestDeviceManager(
devices,
logger: BufferLogger.test(),
terminal: mockTerminal,
terminal: terminal,
);
final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
......@@ -319,19 +289,13 @@ void main() {
ephemeralTwo,
];
final MockTerminal mockTerminal = MockTerminal();
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 FakeTerminal terminal = FakeTerminal()
..setPrompt(<String>['1', '2', 'q', 'Q'], 'q');
final DeviceManager deviceManager = TestDeviceManager(
devices,
logger: BufferLogger.test(),
terminal: mockTerminal,
terminal: terminal,
);
await expectLater(
() async => deviceManager.findTargetDevices(FakeFlutterProject()),
......@@ -429,14 +393,13 @@ void main() {
final List<Device> devices = <Device>[
ephemeralOne,
];
final MockDeviceDiscovery mockDeviceDiscovery = MockDeviceDiscovery();
when(mockDeviceDiscovery.supportsPlatform).thenReturn(true);
when(mockDeviceDiscovery.devices).thenAnswer((_) async => devices);
final MockDeviceDiscovery deviceDiscovery = MockDeviceDiscovery()
..deviceValues = devices;
final DeviceManager deviceManager = TestDeviceManager(
<Device>[],
deviceDiscoveryOverrides: <DeviceDiscovery>[
mockDeviceDiscovery
deviceDiscovery
],
logger: BufferLogger.test(),
terminal: Terminal.test(),
......@@ -447,24 +410,22 @@ void main() {
);
expect(filtered.single, ephemeralOne);
verify(mockDeviceDiscovery.devices).called(1);
verifyNever(mockDeviceDiscovery.discoverDevices(timeout: anyNamed('timeout')));
expect(deviceDiscovery.devicesCalled, 1);
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>[
ephemeralOne,
];
const Duration timeout = Duration(seconds: 2);
final MockDeviceDiscovery mockDeviceDiscovery = MockDeviceDiscovery();
when(mockDeviceDiscovery.supportsPlatform).thenReturn(true);
when(mockDeviceDiscovery.discoverDevices(timeout: timeout)).thenAnswer((_) async => devices);
when(mockDeviceDiscovery.devices).thenAnswer((_) async => devices);
final MockDeviceDiscovery deviceDiscovery = MockDeviceDiscovery()
..deviceValues = devices;
final DeviceManager deviceManager = TestDeviceManager(
<Device>[],
deviceDiscoveryOverrides: <DeviceDiscovery>[
mockDeviceDiscovery
deviceDiscovery
],
logger: BufferLogger.test(),
terminal: Terminal.test(),
......@@ -476,13 +437,13 @@ void main() {
);
expect(filtered.single, ephemeralOne);
verify(mockDeviceDiscovery.devices).called(1);
verify(mockDeviceDiscovery.discoverDevices(timeout: anyNamed('timeout'))).called(1);
expect(deviceDiscovery.devicesCalled, 1);
expect(deviceDiscovery.discoverDevicesCalled, 1);
});
});
group('JSON encode devices', () {
testUsingContext('Consistency of JSON representation', () async {
testWithoutContext('Consistency of JSON representation', () async {
expect(
// This tests that fakeDevices is a list of tuples where "second" is the
// correct JSON representation of the "first". Actual values are irrelevant
......@@ -534,8 +495,28 @@ class TestDeviceManager extends DeviceManager {
}
}
class MockTerminal extends Mock implements AnsiTerminal {}
class MockDeviceDiscovery extends Mock implements DeviceDiscovery {}
class MockDeviceDiscovery extends Fake 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 LongPollingDeviceDiscovery extends PollingDeviceDiscovery {
......@@ -579,3 +560,31 @@ class ThrowingPollingDeviceDiscovery extends PollingDeviceDiscovery {
@override
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