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

Commit c9fa56d

Browse files
Make a mock messenger that can easily mock channels (#56867)
The previous mock required knowing the specific functions used in the binary messenger, this method instead allows test code to provide complete platform channel implementation for testing and make simulated platform channel calls into embedder code.
1 parent 27e3e40 commit c9fa56d

19 files changed

Lines changed: 2165 additions & 1691 deletions

shell/platform/linux/BUILD.gn

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,10 @@ executable("flutter_linux_unittests") {
249249
"fl_view_test.cc",
250250
"fl_window_state_monitor_test.cc",
251251
"key_mapping_test.cc",
252+
"testing/fl_mock_binary_messenger.cc",
252253
"testing/fl_test.cc",
253254
"testing/fl_test_gtk_logs.cc",
254255
"testing/fl_test_gtk_logs.h",
255-
"testing/mock_binary_messenger.cc",
256-
"testing/mock_binary_messenger_response_handle.cc",
257256
"testing/mock_engine.cc",
258257
"testing/mock_epoxy.cc",
259258
"testing/mock_im_context.cc",

shell/platform/linux/fl_binary_messenger_test.cc

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,34 @@
1616
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
1717
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"
1818
#include "flutter/shell/platform/linux/testing/fl_test.h"
19-
#include "flutter/shell/platform/linux/testing/mock_binary_messenger_response_handle.h"
2019
#include "flutter/shell/platform/linux/testing/mock_renderer.h"
2120

21+
G_DECLARE_FINAL_TYPE(FlFakeBinaryMessengerResponseHandle,
22+
fl_fake_binary_messenger_response_handle,
23+
FL,
24+
FAKE_BINARY_MESSENGER_RESPONSE_HANDLE,
25+
FlBinaryMessengerResponseHandle)
26+
27+
struct _FlFakeBinaryMessengerResponseHandle {
28+
FlBinaryMessengerResponseHandle parent_instance;
29+
};
30+
31+
G_DEFINE_TYPE(FlFakeBinaryMessengerResponseHandle,
32+
fl_fake_binary_messenger_response_handle,
33+
fl_binary_messenger_response_handle_get_type());
34+
35+
static void fl_fake_binary_messenger_response_handle_class_init(
36+
FlFakeBinaryMessengerResponseHandleClass* klass) {}
37+
38+
static void fl_fake_binary_messenger_response_handle_init(
39+
FlFakeBinaryMessengerResponseHandle* self) {}
40+
41+
FlFakeBinaryMessengerResponseHandle*
42+
fl_fake_binary_messenger_response_handle_new() {
43+
return FL_FAKE_BINARY_MESSENGER_RESPONSE_HANDLE(
44+
g_object_new(fl_fake_binary_messenger_response_handle_get_type(), NULL));
45+
}
46+
2247
G_DECLARE_FINAL_TYPE(FlFakeBinaryMessenger,
2348
fl_fake_binary_messenger,
2449
FL,
@@ -55,7 +80,7 @@ static gboolean send_message_cb(gpointer user_data) {
5580
g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
5681
self->message_handler(FL_BINARY_MESSENGER(self), "CHANNEL", message,
5782
FL_BINARY_MESSENGER_RESPONSE_HANDLE(
58-
fl_mock_binary_messenger_response_handle_new()),
83+
fl_fake_binary_messenger_response_handle_new()),
5984
self->message_handler_user_data);
6085

6186
return FALSE;
@@ -83,7 +108,7 @@ static gboolean send_response(FlBinaryMessenger* messenger,
83108
GError** error) {
84109
FlFakeBinaryMessenger* self = FL_FAKE_BINARY_MESSENGER(messenger);
85110

86-
EXPECT_TRUE(FL_IS_MOCK_BINARY_MESSENGER_RESPONSE_HANDLE(response_handle));
111+
EXPECT_TRUE(FL_IS_FAKE_BINARY_MESSENGER_RESPONSE_HANDLE(response_handle));
87112

88113
g_autofree gchar* text =
89114
g_strndup(static_cast<const gchar*>(g_bytes_get_data(response, nullptr)),

shell/platform/linux/fl_key_channel_responder.cc

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ struct _FlKeyChannelResponder {
9494
GObject parent_instance;
9595

9696
FlBasicMessageChannel* channel;
97-
98-
FlKeyChannelResponderMock* mock;
9997
};
10098

10199
G_DEFINE_TYPE(FlKeyChannelResponder, fl_key_channel_responder, G_TYPE_OBJECT)
@@ -117,9 +115,6 @@ static void handle_response(GObject* object,
117115
FlBasicMessageChannel* messageChannel = FL_BASIC_MESSAGE_CHANNEL(object);
118116
FlValue* message =
119117
fl_basic_message_channel_send_finish(messageChannel, result, &error);
120-
if (self->mock != nullptr && self->mock->value_converter != nullptr) {
121-
message = self->mock->value_converter(message);
122-
}
123118
bool handled = false;
124119
if (error != nullptr) {
125120
g_warning("Unable to retrieve framework response: %s", error->message);
@@ -152,22 +147,16 @@ static void fl_key_channel_responder_init(FlKeyChannelResponder* self) {}
152147

153148
// Creates a new FlKeyChannelResponder instance, with a messenger used to send
154149
// messages to the framework, and an FlTextInputHandler that is used to handle
155-
// key events that the framework doesn't handle. Mainly for testing purposes, it
156-
// also takes an optional callback to call when a response is received, and an
157-
// optional channel name to use when sending messages.
150+
// key events that the framework doesn't handle.
158151
FlKeyChannelResponder* fl_key_channel_responder_new(
159-
FlBinaryMessenger* messenger,
160-
FlKeyChannelResponderMock* mock) {
152+
FlBinaryMessenger* messenger) {
161153
g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr);
162154

163155
FlKeyChannelResponder* self = FL_KEY_CHANNEL_RESPONDER(
164156
g_object_new(fl_key_channel_responder_get_type(), nullptr));
165-
self->mock = mock;
166157

167158
g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
168-
const char* channel_name =
169-
mock == nullptr ? kChannelName : mock->channel_name;
170-
self->channel = fl_basic_message_channel_new(messenger, channel_name,
159+
self->channel = fl_basic_message_channel_new(messenger, kChannelName,
171160
FL_MESSAGE_CODEC(codec));
172161

173162
return self;

shell/platform/linux/fl_key_channel_responder.h

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,6 @@
1111

1212
typedef FlValue* (*FlValueConverter)(FlValue*);
1313

14-
/**
15-
* FlKeyChannelResponderMock:
16-
*
17-
* Allows mocking of FlKeyChannelResponder methods and values. Only used in
18-
* unittests.
19-
*/
20-
typedef struct _FlKeyChannelResponderMock {
21-
/**
22-
* FlKeyChannelResponderMock::value_converter:
23-
* If #value_converter is not nullptr, then this function is applied to the
24-
* reply of the message, whose return value is taken as the message reply.
25-
*/
26-
FlValueConverter value_converter;
27-
28-
/**
29-
* FlKeyChannelResponderMock::channel_name:
30-
* Mocks the channel name to send the message.
31-
*/
32-
const char* channel_name;
33-
} FlKeyChannelResponderMock;
34-
3514
G_BEGIN_DECLS
3615

3716
G_DECLARE_FINAL_TYPE(FlKeyChannelResponder,
@@ -64,15 +43,13 @@ typedef void (*FlKeyChannelResponderAsyncCallback)(bool handled,
6443
/**
6544
* fl_key_channel_responder_new:
6645
* @messenger: the messenger that the message channel should be built on.
67-
* @mock: options to mock several functionalities. Only used in unittests.
6846
*
6947
* Creates a new #FlKeyChannelResponder.
7048
*
7149
* Returns: a new #FlKeyChannelResponder.
7250
*/
7351
FlKeyChannelResponder* fl_key_channel_responder_new(
74-
FlBinaryMessenger* messenger,
75-
FlKeyChannelResponderMock* mock = nullptr);
52+
FlBinaryMessenger* messenger);
7653

7754
/**
7855
* fl_key_channel_responder_handle_event:

0 commit comments

Comments
 (0)