Flutter Engine
The Flutter Engine
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)
22TEST(FlBasicMessageChannelTest, SendMessageWithoutResponse) {
23 g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
24
25 g_autoptr(FlEngine) engine = make_mock_engine();
27
28 bool called = false;
30 embedder_api->SendPlatformMessage;
32 SendPlatformMessage,
33 ([&called, old_handler](auto engine,
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 =
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.
67static void echo_response_cb(GObject* object,
68 GAsyncResult* result,
69 gpointer user_data) {
70 g_autoptr(GError) error = nullptr;
72 FL_BASIC_MESSAGE_CHANNEL(object), result, &error);
73 EXPECT_NE(message, nullptr);
74 EXPECT_EQ(error, nullptr);
75
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.
83TEST(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!");
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.
100static void failure_response_cb(GObject* object,
101 GAsyncResult* result,
102 gpointer user_data) {
103 g_autoptr(GError) error = nullptr;
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.
113TEST(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!");
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.
130static void message_cb(FlBasicMessageChannel* channel,
132 FlBasicMessageChannelResponseHandle* response_handle,
133 gpointer user_data) {
134 EXPECT_NE(message, nullptr);
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.
147static void response_cb(FlBasicMessageChannel* channel,
149 FlBasicMessageChannelResponseHandle* response_handle,
150 gpointer user_data) {
151 EXPECT_NE(message, nullptr);
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.
161TEST(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));
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));
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.
196static void null_message_response_cb(GObject* object,
197 GAsyncResult* result,
198 gpointer user_data) {
199 g_autoptr(GError) error = nullptr;
201 FL_BASIC_MESSAGE_CHANNEL(object), result, &error);
202 EXPECT_NE(message, nullptr);
203 EXPECT_EQ(error, nullptr);
204
206
207 g_main_loop_quit(static_cast<GMainLoop*>(user_data));
208}
209
210// Checks sending a null message with a response works.
211TEST(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,
221
222 // Blocks here until null_message_response_cb is called.
223 g_main_loop_run(loop);
224}
FlutterEngineResult(* FlutterEngineSendPlatformMessageFnPtr)(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterPlatformMessage *message)
Definition: embedder.h:3227
@ kSuccess
Definition: embedder.h:73
FlutterEngine engine
Definition: main.cc:68
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
G_MODULE_EXPORT void fl_basic_message_channel_set_message_handler(FlBasicMessageChannel *self, FlBasicMessageChannelMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
G_MODULE_EXPORT gboolean fl_basic_message_channel_respond(FlBasicMessageChannel *self, FlBasicMessageChannelResponseHandle *response_handle, FlValue *message, GError **error)
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)
G_MODULE_EXPORT void fl_basic_message_channel_send(FlBasicMessageChannel *self, FlValue *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
static void message_cb(FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
TEST(FlBasicMessageChannelTest, SendMessageWithoutResponse)
static void response_cb(FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
static void failure_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
static void echo_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
static void null_message_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)
FlutterEngineProcTable * fl_engine_get_embedder_api(FlEngine *self)
Definition: fl_engine.cc:599
static FlEngine * make_mock_engine()
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
GAsyncResult * result
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:69
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:65
Win32Message message
#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
void * user_data
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678