[Windows] Improve FlutterWindow unit tests (flutter/engine#50676)

_This was split from https://github.com/flutter/engine/pull/50673 to reduce noise in that PR._

Previously `MockFlutterWindow` called a `FlutterWindow` constructor which created a window & resized it. This change introduces a minimal base constructor for testing purposes. This allows us to skip some noisy mocks in a subsequent change: https://github.com/flutter/engine/pull/50673

This also introduces a fixture for the window unit tests.

Part of https://github.com/flutter/flutter/issues/137267
Part of https://github.com/flutter/flutter/issues/142845

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Loïc Sharma
2024-02-15 16:38:18 -08:00
committed by GitHub
parent 80076b93fb
commit 89b44f4fff
3 changed files with 33 additions and 21 deletions

View File

@@ -121,8 +121,7 @@ FlutterWindow::FlutterWindow(
int height,
std::shared_ptr<WindowsProcTable> windows_proc_table,
std::unique_ptr<TextInputManager> 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);
}

View File

@@ -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_;

View File

@@ -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<MockFlutterWindow> win32window =
std::make_unique<MockFlutterWindow>();
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<HWND>(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<HWND>(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();