| Flutter Windows Embedder |

display_manager_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <cstring>
6 #include "flutter/shell/platform/windows/display_manager.h"
7
8 #include <string>
9 #include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
10 #include "flutter/shell/platform/windows/testing/windows_test.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
13 #include "shell/platform/windows/testing/mock_windows_proc_table.h"
14
15 // Mock Windows API functions to avoid hardware dependencies
16 #define MOCK_WINDOWS_API
17
18 namespace flutter {
19 namespace testing {
20
21 using ::testing::_;
22 using ::testing::AllOf;
23 using ::testing::AtLeast;
24 using ::testing::DoAll;
25 using ::testing::Field;
26 using ::testing::NiceMock;
27 using ::testing::Return;
28 using ::testing::SetArgPointee;
29 using ::testing::StrEq;
30
31 class DisplayManagerWin32Test : public WindowsTest {};
32
33 // Test that the display manager correctly handles multiple monitors
34 TEST_F(DisplayManagerWin32Test, MultipleMonitors) {
35 auto mock_windows_proc_table =
36 std::make_shared<NiceMock<MockWindowsProcTable>>();
37
38 FlutterWindowsEngineBuilder builder(GetContext());
39 builder.SetWindowsProcTable(mock_windows_proc_table);
40 std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
41
42 HMONITOR mock_monitor1 = reinterpret_cast<HMONITOR>(123);
43 HMONITOR mock_monitor2 = reinterpret_cast<HMONITOR>(456);
44
45 MONITORINFOEXW monitor_info1 = {};
46 monitor_info1.cbSize = sizeof(MONITORINFOEXW);
47 monitor_info1.rcMonitor = {0, 0, 1920, 1080};
48 monitor_info1.rcWork = {0, 0, 1920, 1080};
49 monitor_info1.dwFlags = MONITORINFOF_PRIMARY;
50 wcscpy_s(monitor_info1.szDevice, L"\\\\.\\DISPLAY1");
51
52 MONITORINFOEXW monitor_info2 = {};
53 monitor_info2.cbSize = sizeof(MONITORINFOEXW);
54 monitor_info2.rcMonitor = {1920, 0, 1920 + 2560, 1440};
55 monitor_info2.rcWork = {1920, 0, 1920 + 2560, 1440};
56 monitor_info2.dwFlags = 0;
57 wcscpy_s(monitor_info2.szDevice, L"\\\\.\\DISPLAY2");
58
59 EXPECT_CALL(*mock_windows_proc_table, GetMonitorInfoW(mock_monitor1, _))
60 .WillOnce(DoAll(SetArgPointee<1>(monitor_info1), Return(TRUE)));
61 EXPECT_CALL(*mock_windows_proc_table, GetMonitorInfoW(mock_monitor2, _))
62 .WillOnce(DoAll(SetArgPointee<1>(monitor_info2), Return(TRUE)));
63
64 EXPECT_CALL(*mock_windows_proc_table,
65 EnumDisplayMonitors(nullptr, nullptr, _, _))
66 .WillOnce([&](HDC hdc, LPCRECT lprcClip, MONITORENUMPROC lpfnEnum,
67 LPARAM dwData) {
68 lpfnEnum(mock_monitor1, nullptr, &monitor_info1.rcMonitor, dwData);
69 lpfnEnum(mock_monitor2, nullptr, &monitor_info2.rcMonitor, dwData);
70 return TRUE;
71 });
72
73 // Set up GetDpiForMonitor to return different DPI values
74 EXPECT_CALL(*mock_windows_proc_table, GetDpiForMonitor(mock_monitor1, _))
75 .WillRepeatedly(Return(96)); // Default/Standard DPI
76 EXPECT_CALL(*mock_windows_proc_table, GetDpiForMonitor(mock_monitor2, _))
77 .WillRepeatedly(Return(144)); // High DPI
78
79 EXPECT_CALL(*mock_windows_proc_table, EnumDisplaySettings(_, _, _))
80 .WillRepeatedly(Return(TRUE));
81
82 // Create the display manager with the mock engine
83 auto display_manager = std::make_unique<DisplayManagerWin32>(engine.get());
84
85 display_manager->UpdateDisplays();
86 }
87
88 // Test that the display manager correctly handles a display change message
89 TEST_F(DisplayManagerWin32Test, HandleDisplayChangeMessage) {
90 // Create a mock Windows proc table
91 auto mock_windows_proc_table =
92 std::make_shared<NiceMock<MockWindowsProcTable>>();
93
94 // Create a mock engine
95 FlutterWindowsEngineBuilder builder(GetContext());
96 builder.SetWindowsProcTable(mock_windows_proc_table);
97 std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
98
99 EXPECT_CALL(*mock_windows_proc_table, EnumDisplayMonitors(_, _, _, _))
100 .WillRepeatedly(Return(TRUE));
101
102 // Create the display manager with the mock engine
103 auto display_manager = std::make_unique<DisplayManagerWin32>(engine.get());
104
105 // Test handling a display change message
106 HWND dummy_hwnd = reinterpret_cast<HWND>(1);
107 LRESULT result = 0;
108
109 // Verify that WM_DISPLAYCHANGE is handled
110 EXPECT_FALSE(display_manager->HandleWindowMessage(
111 dummy_hwnd, WM_DISPLAYCHANGE, 0, 0, &result));
112
113 // Verify that WM_DPICHANGED is handled
114 EXPECT_FALSE(display_manager->HandleWindowMessage(dummy_hwnd, WM_DPICHANGED,
115 0, 0, &result));
116
117 // Verify that other messages are not handled
118 EXPECT_FALSE(display_manager->HandleWindowMessage(dummy_hwnd, WM_PAINT, 0, 0,
119 &result));
120 }
121
122 } // namespace testing
123 } // namespace flutter
flutter::testing::DisplayManagerWin32Test
Definition: display_manager_unittests.cc:31
display_manager
const DisplayManagerWin32 * display_manager
Definition: display_manager.cc:18
display_manager.h
flutter::testing::TEST_F
TEST_F(AccessibilityPluginTest, DirectAnnounceCall)
Definition: accessibility_plugin_unittests.cc:102
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::GetDpiForMonitor
UINT GetDpiForMonitor(HMONITOR monitor)
Definition: dpi_utils.cc:132