diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 9292b5aa78..a646d94e34 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -1516,6 +1516,7 @@ FILE: ../../../flutter/shell/platform/windows/text_input_manager_win32.h FILE: ../../../flutter/shell/platform/windows/text_input_plugin.cc FILE: ../../../flutter/shell/platform/windows/text_input_plugin.h FILE: ../../../flutter/shell/platform/windows/text_input_plugin_delegate.h +FILE: ../../../flutter/shell/platform/windows/text_input_plugin_unittest.cc FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils.cc FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils.h FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils_unittests.cc diff --git a/engine/src/flutter/shell/platform/windows/BUILD.gn b/engine/src/flutter/shell/platform/windows/BUILD.gn index 31a0b7bd33..8881c7d13b 100644 --- a/engine/src/flutter/shell/platform/windows/BUILD.gn +++ b/engine/src/flutter/shell/platform/windows/BUILD.gn @@ -199,6 +199,7 @@ executable("flutter_windows_unittests") { "testing/mock_window_binding_handler.h", "testing/win32_flutter_window_test.cc", "testing/win32_flutter_window_test.h", + "text_input_plugin_unittest.cc", "win32_dpi_utils_unittests.cc", "win32_flutter_window_unittests.cc", "win32_window_proc_delegate_manager_unittests.cc", diff --git a/engine/src/flutter/shell/platform/windows/key_event_handler_unittests.cc b/engine/src/flutter/shell/platform/windows/key_event_handler_unittests.cc index a78d82ea74..8698e20f2d 100644 --- a/engine/src/flutter/shell/platform/windows/key_event_handler_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/key_event_handler_unittests.cc @@ -15,11 +15,12 @@ namespace flutter { namespace testing { +namespace { static constexpr char kScanCodeKey[] = "scanCode"; static constexpr int kHandledScanCode = 20; static constexpr int kUnhandledScanCode = 21; -std::unique_ptr> CreateResponse(bool handled) { +static std::unique_ptr> CreateResponse(bool handled) { auto response_doc = std::make_unique(rapidjson::kObjectType); auto& allocator = response_doc->GetAllocator(); @@ -27,6 +28,8 @@ std::unique_ptr> CreateResponse(bool handled) { return JsonMessageCodec::GetInstance().EncodeMessage(*response_doc); } +} // namespace + TEST(KeyEventHandlerTest, KeyboardHookHandling) { auto handled_message = CreateResponse(true); auto unhandled_message = CreateResponse(false); 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 8d0dbc39d7..96d846e811 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin.cc +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin.cc @@ -103,11 +103,17 @@ TextInputPlugin::TextInputPlugin(flutter::BinaryMessenger* messenger, TextInputPlugin::~TextInputPlugin() = default; void TextInputPlugin::ComposeBeginHook() { + if (active_model_ == nullptr) { + return; + } active_model_->BeginComposing(); SendStateUpdate(*active_model_); } void TextInputPlugin::ComposeEndHook() { + if (active_model_ == nullptr) { + return; + } active_model_->CommitComposing(); active_model_->EndComposing(); SendStateUpdate(*active_model_); @@ -115,6 +121,9 @@ void TextInputPlugin::ComposeEndHook() { void TextInputPlugin::ComposeChangeHook(const std::u16string& text, int cursor_pos) { + if (active_model_ == nullptr) { + return; + } active_model_->AddText(text); cursor_pos += active_model_->composing_range().base(); active_model_->UpdateComposingText(text); 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 new file mode 100644 index 0000000000..fe3c356b8f --- /dev/null +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin_unittest.cc @@ -0,0 +1,63 @@ +// 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/windows/text_input_plugin.h" + +#include +#include + +#include "flutter/shell/platform/common/json_message_codec.h" +#include "flutter/shell/platform/windows/flutter_windows_view.h" +#include "flutter/shell/platform/windows/testing/test_binary_messenger.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { + +namespace { +static constexpr char kScanCodeKey[] = "scanCode"; +static constexpr int kHandledScanCode = 20; +static constexpr int kUnhandledScanCode = 21; + +static std::unique_ptr> CreateResponse(bool handled) { + auto response_doc = + std::make_unique(rapidjson::kObjectType); + auto& allocator = response_doc->GetAllocator(); + response_doc->AddMember("handled", handled, allocator); + return JsonMessageCodec::GetInstance().EncodeMessage(*response_doc); +} + +class EmptyTextInputPluginDelegate : public TextInputPluginDelegate { + public: + // Notifies delegate that the cursor position has changed. + void OnCursorRectUpdated(const Rect& rect) {} +}; +} // namespace + +TEST(TextInputPluginTest, TextMethodsWorksWithEmptyModel) { + auto handled_message = CreateResponse(true); + auto unhandled_message = CreateResponse(false); + int received_scancode = 0; + + TestBinaryMessenger messenger( + [&received_scancode, &handled_message, &unhandled_message]( + const std::string& channel, const uint8_t* message, + size_t message_size, BinaryReply reply) {}); + EmptyTextInputPluginDelegate delegate; + + int redispatch_scancode = 0; + TextInputPlugin handler(&messenger, &delegate); + + handler.KeyboardHook(nullptr, VK_RETURN, 100, WM_KEYDOWN, '\n', false); + handler.ComposeBeginHook(); + std::u16string text; + text.push_back('\n'); + handler.ComposeChangeHook(text, 1); + handler.ComposeEndHook(); + + // Passes if it did not crash +} + +} // namespace testing +} // namespace flutter