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

Prevent a potential infinite loop. (#66073)

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