Unverified Commit cb232dda authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Prevent a potential infinite loop. (#66073)

parent a029e172
...@@ -309,13 +309,14 @@ class _DefaultBinaryMessenger extends BinaryMessenger { ...@@ -309,13 +309,14 @@ class _DefaultBinaryMessenger extends BinaryMessenger {
@override @override
void setMessageHandler(String channel, MessageHandler? handler) { void setMessageHandler(String channel, MessageHandler? handler) {
if (handler == null) if (handler == null) {
_handlers.remove(channel); _handlers.remove(channel);
else } else {
_handlers[channel] = handler; _handlers[channel] = handler;
ui.channelBuffers.drain(channel, (ByteData? data, ui.PlatformMessageResponseCallback callback) async { ui.channelBuffers.drain(channel, (ByteData? data, ui.PlatformMessageResponseCallback callback) async {
await handlePlatformMessage(channel, data, callback); await handlePlatformMessage(channel, data, callback);
}); });
}
} }
@override @override
......
...@@ -25,6 +25,7 @@ void main() { ...@@ -25,6 +25,7 @@ void main() {
final String reply = await channel.send('hello'); final String reply = await channel.send('hello');
expect(reply, equals('hello world')); expect(reply, equals('hello world'));
}); });
test('can receive string message and send reply', () async { test('can receive string message and send reply', () async {
channel.setMessageHandler((String message) async => message + ' world'); channel.setMessageHandler((String message) async => message + ' world');
String reply; String reply;
...@@ -170,13 +171,25 @@ void main() { ...@@ -170,13 +171,25 @@ void main() {
expect(result, isNull); expect(result, isNull);
}); });
test('can handle method call with no registered plugin', () async { test('can handle method call with no registered plugin (setting before)', () async {
channel.setMethodCallHandler(null); channel.setMethodCallHandler(null);
final ByteData call = jsonMethod.encodeMethodCall(const MethodCall('sayHello', 'hello')); final ByteData call = jsonMethod.encodeMethodCall(const MethodCall('sayHello', 'hello'));
ByteData envelope; ByteData envelope;
await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData result) { await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData result) {
envelope = result; envelope = result;
}); });
await null; // just in case there's something async happening
expect(envelope, isNull);
});
test('can handle method call with no registered plugin (setting after)', () async {
final ByteData call = jsonMethod.encodeMethodCall(const MethodCall('sayHello', 'hello'));
ByteData envelope;
await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage('ch7', call, (ByteData result) {
envelope = result;
});
channel.setMethodCallHandler(null);
await null; // just in case there's something async happening
expect(envelope, isNull); expect(envelope, isNull);
}); });
...@@ -262,6 +275,7 @@ void main() { ...@@ -262,6 +275,7 @@ void main() {
expect(channel.checkMockMethodCallHandler(handler), true); expect(channel.checkMockMethodCallHandler(handler), true);
}); });
}); });
group('EventChannel', () { group('EventChannel', () {
const MessageCodec<dynamic> jsonMessage = JSONMessageCodec(); const MessageCodec<dynamic> jsonMessage = JSONMessageCodec();
const MethodCodec jsonMethod = JSONMethodCodec(); const MethodCodec jsonMethod = JSONMethodCodec();
...@@ -298,6 +312,7 @@ void main() { ...@@ -298,6 +312,7 @@ void main() {
await Future<void>.delayed(Duration.zero); await Future<void>.delayed(Duration.zero);
expect(canceled, isTrue); expect(canceled, isTrue);
}); });
test('can receive error event', () async { test('can receive error event', () async {
ServicesBinding.instance.defaultBinaryMessenger.setMockMessageHandler( ServicesBinding.instance.defaultBinaryMessenger.setMockMessageHandler(
'ch', 'ch',
......
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