Flutter Engine
fl_basic_message_channel_test.cc File Reference
#include "gtest/gtest.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
#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_message_codec.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.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 (FlBasicMessageChannelTest, SendMessageWithoutResponse)
 
static void echo_response_cb (GObject *object, GAsyncResult *result, gpointer user_data)
 
 TEST (FlBasicMessageChannelTest, SendMessageWithResponse)
 
static void failure_response_cb (GObject *object, GAsyncResult *result, gpointer user_data)
 
 TEST (FlBasicMessageChannelTest, SendFailure)
 
static void message_cb (FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
 
static void response_cb (FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
 
 TEST (FlBasicMessageChannelTest, ReceiveMessage)
 

Function Documentation

◆ echo_response_cb()

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

Definition at line 63 of file fl_basic_message_channel_test.cc.

References error, fl_basic_message_channel_send_finish(), fl_value_get_string(), fl_value_get_type(), and FL_VALUE_TYPE_STRING.

Referenced by TEST().

65  {
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 }
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:428
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
void * user_data
GAsyncResult * result
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:642

◆ failure_response_cb()

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

Definition at line 96 of file fl_basic_message_channel_test.cc.

References error, and fl_basic_message_channel_send_finish().

Referenced by TEST().

98  {
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 }
const uint8_t uint32_t uint32_t GError ** error
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
void * user_data
GAsyncResult * result
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)

◆ message_cb()

static void message_cb ( FlBasicMessageChannel *  channel,
FlValue message,
FlBasicMessageChannelResponseHandle *  response_handle,
gpointer  user_data 
)
static

Definition at line 126 of file fl_basic_message_channel_test.cc.

References error, fl_basic_message_channel_respond(), fl_value_get_string(), fl_value_get_type(), fl_value_new_string(), and FL_VALUE_TYPE_STRING.

Referenced by TEST().

129  {
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 }
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
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:265
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:642

◆ response_cb()

static void response_cb ( FlBasicMessageChannel *  channel,
FlValue message,
FlBasicMessageChannelResponseHandle *  response_handle,
gpointer  user_data 
)
static

Definition at line 143 of file fl_basic_message_channel_test.cc.

References fl_basic_message_channel_respond(), fl_value_get_string(), fl_value_get_type(), and FL_VALUE_TYPE_STRING.

Referenced by TEST().

146  {
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 }
G_MODULE_EXPORT gboolean fl_basic_message_channel_respond(FlBasicMessageChannel *self, FlBasicMessageChannelResponseHandle *response_handle, FlValue *message, GError **error)
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:428
void * user_data
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:642

◆ TEST() [1/4]

TEST ( FlBasicMessageChannelTest  ,
SendMessageWithoutResponse   
)

Definition at line 20 of file fl_basic_message_channel_test.cc.

References FlutterPlatformMessage::channel, engine, fl_basic_message_channel_new(), fl_basic_message_channel_send(), fl_binary_messenger_new(), fl_engine_get_embedder_api(), fl_message_codec_decode_message(), fl_standard_message_codec_new(), fl_value_get_string(), fl_value_get_type(), fl_value_new_string(), FL_VALUE_TYPE_STRING, kSuccess, make_mock_engine(), FlutterPlatformMessage::message, FlutterPlatformMessage::message_size, MOCK_ENGINE_PROC, FlutterPlatformMessage::response_handle, and FlutterEngineProcTable::SendPlatformMessage.

20  {
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 }
const char * channel
Definition: embedder.h:758
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:428
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)
FlutterEngineSendPlatformMessageFnPtr SendPlatformMessage
Definition: embedder.h:2386
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
static FlEngine * make_mock_engine()
const uint8_t * message
Definition: embedder.h:759
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 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() [2/4]

TEST ( FlBasicMessageChannelTest  ,
SendMessageWithResponse   
)

Definition at line 79 of file fl_basic_message_channel_test.cc.

References echo_response_cb(), engine, fl_basic_message_channel_new(), fl_basic_message_channel_send(), fl_binary_messenger_new(), fl_standard_message_codec_new(), fl_value_new_string(), and make_mock_engine().

79  {
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 }
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
static void echo_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
static FlEngine * make_mock_engine()
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)
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)

◆ TEST() [3/4]

TEST ( FlBasicMessageChannelTest  ,
SendFailure   
)

Definition at line 109 of file fl_basic_message_channel_test.cc.

References engine, failure_response_cb(), fl_basic_message_channel_new(), fl_basic_message_channel_send(), fl_binary_messenger_new(), fl_standard_message_codec_new(), fl_value_new_string(), and make_mock_engine().

109  {
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 }
static void failure_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
static FlEngine * make_mock_engine()
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)
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)

◆ TEST() [4/4]

TEST ( FlBasicMessageChannelTest  ,
ReceiveMessage   
)

Definition at line 157 of file fl_basic_message_channel_test.cc.

References engine, fl_basic_message_channel_new(), fl_basic_message_channel_send(), fl_basic_message_channel_set_message_handler(), fl_binary_messenger_new(), fl_standard_message_codec_new(), fl_value_new_string(), make_mock_engine(), message_cb(), and response_cb().

157  {
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 }
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
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()
static void response_cb(FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
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)
G_MODULE_EXPORT void fl_basic_message_channel_set_message_handler(FlBasicMessageChannel *self, FlBasicMessageChannelMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)