Flutter Engine
The Flutter Engine
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 {
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}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
void * user_data

◆ 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;
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}
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish(FlBinaryMessenger *self, GAsyncResult *result, GError **error)
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
std::u16string text
Win32Message message

◆ 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;
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}

◆ 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}
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678

◆ 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;
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}

◆ 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}
static void resize_channel(FlBinaryMessenger *messenger, const gchar *channel, int64_t new_size)
static void send_on_channel(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, 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 void set_warns_on_channel_overflow(FlBinaryMessenger *messenger, const gchar *channel, bool warns)
static GBytes * send_on_channel_finish(FlBinaryMessenger *messenger, GAsyncResult *result, GError **error)

◆ 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}

◆ 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};
G_MODULE_EXPORT GObject typedef void(* FlBinaryMessengerMessageHandler)(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)

◆ 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}

◆ 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}

◆ 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;
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}

◆ 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}
return FALSE

◆ 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}

◆ 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}

◆ 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;
597 static_cast<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}
static void * response_from_thread_main(void *user_data)
void * malloc(size_t size)
Definition: allocation.cc:19

◆ 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 {
579
580 fl_binary_messenger_send_response(info->messenger, info->response_handle,
581 nullptr, nullptr);
582
584
585 return nullptr;
586}
static gboolean cleanup_responds_on_background_thread_info(gpointer user_data)

◆ 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(
59 self->message_handler_user_data);
60
61 return FALSE;
62}
FlMockBinaryMessengerResponseHandle * fl_mock_binary_messenger_response_handle_new()

◆ 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}
static gboolean send_ready_cb(gpointer user_data)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback

◆ 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}

◆ 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}

◆ 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}

◆ 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}
static gboolean send_message_cb(gpointer user_data)

◆ 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}

◆ 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;
518 EXPECT_EQ(error, nullptr);
519
521
522 bool called = false;
523
525 embedder_api->SendPlatformMessage;
527 SendPlatformMessage,
528 ([&called, old_handler, loop](auto engine,
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
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}
FlutterEngineResult(* FlutterEngineSendPlatformMessageFnPtr)(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterPlatformMessage *message)
Definition: embedder.h:3227
@ kInvalidArguments
Definition: embedder.h:75
FlutterEngine engine
Definition: main.cc:68
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)
G_MODULE_EXPORT void fl_binary_messenger_set_warns_on_channel_overflow(FlBinaryMessenger *self, const gchar *channel, bool warns)
static gboolean quit_main_loop_cb(gpointer user_data)
FlutterEngineProcTable * fl_engine_get_embedder_api(FlEngine *self)
Definition: fl_engine.cc:599
gboolean fl_engine_start(FlEngine *self, GError **error)
Definition: fl_engine.cc:471
static FlEngine * make_mock_engine()
#define MOCK_ENGINE_PROC(proc, mock_impl)
Function-pointer-based versions of the APIs above.
Definition: embedder.h:3319
FlutterEngineSendPlatformMessageFnPtr SendPlatformMessage
Definition: embedder.h:3333

◆ 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.
642 message_cb, &was_killed,
644
645 g_clear_object(&engine);
646
647 ASSERT_TRUE(was_killed);
648}
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)
static void message_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
static void kill_handler_notify_cb(gpointer was_called)

◆ 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
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}
static void fake_message_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
static FlBinaryMessenger * fl_fake_binary_messenger_new(GMainLoop *loop)

◆ 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}
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
static void fake_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)

◆ 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",
310 loop);
311
312 // Blocks here until nullptr_response_cb is called.
313 g_main_loop_run(loop);
314}
static void nullptr_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)

◆ 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.
391 messenger, "test/messages", message_cb, nullptr, nullptr);
392
393 // Listen for response from the engine.
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}
static void response_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)

◆ 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();
414
415 bool called = false;
416
418 embedder_api->SendPlatformMessage;
420 SendPlatformMessage,
421 ([&called, old_handler](auto engine,
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;
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}
for(const auto glyph :glyphs)
Definition: FontMgrTest.cpp:52
@ kSuccess
Definition: embedder.h:73
G_MODULE_EXPORT void fl_binary_messenger_resize_channel(FlBinaryMessenger *self, const gchar *channel, int64_t new_size)

◆ 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.
615 messenger, "test/messages", message_cb, nullptr, nullptr);
616
617 // Listen for response from the engine.
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}
static void response_from_thread_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)

◆ 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}

◆ 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}
static void failure_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)

◆ 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}
static void echo_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)

◆ 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}

◆ 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();
461
462 bool called = false;
463
465 embedder_api->SendPlatformMessage;
467 SendPlatformMessage,
468 ([&called, old_handler](auto engine,
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;
495 EXPECT_EQ(error, nullptr);
496
497 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
499 false);
500
501 EXPECT_TRUE(called);
502}