[Windows] Fix crash of TextInputPlugin on empty model (flutter/engine#24293)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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<std::vector<uint8_t>> CreateResponse(bool handled) {
|
||||
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();
|
||||
@@ -27,6 +28,8 @@ std::unique_ptr<std::vector<uint8_t>> CreateResponse(bool handled) {
|
||||
return JsonMessageCodec::GetInstance().EncodeMessage(*response_doc);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(KeyEventHandlerTest, KeyboardHookHandling) {
|
||||
auto handled_message = CreateResponse(true);
|
||||
auto unhandled_message = CreateResponse(false);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 <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
|
||||
Reference in New Issue
Block a user