5#include "flutter/fml/macros.h"
6#include "flutter/shell/platform/windows/flutter_window.h"
7#include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
8#include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
9#include "flutter/shell/platform/windows/testing/mock_window_binding_handler_delegate.h"
10#include "flutter/shell/platform/windows/testing/windows_test.h"
11#include "flutter/shell/platform/windows/testing/wm_builders.h"
13#include "gmock/gmock.h"
14#include "gtest/gtest.h"
20using ::testing::AnyNumber;
22using ::testing::Return;
25static constexpr int32_t kDefaultPointerDeviceId = 0;
27class MockFlutterWindow :
public FlutterWindow {
29 MockFlutterWindow(
bool reset_view_on_exit =
true)
30 : reset_view_on_exit_(reset_view_on_exit) {
31 ON_CALL(*
this, GetDpiScale())
34 virtual ~MockFlutterWindow() {
35 if (reset_view_on_exit_) {
41 UINT GetDpi() {
return GetCurrentDPI(); }
47 return HandleMessage(
message, wparam, lparam);
50 MOCK_METHOD(
void, OnDpiScale, (
unsigned int), (
override));
51 MOCK_METHOD(
void, OnResize, (
unsigned int,
unsigned int), (
override));
68 MOCK_METHOD(
void, OnSetCursor, (), (
override));
69 MOCK_METHOD(
float, GetScrollOffsetMultiplier, (), (
override));
70 MOCK_METHOD(
float, GetDpiScale, (), (
override));
71 MOCK_METHOD(
void, UpdateCursorRect, (
const Rect&), (
override));
72 MOCK_METHOD(
void, OnResetImeComposing, (), (
override));
75 MOCK_METHOD(uint32_t, Win32MapVkToChar, (uint32_t), (
override));
76 MOCK_METHOD(HWND, GetWindowHandle, (), (
override));
78 GetAxFragmentRootDelegate,
85 LRESULT Win32DefWindowProc(HWND hWnd,
93 bool reset_view_on_exit_;
97class MockFlutterWindowsView :
public FlutterWindowsView {
100 std::unique_ptr<WindowBindingHandler> window_binding)
106 NotifyWinEventWrapper,
114class FlutterWindowTest :
public WindowsTest {};
118TEST_F(FlutterWindowTest, CreateDestroy) {
123TEST_F(FlutterWindowTest, OnBitmapSurfaceUpdated) {
125 int old_handle_count = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS);
127 constexpr size_t row_bytes = 100 * 4;
128 constexpr size_t height = 100;
129 std::array<char, row_bytes * height> allocation;
132 int new_handle_count = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS);
134 EXPECT_EQ(old_handle_count, new_handle_count);
140TEST_F(FlutterWindowTest, OnCursorRectUpdatedRegularDPI) {
141 MockFlutterWindow win32window;
142 EXPECT_CALL(win32window, GetDpiScale()).WillOnce(Return(1.0));
145 EXPECT_CALL(win32window, UpdateCursorRect(cursor_rect)).Times(1);
147 win32window.OnCursorRectUpdated(cursor_rect);
153TEST_F(FlutterWindowTest, OnCursorRectUpdatedHighDPI) {
154 MockFlutterWindow win32window;
155 EXPECT_CALL(win32window, GetDpiScale()).WillOnce(Return(1.5));
158 EXPECT_CALL(win32window, UpdateCursorRect(expected_cursor_rect)).Times(1);
161 win32window.OnCursorRectUpdated(cursor_rect);
164TEST_F(FlutterWindowTest, OnPointerStarSendsDeviceType) {
167 EXPECT_CALL(delegate, OnWindowStateEvent).Times(AnyNumber());
168 win32window.
SetView(&delegate);
171 EXPECT_CALL(delegate,
173 kDefaultPointerDeviceId, 0))
175 EXPECT_CALL(delegate,
177 kDefaultPointerDeviceId, 0))
179 EXPECT_CALL(delegate,
181 kDefaultPointerDeviceId, 0))
203 kDefaultPointerDeviceId,
207 kDefaultPointerDeviceId,
211 kDefaultPointerDeviceId,
216 EXPECT_CALL(delegate,
218 kDefaultPointerDeviceId))
220 EXPECT_CALL(delegate,
222 kDefaultPointerDeviceId))
224 EXPECT_CALL(delegate,
226 kDefaultPointerDeviceId))
230 kDefaultPointerDeviceId, 0);
232 kDefaultPointerDeviceId, WM_LBUTTONDOWN);
234 kDefaultPointerDeviceId, WM_LBUTTONDOWN);
236 kDefaultPointerDeviceId);
240 kDefaultPointerDeviceId, 0);
242 kDefaultPointerDeviceId, WM_LBUTTONDOWN);
244 kDefaultPointerDeviceId, WM_LBUTTONDOWN);
246 kDefaultPointerDeviceId);
250 kDefaultPointerDeviceId, 0);
252 kDefaultPointerDeviceId, WM_LBUTTONDOWN);
254 kDefaultPointerDeviceId, WM_LBUTTONDOWN);
256 kDefaultPointerDeviceId);
265TEST_F(FlutterWindowTest, OnScrollCallsGetScrollOffsetMultiplier) {
266 MockFlutterWindow win32window;
268 EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber());
269 win32window.SetView(&delegate);
271 EXPECT_CALL(win32window, GetWindowHandle).WillOnce([&win32window]() {
272 return win32window.FlutterWindow::GetWindowHandle();
274 EXPECT_CALL(win32window, GetScrollOffsetMultiplier).WillOnce(Return(120.0f));
276 EXPECT_CALL(delegate,
278 kDefaultPointerDeviceId))
282 kDefaultPointerDeviceId);
285TEST_F(FlutterWindowTest, OnWindowRepaint) {
286 MockFlutterWindow win32window;
288 EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber());
289 win32window.SetView(&delegate);
291 EXPECT_CALL(delegate, OnWindowRepaint()).Times(1);
293 win32window.InjectWindowMessage(WM_PAINT, 0, 0);
296TEST_F(FlutterWindowTest, OnThemeChange) {
297 MockFlutterWindow win32window;
299 EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber());
300 win32window.SetView(&delegate);
302 EXPECT_CALL(delegate, OnHighContrastChanged).Times(1);
304 win32window.InjectWindowMessage(WM_THEMECHANGED, 0, 0);
310TEST_F(FlutterWindowTest, AccessibilityNodeWithoutView) {
311 MockFlutterWindow win32window;
313 EXPECT_EQ(win32window.GetNativeViewAccessible(),
nullptr);
319 std::unique_ptr<FlutterWindowsEngine>
engine =
321 auto win32window = std::make_unique<MockFlutterWindow>();
322 EXPECT_CALL(*win32window.get(), GetAxFragmentRootDelegate())
323 .WillRepeatedly(Return(
nullptr));
324 EXPECT_CALL(*win32window.get(), OnWindowStateEvent).Times(AnyNumber());
325 EXPECT_CALL(*win32window.get(), GetWindowHandle).Times(AnyNumber());
326 MockFlutterWindowsView view{
engine.get(), std::move(win32window)};
327 std::wstring
message =
L"Test alert";
332 IAccessible* alert = view.AlertNode();
335 alert->get_accName(
self, &strptr);
338 alert->get_accDescription(
self, &strptr);
341 alert->get_accValue(
self, &strptr);
345 alert->get_accRole(
self, &role);
346 EXPECT_EQ(role.vt, VT_I4);
347 EXPECT_EQ(role.lVal, ROLE_SYSTEM_ALERT);
350TEST_F(FlutterWindowTest, LifecycleFocusMessages) {
351 MockFlutterWindow win32window;
352 EXPECT_CALL(win32window, GetWindowHandle)
353 .WillRepeatedly(Return(
reinterpret_cast<HWND
>(1)));
357 EXPECT_CALL(delegate, OnWindowStateEvent)
361 EXPECT_CALL(win32window, OnWindowStateEvent)
363 win32window.FlutterWindow::OnWindowStateEvent(
event);
365 EXPECT_CALL(win32window, OnResize).Times(AnyNumber());
367 win32window.SetView(&delegate);
369 win32window.InjectWindowMessage(WM_SIZE, 0, 0);
372 win32window.InjectWindowMessage(WM_SIZE, 0, MAKEWORD(1, 1));
375 win32window.InjectWindowMessage(WM_SETFOCUS, 0, 0);
378 win32window.InjectWindowMessage(WM_KILLFOCUS, 0, 0);
382TEST_F(FlutterWindowTest, CachedLifecycleMessage) {
383 MockFlutterWindow win32window;
384 EXPECT_CALL(win32window, GetWindowHandle)
385 .WillRepeatedly(Return(
reinterpret_cast<HWND
>(1)));
386 EXPECT_CALL(win32window, OnWindowStateEvent)
388 win32window.FlutterWindow::OnWindowStateEvent(
event);
390 EXPECT_CALL(win32window, OnResize).Times(1);
393 win32window.InjectWindowMessage(WM_SIZE, 0, MAKEWORD(1, 1));
396 win32window.InjectWindowMessage(WM_SETFOCUS, 0, 0);
399 bool focused =
false;
400 bool restored =
false;
401 EXPECT_CALL(delegate, OnWindowStateEvent)
410 win32window.SetView(&delegate);
418 HCURSOR cursor = ::GetCursor();
419 EXPECT_EQ(cursor, ::LoadCursor(
nullptr, IDC_IBEAM));
virtual void OnPointerLeave(double x, double y, FlutterPointerDeviceKind device_kind, int32_t device_id)
virtual bool OnBitmapSurfaceUpdated(const void *allocation, size_t row_bytes, size_t height) override
virtual void OnPointerUp(double x, double y, FlutterPointerDeviceKind device_kind, int32_t device_id, UINT button)
virtual void OnPointerDown(double x, double y, FlutterPointerDeviceKind device_kind, int32_t device_id, UINT button)
virtual void OnPointerMove(double x, double y, FlutterPointerDeviceKind device_kind, int32_t device_id, int modifiers_state)
virtual void SetView(WindowBindingHandlerDelegate *view) override
virtual float GetDpiScale() override
virtual void UpdateFlutterCursor(const std::string &cursor_name) override
FlutterWindowsView(FlutterViewId view_id, FlutterWindowsEngine *engine, std::unique_ptr< WindowBindingHandler > window_binding, std::shared_ptr< WindowsProcTable > windows_proc_table=nullptr)
virtual void OnScroll(double x, double y, double delta_x, double delta_y, int scroll_offset_multiplier, FlutterPointerDeviceKind device_kind, int32_t device_id)=0
~MockFlutterWindowsView()
MOCK_METHOD(void, NotifyWinEventWrapper,(ui::AXPlatformNodeWin *, ax::mojom::Event),(override))
MockFlutterWindowsView(FlutterWindowsEngine *engine, std::unique_ptr< WindowBindingHandler > wbh)
@ kFlutterPointerButtonMousePrimary
FlutterPointerDeviceKind
The device type that created a pointer event.
@ kFlutterPointerDeviceKindTouch
@ kFlutterPointerDeviceKindStylus
@ kFlutterPointerDeviceKindMouse
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
SK_API GrDirectContext * GetContext(const SkImage *src)
ObjectPtr Invoke(const Library &lib, const char *name)
TEST_F(DisplayListTest, Defaults)
constexpr LRESULT kWmResultDefault
constexpr int64_t kImplicitViewId
WindowStateEvent
An event representing a change in window state that may update the.
#define EXPECT_TRUE(handle)