Flutter Engine
The Flutter Engine
fl_event_channel_test.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Included first as it collides with the X11 headers.
6#include "gtest/gtest.h"
7
8#include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
9#include "flutter/shell/platform/linux/fl_engine_private.h"
10#include "flutter/shell/platform/linux/fl_method_codec_private.h"
11#include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
12#include "flutter/shell/platform/linux/public/flutter_linux/fl_event_channel.h"
13#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
14#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"
15#include "flutter/shell/platform/linux/testing/mock_renderer.h"
16
17// Data passed in tests.
18typedef struct {
19 GMainLoop* loop;
20 int count;
21} TestData;
22
23// Creates a mock engine that responds to platform messages.
24static FlEngine* make_mock_engine() {
25 g_autoptr(FlDartProject) project = fl_dart_project_new();
26 g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new();
27 g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer));
28 g_autoptr(GError) engine_error = nullptr;
29 EXPECT_TRUE(fl_engine_start(engine, &engine_error));
30 EXPECT_EQ(engine_error, nullptr);
31
32 return static_cast<FlEngine*>(g_object_ref(engine));
33}
34
35// Triggers the engine to start listening to the channel.
36static void listen_channel(FlBinaryMessenger* messenger, FlValue* args) {
37 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
38 g_autoptr(FlMethodChannel) channel = fl_method_channel_new(
39 messenger, "test/standard-method", FL_METHOD_CODEC(codec));
40
41 // Trigger the engine to make a method call.
42 g_autoptr(FlValue) invoke_args = fl_value_new_list();
43 fl_value_append_take(invoke_args, fl_value_new_string("test/standard-event"));
44 fl_value_append_take(invoke_args, fl_value_new_string("listen"));
45 g_autoptr(FlValue) value =
46 args != nullptr ? fl_value_ref(args) : fl_value_new_null();
47 fl_value_append(invoke_args, value);
48 fl_method_channel_invoke_method(channel, "InvokeMethod", invoke_args, nullptr,
49 nullptr, nullptr);
50}
51
52// Triggers the engine to cancel the subscription to the channel.
53static void cancel_channel(FlBinaryMessenger* messenger, FlValue* args) {
54 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
55 g_autoptr(FlMethodChannel) channel = fl_method_channel_new(
56 messenger, "test/standard-method", FL_METHOD_CODEC(codec));
57
58 // Trigger the engine to make a method call.
59 g_autoptr(FlValue) invoke_args = fl_value_new_list();
60 fl_value_append_take(invoke_args, fl_value_new_string("test/standard-event"));
61 fl_value_append_take(invoke_args, fl_value_new_string("cancel"));
62 g_autoptr(FlValue) value =
63 args != nullptr ? fl_value_ref(args) : fl_value_new_null();
64 fl_value_append(invoke_args, value);
65 fl_method_channel_invoke_method(channel, "InvokeMethod", invoke_args, nullptr,
66 nullptr, nullptr);
67}
68
69// Called when the remote end starts listening on the channel.
70static FlMethodErrorResponse* listen_listen_cb(FlEventChannel* channel,
72 gpointer user_data) {
74
75 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
76
77 return nullptr;
78}
79
80// Checks we detect a listen event.
81TEST(FlEventChannelTest, Listen) {
82 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
83
84 g_autoptr(FlEngine) engine = make_mock_engine();
85 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
86 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
87 FlEventChannel* channel = fl_event_channel_new(
88 messenger, "test/standard-event", FL_METHOD_CODEC(codec));
90 nullptr);
91
92 listen_channel(messenger, nullptr);
93
94 // Blocks here until listen_listen_cb called.
95 g_main_loop_run(loop);
96
97 // Manually unref because the compiler complains 'channel' is unused.
98 g_object_unref(channel);
99}
100
101// Called when the remote end starts listening on the channel.
102static FlMethodErrorResponse* listen_exception_listen_cb(
103 FlEventChannel* channel,
104 FlValue* args,
105 gpointer user_data) {
106 return fl_method_error_response_new("LISTEN-ERROR", "LISTEN-ERROR-MESSAGE",
107 nullptr);
108}
109
110// Called when a the test engine notifies us what response we sent in the
111// ListenException test.
113 FlBinaryMessenger* messenger,
114 const gchar* channel,
115 GBytes* message,
116 FlBinaryMessengerResponseHandle* response_handle,
117 gpointer user_data) {
118 fl_binary_messenger_send_response(messenger, response_handle, nullptr,
119 nullptr);
120
121 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
122 g_autoptr(GError) error = nullptr;
123 g_autoptr(FlMethodResponse) response =
124 fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
125 EXPECT_NE(response, nullptr);
126 EXPECT_EQ(error, nullptr);
127
128 EXPECT_TRUE(FL_IS_METHOD_ERROR_RESPONSE(response));
129 EXPECT_STREQ(
130 fl_method_error_response_get_code(FL_METHOD_ERROR_RESPONSE(response)),
131 "LISTEN-ERROR");
132 EXPECT_STREQ(
133 fl_method_error_response_get_message(FL_METHOD_ERROR_RESPONSE(response)),
134 "LISTEN-ERROR-MESSAGE");
135
136 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
137}
138
139// Checks we can generate a listen exception.
140TEST(FlEventChannelTest, ListenException) {
141 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
142
143 g_autoptr(FlEngine) engine = make_mock_engine();
144 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
145 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
146 FlEventChannel* channel = fl_event_channel_new(
147 messenger, "test/standard-event", FL_METHOD_CODEC(codec));
149 nullptr, loop, nullptr);
150
151 // Listen for response to the engine.
153 messenger, "test/responses", listen_exception_response_cb, loop, nullptr);
154
155 listen_channel(messenger, nullptr);
156
157 // Blocks here until listen_exception_response_cb called.
158 g_main_loop_run(loop);
159
160 // Manually unref because the compiler complains 'channel' is unused.
161 g_object_unref(channel);
162}
163
164// Called when the remote end cancels their subscription.
165static FlMethodErrorResponse* cancel_cancel_cb(FlEventChannel* channel,
166 FlValue* args,
167 gpointer user_data) {
169
170 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
171
172 return nullptr;
173}
174
175// Checks we detect a cancel event.
176TEST(FlEventChannelTest, Cancel) {
177 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
178
179 g_autoptr(FlEngine) engine = make_mock_engine();
180 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
181 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
182 FlEventChannel* channel = fl_event_channel_new(
183 messenger, "test/standard-event", FL_METHOD_CODEC(codec));
185 nullptr);
186
187 listen_channel(messenger, nullptr);
188 cancel_channel(messenger, nullptr);
189
190 // Blocks here until cancel_cancel_cb called.
191 g_main_loop_run(loop);
192
193 // Manually unref because the compiler complains 'channel' is unused.
194 g_object_unref(channel);
195}
196
197// Called when the remote end cancels their subscription.
198static FlMethodErrorResponse* cancel_exception_cancel_cb(
199 FlEventChannel* channel,
200 FlValue* args,
201 gpointer user_data) {
202 return fl_method_error_response_new("CANCEL-ERROR", "CANCEL-ERROR-MESSAGE",
203 nullptr);
204}
205
206// Called when a the test engine notifies us what response we sent in the
207// CancelException test.
209 FlBinaryMessenger* messenger,
210 const gchar* channel,
211 GBytes* message,
212 FlBinaryMessengerResponseHandle* response_handle,
213 gpointer user_data) {
214 TestData* data = static_cast<TestData*>(user_data);
215
216 fl_binary_messenger_send_response(messenger, response_handle, nullptr,
217 nullptr);
218
219 data->count++;
220 if (data->count == 2) {
221 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
222 g_autoptr(GError) error = nullptr;
223 g_autoptr(FlMethodResponse) response = fl_method_codec_decode_response(
224 FL_METHOD_CODEC(codec), message, &error);
225 EXPECT_NE(response, nullptr);
226 EXPECT_EQ(error, nullptr);
227
228 EXPECT_TRUE(FL_IS_METHOD_ERROR_RESPONSE(response));
229 EXPECT_STREQ(
230 fl_method_error_response_get_code(FL_METHOD_ERROR_RESPONSE(response)),
231 "CANCEL-ERROR");
233 FL_METHOD_ERROR_RESPONSE(response)),
234 "CANCEL-ERROR-MESSAGE");
235
236 g_main_loop_quit(data->loop);
237 }
238}
239
240// Checks we can generate a cancel exception.
241TEST(FlEventChannelTest, CancelException) {
242 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
244 data.loop = loop;
245 data.count = 0;
246
247 g_autoptr(FlEngine) engine = make_mock_engine();
248 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
249 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
250 FlEventChannel* channel = fl_event_channel_new(
251 messenger, "test/standard-event", FL_METHOD_CODEC(codec));
253 channel, nullptr, cancel_exception_cancel_cb, &data, nullptr);
254
255 // Listen for response to the engine.
257 messenger, "test/responses", cancel_exception_response_cb, &data,
258 nullptr);
259
260 listen_channel(messenger, nullptr);
261 cancel_channel(messenger, nullptr);
262
263 // Blocks here until cancel_exception_response_cb called.
264 g_main_loop_run(loop);
265
266 // Manually unref because the compiler complains 'channel' is unused.
267 g_object_unref(channel);
268}
269
270// Called when the remote end starts listening on the channel.
271static FlMethodErrorResponse* args_listen_cb(FlEventChannel* channel,
272 FlValue* args,
273 gpointer user_data) {
274 g_autoptr(FlValue) expected_args = fl_value_new_string("LISTEN-ARGS");
275 EXPECT_TRUE(fl_value_equal(args, expected_args));
276
277 return nullptr;
278}
279
280// Called when the remote end cancels their subscription.
281static FlMethodErrorResponse* args_cancel_cb(FlEventChannel* channel,
282 FlValue* args,
283 gpointer user_data) {
284 g_autoptr(FlValue) expected_args = fl_value_new_string("CANCEL-ARGS");
285 EXPECT_TRUE(fl_value_equal(args, expected_args));
286
287 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
288
289 return nullptr;
290}
291
292// Checks args are passed to listen/cancel.
293TEST(FlEventChannelTest, Args) {
294 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
295
296 g_autoptr(FlEngine) engine = make_mock_engine();
297 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
298 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
299 FlEventChannel* channel = fl_event_channel_new(
300 messenger, "test/standard-event", FL_METHOD_CODEC(codec));
302 loop, nullptr);
303
304 g_autoptr(FlValue) listen_args = fl_value_new_string("LISTEN-ARGS");
305 listen_channel(messenger, listen_args);
306 g_autoptr(FlValue) cancel_args = fl_value_new_string("CANCEL-ARGS");
307 cancel_channel(messenger, cancel_args);
308
309 // Blocks here until args_cancel_cb called.
310 g_main_loop_run(loop);
311
312 // Manually unref because the compiler complains 'channel' is unused.
313 g_object_unref(channel);
314}
315
316// Called when the remote end starts listening on the channel.
317static FlMethodErrorResponse* send_events_listen_cb(FlEventChannel* channel,
318 FlValue* args,
319 gpointer user_data) {
320 // Send some events.
321 for (int i = 0; i < 5; i++) {
322 g_autoptr(FlValue) event = fl_value_new_int(i);
323 g_autoptr(GError) error = nullptr;
324 EXPECT_TRUE(fl_event_channel_send(channel, event, nullptr, &error));
325 EXPECT_EQ(error, nullptr);
326 }
327
328 return nullptr;
329}
330
331// Called when a the test engine notifies us what event we sent in the
332// Test test.
334 FlBinaryMessenger* messenger,
335 const gchar* channel,
336 GBytes* message,
337 FlBinaryMessengerResponseHandle* response_handle,
338 gpointer user_data) {
339 TestData* data = static_cast<TestData*>(user_data);
340
341 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
342 g_autoptr(GError) error = nullptr;
343 g_autoptr(FlMethodResponse) response =
344 fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
345 EXPECT_NE(response, nullptr);
346 EXPECT_EQ(error, nullptr);
347
349 EXPECT_NE(result, nullptr);
350 EXPECT_EQ(error, nullptr);
351
353 EXPECT_EQ(fl_value_get_int(result), data->count);
354 data->count++;
355
356 fl_binary_messenger_send_response(messenger, response_handle, nullptr,
357 nullptr);
358
359 // Got all the results!
360 if (data->count == 5) {
361 g_main_loop_quit(data->loop);
362 }
363}
364
365// Checks can send events.
366TEST(FlEventChannelTest, Test) {
367 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
369 data.loop = loop;
370 data.count = 0;
371
372 g_autoptr(FlEngine) engine = make_mock_engine();
373 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
374 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
375 FlEventChannel* channel = fl_event_channel_new(
376 messenger, "test/standard-event", FL_METHOD_CODEC(codec));
378 &data, nullptr);
379
380 // Listen for events from the engine.
382 messenger, "test/events", send_events_events_cb, &data, nullptr);
383
384 listen_channel(messenger, nullptr);
385 cancel_channel(messenger, nullptr);
386
387 // Blocks here until send_events_events_cb receives the last event.
388 g_main_loop_run(loop);
389
390 // Manually unref because the compiler complains 'channel' is unused.
391 g_object_unref(channel);
392}
393
394// Check can register an event channel with the same name as one previously
395// used.
396TEST(FlEventChannelTest, ReuseChannel) {
397 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
399 data.loop = loop;
400 data.count = 0;
401
402 // Register an event channel.
403 g_autoptr(FlEngine) engine = make_mock_engine();
404 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
405 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
406 FlEventChannel* channel1 = fl_event_channel_new(
407 messenger, "test/standard-event", FL_METHOD_CODEC(codec));
409 &data, nullptr);
410
411 // Remove this channel
412 g_object_unref(channel1);
413
414 // Register a second channel with the same name.
415 g_autoptr(FlEventChannel) channel2 = fl_event_channel_new(
416 messenger, "test/standard-event", FL_METHOD_CODEC(codec));
418 &data, nullptr);
419
420 // Listen for events from the engine.
422 messenger, "test/events", send_events_events_cb, &data, nullptr);
423
424 listen_channel(messenger, nullptr);
425 cancel_channel(messenger, nullptr);
426
427 // Blocks here until send_events_events_cb receives the last event.
428 g_main_loop_run(loop);
429}
430
431// Check can register an event channel replacing an existing one.
432TEST(FlEventChannelTest, ReplaceChannel) {
433 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
435 data.loop = loop;
436 data.count = 0;
437
438 // Register an event channel.
439 g_autoptr(FlEngine) engine = make_mock_engine();
440 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
441 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
442 FlEventChannel* channel1 = fl_event_channel_new(
443 messenger, "test/standard-event", FL_METHOD_CODEC(codec));
445 &data, nullptr);
446
447 // Register a second channel with the same name.
448 g_autoptr(FlEventChannel) channel2 = fl_event_channel_new(
449 messenger, "test/standard-event", FL_METHOD_CODEC(codec));
451 &data, nullptr);
452
453 // Listen for events from the engine.
455 messenger, "test/events", send_events_events_cb, &data, nullptr);
456
457 listen_channel(messenger, nullptr);
458 cancel_channel(messenger, nullptr);
459
460 // Blocks here until send_events_events_cb receives the last event.
461 g_main_loop_run(loop);
462}
FlutterEngine engine
Definition: main.cc:68
G_MODULE_EXPORT void fl_binary_messenger_set_message_handler_on_channel(FlBinaryMessenger *self, const gchar *channel, FlBinaryMessengerMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()
FlEngine * fl_engine_new(FlDartProject *project, FlRenderer *renderer)
Definition: fl_engine.cc:455
gboolean fl_engine_start(FlEngine *self, GError **error)
Definition: fl_engine.cc:471
G_MODULE_EXPORT gboolean fl_event_channel_send(FlEventChannel *self, FlValue *event, GCancellable *cancellable, GError **error)
G_MODULE_EXPORT void fl_event_channel_set_stream_handlers(FlEventChannel *self, FlEventChannelHandler listen_handler, FlEventChannelHandler cancel_handler, gpointer user_data, GDestroyNotify destroy_notify)
G_MODULE_EXPORT FlEventChannel * fl_event_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static FlMethodErrorResponse * send_events_listen_cb(FlEventChannel *channel, FlValue *args, gpointer user_data)
static void cancel_exception_response_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
static FlMethodErrorResponse * args_cancel_cb(FlEventChannel *channel, FlValue *args, gpointer user_data)
TEST(FlEventChannelTest, Listen)
static void listen_channel(FlBinaryMessenger *messenger, FlValue *args)
static FlMethodErrorResponse * listen_exception_listen_cb(FlEventChannel *channel, FlValue *args, gpointer user_data)
static void send_events_events_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
static FlMethodErrorResponse * listen_listen_cb(FlEventChannel *channel, FlValue *args, gpointer user_data)
static FlMethodErrorResponse * args_listen_cb(FlEventChannel *channel, FlValue *args, gpointer user_data)
static void listen_exception_response_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
static FlMethodErrorResponse * cancel_exception_cancel_cb(FlEventChannel *channel, FlValue *args, gpointer user_data)
static FlEngine * make_mock_engine()
static void cancel_channel(FlBinaryMessenger *messenger, FlValue *args)
static FlMethodErrorResponse * cancel_cancel_cb(FlEventChannel *channel, FlValue *args, gpointer user_data)
FlKeyEvent * event
G_MODULE_EXPORT FlMethodChannel * fl_method_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
G_MODULE_EXPORT void fl_method_channel_invoke_method(FlMethodChannel *self, const gchar *method, FlValue *args, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
FlMethodResponse * fl_method_codec_decode_response(FlMethodCodec *self, GBytes *message, GError **error)
G_MODULE_EXPORT FlValue * fl_method_response_get_result(FlMethodResponse *self, GError **error)
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)
G_MODULE_EXPORT const gchar * fl_method_error_response_get_message(FlMethodErrorResponse *self)
G_MODULE_EXPORT const gchar * fl_method_error_response_get_code(FlMethodErrorResponse *self)
const uint8_t uint32_t uint32_t GError ** error
uint8_t value
G_MODULE_EXPORT FlStandardMethodCodec * fl_standard_method_codec_new()
GAsyncResult * result
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:394
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:668
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:251
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
G_MODULE_EXPORT void fl_value_append(FlValue *self, FlValue *value)
Definition: fl_value.cc:592
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:262
G_MODULE_EXPORT void fl_value_append_take(FlValue *self, FlValue *value)
Definition: fl_value.cc:600
G_MODULE_EXPORT FlValue * fl_value_new_list()
Definition: fl_value.cc:349
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:471
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:65
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:67
Win32Message message
FlMockRenderer * fl_mock_renderer_new(FlMockRendererGetRefreshRate get_refresh_rate)
GMainLoop * loop
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
void * user_data
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678