Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make a mock messenger that can easily mock channels
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.
  • Loading branch information
robert-ancell committed Nov 28, 2024
commit 3104059965a707f0fddb10a5d72ec40599c51b52
3 changes: 1 addition & 2 deletions shell/platform/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ executable("flutter_linux_unittests") {
"testing/fl_test.cc",
"testing/fl_test_gtk_logs.cc",
"testing/fl_test_gtk_logs.h",
"testing/mock_binary_messenger.cc",
"testing/mock_binary_messenger_response_handle.cc",
"testing/fl_mock_binary_messenger.cc",
"testing/mock_engine.cc",
"testing/mock_epoxy.cc",
"testing/mock_im_context.cc",
Expand Down
31 changes: 28 additions & 3 deletions shell/platform/linux/fl_binary_messenger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,34 @@
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"
#include "flutter/shell/platform/linux/testing/fl_test.h"
#include "flutter/shell/platform/linux/testing/mock_binary_messenger_response_handle.h"
#include "flutter/shell/platform/linux/testing/mock_renderer.h"

G_DECLARE_FINAL_TYPE(FlFakeBinaryMessengerResponseHandle,
fl_fake_binary_messenger_response_handle,
FL,
FAKE_BINARY_MESSENGER_RESPONSE_HANDLE,
FlBinaryMessengerResponseHandle)

struct _FlFakeBinaryMessengerResponseHandle {
FlBinaryMessengerResponseHandle parent_instance;
};

G_DEFINE_TYPE(FlFakeBinaryMessengerResponseHandle,
fl_fake_binary_messenger_response_handle,
fl_binary_messenger_response_handle_get_type());

static void fl_fake_binary_messenger_response_handle_class_init(
FlFakeBinaryMessengerResponseHandleClass* klass) {}

static void fl_fake_binary_messenger_response_handle_init(
FlFakeBinaryMessengerResponseHandle* self) {}

FlFakeBinaryMessengerResponseHandle*
fl_fake_binary_messenger_response_handle_new() {
return FL_FAKE_BINARY_MESSENGER_RESPONSE_HANDLE(
g_object_new(fl_fake_binary_messenger_response_handle_get_type(), NULL));
}

G_DECLARE_FINAL_TYPE(FlFakeBinaryMessenger,
fl_fake_binary_messenger,
FL,
Expand Down Expand Up @@ -55,7 +80,7 @@ static gboolean send_message_cb(gpointer user_data) {
g_autoptr(GBytes) message = g_bytes_new(text, strlen(text));
self->message_handler(FL_BINARY_MESSENGER(self), "CHANNEL", message,
FL_BINARY_MESSENGER_RESPONSE_HANDLE(
fl_mock_binary_messenger_response_handle_new()),
fl_fake_binary_messenger_response_handle_new()),
self->message_handler_user_data);

return FALSE;
Expand Down Expand Up @@ -83,7 +108,7 @@ static gboolean send_response(FlBinaryMessenger* messenger,
GError** error) {
FlFakeBinaryMessenger* self = FL_FAKE_BINARY_MESSENGER(messenger);

EXPECT_TRUE(FL_IS_MOCK_BINARY_MESSENGER_RESPONSE_HANDLE(response_handle));
EXPECT_TRUE(FL_IS_FAKE_BINARY_MESSENGER_RESPONSE_HANDLE(response_handle));

g_autofree gchar* text =
g_strndup(static_cast<const gchar*>(g_bytes_get_data(response, nullptr)),
Expand Down
17 changes: 3 additions & 14 deletions shell/platform/linux/fl_key_channel_responder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ struct _FlKeyChannelResponder {
GObject parent_instance;

FlBasicMessageChannel* channel;

FlKeyChannelResponderMock* mock;
};

G_DEFINE_TYPE(FlKeyChannelResponder, fl_key_channel_responder, G_TYPE_OBJECT)
Expand All @@ -117,9 +115,6 @@ static void handle_response(GObject* object,
FlBasicMessageChannel* messageChannel = FL_BASIC_MESSAGE_CHANNEL(object);
FlValue* message =
fl_basic_message_channel_send_finish(messageChannel, result, &error);
if (self->mock != nullptr && self->mock->value_converter != nullptr) {
message = self->mock->value_converter(message);
}
bool handled = false;
if (error != nullptr) {
g_warning("Unable to retrieve framework response: %s", error->message);
Expand Down Expand Up @@ -152,22 +147,16 @@ static void fl_key_channel_responder_init(FlKeyChannelResponder* self) {}

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

FlKeyChannelResponder* self = FL_KEY_CHANNEL_RESPONDER(
g_object_new(fl_key_channel_responder_get_type(), nullptr));
self->mock = mock;

g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
const char* channel_name =
mock == nullptr ? kChannelName : mock->channel_name;
self->channel = fl_basic_message_channel_new(messenger, channel_name,
self->channel = fl_basic_message_channel_new(messenger, kChannelName,
FL_MESSAGE_CODEC(codec));

return self;
Expand Down
25 changes: 1 addition & 24 deletions shell/platform/linux/fl_key_channel_responder.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,6 @@

typedef FlValue* (*FlValueConverter)(FlValue*);

/**
* FlKeyChannelResponderMock:
*
* Allows mocking of FlKeyChannelResponder methods and values. Only used in
* unittests.
*/
typedef struct _FlKeyChannelResponderMock {
/**
* FlKeyChannelResponderMock::value_converter:
* If #value_converter is not nullptr, then this function is applied to the
* reply of the message, whose return value is taken as the message reply.
*/
FlValueConverter value_converter;

/**
* FlKeyChannelResponderMock::channel_name:
* Mocks the channel name to send the message.
*/
const char* channel_name;
} FlKeyChannelResponderMock;

G_BEGIN_DECLS

G_DECLARE_FINAL_TYPE(FlKeyChannelResponder,
Expand Down Expand Up @@ -64,15 +43,13 @@ typedef void (*FlKeyChannelResponderAsyncCallback)(bool handled,
/**
* fl_key_channel_responder_new:
* @messenger: the messenger that the message channel should be built on.
* @mock: options to mock several functionalities. Only used in unittests.
*
* Creates a new #FlKeyChannelResponder.
*
* Returns: a new #FlKeyChannelResponder.
*/
FlKeyChannelResponder* fl_key_channel_responder_new(
FlBinaryMessenger* messenger,
FlKeyChannelResponderMock* mock = nullptr);
FlBinaryMessenger* messenger);

/**
* fl_key_channel_responder_handle_event:
Expand Down
Loading