Allow event channels to reuse names (flutter/engine#41837)

https://github.com/flutter/flutter/issues/126209

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide] and the [C++,
Objective-C, Java style guides].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I added new tests to check the change I am making or feature I am
adding, or Hixie said the PR is test-exempt. See [testing the engine]
for instructions on writing and running engine tests.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I signed the [CLA].
- [x] All existing and new tests are passing.
This commit is contained in:
Robert Ancell
2023-06-26 14:05:34 +12:00
committed by GitHub
parent 5ad007bb80
commit 2e4569257b
2 changed files with 75 additions and 1 deletions

View File

@@ -18,6 +18,9 @@ struct _FlEventChannel {
// Messenger to communicate on.
FlBinaryMessenger* messenger;
// TRUE if the channel has been closed.
gboolean channel_closed;
// Channel name.
gchar* name;
@@ -129,13 +132,14 @@ static void remove_handlers(FlEventChannel* self) {
// Called when the channel handler is closed.
static void channel_closed_cb(gpointer user_data) {
g_autoptr(FlEventChannel) self = FL_EVENT_CHANNEL(user_data);
self->channel_closed = TRUE;
remove_handlers(self);
}
static void fl_event_channel_dispose(GObject* object) {
FlEventChannel* self = FL_EVENT_CHANNEL(object);
if (self->messenger != nullptr) {
if (!self->channel_closed) {
fl_binary_messenger_set_message_handler_on_channel(
self->messenger, self->name, nullptr, nullptr, nullptr);
}

View File

@@ -390,3 +390,73 @@ TEST(FlEventChannelTest, Test) {
// Manually unref because the compiler complains 'channel' is unused.
g_object_unref(channel);
}
// Check can register an event channel with the same name as one previously
// used.
TEST(FlEventChannelTest, ReuseChannel) {
g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
TestData data;
data.loop = loop;
data.count = 0;
// Register an event channel.
g_autoptr(FlEngine) engine = make_mock_engine();
FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
FlEventChannel* channel1 = fl_event_channel_new(
messenger, "test/standard-event", FL_METHOD_CODEC(codec));
fl_event_channel_set_stream_handlers(channel1, send_events_listen_cb, nullptr,
&data, nullptr);
// Remove this channel
g_object_unref(channel1);
// Register a second channel with the same name.
g_autoptr(FlEventChannel) channel2 = fl_event_channel_new(
messenger, "test/standard-event", FL_METHOD_CODEC(codec));
fl_event_channel_set_stream_handlers(channel2, send_events_listen_cb, nullptr,
&data, nullptr);
// Listen for events from the engine.
fl_binary_messenger_set_message_handler_on_channel(
messenger, "test/events", send_events_events_cb, &data, nullptr);
listen_channel(messenger, nullptr);
cancel_channel(messenger, nullptr);
// Blocks here until send_events_events_cb receives the last event.
g_main_loop_run(loop);
}
// Check can register an event channel replacing an existing one.
TEST(FlEventChannelTest, ReplaceChannel) {
g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
TestData data;
data.loop = loop;
data.count = 0;
// Register an event channel.
g_autoptr(FlEngine) engine = make_mock_engine();
FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
FlEventChannel* channel1 = fl_event_channel_new(
messenger, "test/standard-event", FL_METHOD_CODEC(codec));
fl_event_channel_set_stream_handlers(channel1, send_events_listen_cb, nullptr,
&data, nullptr);
// Register a second channel with the same name.
g_autoptr(FlEventChannel) channel2 = fl_event_channel_new(
messenger, "test/standard-event", FL_METHOD_CODEC(codec));
fl_event_channel_set_stream_handlers(channel2, send_events_listen_cb, nullptr,
&data, nullptr);
// Listen for events from the engine.
fl_binary_messenger_set_message_handler_on_channel(
messenger, "test/events", send_events_events_cb, &data, nullptr);
listen_channel(messenger, nullptr);
cancel_channel(messenger, nullptr);
// Blocks here until send_events_events_cb receives the last event.
g_main_loop_run(loop);
}