Unverified Commit 09b2f435 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove mockito from mdns client test (#76747)

parent 33a9d6c1
...@@ -6,14 +6,16 @@ ...@@ -6,14 +6,16 @@
import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/base/logger.dart';
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/ios/devices.dart';
import 'package:flutter_tools/src/mdns_discovery.dart'; import 'package:flutter_tools/src/mdns_discovery.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/reporting/reporting.dart'; import 'package:flutter_tools/src/reporting/reporting.dart';
import 'package:mockito/mockito.dart';
import 'package:multicast_dns/multicast_dns.dart'; import 'package:multicast_dns/multicast_dns.dart';
import 'package:test/fake.dart';
import '../src/common.dart'; import '../src/common.dart';
import '../src/mocks.dart';
void main() { void main() {
group('mDNS Discovery', () { group('mDNS Discovery', () {
...@@ -33,33 +35,9 @@ void main() { ...@@ -33,33 +35,9 @@ void main() {
resetNetworkInterfaceLister(); resetNetworkInterfaceLister();
}); });
MDnsClient getMockClient(
List<PtrResourceRecord> ptrRecords,
Map<String, List<SrvResourceRecord>> srvResponse, {
Map<String, List<TxtResourceRecord>> txtResponse = const <String, List<TxtResourceRecord>>{},
}) {
final MDnsClient client = MockMDnsClient();
when(client.lookup<PtrResourceRecord>(
ResourceRecordQuery.serverPointer(MDnsObservatoryDiscovery.dartObservatoryName),
)).thenAnswer((_) => Stream<PtrResourceRecord>.fromIterable(ptrRecords));
for (final MapEntry<String, List<SrvResourceRecord>> entry in srvResponse.entries) {
when(client.lookup<SrvResourceRecord>(
ResourceRecordQuery.service(entry.key),
)).thenAnswer((_) => Stream<SrvResourceRecord>.fromIterable(entry.value));
}
for (final MapEntry<String, List<TxtResourceRecord>> entry in txtResponse.entries) {
when(client.lookup<TxtResourceRecord>(
ResourceRecordQuery.text(entry.key),
)).thenAnswer((_) => Stream<TxtResourceRecord>.fromIterable(entry.value));
}
return client;
}
testWithoutContext('No ports available', () async { testWithoutContext('No ports available', () async {
final MDnsClient client = getMockClient(<PtrResourceRecord>[], <String, List<SrvResourceRecord>>{}); final MDnsClient client = FakeMDnsClient(<PtrResourceRecord>[], <String, List<SrvResourceRecord>>{});
final MDnsObservatoryDiscovery portDiscovery = MDnsObservatoryDiscovery( final MDnsObservatoryDiscovery portDiscovery = MDnsObservatoryDiscovery(
mdnsClient: client, mdnsClient: client,
...@@ -71,7 +49,7 @@ void main() { ...@@ -71,7 +49,7 @@ void main() {
}); });
testWithoutContext('Prints helpful message when there is no ipv4 link local address.', () async { testWithoutContext('Prints helpful message when there is no ipv4 link local address.', () async {
final MDnsClient client = getMockClient(<PtrResourceRecord>[], <String, List<SrvResourceRecord>>{}); final MDnsClient client = FakeMDnsClient(<PtrResourceRecord>[], <String, List<SrvResourceRecord>>{});
final BufferLogger logger = BufferLogger.test(); final BufferLogger logger = BufferLogger.test();
final MDnsObservatoryDiscovery portDiscovery = MDnsObservatoryDiscovery( final MDnsObservatoryDiscovery portDiscovery = MDnsObservatoryDiscovery(
mdnsClient: client, mdnsClient: client,
...@@ -80,14 +58,14 @@ void main() { ...@@ -80,14 +58,14 @@ void main() {
); );
final Uri uri = await portDiscovery.getObservatoryUri( final Uri uri = await portDiscovery.getObservatoryUri(
'', '',
MockIOSDevice(), FakeIOSDevice(),
); );
expect(uri, isNull); expect(uri, isNull);
expect(logger.errorText, contains('Personal Hotspot')); expect(logger.errorText, contains('Personal Hotspot'));
}); });
testWithoutContext('One port available, no appId', () async { testWithoutContext('One port available, no appId', () async {
final MDnsClient client = getMockClient( final MDnsClient client = FakeMDnsClient(
<PtrResourceRecord>[ <PtrResourceRecord>[
PtrResourceRecord('foo', year3000, domainName: 'bar'), PtrResourceRecord('foo', year3000, domainName: 'bar'),
], ],
...@@ -108,7 +86,7 @@ void main() { ...@@ -108,7 +86,7 @@ void main() {
}); });
testWithoutContext('One port available, no appId, with authCode', () async { testWithoutContext('One port available, no appId, with authCode', () async {
final MDnsClient client = getMockClient( final MDnsClient client = FakeMDnsClient(
<PtrResourceRecord>[ <PtrResourceRecord>[
PtrResourceRecord('foo', year3000, domainName: 'bar'), PtrResourceRecord('foo', year3000, domainName: 'bar'),
], ],
...@@ -135,7 +113,7 @@ void main() { ...@@ -135,7 +113,7 @@ void main() {
}); });
testWithoutContext('Multiple ports available, without appId', () async { testWithoutContext('Multiple ports available, without appId', () async {
final MDnsClient client = getMockClient( final MDnsClient client = FakeMDnsClient(
<PtrResourceRecord>[ <PtrResourceRecord>[
PtrResourceRecord('foo', year3000, domainName: 'bar'), PtrResourceRecord('foo', year3000, domainName: 'bar'),
PtrResourceRecord('baz', year3000, domainName: 'fiz'), PtrResourceRecord('baz', year3000, domainName: 'fiz'),
...@@ -159,7 +137,7 @@ void main() { ...@@ -159,7 +137,7 @@ void main() {
}); });
testWithoutContext('Multiple ports available, with appId', () async { testWithoutContext('Multiple ports available, with appId', () async {
final MDnsClient client = getMockClient( final MDnsClient client = FakeMDnsClient(
<PtrResourceRecord>[ <PtrResourceRecord>[
PtrResourceRecord('foo', year3000, domainName: 'bar'), PtrResourceRecord('foo', year3000, domainName: 'bar'),
PtrResourceRecord('baz', year3000, domainName: 'fiz'), PtrResourceRecord('baz', year3000, domainName: 'fiz'),
...@@ -184,7 +162,7 @@ void main() { ...@@ -184,7 +162,7 @@ void main() {
}); });
testWithoutContext('Multiple ports available per process, with appId', () async { testWithoutContext('Multiple ports available per process, with appId', () async {
final MDnsClient client = getMockClient( final MDnsClient client = FakeMDnsClient(
<PtrResourceRecord>[ <PtrResourceRecord>[
PtrResourceRecord('foo', year3000, domainName: 'bar'), PtrResourceRecord('foo', year3000, domainName: 'bar'),
PtrResourceRecord('baz', year3000, domainName: 'fiz'), PtrResourceRecord('baz', year3000, domainName: 'fiz'),
...@@ -211,7 +189,7 @@ void main() { ...@@ -211,7 +189,7 @@ void main() {
}); });
testWithoutContext('Query returns null', () async { testWithoutContext('Query returns null', () async {
final MDnsClient client = getMockClient( final MDnsClient client = FakeMDnsClient(
<PtrResourceRecord>[], <PtrResourceRecord>[],
<String, List<SrvResourceRecord>>{}, <String, List<SrvResourceRecord>>{},
); );
...@@ -226,10 +204,8 @@ void main() { ...@@ -226,10 +204,8 @@ void main() {
}); });
testWithoutContext('Throws Exception when client throws OSError on start', () async { testWithoutContext('Throws Exception when client throws OSError on start', () async {
final MDnsClient client = MockMDnsClient(); final MDnsClient client = FakeMDnsClient(<PtrResourceRecord>[], <String, List<SrvResourceRecord>>{}, osErrorOnStart: true);
when(client.start()).thenAnswer((_) {
throw const OSError('Operation not suppoted on socket', 102);
});
final MDnsObservatoryDiscovery portDiscovery = MDnsObservatoryDiscovery( final MDnsObservatoryDiscovery portDiscovery = MDnsObservatoryDiscovery(
mdnsClient: client, mdnsClient: client,
...@@ -243,7 +219,7 @@ void main() { ...@@ -243,7 +219,7 @@ void main() {
}); });
testWithoutContext('Correctly builds Observatory URI with hostVmservicePort == 0', () async { testWithoutContext('Correctly builds Observatory URI with hostVmservicePort == 0', () async {
final MDnsClient client = getMockClient( final MDnsClient client = FakeMDnsClient(
<PtrResourceRecord>[ <PtrResourceRecord>[
PtrResourceRecord('foo', year3000, domainName: 'bar'), PtrResourceRecord('foo', year3000, domainName: 'bar'),
], ],
...@@ -254,17 +230,74 @@ void main() { ...@@ -254,17 +230,74 @@ void main() {
}, },
); );
final MockIOSDevice mockDevice = MockIOSDevice(); final FakeIOSDevice device = FakeIOSDevice();
when(mockDevice.portForwarder).thenReturn(const NoOpDevicePortForwarder());
final MDnsObservatoryDiscovery portDiscovery = MDnsObservatoryDiscovery( final MDnsObservatoryDiscovery portDiscovery = MDnsObservatoryDiscovery(
mdnsClient: client, mdnsClient: client,
logger: BufferLogger.test(), logger: BufferLogger.test(),
flutterUsage: TestUsage(), flutterUsage: TestUsage(),
); );
final Uri uri = await portDiscovery.getObservatoryUri('bar', mockDevice, hostVmservicePort: 0); final Uri uri = await portDiscovery.getObservatoryUri('bar', device, hostVmservicePort: 0);
expect(uri.toString(), 'http://127.0.0.1:123/'); expect(uri.toString(), 'http://127.0.0.1:123/');
}); });
}); });
} }
class MockMDnsClient extends Mock implements MDnsClient {} class FakeMDnsClient extends Fake implements MDnsClient {
FakeMDnsClient(this.ptrRecords, this.srvResponse, {
this.txtResponse = const <String, List<TxtResourceRecord>>{},
this.osErrorOnStart = false,
});
final List<PtrResourceRecord> ptrRecords;
final Map<String, List<SrvResourceRecord>> srvResponse;
final Map<String, List<TxtResourceRecord>> txtResponse;
final bool osErrorOnStart;
@override
Future<void> start({
InternetAddress listenAddress,
NetworkInterfacesFactory interfacesFactory,
int mDnsPort = 5353,
InternetAddress mDnsAddress,
}) async {
if (osErrorOnStart) {
throw const OSError('Operation not suppoted on socket', 102);
}
}
@override
Stream<T> lookup<T extends ResourceRecord>(
ResourceRecordQuery query, {
Duration timeout = const Duration(seconds: 5),
}) {
if (T == PtrResourceRecord && query.fullyQualifiedName == MDnsObservatoryDiscovery.dartObservatoryName) {
return Stream<PtrResourceRecord>.fromIterable(ptrRecords) as Stream<T>;
}
if (T == SrvResourceRecord) {
final String key = query.fullyQualifiedName;
return Stream<SrvResourceRecord>.fromIterable(srvResponse[key] ?? <SrvResourceRecord>[]) as Stream<T>;
}
if (T == TxtResourceRecord) {
final String key = query.fullyQualifiedName;
return Stream<TxtResourceRecord>.fromIterable(txtResponse[key] ?? <TxtResourceRecord>[]) as Stream<T>;
}
throw UnsupportedError('Unsupported query type $T');
}
@override
void stop() {}
}
class FakeIOSDevice extends Fake implements IOSDevice {
@override
Future<TargetPlatform> get targetPlatform async => TargetPlatform.ios;
@override
bool isSupported() => true;
@override
bool isSupportedForProject(FlutterProject flutterProject) => true;
@override
DevicePortForwarder get portForwarder => const NoOpDevicePortForwarder();
}
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