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/fl_binary_messenger_private.h"
9 #include "flutter/shell/platform/linux/fl_engine_private.h"
10 #include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
11 #include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
12 #include "flutter/shell/platform/linux/testing/mock_renderer.h"
13 
14 // Creates a mock engine that responds to platform messages.
15 static FlEngine* make_mock_engine() {
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 }
25 
26 // Called when the message response is received in the SendMessage test.
27 static void echo_response_cb(GObject* object,
28  GAsyncResult* result,
29  gpointer user_data) {
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 }
41 
42 // Checks sending a message works.
43 TEST(FlBasicMessageChannelTest, SendMessage) {
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 }
58 
59 // Called when the message response is received in the SendFailure test.
60 static void failure_response_cb(GObject* object,
61  GAsyncResult* result,
62  gpointer user_data) {
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 }
71 
72 // Checks the engine reporting a send failure is handled.
73 TEST(FlBasicMessageChannelTest, SendFailure) {
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 }
88 
89 // Called when a message is received from the engine in the ReceiveMessage test.
90 static void message_cb(FlBasicMessageChannel* channel,
91  FlValue* message,
92  FlBasicMessageChannelResponseHandle* response_handle,
93  gpointer user_data) {
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 }
104 
105 // Called when a the test engine notifies us what response we sent in the
106 // ReceiveMessage test.
107 static void response_cb(FlBasicMessageChannel* channel,
108  FlValue* message,
109  FlBasicMessageChannelResponseHandle* response_handle,
110  gpointer user_data) {
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 }
119 
120 // Checks the shell able to receive and respond to messages from the engine.
121 TEST(FlBasicMessageChannelTest, ReceiveMessage) {
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 }
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
FlMockRenderer * fl_mock_renderer_new()
static void failure_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
FlMethodResponse GError ** error
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
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)
gboolean fl_engine_start(FlEngine *self, GError **error)
Definition: fl_engine.cc:352
static void response_cb(FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
TEST(FlBasicMessageChannelTest, SendMessage)
G_BEGIN_DECLS FlMethodCall 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)
FlEngine * fl_engine_new(FlDartProject *project, FlRenderer *renderer)
Definition: fl_engine.cc:337
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:596
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()
FlBinaryMessenger * fl_binary_messenger_new(FlEngine *engine)