Flutter Engine
fl_binary_messenger_test.cc File Reference
#include "gtest/gtest.h"
#include <cstring>
#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/testing/fl_test.h"
#include "flutter/shell/platform/linux/testing/mock_renderer.h"

Go to the source code of this file.

Functions

 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)
 

Function Documentation

◆ echo_response_cb()

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

Definition at line 34 of file fl_binary_messenger_test.cc.

References error, fl_binary_messenger_send_on_channel_finish(), and text.

Referenced by TEST().

36  {
37  g_autoptr(GError) error = nullptr;
38  g_autoptr(GBytes) message = fl_binary_messenger_send_on_channel_finish(
39  FL_BINARY_MESSENGER(object), result, &error);
40  EXPECT_NE(message, nullptr);
41  EXPECT_EQ(error, nullptr);
42 
43  g_autofree gchar* text =
44  g_strndup(static_cast<const gchar*>(g_bytes_get_data(message, nullptr)),
45  g_bytes_get_size(message));
46  EXPECT_STREQ(text, "Hello World!");
47 
48  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
49 }
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish(FlBinaryMessenger *self, GAsyncResult *result, GError **error)
void * user_data
GAsyncResult * result
std::u16string text

◆ failure_response_cb()

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

Definition at line 98 of file fl_binary_messenger_test.cc.

References error, and fl_binary_messenger_send_on_channel_finish().

Referenced by TEST().

100  {
101  g_autoptr(GError) error = nullptr;
102  g_autoptr(GBytes) message = fl_binary_messenger_send_on_channel_finish(
103  FL_BINARY_MESSENGER(object), result, &error);
104  EXPECT_EQ(message, nullptr);
105  EXPECT_NE(error, nullptr);
106 
107  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
108 }
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish(FlBinaryMessenger *self, GAsyncResult *result, GError **error)
void * user_data
GAsyncResult * result

◆ message_cb()

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

Definition at line 124 of file fl_binary_messenger_test.cc.

References error, fl_binary_messenger_send_response(), and text.

Referenced by TEST().

128  {
129  EXPECT_NE(message, nullptr);
130  g_autofree gchar* text =
131  g_strndup(static_cast<const gchar*>(g_bytes_get_data(message, nullptr)),
132  g_bytes_get_size(message));
133  EXPECT_STREQ(text, "Marco!");
134 
135  const char* response_text = "Polo!";
136  g_autoptr(GBytes) response =
137  g_bytes_new(response_text, strlen(response_text));
138  g_autoptr(GError) error = nullptr;
139  EXPECT_TRUE(fl_binary_messenger_send_response(messenger, response_handle,
140  response, &error));
141  EXPECT_EQ(error, nullptr);
142 }
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
std::u16string text

◆ nullptr_response_cb()

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

Definition at line 67 of file fl_binary_messenger_test.cc.

References error, and fl_binary_messenger_send_on_channel_finish().

Referenced by TEST().

69  {
70  g_autoptr(GError) error = nullptr;
71  g_autoptr(GBytes) message = fl_binary_messenger_send_on_channel_finish(
72  FL_BINARY_MESSENGER(object), result, &error);
73  EXPECT_NE(message, nullptr);
74  EXPECT_EQ(error, nullptr);
75 
76  EXPECT_EQ(g_bytes_get_size(message), static_cast<gsize>(0));
77 
78  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
79 }
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish(FlBinaryMessenger *self, GAsyncResult *result, GError **error)
void * user_data
GAsyncResult * result

◆ response_cb()

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

Definition at line 146 of file fl_binary_messenger_test.cc.

References fl_binary_messenger_send_response(), and text.

Referenced by TEST().

150  {
151  EXPECT_NE(message, nullptr);
152  g_autofree gchar* text =
153  g_strndup(static_cast<const gchar*>(g_bytes_get_data(message, nullptr)),
154  g_bytes_get_size(message));
155  EXPECT_STREQ(text, "Polo!");
156 
157  fl_binary_messenger_send_response(messenger, response_handle, nullptr,
158  nullptr);
159 
160  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
161 }
void * user_data
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
std::u16string text

◆ TEST() [1/6]

TEST ( FlBinaryMessengerTest  ,
SendNullptrMessage   
)

Definition at line 17 of file fl_binary_messenger_test.cc.

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

17  {
18  g_autoptr(FlEngine) engine = make_mock_engine();
19  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
20  fl_binary_messenger_send_on_channel(messenger, "test/echo", nullptr, nullptr,
21  nullptr, nullptr);
22 }
static FlEngine * make_mock_engine()
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)

◆ TEST() [2/6]

TEST ( FlBinaryMessengerTest  ,
SendEmptyMessage   
)

Definition at line 25 of file fl_binary_messenger_test.cc.

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

25  {
26  g_autoptr(FlEngine) engine = make_mock_engine();
27  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
28  g_autoptr(GBytes) message = g_bytes_new(nullptr, 0);
29  fl_binary_messenger_send_on_channel(messenger, "test/echo", message, nullptr,
30  nullptr, nullptr);
31 }
static FlEngine * make_mock_engine()
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)

◆ TEST() [3/6]

TEST ( FlBinaryMessengerTest  ,
SendMessage   
)

Definition at line 52 of file fl_binary_messenger_test.cc.

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

52  {
53  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
54 
55  g_autoptr(FlEngine) engine = make_mock_engine();
56  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
57  const char* text = "Hello World!";
58  g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
59  fl_binary_messenger_send_on_channel(messenger, "test/echo", message, nullptr,
60  echo_response_cb, loop);
61 
62  // Blocks here until echo_response_cb is called.
63  g_main_loop_run(loop);
64 }
static void echo_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
static FlEngine * make_mock_engine()
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
std::u16string text
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)

◆ TEST() [4/6]

TEST ( FlBinaryMessengerTest  ,
NullptrResponse   
)

Definition at line 82 of file fl_binary_messenger_test.cc.

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

82  {
83  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
84 
85  g_autoptr(FlEngine) engine = make_mock_engine();
86  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
87  const char* text = "Hello World!";
88  g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
89  fl_binary_messenger_send_on_channel(messenger, "test/nullptr-response",
90  message, nullptr, nullptr_response_cb,
91  loop);
92 
93  // Blocks here until nullptr_response_cb is called.
94  g_main_loop_run(loop);
95 }
static void nullptr_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
static FlEngine * make_mock_engine()
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
std::u16string text
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)

◆ TEST() [5/6]

TEST ( FlBinaryMessengerTest  ,
SendFailure   
)

Definition at line 111 of file fl_binary_messenger_test.cc.

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

111  {
112  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
113 
114  g_autoptr(FlEngine) engine = make_mock_engine();
115  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
116  fl_binary_messenger_send_on_channel(messenger, "test/failure", nullptr,
117  nullptr, failure_response_cb, loop);
118 
119  // Blocks here until failure_response_cb is called.
120  g_main_loop_run(loop);
121 }
static FlEngine * make_mock_engine()
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 failure_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)

◆ TEST() [6/6]

TEST ( FlBinaryMessengerTest  ,
ReceiveMessage   
)

Definition at line 164 of file fl_binary_messenger_test.cc.

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(), response_cb(), and text.

164  {
165  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
166 
167  g_autoptr(FlEngine) engine = make_mock_engine();
168  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
169 
170  // Listen for messages from the engine.
172  messenger, "test/messages", message_cb, nullptr, nullptr);
173 
174  // Listen for response from the engine.
176  messenger, "test/responses", response_cb, loop, nullptr);
177 
178  // Trigger the engine to send a message.
179  const char* text = "Marco!";
180  g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
181  fl_binary_messenger_send_on_channel(messenger, "test/send-message", message,
182  nullptr, nullptr, nullptr);
183 
184  // Blocks here until response_cb is called.
185  g_main_loop_run(loop);
186 }
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 response_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
static FlEngine * make_mock_engine()
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 message_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
std::u16string text
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)