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 TEST(FlBasicMessageChannelTest, SendMessageWithoutResponse) {
21  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
22 
23  g_autoptr(FlEngine) engine = make_mock_engine();
25 
26  bool called = false;
28  embedder_api->SendPlatformMessage;
29  embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC(
30  SendPlatformMessage,
31  ([&called, old_handler](auto engine,
32  const FlutterPlatformMessage* message) {
33  if (strcmp(message->channel, "test") != 0) {
34  return old_handler(engine, message);
35  }
36 
37  called = true;
38  EXPECT_EQ(message->response_handle, nullptr);
39 
40  g_autoptr(GBytes) message_bytes =
41  g_bytes_new(message->message, message->message_size);
42  g_autoptr(FlStandardMessageCodec) codec =
45  FL_MESSAGE_CODEC(codec), message_bytes, nullptr);
46  EXPECT_EQ(fl_value_get_type(message_value), FL_VALUE_TYPE_STRING);
47  EXPECT_STREQ(fl_value_get_string(message_value), "Hello World!");
48 
49  return kSuccess;
50  }));
51 
52  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
53  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
54  g_autoptr(FlBasicMessageChannel) channel =
55  fl_basic_message_channel_new(messenger, "test", FL_MESSAGE_CODEC(codec));
56  g_autoptr(FlValue) message = fl_value_new_string("Hello World!");
57  fl_basic_message_channel_send(channel, message, nullptr, nullptr, loop);
58 
59  EXPECT_TRUE(called);
60 }
61 
62 // Called when the message response is received in the SendMessage test.
63 static void echo_response_cb(GObject* object,
64  GAsyncResult* result,
65  gpointer user_data) {
66  g_autoptr(GError) error = nullptr;
67  g_autoptr(FlValue) message = fl_basic_message_channel_send_finish(
68  FL_BASIC_MESSAGE_CHANNEL(object), result, &error);
69  EXPECT_NE(message, nullptr);
70  EXPECT_EQ(error, nullptr);
71 
72  EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_STRING);
73  EXPECT_STREQ(fl_value_get_string(message), "Hello World!");
74 
75  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
76 }
77 
78 // Checks sending a message with a response works.
79 TEST(FlBasicMessageChannelTest, SendMessageWithResponse) {
80  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
81 
82  g_autoptr(FlEngine) engine = make_mock_engine();
83  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
84  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
85  g_autoptr(FlBasicMessageChannel) channel = fl_basic_message_channel_new(
86  messenger, "test/echo", FL_MESSAGE_CODEC(codec));
87  g_autoptr(FlValue) message = fl_value_new_string("Hello World!");
88  fl_basic_message_channel_send(channel, message, nullptr, echo_response_cb,
89  loop);
90 
91  // Blocks here until echo_response_cb is called.
92  g_main_loop_run(loop);
93 }
94 
95 // Called when the message response is received in the SendFailure test.
96 static void failure_response_cb(GObject* object,
97  GAsyncResult* result,
98  gpointer user_data) {
99  g_autoptr(GError) error = nullptr;
100  g_autoptr(FlValue) message = fl_basic_message_channel_send_finish(
101  FL_BASIC_MESSAGE_CHANNEL(object), result, &error);
102  EXPECT_EQ(message, nullptr);
103  EXPECT_NE(error, nullptr);
104 
105  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
106 }
107 
108 // Checks the engine reporting a send failure is handled.
109 TEST(FlBasicMessageChannelTest, SendFailure) {
110  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
111 
112  g_autoptr(FlEngine) engine = make_mock_engine();
113  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
114  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
115  g_autoptr(FlBasicMessageChannel) channel = fl_basic_message_channel_new(
116  messenger, "test/failure", FL_MESSAGE_CODEC(codec));
117  g_autoptr(FlValue) message = fl_value_new_string("Hello World!");
118  fl_basic_message_channel_send(channel, message, nullptr, failure_response_cb,
119  loop);
120 
121  // Blocks here until failure_response_cb is called.
122  g_main_loop_run(loop);
123 }
124 
125 // Called when a message is received from the engine in the ReceiveMessage test.
126 static void message_cb(FlBasicMessageChannel* channel,
127  FlValue* message,
128  FlBasicMessageChannelResponseHandle* response_handle,
129  gpointer user_data) {
130  EXPECT_NE(message, nullptr);
131  EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_STRING);
132  EXPECT_STREQ(fl_value_get_string(message), "Marco!");
133 
134  g_autoptr(GError) error = nullptr;
135  g_autoptr(FlValue) response = fl_value_new_string("Polo!");
136  EXPECT_TRUE(fl_basic_message_channel_respond(channel, response_handle,
137  response, &error));
138  EXPECT_EQ(error, nullptr);
139 }
140 
141 // Called when a the test engine notifies us what response we sent in the
142 // ReceiveMessage test.
143 static void response_cb(FlBasicMessageChannel* channel,
144  FlValue* message,
145  FlBasicMessageChannelResponseHandle* response_handle,
146  gpointer user_data) {
147  EXPECT_NE(message, nullptr);
148  EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_STRING);
149  EXPECT_STREQ(fl_value_get_string(message), "Polo!");
150 
151  fl_basic_message_channel_respond(channel, response_handle, nullptr, nullptr);
152 
153  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
154 }
155 
156 // Checks the shell able to receive and respond to messages from the engine.
157 TEST(FlBasicMessageChannelTest, ReceiveMessage) {
158  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
159 
160  g_autoptr(FlEngine) engine = make_mock_engine();
161  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
162 
163  // Listen for messages from the engine.
164  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
165  g_autoptr(FlBasicMessageChannel) messages_channel =
166  fl_basic_message_channel_new(messenger, "test/messages",
167  FL_MESSAGE_CODEC(codec));
169  nullptr, nullptr);
170 
171  // Listen for response from the engine.
172  g_autoptr(FlBasicMessageChannel) responses_channel =
173  fl_basic_message_channel_new(messenger, "test/responses",
174  FL_MESSAGE_CODEC(codec));
176  loop, nullptr);
177 
178  // Triggger the engine to send a message.
179  g_autoptr(FlBasicMessageChannel) control_channel =
180  fl_basic_message_channel_new(messenger, "test/send-message",
181  FL_MESSAGE_CODEC(codec));
182  g_autoptr(FlValue) message = fl_value_new_string("Marco!");
183  fl_basic_message_channel_send(control_channel, message, nullptr, nullptr,
184  nullptr);
185 
186  // Blocks here until response_cb is called.
187  g_main_loop_run(loop);
188 }
const char * channel
Definition: embedder.h:758
G_MODULE_EXPORT gboolean fl_basic_message_channel_respond(FlBasicMessageChannel *self, FlBasicMessageChannelResponseHandle *response_handle, FlValue *message, GError **error)
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:428
static void failure_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
const FlutterPlatformMessageResponseHandle * response_handle
Definition: embedder.h:767
Function-pointer-based versions of the APIs above.
Definition: embedder.h:2372
FlutterEngineProcTable * fl_engine_get_embedder_api(FlEngine *self)
Definition: fl_engine.cc:510
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
void * user_data
FlutterEngineSendPlatformMessageFnPtr SendPlatformMessage
Definition: embedder.h:2386
GAsyncResult * result
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)
static void echo_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
static void message_cb(FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
static FlEngine * make_mock_engine()
const uint8_t * message
Definition: embedder.h:759
static void response_cb(FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
FlutterEngineResult(* FlutterEngineSendPlatformMessageFnPtr)(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterPlatformMessage *message)
Definition: embedder.h:2292
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:265
G_MODULE_EXPORT void fl_basic_message_channel_send(FlBasicMessageChannel *self, FlValue *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
#define MOCK_ENGINE_PROC(proc, mock_impl)
G_MODULE_EXPORT void fl_basic_message_channel_set_message_handler(FlBasicMessageChannel *self, FlBasicMessageChannelMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:642
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)
TEST(FlBasicMessageChannelTest, SendMessageWithoutResponse)