Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e39301f

Browse files
Add tests for FlBinaryMessenger, FlBasicMessageChannel, FlMethodChannel (#18638)
1 parent 8c8c7b0 commit e39301f

File tree

10 files changed

+1300
-7
lines changed

10 files changed

+1300
-7
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,10 +1189,12 @@ FILE: ../../../flutter/shell/platform/glfw/public/flutter_glfw.h
11891189
FILE: ../../../flutter/shell/platform/glfw/text_input_plugin.cc
11901190
FILE: ../../../flutter/shell/platform/glfw/text_input_plugin.h
11911191
FILE: ../../../flutter/shell/platform/linux/fl_basic_message_channel.cc
1192+
FILE: ../../../flutter/shell/platform/linux/fl_basic_message_channel_test.cc
11921193
FILE: ../../../flutter/shell/platform/linux/fl_binary_codec.cc
11931194
FILE: ../../../flutter/shell/platform/linux/fl_binary_codec_test.cc
11941195
FILE: ../../../flutter/shell/platform/linux/fl_binary_messenger.cc
11951196
FILE: ../../../flutter/shell/platform/linux/fl_binary_messenger_private.h
1197+
FILE: ../../../flutter/shell/platform/linux/fl_binary_messenger_test.cc
11961198
FILE: ../../../flutter/shell/platform/linux/fl_dart_project.cc
11971199
FILE: ../../../flutter/shell/platform/linux/fl_dart_project_test.cc
11981200
FILE: ../../../flutter/shell/platform/linux/fl_engine.cc
@@ -1209,6 +1211,7 @@ FILE: ../../../flutter/shell/platform/linux/fl_method_call.cc
12091211
FILE: ../../../flutter/shell/platform/linux/fl_method_call_private.h
12101212
FILE: ../../../flutter/shell/platform/linux/fl_method_channel.cc
12111213
FILE: ../../../flutter/shell/platform/linux/fl_method_channel_private.h
1214+
FILE: ../../../flutter/shell/platform/linux/fl_method_channel_test.cc
12121215
FILE: ../../../flutter/shell/platform/linux/fl_method_codec.cc
12131216
FILE: ../../../flutter/shell/platform/linux/fl_method_codec_private.h
12141217
FILE: ../../../flutter/shell/platform/linux/fl_method_codec_test.cc

shell/platform/embedder/BUILD.gn

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ template("embedder_source_set") {
2424
source_set(target_name) {
2525
sources = [
2626
"embedder.cc",
27-
"embedder.h",
2827
"embedder_engine.cc",
2928
"embedder_engine.h",
3029
"embedder_external_texture_gl.cc",
@@ -75,6 +74,10 @@ template("embedder_source_set") {
7574
"//third_party/skia",
7675
]
7776

77+
public_deps = [
78+
":embedder_headers",
79+
]
80+
7881
public_configs += [ "//flutter:config" ]
7982
}
8083
}
@@ -87,6 +90,14 @@ embedder_source_set("embedder_with_symbol_prefix") {
8790
public_configs = [ ":embedder_prefix_config" ]
8891
}
8992

93+
source_set("embedder_headers") {
94+
public = [
95+
"embedder.h",
96+
]
97+
98+
public_configs = [ "//flutter:config" ]
99+
}
100+
90101
config("embedder_prefix_config") {
91102
defines = [ "FLUTTER_API_SYMBOL_PREFIX=Embedder" ]
92103
}

shell/platform/linux/BUILD.gn

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ config("relative_flutter_linux_headers") {
7070
include_dirs = [ "public" ]
7171
}
7272

73-
source_set("flutter_linux") {
73+
source_set("flutter_linux_sources") {
7474
public = _public_headers
7575

76+
configs += [ "//flutter/shell/platform/linux/config:gtk" ]
77+
7678
sources = [
7779
"fl_basic_message_channel.cc",
7880
"fl_binary_codec.cc",
@@ -99,19 +101,29 @@ source_set("flutter_linux") {
99101
"fl_view.cc",
100102
]
101103

104+
# Set flag to stop headers being directly included (library users should not do this)
105+
defines = [ "FLUTTER_LINUX_COMPILATION" ]
106+
107+
deps = [
108+
"//flutter/shell/platform/common/cpp:common_cpp_input",
109+
"//flutter/shell/platform/embedder:embedder_headers",
110+
"//third_party/rapidjson",
111+
]
112+
}
113+
114+
source_set("flutter_linux") {
102115
configs += [
103116
"//flutter/shell/platform/linux/config:gtk",
104117
"//flutter/shell/platform/linux/config:egl",
105118
"//third_party/khronos:khronos_headers",
106119
]
107120

108-
# Set flag to stop headers being directly included (library users should not do this)
109-
defines = [ "FLUTTER_LINUX_COMPILATION" ]
121+
public_deps = [
122+
":flutter_linux_sources",
123+
]
110124

111125
deps = [
112-
"//flutter/shell/platform/common/cpp:common_cpp_input",
113126
"//flutter/shell/platform/embedder:embedder_with_symbol_prefix",
114-
"//third_party/rapidjson",
115127
]
116128
}
117129

@@ -123,18 +135,24 @@ executable("flutter_linux_unittests") {
123135
testonly = true
124136

125137
sources = [
138+
"fl_basic_message_channel_test.cc",
126139
"fl_binary_codec_test.cc",
140+
"fl_binary_messenger_test.cc",
127141
"fl_dart_project_test.cc",
128142
"fl_json_message_codec_test.cc",
129143
"fl_json_method_codec_test.cc",
130144
"fl_message_codec_test.cc",
145+
"fl_method_channel_test.cc",
131146
"fl_method_codec_test.cc",
132147
"fl_method_response_test.cc",
133148
"fl_standard_message_codec_test.cc",
134149
"fl_standard_method_codec_test.cc",
135150
"fl_string_codec_test.cc",
136151
"fl_value_test.cc",
137152
"testing/fl_test.cc",
153+
"testing/mock_egl.cc",
154+
"testing/mock_engine.cc",
155+
"testing/mock_renderer.cc",
138156
]
139157

140158
public_configs = [ "//flutter:config" ]
@@ -145,8 +163,10 @@ executable("flutter_linux_unittests") {
145163
defines = [ "FLUTTER_LINUX_COMPILATION" ]
146164

147165
deps = [
148-
":flutter_linux",
149166
":flutter_linux_fixtures",
167+
":flutter_linux_sources",
168+
"//flutter/runtime:libdart",
169+
"//flutter/shell/platform/embedder:embedder_headers",
150170
"//flutter/testing",
151171
]
152172
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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("data");
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) error = nullptr;
20+
EXPECT_TRUE(fl_engine_start(engine, &error));
21+
EXPECT_EQ(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));
132+
fl_basic_message_channel_set_message_handler(messages_channel, message_cb,
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));
139+
fl_basic_message_channel_set_message_handler(responses_channel, response_cb,
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+
}

0 commit comments

Comments
 (0)