diff --git a/engine/src/flutter/shell/platform/windows/keyboard_unittests.cc b/engine/src/flutter/shell/platform/windows/keyboard_unittests.cc index b4495b9bd0..0e277e3f59 100644 --- a/engine/src/flutter/shell/platform/windows/keyboard_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/keyboard_unittests.cc @@ -2203,7 +2203,7 @@ TEST_F(KeyboardTest, TextInputSubmit) { tester.InjectPlatformMessage( "flutter/textinput", "TextInput.setClient", - R"|([108, {"inputAction": "TextInputAction.none"}])|"); + R"|([108, {"inputAction": "TextInputAction.none", "viewId": 0}])|"); // Press Enter tester.InjectKeyboardChanges(std::vector{ diff --git a/engine/src/flutter/shell/platform/windows/testing/engine_modifier.h b/engine/src/flutter/shell/platform/windows/testing/engine_modifier.h index de9e5455a4..bbf9925592 100644 --- a/engine/src/flutter/shell/platform/windows/testing/engine_modifier.h +++ b/engine/src/flutter/shell/platform/windows/testing/engine_modifier.h @@ -43,6 +43,11 @@ class EngineModifier { engine_->views_[kImplicitViewId] = view; } + /// Associate a view with a view id. + void SetViewById(FlutterWindowsView* view, FlutterViewId viewId) { + engine_->views_[viewId] = view; + } + /// Reset the start_time field that is used to align vsync events. void SetStartTime(uint64_t start_time_nanos) { engine_->start_time_ = std::chrono::nanoseconds(start_time_nanos); diff --git a/engine/src/flutter/shell/platform/windows/text_input_plugin.cc b/engine/src/flutter/shell/platform/windows/text_input_plugin.cc index a67eca476c..a35ff9aaa8 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin.cc +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin.cc @@ -38,6 +38,7 @@ static constexpr char kDeltaEndKey[] = "deltaEnd"; static constexpr char kDeltasKey[] = "deltas"; static constexpr char kEnableDeltaModel[] = "enableDeltaModel"; static constexpr char kTextInputAction[] = "inputAction"; +static constexpr char kViewId[] = "viewId"; static constexpr char kTextInputType[] = "inputType"; static constexpr char kTextInputTypeName[] = "name"; static constexpr char kComposingBaseKey[] = "composingBase"; @@ -216,12 +217,12 @@ void TextInputPlugin::HandleMethodCall( if (method.compare(kShowMethod) == 0 || method.compare(kHideMethod) == 0) { // These methods are no-ops. } else if (method.compare(kClearClientMethod) == 0) { - // TODO(loicsharma): Remove implicit view assumption. - // https://github.com/flutter/flutter/issues/142845 - FlutterWindowsView* view = engine_->view(kImplicitViewId); + FlutterWindowsView* view = engine_->view(view_id_); if (view == nullptr) { - result->Error(kInternalConsistencyError, - "Text input is not available in Windows headless mode"); + std::stringstream ss; + ss << "Text input is not available because view with view_id=" << view_id_ + << " cannot be found"; + result->Error(kInternalConsistencyError, ss.str()); return; } if (active_model_ != nullptr && active_model_->composing()) { @@ -255,6 +256,15 @@ void TextInputPlugin::HandleMethodCall( enable_delta_model_json->value.IsBool()) { enable_delta_model = enable_delta_model_json->value.GetBool(); } + auto view_id_json = client_config.FindMember(kViewId); + if (view_id_json != client_config.MemberEnd() && + view_id_json->value.IsInt()) { + view_id_ = view_id_json->value.GetInt(); + } else { + result->Error(kBadArgumentError, + "Could not set client, view ID is null."); + return; + } input_action_ = ""; auto input_action_json = client_config.FindMember(kTextInputAction); if (input_action_json != client_config.MemberEnd() && @@ -328,12 +338,12 @@ void TextInputPlugin::HandleMethodCall( TextRange(composing_base, composing_extent), cursor_offset); } } else if (method.compare(kSetMarkedTextRect) == 0) { - // TODO(loicsharma): Remove implicit view assumption. - // https://github.com/flutter/flutter/issues/142845 - FlutterWindowsView* view = engine_->view(kImplicitViewId); + FlutterWindowsView* view = engine_->view(view_id_); if (view == nullptr) { - result->Error(kInternalConsistencyError, - "Text input is not available in Windows headless mode"); + std::stringstream ss; + ss << "Text input is not available because view with view_id=" << view_id_ + << " cannot be found"; + result->Error(kInternalConsistencyError, ss.str()); return; } if (!method_call.arguments() || method_call.arguments()->IsNull()) { @@ -359,12 +369,12 @@ void TextInputPlugin::HandleMethodCall( Rect transformed_rect = GetCursorRect(); view->OnCursorRectUpdated(transformed_rect); } else if (method.compare(kSetEditableSizeAndTransform) == 0) { - // TODO(loicsharma): Remove implicit view assumption. - // https://github.com/flutter/flutter/issues/142845 - FlutterWindowsView* view = engine_->view(kImplicitViewId); + FlutterWindowsView* view = engine_->view(view_id_); if (view == nullptr) { - result->Error(kInternalConsistencyError, - "Text input is not available in Windows headless mode"); + std::stringstream ss; + ss << "Text input is not available because view with view_id=" << view_id_ + << " cannot be found"; + result->Error(kInternalConsistencyError, ss.str()); return; } if (!method_call.arguments() || method_call.arguments()->IsNull()) { diff --git a/engine/src/flutter/shell/platform/windows/text_input_plugin.h b/engine/src/flutter/shell/platform/windows/text_input_plugin.h index a452e293d6..1a836f9db8 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin.h +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin.h @@ -16,6 +16,7 @@ #include "flutter/shell/platform/common/json_method_codec.h" #include "flutter/shell/platform/common/text_editing_delta.h" #include "flutter/shell/platform/common/text_input_model.h" +#include "flutter/shell/platform/embedder/embedder.h" #include "flutter/shell/platform/windows/keyboard_handler_base.h" namespace flutter { @@ -70,6 +71,9 @@ class TextInputPlugin { virtual void ComposeChangeHook(const std::u16string& text, int cursor_pos); private: + // Allows modifying the TextInputPlugin in tests. + friend class TextInputPluginModifier; + // Sends the current state of the given model to the Flutter engine. void SendStateUpdate(const TextInputModel& model); @@ -98,6 +102,9 @@ class TextInputPlugin { // The active client id. int client_id_; + // The active view id. + FlutterViewId view_id_ = 0; + // The active model. nullptr if not set. std::unique_ptr active_model_; diff --git a/engine/src/flutter/shell/platform/windows/text_input_plugin_unittest.cc b/engine/src/flutter/shell/platform/windows/text_input_plugin_unittest.cc index d1fb6ee671..67b43503ed 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin_unittest.cc +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin_unittest.cc @@ -20,6 +20,22 @@ #include "gtest/gtest.h" namespace flutter { + +class TextInputPluginModifier { + public: + explicit TextInputPluginModifier(TextInputPlugin* text_input_plugin) + : text_input_plugin(text_input_plugin) {} + + void SetViewId(FlutterViewId view_id) { + text_input_plugin->view_id_ = view_id; + } + + private: + TextInputPlugin* text_input_plugin; + + FML_DISALLOW_COPY_AND_ASSIGN(TextInputPluginModifier); +}; + namespace testing { namespace { @@ -33,6 +49,7 @@ static constexpr int kDefaultClientId = 42; // Should be identical to constants in text_input_plugin.cc. static constexpr char kChannelName[] = "flutter/textinput"; static constexpr char kEnableDeltaModel[] = "enableDeltaModel"; +static constexpr char kViewId[] = "viewId"; static constexpr char kSetClientMethod[] = "TextInput.setClient"; static constexpr char kAffinityDownstream[] = "TextAffinity.downstream"; static constexpr char kTextKey[] = "text"; @@ -63,6 +80,7 @@ static std::unique_ptr EncodedClientConfig( rapidjson::Value config(rapidjson::kObjectType); config.AddMember("inputAction", input_action, allocator); config.AddMember(kEnableDeltaModel, false, allocator); + config.AddMember(kViewId, 456, allocator); rapidjson::Value type_info(rapidjson::kObjectType); type_info.AddMember("name", type_name, allocator); config.AddMember("inputType", type_info, allocator); @@ -149,7 +167,20 @@ class TextInputPluginTest : public WindowsTest { std::move(window)); EngineModifier modifier{engine_.get()}; - modifier.SetImplicitView(view_.get()); + modifier.SetViewById(view_.get(), 456); + } + + std::unique_ptr AddViewWithId(int view_id) { + EXPECT_NE(engine_, nullptr); + auto window = std::make_unique(); + EXPECT_CALL(*window, SetView).Times(1); + EXPECT_CALL(*window, GetWindowHandle).WillRepeatedly(Return(nullptr)); + auto view = std::make_unique(engine_.get(), + std::move(window)); + + EngineModifier modifier{engine_.get()}; + modifier.SetViewById(view_.get(), view_id); + return view; } private: @@ -194,6 +225,8 @@ TEST_F(TextInputPluginTest, ClearClientResetsComposing) { BinaryReply reply_handler = [](const uint8_t* reply, size_t reply_size) {}; TextInputPlugin handler(&messenger, engine()); + TextInputPluginModifier modifier(&handler); + modifier.SetViewId(456); EXPECT_CALL(*view(), OnResetImeComposing()); @@ -224,9 +257,10 @@ TEST_F(TextInputPluginTest, ClearClientRequiresView) { messenger.SimulateEngineMessage(kChannelName, message->data(), message->size(), reply_handler); - EXPECT_EQ(reply, - "[\"Internal Consistency Error\",\"Text input is not available in " - "Windows headless mode\",null]"); + EXPECT_EQ( + reply, + "[\"Internal Consistency Error\",\"Text input is not available because " + "view with view_id=0 cannot be found\",null]"); } // Verify that the embedder sends state update messages to the framework during @@ -253,6 +287,7 @@ TEST_F(TextInputPluginTest, VerifyComposingSendStateUpdate) { config.AddMember("inputAction", "done", allocator); config.AddMember("inputType", "text", allocator); config.AddMember(kEnableDeltaModel, false, allocator); + config.AddMember(kViewId, 456, allocator); arguments->PushBack(config, allocator); auto message = codec.EncodeMethodCall({"TextInput.setClient", std::move(arguments)}); @@ -392,6 +427,71 @@ TEST_F(TextInputPluginTest, VerifyInputActionSendDoesNotInsertNewLine) { messages.front().begin())); } +TEST_F(TextInputPluginTest, SetClientRequiresViewId) { + UseEngineWithView(); + + TestBinaryMessenger messenger([](const std::string& channel, + const uint8_t* message, size_t message_size, + BinaryReply reply) {}); + + TextInputPlugin handler(&messenger, engine()); + + auto args = std::make_unique(rapidjson::kArrayType); + auto& allocator = args->GetAllocator(); + args->PushBack(123, allocator); // client_id + + rapidjson::Value client_config(rapidjson::kObjectType); + + args->PushBack(client_config, allocator); + auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( + MethodCall(kSetClientMethod, std::move(args))); + + std::string reply; + BinaryReply reply_handler = [&reply](const uint8_t* reply_bytes, + size_t reply_size) { + reply = std::string(reinterpret_cast(reply_bytes), reply_size); + }; + + EXPECT_TRUE(messenger.SimulateEngineMessage(kChannelName, encoded->data(), + encoded->size(), reply_handler)); + EXPECT_EQ( + reply, + "[\"Bad Arguments\",\"Could not set client, view ID is null.\",null]"); +} + +TEST_F(TextInputPluginTest, SetClientRequiresViewIdToBeInteger) { + UseEngineWithView(); + + TestBinaryMessenger messenger([](const std::string& channel, + const uint8_t* message, size_t message_size, + BinaryReply reply) {}); + + TextInputPlugin handler(&messenger, engine()); + + auto args = std::make_unique(rapidjson::kArrayType); + auto& allocator = args->GetAllocator(); + args->PushBack(123, allocator); // client_id + + rapidjson::Value client_config(rapidjson::kObjectType); + client_config.AddMember(kViewId, "Not an integer", allocator); // view_id + + args->PushBack(client_config, allocator); + auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( + MethodCall(kSetClientMethod, std::move(args))); + + std::string reply; + BinaryReply reply_handler = [&reply](const uint8_t* reply_bytes, + size_t reply_size) { + reply = std::string(reinterpret_cast(reply_bytes), reply_size); + }; + + EXPECT_TRUE(messenger.SimulateEngineMessage(kChannelName, encoded->data(), + encoded->size(), reply_handler)); + EXPECT_EQ( + reply, + "[\"Bad Arguments\",\"Could not set client, view ID is null.\",null]"); +} + TEST_F(TextInputPluginTest, TextEditingWorksWithDeltaModel) { UseEngineWithView(); @@ -413,6 +513,7 @@ TEST_F(TextInputPluginTest, TextEditingWorksWithDeltaModel) { rapidjson::Value client_config(rapidjson::kObjectType); client_config.AddMember(kEnableDeltaModel, true, allocator); + client_config.AddMember(kViewId, 456, allocator); args->PushBack(client_config, allocator); auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( @@ -479,6 +580,7 @@ TEST_F(TextInputPluginTest, CompositionCursorPos) { auto& allocator = args->GetAllocator(); args->PushBack(123, allocator); // client_id rapidjson::Value client_config(rapidjson::kObjectType); + client_config.AddMember(kViewId, 456, allocator); args->PushBack(client_config, allocator); auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( MethodCall(kSetClientMethod, std::move(args))); @@ -535,6 +637,8 @@ TEST_F(TextInputPluginTest, TransformCursorRect) { BinaryReply reply_handler = [](const uint8_t* reply, size_t reply_size) {}; TextInputPlugin handler(&messenger, engine()); + TextInputPluginModifier modifier(&handler); + modifier.SetViewId(456); auto& codec = JsonMethodCodec::GetInstance(); @@ -611,9 +715,62 @@ TEST_F(TextInputPluginTest, SetMarkedTextRectRequiresView) { messenger.SimulateEngineMessage(kChannelName, message->data(), message->size(), reply_handler); - EXPECT_EQ(reply, - "[\"Internal Consistency Error\",\"Text input is not available in " - "Windows headless mode\",null]"); + EXPECT_EQ( + reply, + "[\"Internal Consistency Error\",\"Text input is not available because " + "view with view_id=0 cannot be found\",null]"); +} + +TEST_F(TextInputPluginTest, SetAndUseMultipleClients) { + UseEngineWithView(); // Creates the default view + AddViewWithId(789); // Creates the next view + + bool sent_message = false; + TestBinaryMessenger messenger( + [&sent_message](const std::string& channel, const uint8_t* message, + size_t message_size, + BinaryReply reply) { sent_message = true; }); + + TextInputPlugin handler(&messenger, engine()); + + auto const set_client_and_send_message = [&](int client_id, int view_id) { + auto args = std::make_unique(rapidjson::kArrayType); + auto& allocator = args->GetAllocator(); + args->PushBack(client_id, allocator); // client_id + + rapidjson::Value client_config(rapidjson::kObjectType); + client_config.AddMember(kViewId, view_id, allocator); // view_id + + args->PushBack(client_config, allocator); + auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( + MethodCall(kSetClientMethod, std::move(args))); + + std::string reply; + BinaryReply reply_handler = [&reply](const uint8_t* reply_bytes, + size_t reply_size) { + reply = + std::string(reinterpret_cast(reply_bytes), reply_size); + }; + + EXPECT_TRUE(messenger.SimulateEngineMessage( + kChannelName, encoded->data(), encoded->size(), reply_handler)); + + sent_message = false; + handler.ComposeBeginHook(); + EXPECT_TRUE(sent_message); + sent_message = false; + handler.ComposeChangeHook(u"4", 1); + EXPECT_TRUE(sent_message); + sent_message = false; + handler.ComposeCommitHook(); + EXPECT_FALSE(sent_message); + sent_message = false; + handler.ComposeEndHook(); + EXPECT_TRUE(sent_message); + }; + + set_client_and_send_message(123, 456); // Set and send for the first view + set_client_and_send_message(123, 789); // Set and send for the next view } } // namespace testing