Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Improve FlutterWindow unit tests #50676

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions shell/platform/windows/flutter_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion shell/platform/windows/flutter_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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_;
Expand Down
38 changes: 21 additions & 17 deletions shell/platform/windows/flutter_window_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()));
}
Expand Down Expand Up @@ -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);

Expand All @@ -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));

Expand All @@ -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));

Expand All @@ -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());
Expand Down Expand Up @@ -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,
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -300,15 +304,15 @@ 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);
}

// 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())
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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();
Expand Down