| Flutter Linux Embedder |

Classes | Functions
fl_binary_messenger_test.cc File Reference
`#include "gtest/gtest.h"`
`#include <pthread.h>`
`#include <cstring>`
`#include "flutter/shell/platform/embedder/embedder.h"`
`#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"`
`#include "flutter/shell/platform/linux/fl_binary_messenger_private.h"`
`#include "flutter/shell/platform/linux/fl_engine_private.h"`
`#include "flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h"`
`#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"`
`#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"`
`#include "flutter/shell/platform/linux/testing/fl_test.h"`
`#include "flutter/shell/platform/linux/testing/mock_binary_messenger_response_handle.h"`
`#include "flutter/shell/platform/linux/testing/mock_renderer.h"`

Go to the source code of this file.

## Classes
struct | RespondsOnBackgroundThreadInfo

## Functions
| G_DECLARE_FINAL_TYPE (FlFakeBinaryMessenger, fl_fake_binary_messenger, FL, FAKE_BINARY_MESSENGER, GObject) struct _FlFakeBinaryMessenger
static void | fl_fake_binary_messenger_iface_init (FlBinaryMessengerInterface *iface)
| G_DEFINE_TYPE_WITH_CODE (FlFakeBinaryMessenger, fl_fake_binary_messenger, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(fl_binary_messenger_get_type(), fl_fake_binary_messenger_iface_init)) static void fl_fake_binary_messenger_class_init(FlFakeBinaryMessengerClass *klass)
static gboolean | send_message_cb (gpointer user_data)
static void | set_message_handler_on_channel (FlBinaryMessenger *messenger, const gchar *channel, FlBinaryMessengerMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
static gboolean | send_response (FlBinaryMessenger *messenger, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
static gboolean | send_ready_cb (gpointer user_data)
static void | send_on_channel (FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
static GBytes * | send_on_channel_finish (FlBinaryMessenger *messenger, GAsyncResult *result, GError **error)
static void | resize_channel (FlBinaryMessenger *messenger, const gchar *channel, int64_t new_size)
static void | set_warns_on_channel_overflow (FlBinaryMessenger *messenger, const gchar *channel, bool warns)
static void | fl_fake_binary_messenger_init (FlFakeBinaryMessenger *self)
static FlBinaryMessenger * | fl_fake_binary_messenger_new (GMainLoop *loop)
static void | fake_response_cb (GObject *object, GAsyncResult *result, gpointer user_data)
| TEST (FlBinaryMessengerTest, FakeMessengerSend)
static void | fake_message_cb (FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
| TEST (FlBinaryMessengerTest, FakeMessengerReceive)
| TEST (FlBinaryMessengerTest, SendNullptrMessage)
| TEST (FlBinaryMessengerTest, SendEmptyMessage)
static void | echo_response_cb (GObject *object, GAsyncResult *result, gpointer user_data)
| TEST (FlBinaryMessengerTest, SendMessage)
static void | nullptr_response_cb (GObject *object, GAsyncResult *result, gpointer user_data)
| TEST (FlBinaryMessengerTest, NullptrResponse)
static void | failure_response_cb (GObject *object, GAsyncResult *result, gpointer user_data)
| TEST (FlBinaryMessengerTest, SendFailure)
static void | message_cb (FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
static void | response_cb (FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
| TEST (FlBinaryMessengerTest, ReceiveMessage)
| TEST (FlBinaryMessengerTest, ResizeChannel)
| TEST (FlBinaryMessengerTest, WarnsOnOverflowChannel)
static gboolean | quit_main_loop_cb (gpointer user_data)
| TEST (FlBinaryMessengerTest, ControlChannelErrorResponse)
static gboolean | cleanup_responds_on_background_thread_info (gpointer user_data)
static void * | response_from_thread_main (void *user_data)
static void | response_from_thread_cb (FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
| TEST (FlBinaryMessengerTest, RespondOnBackgroundThread)
static void | kill_handler_notify_cb (gpointer was_called)
| TEST (FlBinaryMessengerTest, DeletingEngineClearsHandlers)

## Function Documentation

## ◆ cleanup_responds_on_background_thread_info()

static gboolean cleanup_responds_on_background_thread_info | ( | gpointer | user_data | ) |
| static

Definition at line 562 of file fl_binary_messenger_test.cc.

562 {
563 RespondsOnBackgroundThreadInfo* info =
564 static_cast<RespondsOnBackgroundThreadInfo*>(user_data);
565 GMainLoop* loop = info->loop;
566
567 g_object_unref(info->messenger);
568 g_object_unref(info->response_handle);
569 free(info);
570
571 g_main_loop_quit(static_cast<GMainLoop*>(loop));
572
573 return G_SOURCE_REMOVE;
574 }

References RespondsOnBackgroundThreadInfo::loop, RespondsOnBackgroundThreadInfo::messenger, RespondsOnBackgroundThreadInfo::response_handle, and user_data.

Referenced by response_from_thread_main().

## ◆ echo_response_cb()

static void echo_response_cb | ( | GObject * | object,
|  | GAsyncResult * | result,
|  | gpointer | user_data
| ) |  |
| static

Definition at line 253 of file fl_binary_messenger_test.cc.

255 {
256 g_autoptr(GError) error = nullptr;
257 g_autoptr(GBytes) message = fl_binary_messenger_send_on_channel_finish(
258 FL_BINARY_MESSENGER(object), result, &error);
259 EXPECT_NE(message, nullptr);
260 EXPECT_EQ(error, nullptr);
261
262 g_autofree gchar* text =
263 g_strndup(static_cast<const gchar*>(g_bytes_get_data(message, nullptr)),
264 g_bytes_get_size(message));
265 EXPECT_STREQ(text, "Hello World!");
266
267 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
268 }

References error, fl_binary_messenger_send_on_channel_finish(), result, and user_data.

Referenced by TEST().

## ◆ failure_response_cb()

static void failure_response_cb | ( | GObject * | object,
|  | GAsyncResult * | result,
|  | gpointer | user_data
| ) |  |
| static

Definition at line 317 of file fl_binary_messenger_test.cc.

319 {
320 g_autoptr(GError) error = nullptr;
321 g_autoptr(GBytes) message = fl_binary_messenger_send_on_channel_finish(
322 FL_BINARY_MESSENGER(object), result, &error);
323 EXPECT_EQ(message, nullptr);
324 EXPECT_NE(error, nullptr);
325
326 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
327 }

References error, fl_binary_messenger_send_on_channel_finish(), result, and user_data.

Referenced by TEST().

## ◆ fake_message_cb()

static void fake_message_cb | ( | FlBinaryMessenger * | messenger,
|  | const gchar * | channel,
|  | GBytes * | message,
|  | FlBinaryMessengerResponseHandle * | response_handle,
|  | gpointer | user_data
| ) |  |
| static

Definition at line 199 of file fl_binary_messenger_test.cc.

203 {
204 EXPECT_STREQ(channel, "CHANNEL");
205
206 EXPECT_NE(message, nullptr);
207 g_autofree gchar* text =
208 g_strndup(static_cast<const gchar*>(g_bytes_get_data(message, nullptr)),
209 g_bytes_get_size(message));
210 EXPECT_STREQ(text, "Marco!");
211
212 const char* response_text = "Polo!";
213 g_autoptr(GBytes) response =
214 g_bytes_new(response_text, strlen(response_text));
215 g_autoptr(GError) error = nullptr;
216 EXPECT_TRUE(fl_binary_messenger_send_response(messenger, response_handle,
217 response, &error));
218 EXPECT_EQ(error, nullptr);
219 }

References error, and fl_binary_messenger_send_response().

Referenced by TEST().

## ◆ fake_response_cb()

static void fake_response_cb | ( | GObject * | object,
|  | GAsyncResult * | result,
|  | gpointer | user_data
| ) |  |
| static

Definition at line 165 of file fl_binary_messenger_test.cc.

167 {
168 g_autoptr(GError) error = nullptr;
169 g_autoptr(GBytes) message = fl_binary_messenger_send_on_channel_finish(
170 FL_BINARY_MESSENGER(object), result, &error);
171 EXPECT_NE(message, nullptr);
172 EXPECT_EQ(error, nullptr);
173
174 g_autofree gchar* text =
175 g_strndup(static_cast<const gchar*>(g_bytes_get_data(message, nullptr)),
176 g_bytes_get_size(message));
177 EXPECT_STREQ(text, "Polo!");
178
179 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
180 }

References error, fl_binary_messenger_send_on_channel_finish(), result, and user_data.

Referenced by TEST().

## ◆ fl_fake_binary_messenger_iface_init()

static void fl_fake_binary_messenger_iface_init | ( | FlBinaryMessengerInterface * | iface | ) |
| static

Definition at line 145 of file fl_binary_messenger_test.cc.

146 {
147 iface->set_message_handler_on_channel = set_message_handler_on_channel;
148 iface->send_response = send_response;
149 iface->send_on_channel = send_on_channel;
150 iface->send_on_channel_finish = send_on_channel_finish;
151 iface->resize_channel = resize_channel;
152 iface->set_warns_on_channel_overflow = set_warns_on_channel_overflow;
153 }

References resize_channel(), send_on_channel(), send_on_channel_finish(), send_response(), set_message_handler_on_channel(), and set_warns_on_channel_overflow().

## ◆ fl_fake_binary_messenger_init()

static void fl_fake_binary_messenger_init | ( | FlFakeBinaryMessenger * | self | ) |
| static

Definition at line 155 of file fl_binary_messenger_test.cc.

155 {}

## ◆ fl_fake_binary_messenger_new()

static FlBinaryMessenger* fl_fake_binary_messenger_new | ( | GMainLoop * | loop | ) |
| static

Definition at line 157 of file fl_binary_messenger_test.cc.

157 {
158 FlFakeBinaryMessenger* self = FL_FAKE_BINARY_MESSENGER(
159 g_object_new(fl_fake_binary_messenger_get_type(), NULL));
160 self->loop = loop;
161 return FL_BINARY_MESSENGER(self);
162 }

Referenced by TEST().

## ◆ G_DECLARE_FINAL_TYPE()

G_DECLARE_FINAL_TYPE | ( | FlFakeBinaryMessenger | ,
|  | fl_fake_binary_messenger | ,
|  | FL | ,
|  | FAKE_BINARY_MESSENGER | ,
|  | GObject |
| ) |  |

Definition at line 22 of file fl_binary_messenger_test.cc.

28 {
29 GObject parent_instance;
30
31 GMainLoop* loop;
32 GAsyncReadyCallback send_callback;
33 gpointer send_callback_user_data;
34 FlBinaryMessengerMessageHandler message_handler;
35 gpointer message_handler_user_data;
36 };

References FlBinaryMessengerMessageHandler.

## ◆ G_DEFINE_TYPE_WITH_CODE()

G_DEFINE_TYPE_WITH_CODE | ( | FlFakeBinaryMessenger | ,
|  | fl_fake_binary_messenger | ,
|  | G_TYPE_OBJECT | ,
|  | G_IMPLEMENT_INTERFACE(fl_binary_messenger_get_type(), fl_fake_binary_messenger_iface_init) |
| ) |  |

Definition at line 41 of file fl_binary_messenger_test.cc.

49 {}

## ◆ kill_handler_notify_cb()

static void kill_handler_notify_cb | ( | gpointer | was_called | ) |
| static

Definition at line 631 of file fl_binary_messenger_test.cc.

631 {
632 *static_cast<gboolean*>(was_called) = TRUE;
633 }

References TRUE.

Referenced by TEST().

## ◆ message_cb()

static void message_cb | ( | FlBinaryMessenger * | messenger,
|  | const gchar * | channel,
|  | GBytes * | message,
|  | FlBinaryMessengerResponseHandle * | response_handle,
|  | gpointer | user_data
| ) |  |
| static

Definition at line 343 of file fl_binary_messenger_test.cc.

347 {
348 EXPECT_NE(message, nullptr);
349 g_autofree gchar* text =
350 g_strndup(static_cast<const gchar*>(g_bytes_get_data(message, nullptr)),
351 g_bytes_get_size(message));
352 EXPECT_STREQ(text, "Marco!");
353
354 const char* response_text = "Polo!";
355 g_autoptr(GBytes) response =
356 g_bytes_new(response_text, strlen(response_text));
357 g_autoptr(GError) error = nullptr;
358 EXPECT_TRUE(fl_binary_messenger_send_response(messenger, response_handle,
359 response, &error));
360 EXPECT_EQ(error, nullptr);
361 }

References error, and fl_binary_messenger_send_response().

Referenced by TEST().

## ◆ nullptr_response_cb()

static void nullptr_response_cb | ( | GObject * | object,
|  | GAsyncResult * | result,
|  | gpointer | user_data
| ) |  |
| static

Definition at line 286 of file fl_binary_messenger_test.cc.

288 {
289 g_autoptr(GError) error = nullptr;
290 g_autoptr(GBytes) message = fl_binary_messenger_send_on_channel_finish(
291 FL_BINARY_MESSENGER(object), result, &error);
292 EXPECT_NE(message, nullptr);
293 EXPECT_EQ(error, nullptr);
294
295 EXPECT_EQ(g_bytes_get_size(message), static_cast<gsize>(0));
296
297 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
298 }

References error, fl_binary_messenger_send_on_channel_finish(), result, and user_data.

Referenced by TEST().

## ◆ quit_main_loop_cb()

static gboolean quit_main_loop_cb | ( | gpointer | user_data | ) |
| static

Definition at line 504 of file fl_binary_messenger_test.cc.

504 {
505 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
506 return FALSE;
507 }

References user_data.

Referenced by TEST().

## ◆ resize_channel()

static void resize_channel | ( | FlBinaryMessenger * | messenger,
|  | const gchar * | channel,
|  | int64_t | new_size
| ) |  |
| static

Definition at line 133 of file fl_binary_messenger_test.cc.

135 {
136 // Fake implementation. Do nothing.
137 }

Referenced by fl_fake_binary_messenger_iface_init().

## ◆ response_cb()

static void response_cb | ( | FlBinaryMessenger * | messenger,
|  | const gchar * | channel,
|  | GBytes * | message,
|  | FlBinaryMessengerResponseHandle * | response_handle,
|  | gpointer | user_data
| ) |  |
| static

Definition at line 365 of file fl_binary_messenger_test.cc.

369 {
370 EXPECT_NE(message, nullptr);
371 g_autofree gchar* text =
372 g_strndup(static_cast<const gchar*>(g_bytes_get_data(message, nullptr)),
373 g_bytes_get_size(message));
374 EXPECT_STREQ(text, "Polo!");
375
376 fl_binary_messenger_send_response(messenger, response_handle, nullptr,
377 nullptr);
378
379 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
380 }

References fl_binary_messenger_send_response(), and user_data.

Referenced by TEST().

## ◆ response_from_thread_cb()

static void response_from_thread_cb | ( | FlBinaryMessenger * | messenger,
|  | const gchar * | channel,
|  | GBytes * | message,
|  | FlBinaryMessengerResponseHandle * | response_handle,
|  | gpointer | user_data
| ) |  |
| static

Definition at line 588 of file fl_binary_messenger_test.cc.

593 {
594 EXPECT_NE(message, nullptr);
595 pthread_t thread;
596 RespondsOnBackgroundThreadInfo* info =
597 static_cast<RespondsOnBackgroundThreadInfo*>(
598 malloc(sizeof(RespondsOnBackgroundThreadInfo)));
599 info->messenger = FL_BINARY_MESSENGER(g_object_ref(messenger));
600 info->response_handle =
601 FL_BINARY_MESSENGER_RESPONSE_HANDLE(g_object_ref(response_handle));
602 info->loop = static_cast<GMainLoop*>(user_data);
603 EXPECT_EQ(0,
604 pthread_create(&thread, nullptr, &response_from_thread_main, info));
605 }

References RespondsOnBackgroundThreadInfo::loop, RespondsOnBackgroundThreadInfo::messenger, response_from_thread_main(), RespondsOnBackgroundThreadInfo::response_handle, and user_data.

Referenced by TEST().

## ◆ response_from_thread_main()

static void* response_from_thread_main | ( | void * | user_data | ) |
| static

Definition at line 576 of file fl_binary_messenger_test.cc.

576 {
577 RespondsOnBackgroundThreadInfo* info =
578 static_cast<RespondsOnBackgroundThreadInfo*>(user_data);
579
580 fl_binary_messenger_send_response(info->messenger, info->response_handle,
581 nullptr, nullptr);
582
583 g_idle_add(cleanup_responds_on_background_thread_info, info);
584
585 return nullptr;
586 }

References cleanup_responds_on_background_thread_info(), fl_binary_messenger_send_response(), RespondsOnBackgroundThreadInfo::messenger, RespondsOnBackgroundThreadInfo::response_handle, and user_data.

Referenced by response_from_thread_cb().

## ◆ send_message_cb()

static gboolean send_message_cb | ( | gpointer | user_data | ) |
| static

Definition at line 51 of file fl_binary_messenger_test.cc.

51 {
52 FlFakeBinaryMessenger* self = FL_FAKE_BINARY_MESSENGER(user_data);
53
54 const char* text = "Marco!";
55 g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
56 self->message_handler(FL_BINARY_MESSENGER(self), "CHANNEL", message,
57 FL_BINARY_MESSENGER_RESPONSE_HANDLE(
58 fl_mock_binary_messenger_response_handle_new()),
59 self->message_handler_user_data);
60
61 return FALSE;
62 }

References user_data.

Referenced by set_message_handler_on_channel().

## ◆ send_on_channel()

static void send_on_channel | ( | FlBinaryMessenger * | messenger,
|  | const gchar * | channel,
|  | GBytes * | message,
|  | GCancellable * | cancellable,
|  | GAsyncReadyCallback | callback,
|  | gpointer | user_data
| ) |  |
| static

Definition at line 106 of file fl_binary_messenger_test.cc.

111 {
112 FlFakeBinaryMessenger* self = FL_FAKE_BINARY_MESSENGER(messenger);
113
114 EXPECT_STREQ(channel, "CHANNEL");
115 g_autofree gchar* text =
116 g_strndup(static_cast<const gchar*>(g_bytes_get_data(message, nullptr)),
117 g_bytes_get_size(message));
118 EXPECT_STREQ(text, "Marco!");
119
120 // Send response.
121 self->send_callback = callback;
122 self->send_callback_user_data = user_data;
123 g_idle_add(send_ready_cb, messenger);
124 }

References callback, send_ready_cb(), and user_data.

Referenced by fl_fake_binary_messenger_iface_init().

## ◆ send_on_channel_finish()

static GBytes* send_on_channel_finish | ( | FlBinaryMessenger * | messenger,
|  | GAsyncResult * | result,
|  | GError ** | error
| ) |  |
| static

Definition at line 126 of file fl_binary_messenger_test.cc.

128 {
129 const char* text = "Polo!";
130 return g_bytes_new(text, strlen(text));
131 }

Referenced by fl_fake_binary_messenger_iface_init().

## ◆ send_ready_cb()

static gboolean send_ready_cb | ( | gpointer | user_data | ) |
| static

Definition at line 98 of file fl_binary_messenger_test.cc.

98 {
99 FlFakeBinaryMessenger* self = FL_FAKE_BINARY_MESSENGER(user_data);
100
101 self->send_callback(G_OBJECT(self), NULL, self->send_callback_user_data);
102
103 return FALSE;
104 }

References user_data.

Referenced by send_on_channel().

## ◆ send_response()

static gboolean send_response | ( | FlBinaryMessenger * | messenger,
|  | FlBinaryMessengerResponseHandle * | response_handle,
|  | GBytes * | response,
|  | GError ** | error
| ) |  |
| static

Definition at line 80 of file fl_binary_messenger_test.cc.

83 {
84 FlFakeBinaryMessenger* self = FL_FAKE_BINARY_MESSENGER(messenger);
85
86 EXPECT_TRUE(FL_IS_MOCK_BINARY_MESSENGER_RESPONSE_HANDLE(response_handle));
87
88 g_autofree gchar* text =
89 g_strndup(static_cast<const gchar*>(g_bytes_get_data(response, nullptr)),
90 g_bytes_get_size(response));
91 EXPECT_STREQ(text, "Polo!");
92
93 g_main_loop_quit(self->loop);
94
95 return TRUE;
96 }

References TRUE.

Referenced by fl_fake_binary_messenger_iface_init().

## ◆ set_message_handler_on_channel()

static void set_message_handler_on_channel | ( | FlBinaryMessenger * | messenger,
|  | const gchar * | channel,
|  | FlBinaryMessengerMessageHandler | handler,
|  | gpointer | user_data,
|  | GDestroyNotify | destroy_notify
| ) |  |
| static

Definition at line 64 of file fl_binary_messenger_test.cc.

69 {
70 FlFakeBinaryMessenger* self = FL_FAKE_BINARY_MESSENGER(messenger);
71
72 EXPECT_STREQ(channel, "CHANNEL");
73
74 // Send message.
75 self->message_handler = handler;
76 self->message_handler_user_data = user_data;
77 g_idle_add(send_message_cb, messenger);
78 }

References send_message_cb(), and user_data.

Referenced by fl_fake_binary_messenger_iface_init().

## ◆ set_warns_on_channel_overflow()

static void set_warns_on_channel_overflow | ( | FlBinaryMessenger * | messenger,
|  | const gchar * | channel,
|  | bool | warns
| ) |  |
| static

Definition at line 139 of file fl_binary_messenger_test.cc.

141 {
142 // Fake implementation. Do nothing.
143 }

Referenced by fl_fake_binary_messenger_iface_init().

## ◆ TEST() [1/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | ControlChannelErrorResponse |
| ) |  |

Definition at line 511 of file fl_binary_messenger_test.cc.

511 {
512 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
513 g_autoptr(FlEngine) engine = make_mock_engine();
514 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
515
516 g_autoptr(GError) error = nullptr;
517 EXPECT_TRUE(fl_engine_start(engine, &error));
518 EXPECT_EQ(error, nullptr);
519
520 FlutterEngineProcTable* embedder_api = fl_engine_get_embedder_api(engine);
521
522 bool called = false;
523
524 FlutterEngineSendPlatformMessageFnPtr old_handler =
525 embedder_api->SendPlatformMessage;
526 embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC(
527 SendPlatformMessage,
528 ([&called, old_handler, loop](auto engine,
529 const FlutterPlatformMessage* message) {
530 // Expect to receive a message on the "control" channel.
531 if (strcmp(message->channel, "dev.flutter/channel-buffers") != 0) {
532 return old_handler(engine, message);
533 }
534
535 called = true;
536
537 // Register a callback to quit the main loop when binary messenger work
538 // ends.
539 g_idle_add(quit_main_loop_cb, loop);
540
541 // Simulates an internal error.
542 return kInvalidArguments;
543 }));
544
545 fl_binary_messenger_set_warns_on_channel_overflow(messenger, "flutter/test",
546 false);
547
548 EXPECT_TRUE(called);
549
550 // Blocks here until quit_main_loop_cb is called.
551 g_main_loop_run(loop);
552 }

References engine, error, fl_binary_messenger_new(), fl_binary_messenger_set_warns_on_channel_overflow(), fl_engine_get_embedder_api(), fl_engine_start(), make_mock_engine(), and quit_main_loop_cb().

## ◆ TEST() [2/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | DeletingEngineClearsHandlers |
| ) |  |

Definition at line 635 of file fl_binary_messenger_test.cc.

635 {
636 FlEngine* engine = make_mock_engine();
637 g_autoptr(FlBinaryMessenger) messenger = fl_binary_messenger_new(engine);
638 gboolean was_killed = FALSE;
639
640 // Listen for messages from the engine.
641 fl_binary_messenger_set_message_handler_on_channel(messenger, "test/messages",
642 message_cb, &was_killed,
643 kill_handler_notify_cb);
644
645 g_clear_object(&engine);
646
647 ASSERT_TRUE(was_killed);
648 }

References engine, fl_binary_messenger_new(), fl_binary_messenger_set_message_handler_on_channel(), kill_handler_notify_cb(), make_mock_engine(), and message_cb().

## ◆ TEST() [3/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | FakeMessengerReceive |
| ) |  |

Definition at line 222 of file fl_binary_messenger_test.cc.

222 {
223 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
224
225 g_autoptr(FlBinaryMessenger) messenger = fl_fake_binary_messenger_new(loop);
226 EXPECT_TRUE(FL_IS_FAKE_BINARY_MESSENGER(messenger));
227
228 fl_binary_messenger_set_message_handler_on_channel(
229 messenger, "CHANNEL", fake_message_cb, nullptr, nullptr);
230
231 // Blocks here until response is received in fake messenger.
232 g_main_loop_run(loop);
233 }

References fake_message_cb(), fl_binary_messenger_set_message_handler_on_channel(), and fl_fake_binary_messenger_new().

## ◆ TEST() [4/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | FakeMessengerSend |
| ) |  |

Definition at line 183 of file fl_binary_messenger_test.cc.

183 {
184 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
185
186 g_autoptr(FlBinaryMessenger) messenger = fl_fake_binary_messenger_new(loop);
187 EXPECT_TRUE(FL_IS_FAKE_BINARY_MESSENGER(messenger));
188
189 const char* text = "Marco!";
190 g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
191 fl_binary_messenger_send_on_channel(messenger, "CHANNEL", message, nullptr,
192 fake_response_cb, loop);
193
194 // Blocks here until fake_response_cb is called.
195 g_main_loop_run(loop);
196 }

References fake_response_cb(), fl_binary_messenger_send_on_channel(), and fl_fake_binary_messenger_new().

## ◆ TEST() [5/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | NullptrResponse |
| ) |  |

Definition at line 301 of file fl_binary_messenger_test.cc.

301 {
302 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
303
304 g_autoptr(FlEngine) engine = make_mock_engine();
305 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
306 const char* text = "Hello World!";
307 g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
308 fl_binary_messenger_send_on_channel(messenger, "test/nullptr-response",
309 message, nullptr, nullptr_response_cb,
310 loop);
311
312 // Blocks here until nullptr_response_cb is called.
313 g_main_loop_run(loop);
314 }

References engine, fl_binary_messenger_new(), fl_binary_messenger_send_on_channel(), make_mock_engine(), and nullptr_response_cb().

## ◆ TEST() [6/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | ReceiveMessage |
| ) |  |

Definition at line 383 of file fl_binary_messenger_test.cc.

383 {
384 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
385
386 g_autoptr(FlEngine) engine = make_mock_engine();
387 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
388
389 // Listen for messages from the engine.
390 fl_binary_messenger_set_message_handler_on_channel(
391 messenger, "test/messages", message_cb, nullptr, nullptr);
392
393 // Listen for response from the engine.
394 fl_binary_messenger_set_message_handler_on_channel(
395 messenger, "test/responses", response_cb, loop, nullptr);
396
397 // Trigger the engine to send a message.
398 const char* text = "Marco!";
399 g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
400 fl_binary_messenger_send_on_channel(messenger, "test/send-message", message,
401 nullptr, nullptr, nullptr);
402
403 // Blocks here until response_cb is called.
404 g_main_loop_run(loop);
405 }

References engine, fl_binary_messenger_new(), fl_binary_messenger_send_on_channel(), fl_binary_messenger_set_message_handler_on_channel(), make_mock_engine(), message_cb(), and response_cb().

## ◆ TEST() [7/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | ResizeChannel |
| ) |  |

Definition at line 411 of file fl_binary_messenger_test.cc.

411 {
412 g_autoptr(FlEngine) engine = make_mock_engine();
413 FlutterEngineProcTable* embedder_api = fl_engine_get_embedder_api(engine);
414
415 bool called = false;
416
417 FlutterEngineSendPlatformMessageFnPtr old_handler =
418 embedder_api->SendPlatformMessage;
419 embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC(
420 SendPlatformMessage,
421 ([&called, old_handler](auto engine,
422 const FlutterPlatformMessage* message) {
423 // Expect to receive a message on the "control" channel.
424 if (strcmp(message->channel, "dev.flutter/channel-buffers") != 0) {
425 return old_handler(engine, message);
426 }
427
428 called = true;
429
430 // The expected content was created from the following Dart code:
431 // MethodCall call = MethodCall('resize', ['flutter/test',3]);
432 // StandardMethodCodec().encodeMethodCall(call).buffer.asUint8List();
433 const int expected_message_size = 29;
434 EXPECT_EQ(message->message_size,
435 static_cast<size_t>(expected_message_size));
436 int expected[expected_message_size] = {
437 7, 6, 114, 101, 115, 105, 122, 101, 12, 2,
438 7, 12, 102, 108, 117, 116, 116, 101, 114, 47,
439 116, 101, 115, 116, 3, 3, 0, 0, 0};
440 for (size_t i = 0; i < expected_message_size; i++) {
441 EXPECT_EQ(message->message[i], expected[i]);
442 }
443
444 return kSuccess;
445 }));
446
447 g_autoptr(GError) error = nullptr;
448 EXPECT_TRUE(fl_engine_start(engine, &error));
449 EXPECT_EQ(error, nullptr);
450
451 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
452 fl_binary_messenger_resize_channel(messenger, "flutter/test", 3);
453
454 EXPECT_TRUE(called);
455 }

References engine, error, fl_binary_messenger_new(), fl_binary_messenger_resize_channel(), fl_engine_get_embedder_api(), fl_engine_start(), and make_mock_engine().

## ◆ TEST() [8/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | RespondOnBackgroundThread |
| ) |  |

Definition at line 607 of file fl_binary_messenger_test.cc.

607 {
608 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
609
610 g_autoptr(FlEngine) engine = make_mock_engine();
611 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
612
613 // Listen for messages from the engine.
614 fl_binary_messenger_set_message_handler_on_channel(
615 messenger, "test/messages", message_cb, nullptr, nullptr);
616
617 // Listen for response from the engine.
618 fl_binary_messenger_set_message_handler_on_channel(
619 messenger, "test/responses", response_from_thread_cb, loop, nullptr);
620
621 // Trigger the engine to send a message.
622 const char* text = "Marco!";
623 g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
624 fl_binary_messenger_send_on_channel(messenger, "test/send-message", message,
625 nullptr, nullptr, nullptr);
626
627 // Blocks here until response_cb is called.
628 g_main_loop_run(loop);
629 }

References engine, fl_binary_messenger_new(), fl_binary_messenger_send_on_channel(), fl_binary_messenger_set_message_handler_on_channel(), make_mock_engine(), message_cb(), and response_from_thread_cb().

## ◆ TEST() [9/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | SendEmptyMessage |
| ) |  |

Definition at line 244 of file fl_binary_messenger_test.cc.

244 {
245 g_autoptr(FlEngine) engine = make_mock_engine();
246 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
247 g_autoptr(GBytes) message = g_bytes_new(nullptr, 0);
248 fl_binary_messenger_send_on_channel(messenger, "test/echo", message, nullptr,
249 nullptr, nullptr);
250 }

References engine, fl_binary_messenger_new(), fl_binary_messenger_send_on_channel(), and make_mock_engine().

## ◆ TEST() [10/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | SendFailure |
| ) |  |

Definition at line 330 of file fl_binary_messenger_test.cc.

330 {
331 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
332
333 g_autoptr(FlEngine) engine = make_mock_engine();
334 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
335 fl_binary_messenger_send_on_channel(messenger, "test/failure", nullptr,
336 nullptr, failure_response_cb, loop);
337
338 // Blocks here until failure_response_cb is called.
339 g_main_loop_run(loop);
340 }

References engine, failure_response_cb(), fl_binary_messenger_new(), fl_binary_messenger_send_on_channel(), and make_mock_engine().

## ◆ TEST() [11/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | SendMessage |
| ) |  |

Definition at line 271 of file fl_binary_messenger_test.cc.

271 {
272 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
273
274 g_autoptr(FlEngine) engine = make_mock_engine();
275 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
276 const char* text = "Hello World!";
277 g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
278 fl_binary_messenger_send_on_channel(messenger, "test/echo", message, nullptr,
279 echo_response_cb, loop);
280
281 // Blocks here until echo_response_cb is called.
282 g_main_loop_run(loop);
283 }

References echo_response_cb(), engine, fl_binary_messenger_new(), fl_binary_messenger_send_on_channel(), and make_mock_engine().

## ◆ TEST() [12/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | SendNullptrMessage |
| ) |  |

Definition at line 236 of file fl_binary_messenger_test.cc.

236 {
237 g_autoptr(FlEngine) engine = make_mock_engine();
238 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
239 fl_binary_messenger_send_on_channel(messenger, "test/echo", nullptr, nullptr,
240 nullptr, nullptr);
241 }

References engine, fl_binary_messenger_new(), fl_binary_messenger_send_on_channel(), and make_mock_engine().

## ◆ TEST() [13/13]

TEST | ( | FlBinaryMessengerTest | ,
|  | WarnsOnOverflowChannel |
| ) |  |

Definition at line 458 of file fl_binary_messenger_test.cc.

458 {
459 g_autoptr(FlEngine) engine = make_mock_engine();
460 FlutterEngineProcTable* embedder_api = fl_engine_get_embedder_api(engine);
461
462 bool called = false;
463
464 FlutterEngineSendPlatformMessageFnPtr old_handler =
465 embedder_api->SendPlatformMessage;
466 embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC(
467 SendPlatformMessage,
468 ([&called, old_handler](auto engine,
469 const FlutterPlatformMessage* message) {
470 // Expect to receive a message on the "control" channel.
471 if (strcmp(message->channel, "dev.flutter/channel-buffers") != 0) {
472 return old_handler(engine, message);
473 }
474
475 called = true;
476
477 // The expected content was created from the following Dart code:
478 // MethodCall call = MethodCall('overflow',['flutter/test', true]);
479 // StandardMethodCodec().encodeMethodCall(call).buffer.asUint8List();
480 const int expected_message_size = 27;
481 EXPECT_EQ(message->message_size,
482 static_cast<size_t>(expected_message_size));
483 int expected[expected_message_size] = {
484 7, 8, 111, 118, 101, 114, 102, 108, 111, 119, 12, 2, 7, 12,
485 102, 108, 117, 116, 116, 101, 114, 47, 116, 101, 115, 116, 1};
486 for (size_t i = 0; i < expected_message_size; i++) {
487 EXPECT_EQ(message->message[i], expected[i]);
488 }
489
490 return kSuccess;
491 }));
492
493 g_autoptr(GError) error = nullptr;
494 EXPECT_TRUE(fl_engine_start(engine, &error));
495 EXPECT_EQ(error, nullptr);
496
497 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
498 fl_binary_messenger_set_warns_on_channel_overflow(messenger, "flutter/test",
499 false);
500
501 EXPECT_TRUE(called);
502 }

References engine, error, fl_binary_messenger_new(), fl_binary_messenger_set_warns_on_channel_overflow(), fl_engine_get_embedder_api(), fl_engine_start(), and make_mock_engine().

response_from_thread_cb
static void response_from_thread_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
Definition: fl_binary_messenger_test.cc:588
fl_engine_get_embedder_api
FlutterEngineProcTable * fl_engine_get_embedder_api(FlEngine *self)
Definition: fl_engine.cc:579
send_ready_cb
static gboolean send_ready_cb(gpointer user_data)
Definition: fl_binary_messenger_test.cc:98
kill_handler_notify_cb
static void kill_handler_notify_cb(gpointer was_called)
Definition: fl_binary_messenger_test.cc:631
RespondsOnBackgroundThreadInfo::messenger
FlBinaryMessenger * messenger
Definition: fl_binary_messenger_test.cc:557
echo_response_cb
static void echo_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
Definition: fl_binary_messenger_test.cc:253
fl_fake_binary_messenger_new
static FlBinaryMessenger * fl_fake_binary_messenger_new(GMainLoop *loop)
Definition: fl_binary_messenger_test.cc:157
cleanup_responds_on_background_thread_info
static gboolean cleanup_responds_on_background_thread_info(gpointer user_data)
Definition: fl_binary_messenger_test.cc:562
response_cb
static void response_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
Definition: fl_binary_messenger_test.cc:365
user_data
FlKeyEvent uint64_t FlKeyResponderAsyncCallback gpointer user_data
Definition: fl_key_channel_responder.cc:121
set_warns_on_channel_overflow
static void set_warns_on_channel_overflow(FlBinaryMessenger *messenger, const gchar *channel, bool warns)
Definition: fl_binary_messenger_test.cc:139
fl_binary_messenger_new
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)
Definition: fl_binary_messenger.cc:406
quit_main_loop_cb
static gboolean quit_main_loop_cb(gpointer user_data)
Definition: fl_binary_messenger_test.cc:504
make_mock_engine
static FlEngine * make_mock_engine()
Definition: fl_event_channel_test.cc:24
response_from_thread_main
static void * response_from_thread_main(void *user_data)
Definition: fl_binary_messenger_test.cc:576
nullptr_response_cb
static void nullptr_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
Definition: fl_binary_messenger_test.cc:286
send_response
static gboolean send_response(FlBinaryMessenger *messenger, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
Definition: fl_binary_messenger_test.cc:80
RespondsOnBackgroundThreadInfo
Definition: fl_binary_messenger_test.cc:556
fl_binary_messenger_set_warns_on_channel_overflow
G_MODULE_EXPORT void fl_binary_messenger_set_warns_on_channel_overflow(FlBinaryMessenger *self, const gchar *channel, bool warns)
Definition: fl_binary_messenger.cc:484
send_on_channel
static void send_on_channel(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_binary_messenger_test.cc:106
fake_response_cb
static void fake_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
Definition: fl_binary_messenger_test.cc:165
fl_binary_messenger_set_message_handler_on_channel
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)
Definition: fl_binary_messenger.cc:424
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
FlBinaryMessengerMessageHandler
G_MODULE_EXPORT GObject typedef void(* FlBinaryMessengerMessageHandler)(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
Definition: fl_binary_messenger.h:67
RespondsOnBackgroundThreadInfo::response_handle
FlBinaryMessengerResponseHandle * response_handle
Definition: fl_binary_messenger_test.cc:558
fl_binary_messenger_send_on_channel_finish
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish(FlBinaryMessenger *self, GAsyncResult *result, GError **error)
Definition: fl_binary_messenger.cc:465
failure_response_cb
static void failure_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
Definition: fl_binary_messenger_test.cc:317
result
GAsyncResult * result
Definition: fl_text_input_plugin.cc:106
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
message_cb
static void message_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
Definition: fl_binary_messenger_test.cc:343
RespondsOnBackgroundThreadInfo::loop
GMainLoop * loop
Definition: fl_binary_messenger_test.cc:559
set_message_handler_on_channel
static void set_message_handler_on_channel(FlBinaryMessenger *messenger, const gchar *channel, FlBinaryMessengerMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
Definition: fl_binary_messenger_test.cc:64
fl_engine_start
gboolean fl_engine_start(FlEngine *self, GError **error)
Definition: fl_engine.cc:471
fake_message_cb
static void fake_message_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
Definition: fl_binary_messenger_test.cc:199
resize_channel
static void resize_channel(FlBinaryMessenger *messenger, const gchar *channel, int64_t new_size)
Definition: fl_binary_messenger_test.cc:133
engine
FlEngine * engine
Definition: fl_view_accessible.cc:26
callback
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
Definition: fl_key_channel_responder.cc:120
fl_binary_messenger_resize_channel
G_MODULE_EXPORT void fl_binary_messenger_resize_channel(FlBinaryMessenger *self, const gchar *channel, int64_t new_size)
Definition: fl_binary_messenger.cc:475
fl_binary_messenger_send_response
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
Definition: fl_binary_messenger.cc:438
send_message_cb
static gboolean send_message_cb(gpointer user_data)
Definition: fl_binary_messenger_test.cc:51
fl_binary_messenger_send_on_channel
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_binary_messenger.cc:451
send_on_channel_finish
static GBytes * send_on_channel_finish(FlBinaryMessenger *messenger, GAsyncResult *result, GError **error)
Definition: fl_binary_messenger_test.cc:126