未验证 提交 d2c326e6 编写于 作者: T Tong Mu 提交者: GitHub

[Windows] Fix crash of TextInputPlugin on empty model (#24293)

上级 2e10a974
...@@ -1516,6 +1516,7 @@ FILE: ../../../flutter/shell/platform/windows/text_input_manager_win32.h ...@@ -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.cc
FILE: ../../../flutter/shell/platform/windows/text_input_plugin.h 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_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.cc
FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils.h FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils.h
FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils_unittests.cc FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils_unittests.cc
......
...@@ -199,6 +199,7 @@ executable("flutter_windows_unittests") { ...@@ -199,6 +199,7 @@ executable("flutter_windows_unittests") {
"testing/mock_window_binding_handler.h", "testing/mock_window_binding_handler.h",
"testing/win32_flutter_window_test.cc", "testing/win32_flutter_window_test.cc",
"testing/win32_flutter_window_test.h", "testing/win32_flutter_window_test.h",
"text_input_plugin_unittest.cc",
"win32_dpi_utils_unittests.cc", "win32_dpi_utils_unittests.cc",
"win32_flutter_window_unittests.cc", "win32_flutter_window_unittests.cc",
"win32_window_proc_delegate_manager_unittests.cc", "win32_window_proc_delegate_manager_unittests.cc",
......
...@@ -15,11 +15,12 @@ ...@@ -15,11 +15,12 @@
namespace flutter { namespace flutter {
namespace testing { namespace testing {
namespace {
static constexpr char kScanCodeKey[] = "scanCode"; static constexpr char kScanCodeKey[] = "scanCode";
static constexpr int kHandledScanCode = 20; static constexpr int kHandledScanCode = 20;
static constexpr int kUnhandledScanCode = 21; static constexpr int kUnhandledScanCode = 21;
std::unique_ptr<std::vector<uint8_t>> CreateResponse(bool handled) { static std::unique_ptr<std::vector<uint8_t>> CreateResponse(bool handled) {
auto response_doc = auto response_doc =
std::make_unique<rapidjson::Document>(rapidjson::kObjectType); std::make_unique<rapidjson::Document>(rapidjson::kObjectType);
auto& allocator = response_doc->GetAllocator(); auto& allocator = response_doc->GetAllocator();
...@@ -27,6 +28,8 @@ std::unique_ptr<std::vector<uint8_t>> CreateResponse(bool handled) { ...@@ -27,6 +28,8 @@ std::unique_ptr<std::vector<uint8_t>> CreateResponse(bool handled) {
return JsonMessageCodec::GetInstance().EncodeMessage(*response_doc); return JsonMessageCodec::GetInstance().EncodeMessage(*response_doc);
} }
} // namespace
TEST(KeyEventHandlerTest, KeyboardHookHandling) { TEST(KeyEventHandlerTest, KeyboardHookHandling) {
auto handled_message = CreateResponse(true); auto handled_message = CreateResponse(true);
auto unhandled_message = CreateResponse(false); auto unhandled_message = CreateResponse(false);
......
...@@ -103,11 +103,17 @@ TextInputPlugin::TextInputPlugin(flutter::BinaryMessenger* messenger, ...@@ -103,11 +103,17 @@ TextInputPlugin::TextInputPlugin(flutter::BinaryMessenger* messenger,
TextInputPlugin::~TextInputPlugin() = default; TextInputPlugin::~TextInputPlugin() = default;
void TextInputPlugin::ComposeBeginHook() { void TextInputPlugin::ComposeBeginHook() {
if (active_model_ == nullptr) {
return;
}
active_model_->BeginComposing(); active_model_->BeginComposing();
SendStateUpdate(*active_model_); SendStateUpdate(*active_model_);
} }
void TextInputPlugin::ComposeEndHook() { void TextInputPlugin::ComposeEndHook() {
if (active_model_ == nullptr) {
return;
}
active_model_->CommitComposing(); active_model_->CommitComposing();
active_model_->EndComposing(); active_model_->EndComposing();
SendStateUpdate(*active_model_); SendStateUpdate(*active_model_);
...@@ -115,6 +121,9 @@ void TextInputPlugin::ComposeEndHook() { ...@@ -115,6 +121,9 @@ void TextInputPlugin::ComposeEndHook() {
void TextInputPlugin::ComposeChangeHook(const std::u16string& text, void TextInputPlugin::ComposeChangeHook(const std::u16string& text,
int cursor_pos) { int cursor_pos) {
if (active_model_ == nullptr) {
return;
}
active_model_->AddText(text); active_model_->AddText(text);
cursor_pos += active_model_->composing_range().base(); cursor_pos += active_model_->composing_range().base();
active_model_->UpdateComposingText(text); active_model_->UpdateComposingText(text);
......
// 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 <rapidjson/document.h>
#include <memory>
#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<std::vector<uint8_t>> CreateResponse(bool handled) {
auto response_doc =
std::make_unique<rapidjson::Document>(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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册