From ebbae704d99ab5426a80b609feb818bb1c5c148e Mon Sep 17 00:00:00 2001 From: Loic Sharma Date: Wed, 14 Feb 2024 17:45:22 -0800 Subject: [PATCH] [Windows] Improve window unit tests --- shell/platform/windows/flutter_window.cc | 11 ++++-- shell/platform/windows/flutter_window.h | 5 ++- .../windows/flutter_window_unittests.cc | 38 ++++++++++--------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/shell/platform/windows/flutter_window.cc b/shell/platform/windows/flutter_window.cc index dfa231b099642..82e6cd9c486e5 100644 --- a/shell/platform/windows/flutter_window.cc +++ b/shell/platform/windows/flutter_window.cc @@ -121,8 +121,7 @@ FlutterWindow::FlutterWindow( int height, std::shared_ptr windows_proc_table, std::unique_ptr text_input_manager) - : binding_handler_delegate_(nullptr), - touch_id_generator_(kMinTouchDeviceId, kMaxTouchDeviceId), + : touch_id_generator_(kMinTouchDeviceId, kMaxTouchDeviceId), windows_proc_table_(std::move(windows_proc_table)), text_input_manager_(std::move(text_input_manager)), ax_fragment_root_(nullptr) { @@ -148,13 +147,19 @@ FlutterWindow::FlutterWindow( current_cursor_ = ::LoadCursor(nullptr, IDC_ARROW); } +// Base constructor for mocks +FlutterWindow::FlutterWindow() + : touch_id_generator_(kMinTouchDeviceId, kMaxTouchDeviceId) {} + FlutterWindow::~FlutterWindow() { Destroy(); } void FlutterWindow::SetView(WindowBindingHandlerDelegate* window) { binding_handler_delegate_ = window; - direct_manipulation_owner_->SetBindingHandlerDelegate(window); + if (direct_manipulation_owner_) { + direct_manipulation_owner_->SetBindingHandlerDelegate(window); + } if (restored_ && window) { OnWindowStateEvent(WindowStateEvent::kShow); } diff --git a/shell/platform/windows/flutter_window.h b/shell/platform/windows/flutter_window.h index ef992cde41c63..b8560794ec36b 100644 --- a/shell/platform/windows/flutter_window.h +++ b/shell/platform/windows/flutter_window.h @@ -194,6 +194,9 @@ class FlutterWindow : public KeyboardManager::WindowDelegate, virtual void OnWindowStateEvent(WindowStateEvent event); protected: + // Base constructor for mocks. + FlutterWindow(); + // Win32's DefWindowProc. // // Used as the fallback behavior of HandleMessage. Exposed for dependency @@ -321,7 +324,7 @@ class FlutterWindow : public KeyboardManager::WindowDelegate, // A pointer to a FlutterWindowsView that can be used to update engine // windowing and input state. - WindowBindingHandlerDelegate* binding_handler_delegate_; + WindowBindingHandlerDelegate* binding_handler_delegate_ = nullptr; // The last cursor set by Flutter. Defaults to the arrow cursor. HCURSOR current_cursor_; diff --git a/shell/platform/windows/flutter_window_unittests.cc b/shell/platform/windows/flutter_window_unittests.cc index e2fdb1bdb8cbd..270f4fe718c80 100644 --- a/shell/platform/windows/flutter_window_unittests.cc +++ b/shell/platform/windows/flutter_window_unittests.cc @@ -6,6 +6,7 @@ #include "flutter/shell/platform/windows/flutter_window.h" #include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h" #include "flutter/shell/platform/windows/testing/mock_window_binding_handler_delegate.h" +#include "flutter/shell/platform/windows/testing/windows_test.h" #include "flutter/shell/platform/windows/testing/wm_builders.h" #include "gmock/gmock.h" @@ -25,7 +26,7 @@ static constexpr int32_t kDefaultPointerDeviceId = 0; class MockFlutterWindow : public FlutterWindow { public: MockFlutterWindow(bool reset_view_on_exit = true) - : FlutterWindow(800, 600), reset_view_on_exit_(reset_view_on_exit) { + : FlutterWindow(), reset_view_on_exit_(reset_view_on_exit) { ON_CALL(*this, GetDpiScale()) .WillByDefault(Return(this->FlutterWindow::GetDpiScale())); } @@ -107,14 +108,16 @@ class MockFlutterWindowsView : public FlutterWindowsView { FML_DISALLOW_COPY_AND_ASSIGN(MockFlutterWindowsView); }; +class FlutterWindowTest : public WindowsTest {}; + } // namespace -TEST(FlutterWindowTest, CreateDestroy) { +TEST_F(FlutterWindowTest, CreateDestroy) { FlutterWindow window(800, 600); ASSERT_TRUE(TRUE); } -TEST(FlutterWindowTest, OnBitmapSurfaceUpdated) { +TEST_F(FlutterWindowTest, OnBitmapSurfaceUpdated) { FlutterWindow win32window(100, 100); int old_handle_count = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS); @@ -131,7 +134,7 @@ TEST(FlutterWindowTest, OnBitmapSurfaceUpdated) { // Tests that composing rect updates are transformed from Flutter logical // coordinates to device coordinates and passed to the text input manager // when the DPI scale is 100% (96 DPI). -TEST(FlutterWindowTest, OnCursorRectUpdatedRegularDPI) { +TEST_F(FlutterWindowTest, OnCursorRectUpdatedRegularDPI) { MockFlutterWindow win32window; EXPECT_CALL(win32window, GetDpiScale()).WillOnce(Return(1.0)); @@ -144,7 +147,7 @@ TEST(FlutterWindowTest, OnCursorRectUpdatedRegularDPI) { // Tests that composing rect updates are transformed from Flutter logical // coordinates to device coordinates and passed to the text input manager // when the DPI scale is 150% (144 DPI). -TEST(FlutterWindowTest, OnCursorRectUpdatedHighDPI) { +TEST_F(FlutterWindowTest, OnCursorRectUpdatedHighDPI) { MockFlutterWindow win32window; EXPECT_CALL(win32window, GetDpiScale()).WillOnce(Return(1.5)); @@ -155,7 +158,7 @@ TEST(FlutterWindowTest, OnCursorRectUpdatedHighDPI) { win32window.OnCursorRectUpdated(cursor_rect); } -TEST(FlutterWindowTest, OnPointerStarSendsDeviceType) { +TEST_F(FlutterWindowTest, OnPointerStarSendsDeviceType) { FlutterWindow win32window(100, 100); MockWindowBindingHandlerDelegate delegate; EXPECT_CALL(delegate, OnWindowStateEvent).Times(AnyNumber()); @@ -256,15 +259,16 @@ TEST(FlutterWindowTest, OnPointerStarSendsDeviceType) { // Tests that calls to OnScroll in turn calls GetScrollOffsetMultiplier // for mapping scroll ticks to pixels. -TEST(FlutterWindowTest, OnScrollCallsGetScrollOffsetMultiplier) { +TEST_F(FlutterWindowTest, OnScrollCallsGetScrollOffsetMultiplier) { MockFlutterWindow win32window; MockWindowBindingHandlerDelegate delegate; EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber()); win32window.SetView(&delegate); - ON_CALL(win32window, GetScrollOffsetMultiplier()) - .WillByDefault(Return(120.0f)); - EXPECT_CALL(win32window, GetScrollOffsetMultiplier()).Times(1); + EXPECT_CALL(win32window, GetWindowHandle).WillOnce([&win32window]() { + return win32window.FlutterWindow::GetWindowHandle(); + }); + EXPECT_CALL(win32window, GetScrollOffsetMultiplier).WillOnce(Return(120.0f)); EXPECT_CALL(delegate, OnScroll(_, _, 0, 0, 120.0f, kFlutterPointerDeviceKindMouse, @@ -275,7 +279,7 @@ TEST(FlutterWindowTest, OnScrollCallsGetScrollOffsetMultiplier) { kDefaultPointerDeviceId); } -TEST(FlutterWindowTest, OnWindowRepaint) { +TEST_F(FlutterWindowTest, OnWindowRepaint) { MockFlutterWindow win32window; MockWindowBindingHandlerDelegate delegate; EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber()); @@ -286,7 +290,7 @@ TEST(FlutterWindowTest, OnWindowRepaint) { win32window.InjectWindowMessage(WM_PAINT, 0, 0); } -TEST(FlutterWindowTest, OnThemeChange) { +TEST_F(FlutterWindowTest, OnThemeChange) { MockFlutterWindow win32window; MockWindowBindingHandlerDelegate delegate; EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber()); @@ -300,7 +304,7 @@ TEST(FlutterWindowTest, OnThemeChange) { // The window should return no root accessibility node if // it isn't attached to a view. // Regression test for https://github.com/flutter/flutter/issues/129791 -TEST(FlutterWindowTest, AccessibilityNodeWithoutView) { +TEST_F(FlutterWindowTest, AccessibilityNodeWithoutView) { MockFlutterWindow win32window; EXPECT_EQ(win32window.GetNativeViewAccessible(), nullptr); @@ -308,7 +312,7 @@ TEST(FlutterWindowTest, AccessibilityNodeWithoutView) { // Ensure that announcing the alert propagates the message to the alert node. // Different screen readers use different properties for alerts. -TEST(FlutterWindowTest, AlertNode) { +TEST_F(FlutterWindowTest, AlertNode) { std::unique_ptr win32window = std::make_unique(); EXPECT_CALL(*win32window.get(), GetAxFragmentRootDelegate()) @@ -338,7 +342,7 @@ TEST(FlutterWindowTest, AlertNode) { EXPECT_EQ(role.lVal, ROLE_SYSTEM_ALERT); } -TEST(FlutterWindowTest, LifecycleFocusMessages) { +TEST_F(FlutterWindowTest, LifecycleFocusMessages) { MockFlutterWindow win32window; EXPECT_CALL(win32window, GetWindowHandle) .WillRepeatedly(Return(reinterpret_cast(1))); @@ -370,7 +374,7 @@ TEST(FlutterWindowTest, LifecycleFocusMessages) { EXPECT_EQ(last_event, WindowStateEvent::kUnfocus); } -TEST(FlutterWindowTest, CachedLifecycleMessage) { +TEST_F(FlutterWindowTest, CachedLifecycleMessage) { MockFlutterWindow win32window; EXPECT_CALL(win32window, GetWindowHandle) .WillRepeatedly(Return(reinterpret_cast(1))); @@ -403,7 +407,7 @@ TEST(FlutterWindowTest, CachedLifecycleMessage) { EXPECT_TRUE(restored); } -TEST(FlutterWindowTest, UpdateCursor) { +TEST_F(FlutterWindowTest, UpdateCursor) { FlutterWindow win32window(100, 100); win32window.UpdateFlutterCursor("text"); HCURSOR cursor = ::GetCursor();