Skip to content

Commit 2b9ea14

Browse files
committed
Better ModifyRegularWindow test
1 parent b44e867 commit 2b9ea14

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

engine/src/flutter/shell/platform/windows/flutter_host_window_controller.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,10 @@ bool FlutterHostWindowController::ModifyHostWindow(
7272
HWND const window_handle = window->GetWindowHandle();
7373

7474
std::optional<Size> changed_size;
75+
Size const size_before = GetViewSize(view_id);
76+
7577
if (settings.size.has_value()) {
76-
Size const view_size_before = GetViewSize(view_id);
7778
window->SetClientSize(*settings.size);
78-
Size const view_size_after = GetViewSize(view_id);
79-
if (!(view_size_before == view_size_after)) {
80-
changed_size = view_size_after;
81-
}
8279
}
8380
if (settings.title.has_value()) {
8481
window->SetTitle(*settings.title);
@@ -102,6 +99,11 @@ bool FlutterHostWindowController::ModifyHostWindow(
10299
}
103100
}
104101

102+
Size const size_after = GetViewSize(view_id);
103+
if (!(size_before == size_after)) {
104+
changed_size = size_after;
105+
}
106+
105107
if (changed_size) {
106108
SendOnWindowChanged(view_id, changed_size, std::nullopt);
107109
}

engine/src/flutter/shell/platform/windows/flutter_host_window_controller_unittests.cc

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,41 @@ void PumpMessage() {
3434
}
3535
}
3636

37+
Size GetLogicalClientSize(HWND hwnd) {
38+
RECT rect;
39+
GetClientRect(hwnd, &rect);
40+
double const dpr = FlutterDesktopGetDpiForHWND(hwnd) /
41+
static_cast<double>(USER_DEFAULT_SCREEN_DPI);
42+
double const width = rect.right / dpr;
43+
double const height = rect.bottom / dpr;
44+
return {width, height};
45+
}
46+
47+
std::wstring GetWindowTitle(HWND hwnd) {
48+
int length = GetWindowTextLengthW(hwnd);
49+
if (length == 0)
50+
return L"";
51+
52+
std::vector<wchar_t> buffer(length + 1);
53+
GetWindowTextW(hwnd, buffer.data(), length + 1);
54+
return std::wstring(buffer.data());
55+
}
56+
57+
std::wstring StringToWstring(std::string_view str) {
58+
if (str.empty()) {
59+
return {};
60+
}
61+
if (int buffer_size =
62+
MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), nullptr, 0)) {
63+
std::wstring wide_str(buffer_size, L'\0');
64+
if (MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), &wide_str[0],
65+
buffer_size)) {
66+
return wide_str;
67+
}
68+
}
69+
return {};
70+
}
71+
3772
class FlutterHostWindowControllerTest : public WindowsTest {
3873
public:
3974
FlutterHostWindowControllerTest() = default;
@@ -58,7 +93,7 @@ class FlutterHostWindowControllerTest : public WindowsTest {
5893
FML_CHECK(SUCCEEDED(::CoInitializeEx(nullptr, COINIT_MULTITHREADED)));
5994
}
6095

61-
void SetDpiAwareness() {
96+
void SetDpiAwareness() const {
6297
HMODULE user32_module = LoadLibraryA("user32.dll");
6398
if (!user32_module) {
6499
return;
@@ -98,8 +133,8 @@ TEST_F(FlutterHostWindowControllerTest, CreateRegularWindow) {
98133
ASSERT_TRUE(result.has_value());
99134
EXPECT_NE(engine()->view(result->view_id), nullptr);
100135
EXPECT_EQ(result->archetype, settings.archetype);
101-
EXPECT_GE(result->size.width(), settings.size.width());
102-
EXPECT_GE(result->size.height(), settings.size.height());
136+
EXPECT_EQ(result->size.width(), settings.size.width());
137+
EXPECT_EQ(result->size.height(), settings.size.height());
103138
EXPECT_FALSE(result->parent_id.has_value());
104139

105140
// Ensure the window was successfully retrieved.
@@ -114,7 +149,6 @@ TEST_F(FlutterHostWindowControllerTest, ModifyRegularWindow) {
114149
.archetype = WindowArchetype::kRegular,
115150
.size = {800.0, 600.0},
116151
.title = "window",
117-
.state = WindowState::kMinimized,
118152
};
119153

120154
// Create the window.
@@ -129,8 +163,7 @@ TEST_F(FlutterHostWindowControllerTest, ModifyRegularWindow) {
129163
// Define the modifications to be applied to the window.
130164
WindowModificationSettings const modification_settings = {
131165
.size = Size{200.0, 200.0},
132-
.title = "new title",
133-
.state = WindowState::kRestored,
166+
.title = "new title 😉",
134167
};
135168

136169
// Test messenger with a handler for onWindowChanged.
@@ -172,8 +205,8 @@ TEST_F(FlutterHostWindowControllerTest, ModifyRegularWindow) {
172205
ASSERT_NE(value_width, nullptr);
173206
auto const* value_height = std::get_if<double>(&value_size->at(1));
174207
ASSERT_NE(value_height, nullptr);
175-
EXPECT_GE(*value_width, modification_settings.size->width());
176-
EXPECT_GE(*value_height, modification_settings.size->height());
208+
EXPECT_EQ(*value_width, modification_settings.size->width());
209+
EXPECT_EQ(*value_height, modification_settings.size->height());
177210

178211
done = true;
179212
}
@@ -186,6 +219,17 @@ TEST_F(FlutterHostWindowControllerTest, ModifyRegularWindow) {
186219
EXPECT_TRUE(host_window_controller()->ModifyHostWindow(
187220
metadata->view_id, modification_settings));
188221

222+
// Validate the modified settings.
223+
HWND const window_handle = host_window_controller()
224+
->GetHostWindow(metadata->view_id)
225+
->GetWindowHandle();
226+
Size const new_size = GetLogicalClientSize(window_handle);
227+
EXPECT_EQ(new_size.width(), modification_settings.size->width());
228+
EXPECT_EQ(new_size.height(), modification_settings.size->height());
229+
std::wstring const new_title = GetWindowTitle(window_handle);
230+
std::wstring const old_title = StringToWstring(*modification_settings.title);
231+
EXPECT_STREQ(new_title.c_str(), old_title.c_str());
232+
189233
// Pump messages for the Windows platform task runner.
190234
while (!done) {
191235
PumpMessage();

0 commit comments

Comments
 (0)