diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 42a60f1fb1..b2ad478cbd 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -2216,6 +2216,7 @@ FILE: ../../../flutter/shell/platform/linux/fl_task_runner.cc FILE: ../../../flutter/shell/platform/linux/fl_task_runner.h FILE: ../../../flutter/shell/platform/linux/fl_text_input_plugin.cc FILE: ../../../flutter/shell/platform/linux/fl_text_input_plugin.h +FILE: ../../../flutter/shell/platform/linux/fl_text_input_plugin_test.cc FILE: ../../../flutter/shell/platform/linux/fl_texture.cc FILE: ../../../flutter/shell/platform/linux/fl_texture_gl.cc FILE: ../../../flutter/shell/platform/linux/fl_texture_gl_private.h diff --git a/engine/src/flutter/shell/platform/linux/BUILD.gn b/engine/src/flutter/shell/platform/linux/BUILD.gn index d444ed4d09..42d738e01f 100644 --- a/engine/src/flutter/shell/platform/linux/BUILD.gn +++ b/engine/src/flutter/shell/platform/linux/BUILD.gn @@ -216,6 +216,7 @@ executable("flutter_linux_unittests") { "fl_standard_message_codec_test.cc", "fl_standard_method_codec_test.cc", "fl_string_codec_test.cc", + "fl_text_input_plugin_test.cc", "fl_texture_gl_test.cc", "fl_texture_registrar_test.cc", "fl_value_test.cc", diff --git a/engine/src/flutter/shell/platform/linux/fl_text_input_plugin.cc b/engine/src/flutter/shell/platform/linux/fl_text_input_plugin.cc index 7f1b7f94e4..6dff9bc33b 100644 --- a/engine/src/flutter/shell/platform/linux/fl_text_input_plugin.cc +++ b/engine/src/flutter/shell/platform/linux/fl_text_input_plugin.cc @@ -85,8 +85,8 @@ struct FlTextInputPluginPrivate { flutter::TextInputModel* text_model; - // The owning Flutter view. - FlView* view; + // The owning native window. + GdkWindow* window; // A 4x4 matrix that maps from `EditableText` local coordinates to the // coordinate system of `PipelineOwner.rootNode`. @@ -260,8 +260,7 @@ static void im_preedit_start_cb(FlTextInputPlugin* self) { priv->text_model->BeginComposing(); // Set the native window used for system input method windows. - GdkWindow* window = gtk_widget_get_window(GTK_WIDGET(priv->view)); - gtk_im_context_set_client_window(priv->im_context, window); + gtk_im_context_set_client_window(priv->im_context, priv->window); } // Signal handler for GtkIMContext::preedit-changed @@ -420,8 +419,7 @@ static FlMethodResponse* show(FlTextInputPlugin* self) { } // Set the native window used for system input method windows. - GdkWindow* window = gtk_widget_get_window(GTK_WIDGET(priv->view)); - gtk_im_context_set_client_window(priv->im_context, window); + gtk_im_context_set_client_window(priv->im_context, priv->window); gtk_im_context_focus_in(priv->im_context); @@ -601,7 +599,7 @@ static void fl_text_input_plugin_dispose(GObject* object) { delete priv->text_model; priv->text_model = nullptr; } - priv->view = nullptr; + priv->window = nullptr; G_OBJECT_CLASS(fl_text_input_plugin_parent_class)->dispose(object); } @@ -728,7 +726,7 @@ static void fl_text_input_plugin_init(FlTextInputPlugin* self) { FlTextInputPlugin* fl_text_input_plugin_new( FlBinaryMessenger* messenger, - FlView* view, + GdkWindow* window, FlTextInputPluginImFilter im_filter) { g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr); g_return_val_if_fail(im_filter != nullptr, nullptr); @@ -743,7 +741,7 @@ FlTextInputPlugin* fl_text_input_plugin_new( fl_method_channel_new(messenger, kChannelName, FL_METHOD_CODEC(codec)); fl_method_channel_set_method_call_handler(priv->channel, method_call_cb, self, nullptr); - priv->view = view; + priv->window = window; priv->im_filter = im_filter; return self; diff --git a/engine/src/flutter/shell/platform/linux/fl_text_input_plugin.h b/engine/src/flutter/shell/platform/linux/fl_text_input_plugin.h index f8d92b58f2..9ca2924a07 100644 --- a/engine/src/flutter/shell/platform/linux/fl_text_input_plugin.h +++ b/engine/src/flutter/shell/platform/linux/fl_text_input_plugin.h @@ -5,11 +5,10 @@ #ifndef FLUTTER_SHELL_TEXT_INPUT_LINUX_FL_TEXT_INPUT_PLUGIN_H_ #define FLUTTER_SHELL_TEXT_INPUT_LINUX_FL_TEXT_INPUT_PLUGIN_H_ -#include +#include #include "flutter/shell/platform/linux/fl_key_event.h" #include "flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h" -#include "flutter/shell/platform/linux/public/flutter_linux/fl_view.h" /** * FlTextInputPluginImFilter: @@ -51,7 +50,7 @@ struct _FlTextInputPluginClass { /** * fl_text_input_plugin_new: * @messenger: an #FlBinaryMessenger. - * @view: the #FlView with which the text input plugin is associated. + * @window: the #GdkWindow with which the text input plugin is associated. * @im_filter: a function used to allow an input method to internally handle * key press and release events. Typically a wrap of * #gtk_im_context_filter_keypress. Must not be nullptr. @@ -63,7 +62,7 @@ struct _FlTextInputPluginClass { */ FlTextInputPlugin* fl_text_input_plugin_new( FlBinaryMessenger* messenger, - FlView* view, + GdkWindow* window, FlTextInputPluginImFilter im_filter); /** diff --git a/engine/src/flutter/shell/platform/linux/fl_text_input_plugin_test.cc b/engine/src/flutter/shell/platform/linux/fl_text_input_plugin_test.cc new file mode 100644 index 0000000000..a251738a16 --- /dev/null +++ b/engine/src/flutter/shell/platform/linux/fl_text_input_plugin_test.cc @@ -0,0 +1,85 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux/fl_text_input_plugin.h" +#include "flutter/shell/platform/linux/fl_method_codec_private.h" +#include "flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h" +#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_method_codec.h" +#include "flutter/shell/platform/linux/public/flutter_linux/fl_value.h" +#include "flutter/shell/platform/linux/testing/fl_test.h" +#include "flutter/shell/platform/linux/testing/mock_binary_messenger.h" +#include "flutter/shell/platform/linux/testing/mock_binary_messenger_response_handle.h" +#include "flutter/testing/testing.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +void printTo(FlMethodResponse* response, ::std::ostream* os) { + *os << ::testing::PrintToString( + fl_method_response_get_result(response, nullptr)); +} + +MATCHER_P(SuccessResponse, result, "") { + g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new(); + g_autoptr(FlMethodResponse) response = + fl_method_codec_decode_response(FL_METHOD_CODEC(codec), arg, nullptr); + if (fl_value_equal(fl_method_response_get_result(response, nullptr), + result)) { + return true; + } + *result_listener << ::testing::PrintToString(response); + return false; +} + +static FlValue* build_map(std::map args) { + FlValue* value = fl_value_new_map(); + for (auto it = args.begin(); it != args.end(); ++it) { + fl_value_set_string_take(value, it->first, it->second); + } + return value; +} + +static FlValue* build_list(std::vector args) { + FlValue* value = fl_value_new_list(); + for (auto it = args.begin(); it != args.end(); ++it) { + fl_value_append_take(value, *it); + } + return value; +} + +TEST(FlTextInputPluginTest, SetClient) { + ::testing::NiceMock mock; + g_autoptr(FlBinaryMessenger) messenger = fl_binary_messenger_new_mock(&mock); + auto filter = + +[](GtkIMContext* im_context, gpointer gdk_event) { return false; }; + + fl_text_input_plugin_new(messenger, nullptr, + FlTextInputPluginImFilter(filter)); + + EXPECT_TRUE(mock.HasMessageHandler("flutter/textinput")); + + g_autoptr(FlValue) args = build_list({ + fl_value_new_int(1), // client id + build_map({ + {"inputAction", fl_value_new_string("")}, + {"inputType", build_map({ + {"name", fl_value_new_string("TextInputType.text")}, + })}, + {"enableDeltaModel", fl_value_new_bool(false)}, + }), + }); + + g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new(); + g_autoptr(GBytes) message = fl_method_codec_encode_method_call( + FL_METHOD_CODEC(codec), "TextInput.setClient", args, nullptr); + + g_autoptr(FlValue) null = fl_value_new_null(); + EXPECT_CALL(mock, fl_binary_messenger_send_response( + ::testing::Eq(messenger), + ::testing::A(), + SuccessResponse(null), ::testing::A())) + .WillOnce(::testing::Return(true)); + + mock.ReceiveMessage(messenger, "flutter/textinput", message); +} diff --git a/engine/src/flutter/shell/platform/linux/fl_view.cc b/engine/src/flutter/shell/platform/linux/fl_view.cc index 4e360534c3..2c0d468b4d 100644 --- a/engine/src/flutter/shell/platform/linux/fl_view.cc +++ b/engine/src/flutter/shell/platform/linux/fl_view.cc @@ -103,8 +103,9 @@ static gboolean text_input_im_filter_by_gtk(GtkIMContext* im_context, // Initialize keyboard manager. static void init_keyboard(FlView* self) { FlBinaryMessenger* messenger = fl_engine_get_binary_messenger(self->engine); - self->text_input_plugin = - fl_text_input_plugin_new(messenger, self, text_input_im_filter_by_gtk); + self->text_input_plugin = fl_text_input_plugin_new( + messenger, gtk_widget_get_window(GTK_WIDGET(self)), + text_input_im_filter_by_gtk); self->keyboard_manager = fl_keyboard_manager_new(FL_KEYBOARD_VIEW_DELEGATE(self)); } diff --git a/engine/src/flutter/shell/platform/linux/testing/mock_binary_messenger.cc b/engine/src/flutter/shell/platform/linux/testing/mock_binary_messenger.cc index fc2f8dd621..ccc5f596f1 100644 --- a/engine/src/flutter/shell/platform/linux/testing/mock_binary_messenger.cc +++ b/engine/src/flutter/shell/platform/linux/testing/mock_binary_messenger.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "flutter/shell/platform/linux/testing/mock_binary_messenger.h" +#include "flutter/shell/platform/linux/testing/mock_binary_messenger_response_handle.h" using namespace flutter::testing; @@ -17,6 +18,30 @@ struct _FlMockBinaryMessenger { MockBinaryMessenger* mock; }; +bool MockBinaryMessenger::HasMessageHandler(const gchar* channel) const { + return message_handlers.at(channel) != nullptr; +} + +void MockBinaryMessenger::SetMessageHandler( + const gchar* channel, + FlBinaryMessengerMessageHandler handler, + gpointer user_data) { + message_handlers[channel] = handler; + user_datas[channel] = user_data; +} + +void MockBinaryMessenger::ReceiveMessage(FlBinaryMessenger* messenger, + const gchar* channel, + GBytes* message) { + FlBinaryMessengerMessageHandler handler = message_handlers[channel]; + if (response_handles[channel] == nullptr) { + response_handles[channel] = FL_BINARY_MESSENGER_RESPONSE_HANDLE( + fl_mock_binary_messenger_response_handle_new()); + } + handler(messenger, channel, message, response_handles[channel], + user_datas[channel]); +} + static void fl_mock_binary_messenger_iface_init( FlBinaryMessengerInterface* iface); @@ -38,6 +63,7 @@ static void fl_mock_binary_messenger_set_message_handler_on_channel( GDestroyNotify destroy_notify) { g_return_if_fail(FL_IS_MOCK_BINARY_MESSENGER(messenger)); FlMockBinaryMessenger* self = FL_MOCK_BINARY_MESSENGER(messenger); + self->mock->SetMessageHandler(channel, handler, user_data); self->mock->fl_binary_messenger_set_message_handler_on_channel( messenger, channel, handler, user_data, destroy_notify); } diff --git a/engine/src/flutter/shell/platform/linux/testing/mock_binary_messenger.h b/engine/src/flutter/shell/platform/linux/testing/mock_binary_messenger.h index 74d9f714bb..dee2a0cbaa 100644 --- a/engine/src/flutter/shell/platform/linux/testing/mock_binary_messenger.h +++ b/engine/src/flutter/shell/platform/linux/testing/mock_binary_messenger.h @@ -5,6 +5,8 @@ #ifndef FLUTTER_SHELL_PLATFORM_LINUX_TESTING_MOCK_BINARY_MESSENGER_H_ #define FLUTTER_SHELL_PLATFORM_LINUX_TESTING_MOCK_BINARY_MESSENGER_H_ +#include + #include "flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h" #include "gmock/gmock.h" @@ -40,6 +42,23 @@ class MockBinaryMessenger { GBytes*(FlBinaryMessenger* messenger, GAsyncResult* result, GError** error)); + + bool HasMessageHandler(const gchar* channel) const; + + void SetMessageHandler(const gchar* channel, + FlBinaryMessengerMessageHandler handler, + gpointer user_data); + + void ReceiveMessage(FlBinaryMessenger* messenger, + const gchar* channel, + GBytes* message); + + private: + std::unordered_map + message_handlers; + std::unordered_map + response_handles; + std::unordered_map user_datas; }; } // namespace testing