Flutter Engine
fl_binary_messenger_test.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Included first as it collides with the X11 headers.
6 #include "gtest/gtest.h"
7 
8 #include <cstring>
9 
10 #include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
11 #include "flutter/shell/platform/linux/fl_engine_private.h"
12 #include "flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h"
13 #include "flutter/shell/platform/linux/testing/fl_test.h"
14 #include "flutter/shell/platform/linux/testing/mock_renderer.h"
15 
16 // Checks sending nullptr for a message works.
17 TEST(FlBinaryMessengerTest, SendNullptrMessage) {
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 }
23 
24 // Checks sending a zero length message works.
25 TEST(FlBinaryMessengerTest, SendEmptyMessage) {
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 }
32 
33 // Called when the message response is received in the SendMessage test.
34 static void echo_response_cb(GObject* object,
35  GAsyncResult* result,
36  gpointer user_data) {
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 }
50 
51 // Checks sending a message works.
52 TEST(FlBinaryMessengerTest, SendMessage) {
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 }
65 
66 // Called when the message response is received in the NullptrResponse test.
67 static void nullptr_response_cb(GObject* object,
68  GAsyncResult* result,
69  gpointer user_data) {
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 }
80 
81 // Checks the engine returning a nullptr message work.
82 TEST(FlBinaryMessengerTest, NullptrResponse) {
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 }
96 
97 // Called when the message response is received in the SendFailure test.
98 static void failure_response_cb(GObject* object,
99  GAsyncResult* result,
100  gpointer user_data) {
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 }
109 
110 // Checks the engine reporting a send failure is handled.
111 TEST(FlBinaryMessengerTest, SendFailure) {
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 }
122 
123 // Called when a message is received from the engine in the ReceiveMessage test.
124 static void message_cb(FlBinaryMessenger* messenger,
125  const gchar* channel,
126  GBytes* message,
127  FlBinaryMessengerResponseHandle* response_handle,
128  gpointer user_data) {
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 }
143 
144 // Called when a the test engine notifies us what response we sent in the
145 // ReceiveMessage test.
146 static void response_cb(FlBinaryMessenger* messenger,
147  const gchar* channel,
148  GBytes* message,
149  FlBinaryMessengerResponseHandle* response_handle,
150  gpointer user_data) {
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 }
162 
163 // Checks the shell able to receive and respond to messages from the engine.
164 TEST(FlBinaryMessengerTest, ReceiveMessage) {
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)
const uint8_t uint32_t uint32_t GError ** error
static void response_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish(FlBinaryMessenger *self, GAsyncResult *result, GError **error)
void * user_data
#define SendMessage
GAsyncResult * result
static void nullptr_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
static void echo_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
static FlEngine * make_mock_engine()
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
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
TEST(FlBinaryMessengerTest, SendNullptrMessage)
static void failure_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)