| Flutter Linux Embedder |

fl_basic_message_channel_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 "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
9
10 #include "flutter/shell/platform/embedder/embedder.h"
11 #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
12 #include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
13 #include "flutter/shell/platform/linux/fl_engine_private.h"
14 #include "flutter/shell/platform/linux/public/flutter_linux/fl_message_codec.h"
15 #include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
16 #include "flutter/shell/platform/linux/testing/fl_test.h"
17 #include "flutter/shell/platform/linux/testing/mock_renderer.h"
18
19 // Checks sending a message without a response works.
20 // MOCK_ENGINE_PROC is leaky by design
21 // NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
22 TEST(FlBasicMessageChannelTest, SendMessageWithoutResponse) {
23 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
24
25 g_autoptr(FlEngine) engine = make_mock_engine();
26 FlutterEngineProcTable* embedder_api = fl_engine_get_embedder_api(engine);
27
28 bool called = false;
29 FlutterEngineSendPlatformMessageFnPtr old_handler =
30 embedder_api->SendPlatformMessage;
31 embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC(
32 SendPlatformMessage,
33 ([&called, old_handler](auto engine,
34 const FlutterPlatformMessage* message) {
35 if (strcmp(message->channel, "test") != 0) {
36 return old_handler(engine, message);
37 }
38
39 called = true;
40 EXPECT_EQ(message->response_handle, nullptr);
41
42 g_autoptr(GBytes) message_bytes =
43 g_bytes_new(message->message, message->message_size);
44 g_autoptr(FlStandardMessageCodec) codec =
45 fl_standard_message_codec_new();
46 FlValue* message_value = fl_message_codec_decode_message(
47 FL_MESSAGE_CODEC(codec), message_bytes, nullptr);
48 EXPECT_EQ(fl_value_get_type(message_value), FL_VALUE_TYPE_STRING);
49 EXPECT_STREQ(fl_value_get_string(message_value), "Hello World!");
50
51 return kSuccess;
52 }));
53
54 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
55 g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
56 g_autoptr(FlBasicMessageChannel) channel =
57 fl_basic_message_channel_new(messenger, "test", FL_MESSAGE_CODEC(codec));
58 g_autoptr(FlValue) message = fl_value_new_string("Hello World!");
59 fl_basic_message_channel_send(channel, message, nullptr, nullptr, loop);
60
61 EXPECT_TRUE(called);
62 }
63 // NOLINTEND(clang-analyzer-core.StackAddressEscape)
64
65 // Called when the message response is received in the SendMessageWithResponse
66 // test.
67 static void echo_response_cb(GObject* object,
68 GAsyncResult* result,
69 gpointer user_data) {
70 g_autoptr(GError) error = nullptr;
71 g_autoptr(FlValue) message = fl_basic_message_channel_send_finish(
72 FL_BASIC_MESSAGE_CHANNEL(object), result, &error);
73 EXPECT_NE(message, nullptr);
74 EXPECT_EQ(error, nullptr);
75
76 EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_STRING);
77 EXPECT_STREQ(fl_value_get_string(message), "Hello World!");
78
79 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
80 }
81
82 // Checks sending a message with a response works.
83 TEST(FlBasicMessageChannelTest, SendMessageWithResponse) {
84 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
85
86 g_autoptr(FlEngine) engine = make_mock_engine();
87 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
88 g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
89 g_autoptr(FlBasicMessageChannel) channel = fl_basic_message_channel_new(
90 messenger, "test/echo", FL_MESSAGE_CODEC(codec));
91 g_autoptr(FlValue) message = fl_value_new_string("Hello World!");
92 fl_basic_message_channel_send(channel, message, nullptr, echo_response_cb,
93 loop);
94
95 // Blocks here until echo_response_cb is called.
96 g_main_loop_run(loop);
97 }
98
99 // Called when the message response is received in the SendFailure test.
100 static void failure_response_cb(GObject* object,
101 GAsyncResult* result,
102 gpointer user_data) {
103 g_autoptr(GError) error = nullptr;
104 g_autoptr(FlValue) message = fl_basic_message_channel_send_finish(
105 FL_BASIC_MESSAGE_CHANNEL(object), result, &error);
106 EXPECT_EQ(message, nullptr);
107 EXPECT_NE(error, nullptr);
108
109 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
110 }
111
112 // Checks the engine reporting a send failure is handled.
113 TEST(FlBasicMessageChannelTest, SendFailure) {
114 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
115
116 g_autoptr(FlEngine) engine = make_mock_engine();
117 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
118 g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
119 g_autoptr(FlBasicMessageChannel) channel = fl_basic_message_channel_new(
120 messenger, "test/failure", FL_MESSAGE_CODEC(codec));
121 g_autoptr(FlValue) message = fl_value_new_string("Hello World!");
122 fl_basic_message_channel_send(channel, message, nullptr, failure_response_cb,
123 loop);
124
125 // Blocks here until failure_response_cb is called.
126 g_main_loop_run(loop);
127 }
128
129 // Called when a message is received from the engine in the ReceiveMessage test.
130 static void message_cb(FlBasicMessageChannel* channel,
131 FlValue* message,
132 FlBasicMessageChannelResponseHandle* response_handle,
133 gpointer user_data) {
134 EXPECT_NE(message, nullptr);
135 EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_STRING);
136 EXPECT_STREQ(fl_value_get_string(message), "Marco!");
137
138 g_autoptr(GError) error = nullptr;
139 g_autoptr(FlValue) response = fl_value_new_string("Polo!");
140 EXPECT_TRUE(fl_basic_message_channel_respond(channel, response_handle,
141 response, &error));
142 EXPECT_EQ(error, nullptr);
143 }
144
145 // Called when a the test engine notifies us what response we sent in the
146 // ReceiveMessage test.
147 static void response_cb(FlBasicMessageChannel* channel,
148 FlValue* message,
149 FlBasicMessageChannelResponseHandle* response_handle,
150 gpointer user_data) {
151 EXPECT_NE(message, nullptr);
152 EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_STRING);
153 EXPECT_STREQ(fl_value_get_string(message), "Polo!");
154
155 fl_basic_message_channel_respond(channel, response_handle, nullptr, nullptr);
156
157 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
158 }
159
160 // Checks the shell able to receive and respond to messages from the engine.
161 TEST(FlBasicMessageChannelTest, ReceiveMessage) {
162 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
163
164 g_autoptr(FlEngine) engine = make_mock_engine();
165 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
166
167 // Listen for messages from the engine.
168 g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
169 g_autoptr(FlBasicMessageChannel) messages_channel =
170 fl_basic_message_channel_new(messenger, "test/messages",
171 FL_MESSAGE_CODEC(codec));
172 fl_basic_message_channel_set_message_handler(messages_channel, message_cb,
173 nullptr, nullptr);
174
175 // Listen for response from the engine.
176 g_autoptr(FlBasicMessageChannel) responses_channel =
177 fl_basic_message_channel_new(messenger, "test/responses",
178 FL_MESSAGE_CODEC(codec));
179 fl_basic_message_channel_set_message_handler(responses_channel, response_cb,
180 loop, nullptr);
181
182 // Triggger the engine to send a message.
183 g_autoptr(FlBasicMessageChannel) control_channel =
184 fl_basic_message_channel_new(messenger, "test/send-message",
185 FL_MESSAGE_CODEC(codec));
186 g_autoptr(FlValue) message = fl_value_new_string("Marco!");
187 fl_basic_message_channel_send(control_channel, message, nullptr, nullptr,
188 nullptr);
189
190 // Blocks here until response_cb is called.
191 g_main_loop_run(loop);
192 }
193
194 // Called when the message response is received in the
195 // SendNullMessageWithResponse test.
196 static void null_message_response_cb(GObject* object,
197 GAsyncResult* result,
198 gpointer user_data) {
199 g_autoptr(GError) error = nullptr;
200 g_autoptr(FlValue) message = fl_basic_message_channel_send_finish(
201 FL_BASIC_MESSAGE_CHANNEL(object), result, &error);
202 EXPECT_NE(message, nullptr);
203 EXPECT_EQ(error, nullptr);
204
205 EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_NULL);
206
207 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
208 }
209
210 // Checks sending a null message with a response works.
211 TEST(FlBasicMessageChannelTest, SendNullMessageWithResponse) {
212 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
213
214 g_autoptr(FlEngine) engine = make_mock_engine();
215 FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
216 g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
217 g_autoptr(FlBasicMessageChannel) channel = fl_basic_message_channel_new(
218 messenger, "test/echo", FL_MESSAGE_CODEC(codec));
219 fl_basic_message_channel_send(channel, nullptr, nullptr,
220 null_message_response_cb, loop);
221
222 // Blocks here until null_message_response_cb is called.
223 g_main_loop_run(loop);
224 }
fl_engine_get_embedder_api
FlutterEngineProcTable * fl_engine_get_embedder_api(FlEngine *self)
Definition: fl_engine.cc:602
response_cb
static void response_cb(FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
Definition: fl_basic_message_channel_test.cc:147
fl_standard_message_codec_new
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
Definition: fl_standard_message_codec.cc:637
fl_basic_message_channel_send
G_MODULE_EXPORT void fl_basic_message_channel_send(FlBasicMessageChannel *self, FlValue *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_basic_message_channel.cc:226
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
failure_response_cb
static void failure_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
Definition: fl_basic_message_channel_test.cc:100
user_data
FlKeyEvent uint64_t FlKeyResponderAsyncCallback gpointer user_data
Definition: fl_key_channel_responder.cc:121
null_message_response_cb
static void null_message_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
Definition: fl_basic_message_channel_test.cc:196
fl_basic_message_channel.h
fl_value_get_string
const G_MODULE_EXPORT gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
fl_binary_messenger_new
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)
Definition: fl_binary_messenger.cc:399
FL_VALUE_TYPE_NULL
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:65
make_mock_engine
static FlEngine * make_mock_engine()
Definition: fl_event_channel_test.cc:24
fl_basic_message_channel_set_message_handler
G_MODULE_EXPORT void fl_basic_message_channel_set_message_handler(FlBasicMessageChannel *self, FlBasicMessageChannelMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
Definition: fl_basic_message_channel.cc:176
fl_message_codec_decode_message
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
Definition: fl_message_codec.cc:33
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:69
fl_message_codec.h
fl_engine_private.h
TEST
TEST(FlBasicMessageChannelTest, SendMessageWithoutResponse)
Definition: fl_basic_message_channel_test.cc:22
echo_response_cb
static void echo_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
Definition: fl_basic_message_channel_test.cc:67
fl_binary_messenger_private.h
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
fl_basic_message_channel_respond
G_MODULE_EXPORT gboolean fl_basic_message_channel_respond(FlBasicMessageChannel *self, FlBasicMessageChannelResponseHandle *response_handle, FlValue *message, GError **error)
Definition: fl_basic_message_channel.cc:204
fl_standard_message_codec.h
fl_basic_message_channel_send_finish
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)
Definition: fl_basic_message_channel.cc:253
fl_basic_message_channel_new
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
Definition: fl_basic_message_channel.cc:154
message_cb
static void message_cb(FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
Definition: fl_basic_message_channel_test.cc:130
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276