6 #include "gtest/gtest.h"
11 #include "flutter/shell/platform/embedder/embedder.h"
12 #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
18 #include "flutter/shell/platform/linux/testing/fl_test.h"
19 #include "flutter/shell/platform/linux/testing/mock_binary_messenger_response_handle.h"
20 #include "flutter/shell/platform/linux/testing/mock_renderer.h"
23 fl_fake_binary_messenger,
25 FAKE_BINARY_MESSENGER,
28 struct _FlFakeBinaryMessenger {
29 GObject parent_instance;
32 GAsyncReadyCallback send_callback;
33 gpointer send_callback_user_data;
35 gpointer message_handler_user_data;
39 FlBinaryMessengerInterface* iface);
42 FlFakeBinaryMessenger,
43 fl_fake_binary_messenger,
45 G_IMPLEMENT_INTERFACE(fl_binary_messenger_get_type(),
48 static void fl_fake_binary_messenger_class_init(
49 FlFakeBinaryMessengerClass* klass) {}
52 FlFakeBinaryMessenger*
self = FL_FAKE_BINARY_MESSENGER(
user_data);
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);
65 FlBinaryMessenger* messenger,
69 GDestroyNotify destroy_notify) {
70 FlFakeBinaryMessenger*
self = FL_FAKE_BINARY_MESSENGER(messenger);
72 EXPECT_STREQ(channel,
"CHANNEL");
75 self->message_handler = handler;
76 self->message_handler_user_data =
user_data;
81 FlBinaryMessengerResponseHandle* response_handle,
84 FlFakeBinaryMessenger*
self = FL_FAKE_BINARY_MESSENGER(messenger);
86 EXPECT_TRUE(FL_IS_MOCK_BINARY_MESSENGER_RESPONSE_HANDLE(response_handle));
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!");
93 g_main_loop_quit(self->loop);
99 FlFakeBinaryMessenger*
self = FL_FAKE_BINARY_MESSENGER(
user_data);
101 self->send_callback(G_OBJECT(
self), NULL, self->send_callback_user_data);
107 const gchar* channel,
109 GCancellable* cancellable,
112 FlFakeBinaryMessenger*
self = FL_FAKE_BINARY_MESSENGER(messenger);
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!");
122 self->send_callback_user_data =
user_data;
129 const char* text =
"Polo!";
130 return g_bytes_new(text, strlen(text));
134 const gchar* channel,
140 const gchar* channel,
146 FlBinaryMessengerInterface* iface) {
158 FlFakeBinaryMessenger*
self = FL_FAKE_BINARY_MESSENGER(
159 g_object_new(fl_fake_binary_messenger_get_type(), NULL));
161 return FL_BINARY_MESSENGER(
self);
168 g_autoptr(GError)
error =
nullptr;
171 EXPECT_NE(message,
nullptr);
172 EXPECT_EQ(
error,
nullptr);
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!");
179 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
183 TEST(FlBinaryMessengerTest, FakeMessengerSend) {
184 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
187 EXPECT_TRUE(FL_IS_FAKE_BINARY_MESSENGER(messenger));
189 const char* text =
"Marco!";
190 g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
195 g_main_loop_run(loop);
200 const gchar* channel,
202 FlBinaryMessengerResponseHandle* response_handle,
204 EXPECT_STREQ(channel,
"CHANNEL");
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!");
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;
218 EXPECT_EQ(
error,
nullptr);
222 TEST(FlBinaryMessengerTest, FakeMessengerReceive) {
223 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
226 EXPECT_TRUE(FL_IS_FAKE_BINARY_MESSENGER(messenger));
232 g_main_loop_run(loop);
236 TEST(FlBinaryMessengerTest, SendNullptrMessage) {
244 TEST(FlBinaryMessengerTest, SendEmptyMessage) {
247 g_autoptr(GBytes) message = g_bytes_new(
nullptr, 0);
256 g_autoptr(GError)
error =
nullptr;
259 EXPECT_NE(message,
nullptr);
260 EXPECT_EQ(
error,
nullptr);
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!");
267 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
271 TEST(FlBinaryMessengerTest, SendMessage) {
272 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
276 const char* text =
"Hello World!";
277 g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
282 g_main_loop_run(loop);
289 g_autoptr(GError)
error =
nullptr;
292 EXPECT_NE(message,
nullptr);
293 EXPECT_EQ(
error,
nullptr);
295 EXPECT_EQ(g_bytes_get_size(message),
static_cast<gsize
>(0));
297 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
301 TEST(FlBinaryMessengerTest, NullptrResponse) {
302 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
306 const char* text =
"Hello World!";
307 g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
313 g_main_loop_run(loop);
320 g_autoptr(GError)
error =
nullptr;
323 EXPECT_EQ(message,
nullptr);
324 EXPECT_NE(
error,
nullptr);
326 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
330 TEST(FlBinaryMessengerTest, SendFailure) {
331 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
339 g_main_loop_run(loop);
344 const gchar* channel,
346 FlBinaryMessengerResponseHandle* response_handle,
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!");
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;
360 EXPECT_EQ(
error,
nullptr);
366 const gchar* channel,
368 FlBinaryMessengerResponseHandle* response_handle,
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!");
379 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
383 TEST(FlBinaryMessengerTest, ReceiveMessage) {
384 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
391 messenger,
"test/messages",
message_cb,
nullptr,
nullptr);
395 messenger,
"test/responses",
response_cb, loop,
nullptr);
398 const char* text =
"Marco!";
399 g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
401 nullptr,
nullptr,
nullptr);
404 g_main_loop_run(loop);
417 FlutterEngineSendPlatformMessageFnPtr old_handler =
418 embedder_api->SendPlatformMessage;
419 embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC(
421 ([&called, old_handler](
auto engine,
422 const FlutterPlatformMessage* message) {
424 if (strcmp(message->channel,
"dev.flutter/channel-buffers") != 0) {
425 return old_handler(engine, message);
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]);
447 g_autoptr(GError)
error =
nullptr;
449 EXPECT_EQ(
error,
nullptr);
458 TEST(FlBinaryMessengerTest, WarnsOnOverflowChannel) {
464 FlutterEngineSendPlatformMessageFnPtr old_handler =
465 embedder_api->SendPlatformMessage;
466 embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC(
468 ([&called, old_handler](
auto engine,
469 const FlutterPlatformMessage* message) {
471 if (strcmp(message->channel,
"dev.flutter/channel-buffers") != 0) {
472 return old_handler(engine, message);
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]);
493 g_autoptr(GError)
error =
nullptr;
495 EXPECT_EQ(
error,
nullptr);
505 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
511 TEST(FlBinaryMessengerTest, ControlChannelErrorResponse) {
512 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
516 g_autoptr(GError)
error =
nullptr;
518 EXPECT_EQ(
error,
nullptr);
524 FlutterEngineSendPlatformMessageFnPtr old_handler =
525 embedder_api->SendPlatformMessage;
526 embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC(
528 ([&called, old_handler, loop](
auto engine,
529 const FlutterPlatformMessage* message) {
531 if (strcmp(message->channel,
"dev.flutter/channel-buffers") != 0) {
532 return old_handler(engine, message);
542 return kInvalidArguments;
551 g_main_loop_run(loop);
565 GMainLoop* loop = info->
loop;
571 g_main_loop_quit(
static_cast<GMainLoop*
>(loop));
573 return G_SOURCE_REMOVE;
589 FlBinaryMessenger* messenger,
590 const gchar* channel,
592 FlBinaryMessengerResponseHandle* response_handle,
594 EXPECT_NE(message,
nullptr);
599 info->
messenger = FL_BINARY_MESSENGER(g_object_ref(messenger));
601 FL_BINARY_MESSENGER_RESPONSE_HANDLE(g_object_ref(response_handle));
607 TEST(FlBinaryMessengerTest, RespondOnBackgroundThread) {
608 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
615 messenger,
"test/messages",
message_cb,
nullptr,
nullptr);
622 const char* text =
"Marco!";
623 g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
625 nullptr,
nullptr,
nullptr);
628 g_main_loop_run(loop);
632 *
static_cast<gboolean*
>(was_called) =
TRUE;
635 TEST(FlBinaryMessengerTest, DeletingEngineClearsHandlers) {
638 gboolean was_killed = FALSE;
645 g_clear_object(&engine);
647 ASSERT_TRUE(was_killed);