From 8c6d3d24927e09a3d36d5c309445a3b2bd242a94 Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Fri, 12 Nov 2021 14:55:28 -0800 Subject: [PATCH] Check for both compose string and result string. (flutter/engine#29620) It turns out that unlike the Microsoft IME, Sogou sends a single IME_COMPOSITION message that has both the GCS_COMPSTR and GCS_RESULTSTR flags set, and we check for a composition string before result string, with an "else" clause to check for the result string, so if GCS_COMPSTR is set, we never check for a result string. --- .../flutter/shell/platform/windows/BUILD.gn | 2 + .../testing/mock_text_input_manager_win32.cc | 16 ++++ .../testing/mock_text_input_manager_win32.h | 37 +++++++++ .../windows/testing/mock_window_win32.cc | 10 ++- .../windows/testing/mock_window_win32.h | 6 ++ .../windows/text_input_manager_win32.h | 8 +- .../shell/platform/windows/window_win32.cc | 38 +++++---- .../shell/platform/windows/window_win32.h | 33 ++++---- .../windows/window_win32_unittests.cc | 83 +++++++++++++++++++ 9 files changed, 197 insertions(+), 36 deletions(-) create mode 100644 engine/src/flutter/shell/platform/windows/testing/mock_text_input_manager_win32.cc create mode 100644 engine/src/flutter/shell/platform/windows/testing/mock_text_input_manager_win32.h diff --git a/engine/src/flutter/shell/platform/windows/BUILD.gn b/engine/src/flutter/shell/platform/windows/BUILD.gn index 029305ee95..3cfcfdb81f 100644 --- a/engine/src/flutter/shell/platform/windows/BUILD.gn +++ b/engine/src/flutter/shell/platform/windows/BUILD.gn @@ -253,6 +253,8 @@ executable("flutter_windows_unittests") { "platform_handler_unittests.cc", "testing/flutter_window_win32_test.cc", "testing/flutter_window_win32_test.h", + "testing/mock_text_input_manager_win32.cc", + "testing/mock_text_input_manager_win32.h", "testing/mock_window_binding_handler.cc", "testing/mock_window_binding_handler.h", "testing/mock_window_win32.cc", diff --git a/engine/src/flutter/shell/platform/windows/testing/mock_text_input_manager_win32.cc b/engine/src/flutter/shell/platform/windows/testing/mock_text_input_manager_win32.cc new file mode 100644 index 0000000000..0d4d4cc2eb --- /dev/null +++ b/engine/src/flutter/shell/platform/windows/testing/mock_text_input_manager_win32.cc @@ -0,0 +1,16 @@ +// 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/testing/mock_text_input_manager_win32.h" + +namespace flutter { +namespace testing { + +MockTextInputManagerWin32::MockTextInputManagerWin32() + : TextInputManagerWin32(){}; + +MockTextInputManagerWin32::~MockTextInputManagerWin32() = default; + +} // namespace testing +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/testing/mock_text_input_manager_win32.h b/engine/src/flutter/shell/platform/windows/testing/mock_text_input_manager_win32.h new file mode 100644 index 0000000000..eb900c00f5 --- /dev/null +++ b/engine/src/flutter/shell/platform/windows/testing/mock_text_input_manager_win32.h @@ -0,0 +1,37 @@ +// 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. + +#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_MOCK_TEXT_INPUT_MANAGER_WIN32_H_ +#define FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_MOCK_TEXT_INPUT_MANAGER_WIN32_H_ + +#include +#include +#include + +#include "flutter/shell/platform/windows/text_input_manager_win32.h" +#include "gmock/gmock.h" + +namespace flutter { +namespace testing { + +/// Mock for the |WindowWin32| base class. +class MockTextInputManagerWin32 : public TextInputManagerWin32 { + public: + MockTextInputManagerWin32(); + virtual ~MockTextInputManagerWin32(); + + // Prevent copying. + MockTextInputManagerWin32(MockTextInputManagerWin32 const&) = delete; + MockTextInputManagerWin32& operator=(MockTextInputManagerWin32 const&) = + delete; + + MOCK_CONST_METHOD0(GetComposingString, std::optional()); + MOCK_CONST_METHOD0(GetResultString, std::optional()); + MOCK_CONST_METHOD0(GetComposingCursorPosition, long()); +}; + +} // namespace testing +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_MOCK_TEXT_INPUT_MANAGER_WIN32_H_ diff --git a/engine/src/flutter/shell/platform/windows/testing/mock_window_win32.cc b/engine/src/flutter/shell/platform/windows/testing/mock_window_win32.cc index 99cc67898f..4376c277db 100644 --- a/engine/src/flutter/shell/platform/windows/testing/mock_window_win32.cc +++ b/engine/src/flutter/shell/platform/windows/testing/mock_window_win32.cc @@ -6,8 +6,10 @@ namespace flutter { namespace testing { - MockWin32Window::MockWin32Window() : WindowWin32(){}; +MockWin32Window::MockWin32Window( + std::unique_ptr text_input_manager) + : WindowWin32(std::move(text_input_manager)){}; MockWin32Window::~MockWin32Window() = default; @@ -35,5 +37,11 @@ LRESULT MockWin32Window::Win32SendMessage(HWND hWnd, return HandleMessage(message, wparam, lparam); } +void MockWin32Window::CallOnImeComposition(UINT const message, + WPARAM const wparam, + LPARAM const lparam) { + WindowWin32::OnImeComposition(message, wparam, lparam); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/testing/mock_window_win32.h b/engine/src/flutter/shell/platform/windows/testing/mock_window_win32.h index 21d6840704..406cf1b9b0 100644 --- a/engine/src/flutter/shell/platform/windows/testing/mock_window_win32.h +++ b/engine/src/flutter/shell/platform/windows/testing/mock_window_win32.h @@ -18,6 +18,7 @@ namespace testing { class MockWin32Window : public WindowWin32, public MockMessageQueue { public: MockWin32Window(); + MockWin32Window(std::unique_ptr text_input_manager); virtual ~MockWin32Window(); // Prevent copying. @@ -51,6 +52,11 @@ class MockWin32Window : public WindowWin32, public MockMessageQueue { MOCK_METHOD0(OnComposeCommit, void()); MOCK_METHOD0(OnComposeEnd, void()); MOCK_METHOD2(OnComposeChange, void(const std::u16string&, int)); + MOCK_METHOD3(OnImeComposition, void(UINT const, WPARAM const, LPARAM const)); + + void CallOnImeComposition(UINT const message, + WPARAM const wparam, + LPARAM const lparam); protected: LRESULT Win32DefWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); diff --git a/engine/src/flutter/shell/platform/windows/text_input_manager_win32.h b/engine/src/flutter/shell/platform/windows/text_input_manager_win32.h index 0565efb92c..3ca6d09e48 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_manager_win32.h +++ b/engine/src/flutter/shell/platform/windows/text_input_manager_win32.h @@ -28,7 +28,7 @@ namespace flutter { class TextInputManagerWin32 { public: TextInputManagerWin32() noexcept = default; - ~TextInputManagerWin32() = default; + virtual ~TextInputManagerWin32() = default; TextInputManagerWin32(const TextInputManagerWin32&) = delete; TextInputManagerWin32& operator=(const TextInputManagerWin32&) = delete; @@ -60,7 +60,7 @@ class TextInputManagerWin32 { void UpdateCaretRect(const Rect& rect); // Returns the cursor position relative to the start of the composing range. - long GetComposingCursorPosition() const; + virtual long GetComposingCursorPosition() const; // Returns the contents of the composing string. // @@ -68,14 +68,14 @@ class TextInputManagerWin32 { // GCS_COMPSTR flag is set in the lparam. In some IMEs, this string may also // be set in events where the GCS_RESULTSTR flag is set. This contains the // in-progress composing string. - std::optional GetComposingString() const; + virtual std::optional GetComposingString() const; // Returns the contents of the result string. // // This may be called in response to WM_IME_COMPOSITION events where the // GCS_RESULTSTR flag is set in the lparam. This contains the final string to // be committed in the composing region when composition is ended. - std::optional GetResultString() const; + virtual std::optional GetResultString() const; /// Aborts IME composing. /// diff --git a/engine/src/flutter/shell/platform/windows/window_win32.cc b/engine/src/flutter/shell/platform/windows/window_win32.cc index 5dcb2a618d..1f9830662b 100644 --- a/engine/src/flutter/shell/platform/windows/window_win32.cc +++ b/engine/src/flutter/shell/platform/windows/window_win32.cc @@ -44,12 +44,19 @@ static const int kMaxTouchDeviceId = 128; } // namespace -WindowWin32::WindowWin32() - : touch_id_generator_(kMinTouchDeviceId, kMaxTouchDeviceId) { +WindowWin32::WindowWin32() : WindowWin32(nullptr) {} + +WindowWin32::WindowWin32( + std::unique_ptr text_input_manager) + : touch_id_generator_(kMinTouchDeviceId, kMaxTouchDeviceId), + text_input_manager_(std::move(text_input_manager)) { // Get the DPI of the primary monitor as the initial DPI. If Per-Monitor V2 is // supported, |current_dpi_| should be updated in the // kWmDpiChangedBeforeParent message. current_dpi_ = GetDpiForHWND(nullptr); + if (text_input_manager_ == nullptr) { + text_input_manager_ = std::make_unique(); + } } WindowWin32::~WindowWin32() { @@ -119,7 +126,7 @@ LRESULT CALLBACK WindowWin32::WndProc(HWND const window, auto that = static_cast(cs->lpCreateParams); that->window_handle_ = window; - that->text_input_manager_.SetWindowHandle(window); + that->text_input_manager_->SetWindowHandle(window); RegisterTouchWindow(window, 0); } else if (WindowWin32* that = GetThisFromHandle(window)) { return that->HandleMessage(message, wparam, lparam); @@ -170,14 +177,14 @@ void WindowWin32::OnImeSetContext(UINT const message, WPARAM const wparam, LPARAM const lparam) { if (wparam != 0) { - text_input_manager_.CreateImeWindow(); + text_input_manager_->CreateImeWindow(); } } void WindowWin32::OnImeStartComposition(UINT const message, WPARAM const wparam, LPARAM const lparam) { - text_input_manager_.CreateImeWindow(); + text_input_manager_->CreateImeWindow(); OnComposeBegin(); } @@ -185,21 +192,22 @@ void WindowWin32::OnImeComposition(UINT const message, WPARAM const wparam, LPARAM const lparam) { // Update the IME window position. - text_input_manager_.UpdateImeWindow(); + text_input_manager_->UpdateImeWindow(); if (lparam & GCS_COMPSTR) { // Read the in-progress composing string. - long pos = text_input_manager_.GetComposingCursorPosition(); + long pos = text_input_manager_->GetComposingCursorPosition(); std::optional text = - text_input_manager_.GetComposingString(); + text_input_manager_->GetComposingString(); if (text) { OnComposeChange(text.value(), pos); } - } else if (lparam & GCS_RESULTSTR) { + } + if (lparam & GCS_RESULTSTR) { // Commit but don't end composing. // Read the committed composing string. - long pos = text_input_manager_.GetComposingCursorPosition(); - std::optional text = text_input_manager_.GetResultString(); + long pos = text_input_manager_->GetComposingCursorPosition(); + std::optional text = text_input_manager_->GetResultString(); if (text) { OnComposeChange(text.value(), pos); OnComposeCommit(); @@ -210,7 +218,7 @@ void WindowWin32::OnImeComposition(UINT const message, void WindowWin32::OnImeEndComposition(UINT const message, WPARAM const wparam, LPARAM const lparam) { - text_input_manager_.DestroyImeWindow(); + text_input_manager_->DestroyImeWindow(); OnComposeEnd(); } @@ -223,11 +231,11 @@ void WindowWin32::OnImeRequest(UINT const message, } void WindowWin32::AbortImeComposing() { - text_input_manager_.AbortComposing(); + text_input_manager_->AbortComposing(); } void WindowWin32::UpdateCursorRect(const Rect& rect) { - text_input_manager_.UpdateCaretRect(rect); + text_input_manager_->UpdateCaretRect(rect); } static uint16_t ResolveKeyCode(uint16_t original, @@ -588,7 +596,7 @@ HWND WindowWin32::GetWindowHandle() { void WindowWin32::Destroy() { if (window_handle_) { - text_input_manager_.SetWindowHandle(nullptr); + text_input_manager_->SetWindowHandle(nullptr); DestroyWindow(window_handle_); window_handle_ = nullptr; } diff --git a/engine/src/flutter/shell/platform/windows/window_win32.h b/engine/src/flutter/shell/platform/windows/window_win32.h index d2ac6194f1..c106b82819 100644 --- a/engine/src/flutter/shell/platform/windows/window_win32.h +++ b/engine/src/flutter/shell/platform/windows/window_win32.h @@ -25,6 +25,7 @@ namespace flutter { class WindowWin32 { public: WindowWin32(); + WindowWin32(std::unique_ptr text_input_manager); virtual ~WindowWin32(); // Initializes as a child window with size using |width| and |height| and @@ -143,31 +144,31 @@ class WindowWin32 { // Called when a window is activated in order to configure IME support for // multi-step text input. - void OnImeSetContext(UINT const message, - WPARAM const wparam, - LPARAM const lparam); + virtual void OnImeSetContext(UINT const message, + WPARAM const wparam, + LPARAM const lparam); // Called when multi-step text input begins when using an IME. - void OnImeStartComposition(UINT const message, - WPARAM const wparam, - LPARAM const lparam); + virtual void OnImeStartComposition(UINT const message, + WPARAM const wparam, + LPARAM const lparam); // Called when edits/commit of multi-step text input occurs when using an IME. - void OnImeComposition(UINT const message, - WPARAM const wparam, - LPARAM const lparam); + virtual void OnImeComposition(UINT const message, + WPARAM const wparam, + LPARAM const lparam); // Called when multi-step text input ends when using an IME. - void OnImeEndComposition(UINT const message, - WPARAM const wparam, - LPARAM const lparam); + virtual void OnImeEndComposition(UINT const message, + WPARAM const wparam, + LPARAM const lparam); // Called when the user triggers an IME-specific request such as input // reconversion, where an existing input sequence is returned to composing // mode to select an alternative candidate conversion. - void OnImeRequest(UINT const message, - WPARAM const wparam, - LPARAM const lparam); + virtual void OnImeRequest(UINT const message, + WPARAM const wparam, + LPARAM const lparam); // Called when the app ends IME composing, such as when the text input client // is cleared or changed. @@ -264,7 +265,7 @@ class WindowWin32 { std::map text_for_scancode_on_redispatch_; // Manages IME state. - TextInputManagerWin32 text_input_manager_; + std::unique_ptr text_input_manager_; // Used for temporarily storing the WM_TOUCH-provided touch points. std::vector touch_points_; diff --git a/engine/src/flutter/shell/platform/windows/window_win32_unittests.cc b/engine/src/flutter/shell/platform/windows/window_win32_unittests.cc index 92fbf009fd..e1d596a5d7 100644 --- a/engine/src/flutter/shell/platform/windows/window_win32_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/window_win32_unittests.cc @@ -2,10 +2,13 @@ // 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/testing/mock_text_input_manager_win32.h" #include "flutter/shell/platform/windows/testing/mock_window_win32.h" #include "gtest/gtest.h" using testing::_; +using testing::Invoke; +using testing::Return; namespace flutter { namespace testing { @@ -32,6 +35,86 @@ TEST(MockWin32Window, VerticalScroll) { window.InjectWindowMessage(WM_MOUSEWHEEL, MAKEWPARAM(0, scroll_amount), 0); } +TEST(MockWin32Window, OnImeCompositionCompose) { + MockTextInputManagerWin32* text_input_manager = + new MockTextInputManagerWin32(); + std::unique_ptr text_input_manager_ptr( + text_input_manager); + MockWin32Window window(std::move(text_input_manager_ptr)); + EXPECT_CALL(*text_input_manager, GetComposingString()) + .WillRepeatedly( + Return(std::optional(std::u16string(u"nihao")))); + EXPECT_CALL(*text_input_manager, GetResultString()) + .WillRepeatedly( + Return(std::optional(std::u16string(u"`}")))); + EXPECT_CALL(*text_input_manager, GetComposingCursorPosition()) + .WillRepeatedly(Return((int)0)); + + EXPECT_CALL(window, OnComposeChange(std::u16string(u"nihao"), 0)).Times(1); + EXPECT_CALL(window, OnComposeChange(std::u16string(u"`}"), 0)).Times(0); + EXPECT_CALL(window, OnComposeCommit()).Times(0); + ON_CALL(window, OnImeComposition) + .WillByDefault(Invoke(&window, &MockWin32Window::CallOnImeComposition)); + EXPECT_CALL(window, OnImeComposition(_, _, _)).Times(1); + + // Send an IME_COMPOSITION event that contains just the composition string. + window.InjectWindowMessage(WM_IME_COMPOSITION, 0, GCS_COMPSTR); +} + +TEST(MockWin32Window, OnImeCompositionResult) { + MockTextInputManagerWin32* text_input_manager = + new MockTextInputManagerWin32(); + std::unique_ptr text_input_manager_ptr( + text_input_manager); + MockWin32Window window(std::move(text_input_manager_ptr)); + EXPECT_CALL(*text_input_manager, GetComposingString()) + .WillRepeatedly( + Return(std::optional(std::u16string(u"nihao")))); + EXPECT_CALL(*text_input_manager, GetResultString()) + .WillRepeatedly( + Return(std::optional(std::u16string(u"`}")))); + EXPECT_CALL(*text_input_manager, GetComposingCursorPosition()) + .WillRepeatedly(Return((int)0)); + + EXPECT_CALL(window, OnComposeChange(std::u16string(u"nihao"), 0)).Times(0); + EXPECT_CALL(window, OnComposeChange(std::u16string(u"`}"), 0)).Times(1); + EXPECT_CALL(window, OnComposeCommit()).Times(1); + ON_CALL(window, OnImeComposition) + .WillByDefault(Invoke(&window, &MockWin32Window::CallOnImeComposition)); + EXPECT_CALL(window, OnImeComposition(_, _, _)).Times(1); + + // Send an IME_COMPOSITION event that contains just the result string. + window.InjectWindowMessage(WM_IME_COMPOSITION, 0, GCS_RESULTSTR); +} + +TEST(MockWin32Window, OnImeCompositionComposeAndResult) { + MockTextInputManagerWin32* text_input_manager = + new MockTextInputManagerWin32(); + std::unique_ptr text_input_manager_ptr( + text_input_manager); + MockWin32Window window(std::move(text_input_manager_ptr)); + EXPECT_CALL(*text_input_manager, GetComposingString()) + .WillRepeatedly( + Return(std::optional(std::u16string(u"nihao")))); + EXPECT_CALL(*text_input_manager, GetResultString()) + .WillRepeatedly( + Return(std::optional(std::u16string(u"`}")))); + EXPECT_CALL(*text_input_manager, GetComposingCursorPosition()) + .WillRepeatedly(Return((int)0)); + + EXPECT_CALL(window, OnComposeChange(std::u16string(u"nihao"), 0)).Times(1); + EXPECT_CALL(window, OnComposeChange(std::u16string(u"`}"), 0)).Times(1); + EXPECT_CALL(window, OnComposeCommit()).Times(1); + ON_CALL(window, OnImeComposition) + .WillByDefault(Invoke(&window, &MockWin32Window::CallOnImeComposition)); + EXPECT_CALL(window, OnImeComposition(_, _, _)).Times(1); + + // send an IME_COMPOSITION event that contains both the result string and the + // composition string. + window.InjectWindowMessage(WM_IME_COMPOSITION, 0, + GCS_COMPSTR | GCS_RESULTSTR); +} + TEST(MockWin32Window, HorizontalScroll) { MockWin32Window window; const int scroll_amount = 10;