Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 7a83557

Browse files
authored
[Windows] Improve FlutterWindow unit tests (#50676)
_This was split from #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: #50673 This also introduces a fixture for the window unit tests. Part of flutter/flutter#137267 Part of flutter/flutter#142845 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent b4e4d17 commit 7a83557

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

shell/platform/windows/flutter_window.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ FlutterWindow::FlutterWindow(
121121
int height,
122122
std::shared_ptr<WindowsProcTable> windows_proc_table,
123123
std::unique_ptr<TextInputManager> text_input_manager)
124-
: binding_handler_delegate_(nullptr),
125-
touch_id_generator_(kMinTouchDeviceId, kMaxTouchDeviceId),
124+
: touch_id_generator_(kMinTouchDeviceId, kMaxTouchDeviceId),
126125
windows_proc_table_(std::move(windows_proc_table)),
127126
text_input_manager_(std::move(text_input_manager)),
128127
ax_fragment_root_(nullptr) {
@@ -148,13 +147,19 @@ FlutterWindow::FlutterWindow(
148147
current_cursor_ = ::LoadCursor(nullptr, IDC_ARROW);
149148
}
150149

150+
// Base constructor for mocks
151+
FlutterWindow::FlutterWindow()
152+
: touch_id_generator_(kMinTouchDeviceId, kMaxTouchDeviceId) {}
153+
151154
FlutterWindow::~FlutterWindow() {
152155
Destroy();
153156
}
154157

155158
void FlutterWindow::SetView(WindowBindingHandlerDelegate* window) {
156159
binding_handler_delegate_ = window;
157-
direct_manipulation_owner_->SetBindingHandlerDelegate(window);
160+
if (direct_manipulation_owner_) {
161+
direct_manipulation_owner_->SetBindingHandlerDelegate(window);
162+
}
158163
if (restored_ && window) {
159164
OnWindowStateEvent(WindowStateEvent::kShow);
160165
}

shell/platform/windows/flutter_window.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ class FlutterWindow : public KeyboardManager::WindowDelegate,
194194
virtual void OnWindowStateEvent(WindowStateEvent event);
195195

196196
protected:
197+
// Base constructor for mocks.
198+
FlutterWindow();
199+
197200
// Win32's DefWindowProc.
198201
//
199202
// Used as the fallback behavior of HandleMessage. Exposed for dependency
@@ -321,7 +324,7 @@ class FlutterWindow : public KeyboardManager::WindowDelegate,
321324

322325
// A pointer to a FlutterWindowsView that can be used to update engine
323326
// windowing and input state.
324-
WindowBindingHandlerDelegate* binding_handler_delegate_;
327+
WindowBindingHandlerDelegate* binding_handler_delegate_ = nullptr;
325328

326329
// The last cursor set by Flutter. Defaults to the arrow cursor.
327330
HCURSOR current_cursor_;

shell/platform/windows/flutter_window_unittests.cc

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "flutter/shell/platform/windows/flutter_window.h"
77
#include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
88
#include "flutter/shell/platform/windows/testing/mock_window_binding_handler_delegate.h"
9+
#include "flutter/shell/platform/windows/testing/windows_test.h"
910
#include "flutter/shell/platform/windows/testing/wm_builders.h"
1011

1112
#include "gmock/gmock.h"
@@ -25,7 +26,7 @@ static constexpr int32_t kDefaultPointerDeviceId = 0;
2526
class MockFlutterWindow : public FlutterWindow {
2627
public:
2728
MockFlutterWindow(bool reset_view_on_exit = true)
28-
: FlutterWindow(800, 600), reset_view_on_exit_(reset_view_on_exit) {
29+
: FlutterWindow(), reset_view_on_exit_(reset_view_on_exit) {
2930
ON_CALL(*this, GetDpiScale())
3031
.WillByDefault(Return(this->FlutterWindow::GetDpiScale()));
3132
}
@@ -107,14 +108,16 @@ class MockFlutterWindowsView : public FlutterWindowsView {
107108
FML_DISALLOW_COPY_AND_ASSIGN(MockFlutterWindowsView);
108109
};
109110

111+
class FlutterWindowTest : public WindowsTest {};
112+
110113
} // namespace
111114

112-
TEST(FlutterWindowTest, CreateDestroy) {
115+
TEST_F(FlutterWindowTest, CreateDestroy) {
113116
FlutterWindow window(800, 600);
114117
ASSERT_TRUE(TRUE);
115118
}
116119

117-
TEST(FlutterWindowTest, OnBitmapSurfaceUpdated) {
120+
TEST_F(FlutterWindowTest, OnBitmapSurfaceUpdated) {
118121
FlutterWindow win32window(100, 100);
119122
int old_handle_count = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS);
120123

@@ -131,7 +134,7 @@ TEST(FlutterWindowTest, OnBitmapSurfaceUpdated) {
131134
// Tests that composing rect updates are transformed from Flutter logical
132135
// coordinates to device coordinates and passed to the text input manager
133136
// when the DPI scale is 100% (96 DPI).
134-
TEST(FlutterWindowTest, OnCursorRectUpdatedRegularDPI) {
137+
TEST_F(FlutterWindowTest, OnCursorRectUpdatedRegularDPI) {
135138
MockFlutterWindow win32window;
136139
EXPECT_CALL(win32window, GetDpiScale()).WillOnce(Return(1.0));
137140

@@ -144,7 +147,7 @@ TEST(FlutterWindowTest, OnCursorRectUpdatedRegularDPI) {
144147
// Tests that composing rect updates are transformed from Flutter logical
145148
// coordinates to device coordinates and passed to the text input manager
146149
// when the DPI scale is 150% (144 DPI).
147-
TEST(FlutterWindowTest, OnCursorRectUpdatedHighDPI) {
150+
TEST_F(FlutterWindowTest, OnCursorRectUpdatedHighDPI) {
148151
MockFlutterWindow win32window;
149152
EXPECT_CALL(win32window, GetDpiScale()).WillOnce(Return(1.5));
150153

@@ -155,7 +158,7 @@ TEST(FlutterWindowTest, OnCursorRectUpdatedHighDPI) {
155158
win32window.OnCursorRectUpdated(cursor_rect);
156159
}
157160

158-
TEST(FlutterWindowTest, OnPointerStarSendsDeviceType) {
161+
TEST_F(FlutterWindowTest, OnPointerStarSendsDeviceType) {
159162
FlutterWindow win32window(100, 100);
160163
MockWindowBindingHandlerDelegate delegate;
161164
EXPECT_CALL(delegate, OnWindowStateEvent).Times(AnyNumber());
@@ -256,15 +259,16 @@ TEST(FlutterWindowTest, OnPointerStarSendsDeviceType) {
256259

257260
// Tests that calls to OnScroll in turn calls GetScrollOffsetMultiplier
258261
// for mapping scroll ticks to pixels.
259-
TEST(FlutterWindowTest, OnScrollCallsGetScrollOffsetMultiplier) {
262+
TEST_F(FlutterWindowTest, OnScrollCallsGetScrollOffsetMultiplier) {
260263
MockFlutterWindow win32window;
261264
MockWindowBindingHandlerDelegate delegate;
262265
EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber());
263266
win32window.SetView(&delegate);
264267

265-
ON_CALL(win32window, GetScrollOffsetMultiplier())
266-
.WillByDefault(Return(120.0f));
267-
EXPECT_CALL(win32window, GetScrollOffsetMultiplier()).Times(1);
268+
EXPECT_CALL(win32window, GetWindowHandle).WillOnce([&win32window]() {
269+
return win32window.FlutterWindow::GetWindowHandle();
270+
});
271+
EXPECT_CALL(win32window, GetScrollOffsetMultiplier).WillOnce(Return(120.0f));
268272

269273
EXPECT_CALL(delegate,
270274
OnScroll(_, _, 0, 0, 120.0f, kFlutterPointerDeviceKindMouse,
@@ -275,7 +279,7 @@ TEST(FlutterWindowTest, OnScrollCallsGetScrollOffsetMultiplier) {
275279
kDefaultPointerDeviceId);
276280
}
277281

278-
TEST(FlutterWindowTest, OnWindowRepaint) {
282+
TEST_F(FlutterWindowTest, OnWindowRepaint) {
279283
MockFlutterWindow win32window;
280284
MockWindowBindingHandlerDelegate delegate;
281285
EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber());
@@ -286,7 +290,7 @@ TEST(FlutterWindowTest, OnWindowRepaint) {
286290
win32window.InjectWindowMessage(WM_PAINT, 0, 0);
287291
}
288292

289-
TEST(FlutterWindowTest, OnThemeChange) {
293+
TEST_F(FlutterWindowTest, OnThemeChange) {
290294
MockFlutterWindow win32window;
291295
MockWindowBindingHandlerDelegate delegate;
292296
EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber());
@@ -300,15 +304,15 @@ TEST(FlutterWindowTest, OnThemeChange) {
300304
// The window should return no root accessibility node if
301305
// it isn't attached to a view.
302306
// Regression test for https://github.com/flutter/flutter/issues/129791
303-
TEST(FlutterWindowTest, AccessibilityNodeWithoutView) {
307+
TEST_F(FlutterWindowTest, AccessibilityNodeWithoutView) {
304308
MockFlutterWindow win32window;
305309

306310
EXPECT_EQ(win32window.GetNativeViewAccessible(), nullptr);
307311
}
308312

309313
// Ensure that announcing the alert propagates the message to the alert node.
310314
// Different screen readers use different properties for alerts.
311-
TEST(FlutterWindowTest, AlertNode) {
315+
TEST_F(FlutterWindowTest, AlertNode) {
312316
std::unique_ptr<MockFlutterWindow> win32window =
313317
std::make_unique<MockFlutterWindow>();
314318
EXPECT_CALL(*win32window.get(), GetAxFragmentRootDelegate())
@@ -338,7 +342,7 @@ TEST(FlutterWindowTest, AlertNode) {
338342
EXPECT_EQ(role.lVal, ROLE_SYSTEM_ALERT);
339343
}
340344

341-
TEST(FlutterWindowTest, LifecycleFocusMessages) {
345+
TEST_F(FlutterWindowTest, LifecycleFocusMessages) {
342346
MockFlutterWindow win32window;
343347
EXPECT_CALL(win32window, GetWindowHandle)
344348
.WillRepeatedly(Return(reinterpret_cast<HWND>(1)));
@@ -370,7 +374,7 @@ TEST(FlutterWindowTest, LifecycleFocusMessages) {
370374
EXPECT_EQ(last_event, WindowStateEvent::kUnfocus);
371375
}
372376

373-
TEST(FlutterWindowTest, CachedLifecycleMessage) {
377+
TEST_F(FlutterWindowTest, CachedLifecycleMessage) {
374378
MockFlutterWindow win32window;
375379
EXPECT_CALL(win32window, GetWindowHandle)
376380
.WillRepeatedly(Return(reinterpret_cast<HWND>(1)));
@@ -403,7 +407,7 @@ TEST(FlutterWindowTest, CachedLifecycleMessage) {
403407
EXPECT_TRUE(restored);
404408
}
405409

406-
TEST(FlutterWindowTest, UpdateCursor) {
410+
TEST_F(FlutterWindowTest, UpdateCursor) {
407411
FlutterWindow win32window(100, 100);
408412
win32window.UpdateFlutterCursor("text");
409413
HCURSOR cursor = ::GetCursor();

0 commit comments

Comments
 (0)