Flutter Engine
fl_basic_message_channel_test.cc File Reference
#include "gtest/gtest.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_basic_message_channel.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
#include "flutter/shell/platform/linux/testing/mock_renderer.h"

Go to the source code of this file.

Functions

static FlEngine * make_mock_engine ()
 
static void echo_response_cb (GObject *object, GAsyncResult *result, gpointer user_data)
 
 TEST (FlBasicMessageChannelTest, SendMessage)
 
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 27 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().

29  {
30  g_autoptr(GError) error = nullptr;
31  g_autoptr(FlValue) message = fl_basic_message_channel_send_finish(
32  FL_BASIC_MESSAGE_CHANNEL(object), result, &error);
33  EXPECT_NE(message, nullptr);
34  EXPECT_EQ(error, nullptr);
35 
36  EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_STRING);
37  EXPECT_STREQ(fl_value_get_string(message), "Hello World!");
38 
39  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
40 }
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:395
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
FlMethodResponse GError ** error
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)
G_BEGIN_DECLS FlValue gpointer user_data
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:596

◆ failure_response_cb()

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

Definition at line 60 of file fl_basic_message_channel_test.cc.

References error, and fl_basic_message_channel_send_finish().

Referenced by TEST().

62  {
63  g_autoptr(GError) error = nullptr;
64  g_autoptr(FlValue) message = fl_basic_message_channel_send_finish(
65  FL_BASIC_MESSAGE_CHANNEL(object), result, &error);
66  EXPECT_EQ(message, nullptr);
67  EXPECT_NE(error, nullptr);
68 
69  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
70 }
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
FlMethodResponse GError ** error
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)
G_BEGIN_DECLS FlValue gpointer user_data

◆ make_mock_engine()

static FlEngine* make_mock_engine ( )
static

Definition at line 15 of file fl_basic_message_channel_test.cc.

References engine, fl_dart_project_new(), fl_engine_new(), fl_engine_start(), and fl_mock_renderer_new().

Referenced by TEST().

15  {
16  g_autoptr(FlDartProject) project = fl_dart_project_new();
17  g_autoptr(FlMockRenderer) renderer = fl_mock_renderer_new();
18  g_autoptr(FlEngine) engine = fl_engine_new(project, FL_RENDERER(renderer));
19  g_autoptr(GError) engine_error = nullptr;
20  EXPECT_TRUE(fl_engine_start(engine, &engine_error));
21  EXPECT_EQ(engine_error, nullptr);
22 
23  return static_cast<FlEngine*>(g_object_ref(engine));
24 }
FlMockRenderer * fl_mock_renderer_new()
gboolean fl_engine_start(FlEngine *self, GError **error)
Definition: fl_engine.cc:352
FlEngine * fl_engine_new(FlDartProject *project, FlRenderer *renderer)
Definition: fl_engine.cc:337
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()

◆ message_cb()

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

Definition at line 90 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().

93  {
94  EXPECT_NE(message, nullptr);
95  EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_STRING);
96  EXPECT_STREQ(fl_value_get_string(message), "Marco!");
97 
98  g_autoptr(GError) error = nullptr;
99  g_autoptr(FlValue) response = fl_value_new_string("Polo!");
100  EXPECT_TRUE(fl_basic_message_channel_respond(channel, response_handle,
101  response, &error));
102  EXPECT_EQ(error, nullptr);
103 }
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:395
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
FlMethodResponse GError ** error
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:247
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:596

◆ response_cb()

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

Definition at line 107 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().

110  {
111  EXPECT_NE(message, nullptr);
112  EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_STRING);
113  EXPECT_STREQ(fl_value_get_string(message), "Polo!");
114 
115  fl_basic_message_channel_respond(channel, response_handle, nullptr, nullptr);
116 
117  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
118 }
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:395
G_BEGIN_DECLS FlValue gpointer user_data
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:596

◆ TEST() [1/3]

TEST ( FlBasicMessageChannelTest  ,
SendMessage   
)

Definition at line 43 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().

43  {
44  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
45 
46  g_autoptr(FlEngine) engine = make_mock_engine();
47  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
48  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
49  g_autoptr(FlBasicMessageChannel) channel = fl_basic_message_channel_new(
50  messenger, "test/echo", FL_MESSAGE_CODEC(codec));
51  g_autoptr(FlValue) message = fl_value_new_string("Hello World!");
52  fl_basic_message_channel_send(channel, message, nullptr, echo_response_cb,
53  loop);
54 
55  // Blocks here until echo_response_cb is called.
56  g_main_loop_run(loop);
57 }
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
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:247
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() [2/3]

TEST ( FlBasicMessageChannelTest  ,
SendFailure   
)

Definition at line 73 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().

73  {
74  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
75 
76  g_autoptr(FlEngine) engine = make_mock_engine();
77  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
78  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
79  g_autoptr(FlBasicMessageChannel) channel = fl_basic_message_channel_new(
80  messenger, "test/failure", FL_MESSAGE_CODEC(codec));
81  g_autoptr(FlValue) message = fl_value_new_string("Hello World!");
82  fl_basic_message_channel_send(channel, message, nullptr, failure_response_cb,
83  loop);
84 
85  // Blocks here until failure_response_cb is called.
86  g_main_loop_run(loop);
87 }
static void failure_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
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:247
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/3]

TEST ( FlBasicMessageChannelTest  ,
ReceiveMessage   
)

Definition at line 121 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().

121  {
122  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
123 
124  g_autoptr(FlEngine) engine = make_mock_engine();
125  FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
126 
127  // Listen for messages from the engine.
128  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
129  g_autoptr(FlBasicMessageChannel) messages_channel =
130  fl_basic_message_channel_new(messenger, "test/messages",
131  FL_MESSAGE_CODEC(codec));
133  nullptr, nullptr);
134 
135  // Listen for response from the engine.
136  g_autoptr(FlBasicMessageChannel) responses_channel =
137  fl_basic_message_channel_new(messenger, "test/responses",
138  FL_MESSAGE_CODEC(codec));
140  loop, nullptr);
141 
142  // Triggger the engine to send a message.
143  g_autoptr(FlBasicMessageChannel) control_channel =
144  fl_basic_message_channel_new(messenger, "test/send-message",
145  FL_MESSAGE_CODEC(codec));
146  g_autoptr(FlValue) message = fl_value_new_string("Marco!");
147  fl_basic_message_channel_send(control_channel, message, nullptr, nullptr,
148  nullptr);
149 
150  // Blocks here until response_cb is called.
151  g_main_loop_run(loop);
152 }
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
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 void response_cb(FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
static FlEngine * make_mock_engine()
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:247
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)