Skip to content

Commit 58287ac

Browse files
authored
Convert services tests to NNBD (flutter#62694)
This converts the packages/flutter/test/services directory to NNBD, now that the services package is converted. I changed the signature of checkMessageHandler and checkMockMessageHandler on BinaryMessenger to take a nullable handler, since the tests wanted to check to make sure a handler wasn't set, and that functionality no longer works if the handler is non-nullable.
1 parent ddab09f commit 58287ac

27 files changed

+350
-396
lines changed

dev/bots/firebase_testlab.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Use of this source code is governed by a BSD-style license that can be
44
# found in the LICENSE file.
55

6+
set -e
7+
68
# The tests to run on Firebase Test Lab.
79
# Currently, the test consists on building an Android App Bundle and ensuring
810
# that the app doesn't crash upon startup.
@@ -26,15 +28,19 @@ devices=(
2628
"model=griffin,version=24"
2729
)
2830

29-
set -e
30-
3131
GIT_REVISION=$(git rev-parse HEAD)
3232

3333
DEVICE_FLAG=""
3434
for device in ${devices[*]}; do
3535
DEVICE_FLAG+="--device $device "
3636
done
3737

38+
# If running tests locally, the key env var needs to be set.
39+
if [[ -z $GCLOUD_FIREBASE_TESTLAB_KEY ]]; then
40+
echo "Not running firebase test lab tests because GCLOUD_FIREBASE_TESTLAB_KEY isn't set."
41+
exit 0
42+
fi
43+
3844
# New contributors will not have permissions to run this test - they won't be
3945
# able to access the service account information. We should just mark the test
4046
# as passed - it will run fine on post submit, where it will still catch

packages/flutter/lib/src/services/binary_messenger.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ abstract class BinaryMessenger {
4848
///
4949
/// This method is useful for tests or test harnesses that want to assert the
5050
/// handler for the specified channel has not been altered by a previous test.
51+
///
52+
/// Passing null for the `handler` returns true if the handler for the
53+
/// `channel` is not set.
5154
bool checkMessageHandler(String channel, MessageHandler? handler);
5255

5356
/// Set a mock callback for intercepting messages from the [send] method on
@@ -69,6 +72,9 @@ abstract class BinaryMessenger {
6972
/// This method is useful for tests or test harnesses that want to assert the
7073
/// mock handler for the specified channel has not been altered by a previous
7174
/// test.
75+
///
76+
/// Passing null for the `handler` returns true if the handler for the
77+
/// `channel` is not set.
7278
bool checkMockMessageHandler(String channel, MessageHandler? handler);
7379
}
7480

packages/flutter/lib/src/services/platform_channel.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ class MethodChannel {
389389
///
390390
/// This method is useful for tests or test harnesses that want to assert the
391391
/// handler for the specified channel has not been altered by a previous test.
392+
///
393+
/// Passing null for the `handler` returns true if the handler for the channel
394+
/// is not set.
392395
bool checkMethodCallHandler(Future<dynamic> Function(MethodCall call)? handler) => _methodChannelHandlers[this] == handler;
393396

394397
/// Sets a mock callback for intercepting method invocations on this channel.
@@ -422,6 +425,9 @@ class MethodChannel {
422425
///
423426
/// This method is useful for tests or test harnesses that want to assert the
424427
/// handler for the specified channel has not been altered by a previous test.
428+
///
429+
/// Passing null for the `handler` returns true if the handler for the channel
430+
/// is not set.
425431
bool checkMockMethodCallHandler(Future<dynamic> Function(MethodCall call)? handler) => _methodChannelMockHandlers[this] == handler;
426432

427433
Future<ByteData?> _handleAsMethodCall(ByteData? message, Future<dynamic> handler(MethodCall call)) async {

packages/flutter/test/services/asset_bundle_test.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// @dart = 2.8
6-
75
import 'dart:convert';
86
import 'dart:typed_data';
97

@@ -41,7 +39,7 @@ void main() {
4139

4240
expect(bundle.loadCallCount['one'], 1);
4341

44-
Object loadException;
42+
late Object loadException;
4543
try {
4644
await bundle.loadString('foo');
4745
} catch (e) {
@@ -61,7 +59,7 @@ void main() {
6159
test('NetworkAssetBundle control test', () async {
6260
final Uri uri = Uri.http('example.org', '/path');
6361
final NetworkAssetBundle bundle = NetworkAssetBundle(uri);
64-
FlutterError error;
62+
late FlutterError error;
6563
try {
6664
await bundle.load('key');
6765
} on FlutterError catch (e) {

packages/flutter/test/services/autofill_test.dart

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// @dart = 2.8
6-
75
import 'dart:convert' show utf8;
86

97
import 'package:flutter/services.dart';
@@ -13,13 +11,12 @@ void main() {
1311
TestWidgetsFlutterBinding.ensureInitialized();
1412

1513
group('TextInput message channels', () {
16-
FakeTextChannel fakeTextChannel;
17-
FakeAutofillScope scope;
14+
late FakeTextChannel fakeTextChannel;
15+
final FakeAutofillScope scope = FakeAutofillScope();
1816

1917
setUp(() {
2018
fakeTextChannel = FakeTextChannel((MethodCall call) async {});
2119
TextInput.setChannel(fakeTextChannel);
22-
scope ??= FakeAutofillScope();
2320
scope.clients.clear();
2421
});
2522

@@ -28,35 +25,8 @@ void main() {
2825
TextInput.setChannel(SystemChannels.textInput);
2926
});
3027

31-
test('mandatory fields are mandatory', () async {
32-
AutofillConfiguration config;
33-
try {
34-
config = AutofillConfiguration(
35-
uniqueIdentifier: null,
36-
autofillHints: const <String>['test'],
37-
currentEditingValue: const TextEditingValue(),
38-
);
39-
} catch (e) {
40-
expect(e.toString(), contains('uniqueIdentifier != null'));
41-
}
42-
43-
expect(config, isNull);
44-
45-
try {
46-
config = AutofillConfiguration(
47-
uniqueIdentifier: 'id',
48-
autofillHints: null,
49-
currentEditingValue: const TextEditingValue(),
50-
);
51-
} catch (e) {
52-
expect(e.toString(), contains('autofillHints != null'));
53-
}
54-
55-
expect(config, isNull);
56-
});
57-
5828
test('throws if the hint list is empty', () async {
59-
Map<String, dynamic> json;
29+
Map<String, dynamic>? json;
6030
try {
6131
const AutofillConfiguration config = AutofillConfiguration(
6232
uniqueIdentifier: 'id',
@@ -113,7 +83,7 @@ void main() {
11383
]);
11484

11585
const TextEditingValue text2 = TextEditingValue(text: 'Text 2');
116-
fakeTextChannel.incoming(MethodCall(
86+
fakeTextChannel.incoming?.call(MethodCall(
11787
'TextInputClient.updateEditingStateWithTag',
11888
<dynamic>[0, <String, dynamic>{ client2.autofillId : text2.toJSON() }],
11989
));
@@ -130,7 +100,7 @@ class FakeAutofillClient implements TextInputClient, AutofillClient {
130100
String get autofillId => hashCode.toString();
131101

132102
@override
133-
TextInputConfiguration textInputConfiguration;
103+
late TextInputConfiguration textInputConfiguration;
134104

135105
@override
136106
void updateEditingValue(TextEditingValue newEditingValue) {
@@ -139,7 +109,7 @@ class FakeAutofillClient implements TextInputClient, AutofillClient {
139109
}
140110

141111
@override
142-
AutofillScope currentAutofillScope;
112+
AutofillScope? currentAutofillScope;
143113

144114
String latestMethodCall = '';
145115

@@ -179,7 +149,7 @@ class FakeAutofillScope with AutofillScopeMixin implements AutofillScope {
179149
Iterable<AutofillClient> get autofillClients => clients.values;
180150

181151
@override
182-
AutofillClient getAutofillClient(String autofillId) => clients[autofillId];
152+
AutofillClient getAutofillClient(String autofillId) => clients[autofillId]!;
183153

184154
void register(AutofillClient client) {
185155
clients.putIfAbsent(client.autofillId, () => client);
@@ -190,7 +160,7 @@ class FakeTextChannel implements MethodChannel {
190160
FakeTextChannel(this.outgoing) : assert(outgoing != null);
191161

192162
Future<dynamic> Function(MethodCall) outgoing;
193-
Future<void> Function(MethodCall) incoming;
163+
Future<void> Function(MethodCall)? incoming;
194164

195165
List<MethodCall> outgoingCalls = <MethodCall>[];
196166

@@ -217,18 +187,18 @@ class FakeTextChannel implements MethodChannel {
217187
String get name => 'flutter/textinput';
218188

219189
@override
220-
void setMethodCallHandler(Future<void> Function(MethodCall call) handler) {
190+
void setMethodCallHandler(Future<void> Function(MethodCall call)? handler) {
221191
incoming = handler;
222192
}
223193

224194
@override
225-
bool checkMethodCallHandler(Future<void> Function(MethodCall call) handler) => throw UnimplementedError();
195+
bool checkMethodCallHandler(Future<void> Function(MethodCall call)? handler) => throw UnimplementedError();
226196

227197
@override
228-
void setMockMethodCallHandler(Future<void> Function(MethodCall call) handler) => throw UnimplementedError();
198+
void setMockMethodCallHandler(Future<void> Function(MethodCall call)? handler) => throw UnimplementedError();
229199

230200
@override
231-
bool checkMockMethodCallHandler(Future<void> Function(MethodCall call) handler) => throw UnimplementedError();
201+
bool checkMockMethodCallHandler(Future<void> Function(MethodCall call)? handler) => throw UnimplementedError();
232202

233203
void validateOutgoingMethodCalls(List<MethodCall> calls) {
234204
expect(outgoingCalls.length, calls.length);

packages/flutter/test/services/binding_test.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// @dart = 2.8
6-
75
import 'dart:typed_data';
86

97
import 'package:flutter/foundation.dart';
@@ -45,7 +43,7 @@ class TestBinding extends BindingBase with SchedulerBinding, ServicesBinding {
4543
@override
4644
BinaryMessenger createBinaryMessenger() {
4745
return super.createBinaryMessenger()
48-
..setMockMessageHandler('flutter/assets', (ByteData message) async {
46+
..setMockMessageHandler('flutter/assets', (ByteData? message) async {
4947
if (const StringCodec().decodeMessage(message) == 'NOTICES') {
5048
return const StringCodec().encodeMessage(licenses);
5149
}

packages/flutter/test/services/channel_buffers_test.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// @dart = 2.8
6-
75
// TODO(yjbanov): enable Web when https://github.com/flutter/engine/pull/12747 rolls into the framework.
86
@TestOn('!chrome')
97

@@ -35,15 +33,15 @@ void main() {
3533
final TestChannelBuffersFlutterBinding binding = TestChannelBuffersFlutterBinding();
3634
expect(binding.defaultBinaryMessenger, isNotNull);
3735
bool didCallCallback = false;
38-
final ui.PlatformMessageResponseCallback callback = (ByteData responseData) {
36+
final ui.PlatformMessageResponseCallback callback = (ByteData? responseData) {
3937
didCallCallback = true;
4038
};
4139
const String payload = 'bar';
4240
final ByteData data = _makeByteData(payload);
4341
ui.channelBuffers.push(channel, data, callback);
4442
bool didDrainData = false;
45-
binding.defaultBinaryMessenger.setMessageHandler(channel, (ByteData message) async {
46-
expect(_getString(message), payload);
43+
binding.defaultBinaryMessenger.setMessageHandler(channel, (ByteData? message) async {
44+
expect(_getString(message!), payload);
4745
didDrainData = true;
4846
return null;
4947
});

packages/flutter/test/services/default_binary_messenger_test.dart

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// @dart = 2.8
6-
75
import 'dart:convert';
86
import 'dart:typed_data';
97
import 'dart:ui' as ui;
@@ -23,22 +21,21 @@ void main() {
2321
test('default binary messenger calls callback once', () async {
2422
int count = 0;
2523
const String channel = 'foo';
26-
ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
27-
channel, _makeByteData('bar'), (ByteData message) async {
24+
ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
25+
channel, _makeByteData('bar'), (ByteData? message) async {
2826
count += 1;
2927
});
3028
expect(count, equals(0));
3129
await ui.channelBuffers.drain(channel,
32-
(ByteData data, ui.PlatformMessageResponseCallback callback) {
30+
(ByteData? data, ui.PlatformMessageResponseCallback callback) async {
3331
callback(null);
34-
return null;
3532
});
3633
expect(count, equals(1));
3734
});
3835

3936
test('can check the handler', () {
40-
Future<ByteData> handler(ByteData call) => Future<ByteData>.value(null);
41-
final BinaryMessenger messenger = ServicesBinding.instance.defaultBinaryMessenger;
37+
Future<ByteData> handler(ByteData? call) => Future<ByteData>.value(null);
38+
final BinaryMessenger messenger = ServicesBinding.instance!.defaultBinaryMessenger;
4239

4340
expect(messenger.checkMessageHandler('test_channel', null), true);
4441
expect(messenger.checkMessageHandler('test_channel', handler), false);
@@ -48,8 +45,8 @@ void main() {
4845
});
4946

5047
test('can check the mock handler', () {
51-
Future<ByteData> handler(ByteData call) => Future<ByteData>.value(null);
52-
final BinaryMessenger messenger = ServicesBinding.instance.defaultBinaryMessenger;
48+
Future<ByteData> handler(ByteData? call) => Future<ByteData>.value(null);
49+
final BinaryMessenger messenger = ServicesBinding.instance!.defaultBinaryMessenger;
5350

5451
expect(messenger.checkMockMessageHandler('test_channel', null), true);
5552
expect(messenger.checkMockMessageHandler('test_channel', handler), false);

0 commit comments

Comments
 (0)