Flutter Windows Embedder
window_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 <algorithm>
6 #include <vector>
7 
9 #include "flutter/shell/platform/windows/testing/egl/mock_context.h"
10 #include "flutter/shell/platform/windows/testing/egl/mock_manager.h"
11 #include "flutter/shell/platform/windows/testing/egl/mock_window_surface.h"
12 #include "flutter/shell/platform/windows/testing/engine_modifier.h"
13 #include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
14 #include "flutter/shell/platform/windows/testing/windows_test.h"
16 #include "gmock/gmock.h"
17 #include "gtest/gtest.h"
18 
19 namespace flutter {
20 namespace testing {
21 
22 namespace {
23 
24 using ::testing::NiceMock;
25 using ::testing::Return;
26 
27 // Builds a mock |WindowSurface| whose lifecycle operations all succeed. Used to
28 // keep the EGL surface lifecycle deterministic in tests so that resize paths
29 // (e.g. |FlutterWindowsView::OnFrameGenerated|) do not perform real, flaky
30 // cross-thread ANGLE/D3D calls against an actual GPU surface.
31 std::unique_ptr<egl::MockWindowSurface> CreateMockWindowSurface() {
32  auto surface = std::make_unique<NiceMock<egl::MockWindowSurface>>();
33  ON_CALL(*surface, IsValid).WillByDefault(Return(true));
34  ON_CALL(*surface, MakeCurrent).WillByDefault(Return(true));
35  ON_CALL(*surface, SetVSyncEnabled).WillByDefault(Return(true));
36  ON_CALL(*surface, Destroy).WillByDefault(Return(true));
37  return surface;
38 }
39 
40 class WindowManagerTest : public WindowsTest {
41  public:
42  WindowManagerTest() = default;
43  virtual ~WindowManagerTest() = default;
44 
45  protected:
46  void SetUp() override {
47  auto& context = GetContext();
48  FlutterWindowsEngineBuilder builder(context);
49  ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
50 
51  engine_ = builder.Build();
52  ASSERT_TRUE(engine_);
53 
54  engine_->SetRootIsolateCreateCallback(context.GetRootIsolateCallback());
55  ASSERT_TRUE(engine_->Run("testWindowController"));
56 
57  bool signalled = false;
58  context.AddFfiNativeFunction("Signal", CREATE_FFI_LAMBDA([&]() {
59  isolate_ = flutter::Isolate::Current();
60  signalled = true;
61  }));
62  while (!signalled) {
63  engine_->task_runner()->ProcessTasks();
64  }
65 
66  // Replace the engine's EGL manager with a permissive mock so that EGL
67  // surface creation and resizing are deterministic and never issue real,
68  // cross-thread GPU calls. These tests do not exercise real EGL rendering,
69  // and without this, tests that drive |FlutterWindowsView::OnFrameGenerated|
70  // from the test thread race the engine's raster thread for the EGL context,
71  // intermittently producing an EGL_BAD_ACCESS and a crash. Installed here,
72  // before any windows (and thus render surfaces) are created.
73  auto egl_manager = std::make_unique<NiceMock<egl::MockManager>>();
74  ON_CALL(*egl_manager, CreateWindowSurface)
75  .WillByDefault(
76  [](HWND, size_t, size_t) { return CreateMockWindowSurface(); });
77  ON_CALL(*egl_manager, render_context)
78  .WillByDefault(Return(&mock_egl_context_));
79  ON_CALL(mock_egl_context_, ClearCurrent).WillByDefault(Return(true));
80  ON_CALL(mock_egl_context_, MakeCurrent).WillByDefault(Return(true));
81  EngineModifier{engine_.get()}.SetEGLManager(std::move(egl_manager));
82  }
83 
84  void TearDown() override { engine_->Stop(); }
85 
86  int64_t engine_id() { return reinterpret_cast<int64_t>(engine_.get()); }
87  flutter::Isolate& isolate() { return *isolate_; }
88  RegularWindowCreationRequest* regular_creation_request() {
89  return &regular_creation_request_;
90  }
91  FlutterWindowsEngine* engine() { return engine_.get(); }
92 
93  // Creates a regular window to act as the parent of a satellite, and returns
94  // its window handle.
95  HWND CreateParentWindow() {
98  engine_id(), regular_creation_request()));
99  }
100 
101  // Places a satellite at the origin of its parent's client area, offset by
102  // (20, 30), at its requested size.
103  static void OffsetPositionCallback(const WindowSize& child_size,
104  const WindowRect& parent_rect,
105  const WindowRect& display_rect,
106  WindowRect& output_rect) {
107  output_rect.left = parent_rect.left + 20;
108  output_rect.top = parent_rect.top + 30;
109  output_rect.width = child_size.width;
110  output_rect.height = child_size.height;
111  }
112 
113  // Returns a fixed-size, resizable satellite creation request anchored to
114  // |parent|.
115  static SatelliteWindowCreationRequest SatelliteCreationRequest(
116  HWND parent,
117  GetWindowPositionCallback get_position_callback) {
118  return SatelliteWindowCreationRequest{
119  .preferred_size = {.has_preferred_view_size = true,
120  .preferred_view_width = 200,
121  .preferred_view_height = 150},
122  .preferred_constraints = {.has_view_constraints = true,
123  .view_min_width = 100,
124  .view_min_height = 50,
125  .view_max_width = 300,
126  .view_max_height = 200},
127  .parent = parent,
128  .get_position_callback = get_position_callback,
129  .title = L"Satellite",
130  .sized_to_content = false,
131  .resizable = true};
132  }
133 
134  private:
135  std::unique_ptr<FlutterWindowsEngine> engine_;
136  NiceMock<egl::MockContext> mock_egl_context_;
137  std::optional<flutter::Isolate> isolate_;
138  RegularWindowCreationRequest regular_creation_request_{
139  .preferred_size =
140  {
141  .has_preferred_view_size = true,
142  .preferred_view_width = 800,
143  .preferred_view_height = 600,
144  },
145  };
146 
147  FML_DISALLOW_COPY_AND_ASSIGN(WindowManagerTest);
148 };
149 
150 } // namespace
151 
152 TEST_F(WindowManagerTest, WindowingInitialize) {
153  IsolateScope isolate_scope(isolate());
154 
155  static bool received_message = false;
156  WindowingInitRequest init_request{
157  .on_message = [](WindowsMessage* message) { received_message = true; }};
158 
159  InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
160  const int64_t view_id =
162  engine_id(), regular_creation_request());
164  engine_id(), view_id));
165 
166  EXPECT_TRUE(received_message);
167 }
168 
169 TEST_F(WindowManagerTest, CreateRegularWindow) {
170  IsolateScope isolate_scope(isolate());
171 
172  const int64_t view_id =
174  engine_id(), regular_creation_request());
175  EXPECT_EQ(view_id, 1);
176 }
177 
178 TEST_F(WindowManagerTest, GetWindowHandle) {
179  IsolateScope isolate_scope(isolate());
180 
181  const int64_t view_id =
183  engine_id(), regular_creation_request());
184  const HWND window_handle =
186  view_id);
187  EXPECT_NE(window_handle, nullptr);
188 }
189 
190 TEST_F(WindowManagerTest, GetWindowSize) {
191  IsolateScope isolate_scope(isolate());
192 
193  const int64_t view_id =
195  engine_id(), regular_creation_request());
196  const HWND window_handle =
198  view_id);
199 
200  ActualWindowSize size =
202 
203  EXPECT_EQ(size.width,
204  regular_creation_request()->preferred_size.preferred_view_width);
205  EXPECT_EQ(size.height,
206  regular_creation_request()->preferred_size.preferred_view_height);
207 }
208 
209 TEST_F(WindowManagerTest, SetWindowSize) {
210  IsolateScope isolate_scope(isolate());
211 
212  const int64_t view_id =
214  engine_id(), regular_creation_request());
215  const HWND window_handle =
217  view_id);
218 
219  WindowSizeRequest requestedSize{
220 
221  .has_preferred_view_size = true,
222  .preferred_view_width = 640,
223  .preferred_view_height = 480,
224  };
226  &requestedSize);
227 
228  ActualWindowSize actual_size =
230  EXPECT_EQ(actual_size.width, 640);
231  EXPECT_EQ(actual_size.height, 480);
232 }
233 
234 TEST_F(WindowManagerTest, CanConstrainByMinimiumSize) {
235  IsolateScope isolate_scope(isolate());
236 
237  const int64_t view_id =
239  engine_id(), regular_creation_request());
240  const HWND window_handle =
242  view_id);
243  WindowConstraints constraints{.has_view_constraints = true,
244  .view_min_width = 900,
245  .view_min_height = 700,
246  .view_max_width = 10000,
247  .view_max_height = 10000};
249  &constraints);
250 
251  ActualWindowSize actual_size =
253  EXPECT_EQ(actual_size.width, 900);
254  EXPECT_EQ(actual_size.height, 700);
255 }
256 
257 TEST_F(WindowManagerTest, CanConstrainByMaximumSize) {
258  IsolateScope isolate_scope(isolate());
259 
260  const int64_t view_id =
262  engine_id(), regular_creation_request());
263  const HWND window_handle =
265  view_id);
266  WindowConstraints constraints{.has_view_constraints = true,
267  .view_min_width = 0,
268  .view_min_height = 0,
269  .view_max_width = 500,
270  .view_max_height = 500};
272  &constraints);
273 
274  ActualWindowSize actual_size =
276  EXPECT_EQ(actual_size.width, 500);
277  EXPECT_EQ(actual_size.height, 500);
278 }
279 
280 TEST_F(WindowManagerTest, CanFullscreenWindow) {
281  IsolateScope isolate_scope(isolate());
282 
283  const int64_t view_id =
285  engine_id(), regular_creation_request());
286  const HWND window_handle =
288  view_id);
289 
290  FullscreenRequest request{.fullscreen = true, .has_display_id = false};
292 
293  int screen_width = GetSystemMetrics(SM_CXSCREEN);
294  int screen_height = GetSystemMetrics(SM_CYSCREEN);
295  ActualWindowSize actual_size =
297  EXPECT_EQ(actual_size.width, screen_width);
298  EXPECT_EQ(actual_size.height, screen_height);
299  EXPECT_TRUE(
301 }
302 
303 TEST_F(WindowManagerTest, CanUnfullscreenWindow) {
304  IsolateScope isolate_scope(isolate());
305 
306  const int64_t view_id =
308  engine_id(), regular_creation_request());
309  const HWND window_handle =
311  view_id);
312 
313  FullscreenRequest request{.fullscreen = true, .has_display_id = false};
315 
316  request.fullscreen = false;
318 
319  ActualWindowSize actual_size =
321  EXPECT_EQ(actual_size.width, 800);
322  EXPECT_EQ(actual_size.height, 600);
323  EXPECT_FALSE(
325 }
326 
327 TEST_F(WindowManagerTest, CanSetWindowSizeWhileFullscreen) {
328  IsolateScope isolate_scope(isolate());
329 
330  const int64_t view_id =
332  engine_id(), regular_creation_request());
333  const HWND window_handle =
335  view_id);
336 
337  FullscreenRequest request{.fullscreen = true, .has_display_id = false};
339 
340  WindowSizeRequest requestedSize{
341 
342  .has_preferred_view_size = true,
343  .preferred_view_width = 500,
344  .preferred_view_height = 500,
345  };
347  &requestedSize);
348 
349  request.fullscreen = false;
351 
352  ActualWindowSize actual_size =
354  EXPECT_EQ(actual_size.width, 500);
355  EXPECT_EQ(actual_size.height, 500);
356 }
357 
358 TEST_F(WindowManagerTest, CanSetWindowConstraintsWhileFullscreen) {
359  IsolateScope isolate_scope(isolate());
360 
361  const int64_t view_id =
363  engine_id(), regular_creation_request());
364  const HWND window_handle =
366  view_id);
367 
368  FullscreenRequest request{.fullscreen = true, .has_display_id = false};
370 
371  WindowConstraints constraints{.has_view_constraints = true,
372  .view_min_width = 0,
373  .view_min_height = 0,
374  .view_max_width = 500,
375  .view_max_height = 500};
377  &constraints);
378 
379  request.fullscreen = false;
381 
382  ActualWindowSize actual_size =
384  EXPECT_EQ(actual_size.width, 500);
385  EXPECT_EQ(actual_size.height, 500);
386 }
387 
388 TEST_F(WindowManagerTest, CreateModelessDialogWindow) {
389  IsolateScope isolate_scope(isolate());
390  DialogWindowCreationRequest creation_request{
392  .preferred_view_width = 800,
393  .preferred_view_height = 600},
394  .preferred_constraints = {.has_view_constraints = false},
395  .title = L"Hello World",
396  .parent_or_null = nullptr};
397  const int64_t view_id =
399  engine_id(), &creation_request);
400  EXPECT_EQ(view_id, 1);
401 }
402 
403 TEST_F(WindowManagerTest, CreateModalDialogWindow) {
404  IsolateScope isolate_scope(isolate());
405 
406  const int64_t parent_view_id =
408  engine_id(), regular_creation_request());
409  const HWND parent_window_handle =
411  engine_id(), parent_view_id);
412 
413  DialogWindowCreationRequest creation_request{
414  .preferred_size =
415  {
416  .has_preferred_view_size = true,
417  .preferred_view_width = 800,
418  .preferred_view_height = 600,
419  },
420  .preferred_constraints = {.has_view_constraints = false},
421  .title = L"Hello World",
422  .parent_or_null = parent_window_handle};
423 
424  const int64_t view_id =
426  engine_id(), &creation_request);
427  EXPECT_EQ(view_id, 2);
428 
429  const HWND window_handle =
431  view_id);
432  HostWindow* host_window = HostWindow::GetThisFromHandle(window_handle);
433  EXPECT_EQ(host_window->GetOwnerWindow()->GetWindowHandle(),
434  parent_window_handle);
435 }
436 
437 TEST_F(WindowManagerTest, DeactivatedRegularWindowDoesNotReactivateItself) {
438  IsolateScope isolate_scope(isolate());
439 
440  // Two independent top-level regular windows (regular windows have no Win32
441  // owner). |window_handle| is deactivated while |active_window_handle| holds
442  // activation.
443  const int64_t window_view_id =
445  engine_id(), regular_creation_request());
446  const HWND window_handle =
448  engine_id(), window_view_id);
449 
450  const int64_t active_view_id =
452  engine_id(), regular_creation_request());
453  const HWND active_window_handle =
455  engine_id(), active_view_id);
456 
457  SetActiveWindow(active_window_handle);
458  ASSERT_EQ(GetActiveWindow(), active_window_handle);
459 
460  // A window receiving WM_ACTIVATE with WA_INACTIVE must not focus its own
461  // content: SetFocus activates the parent of the focused window (the window
462  // itself), which would reactivate the deactivated window, pull it back to the
463  // top of the z-order, and take activation away from the active window.
464  SendMessage(window_handle, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), 0);
465 
466  EXPECT_EQ(GetActiveWindow(), active_window_handle);
467 }
468 
469 TEST_F(WindowManagerTest, DialogCanNeverBeFullscreen) {
470  IsolateScope isolate_scope(isolate());
471 
472  DialogWindowCreationRequest creation_request{
474  .preferred_view_width = 800,
475  .preferred_view_height = 600},
476  .preferred_constraints = {.has_view_constraints = false},
477  .title = L"Hello World",
478  .parent_or_null = nullptr};
479 
480  const int64_t view_id =
482  engine_id(), &creation_request);
483  const HWND window_handle =
485  view_id);
486 
487  FullscreenRequest request{.fullscreen = true, .has_display_id = false};
489  EXPECT_FALSE(
491 }
492 
493 TEST_F(WindowManagerTest, CreateTooltipWindow) {
494  IsolateScope isolate_scope(isolate());
495 
496  const int64_t parent_view_id =
498  engine_id(), regular_creation_request());
499  const HWND parent_window_handle =
501  engine_id(), parent_view_id);
502 
503  auto position_callback =
504  [](const WindowSize& child_size, const WindowRect& parent_rect,
505  const WindowRect& display_rect, WindowRect& rect) {
506  rect.left = parent_rect.left + 10;
507  rect.top = parent_rect.top + 10;
508  rect.width = child_size.width;
509  rect.height = child_size.height;
510  };
511 
512  TooltipWindowCreationRequest creation_request{
514  .view_min_width = 100,
515  .view_min_height = 50,
516  .view_max_width = 300,
517  .view_max_height = 200},
518  .parent = parent_window_handle,
519  .get_position_callback = position_callback};
520 
521  const int64_t tooltip_view_id =
523  engine_id(), &creation_request);
524 
525  EXPECT_NE(tooltip_view_id, -1);
526  HWND tooltip_window_handle =
528  engine_id(), tooltip_view_id);
529  EXPECT_NE(tooltip_window_handle, nullptr);
530 }
531 
532 TEST_F(WindowManagerTest, TooltipWindowHasNoActivateStyle) {
533  IsolateScope isolate_scope(isolate());
534 
535  const int64_t parent_view_id =
537  engine_id(), regular_creation_request());
538  const HWND parent_window_handle =
540  engine_id(), parent_view_id);
541 
542  auto position_callback =
543  [](const WindowSize& child_size, const WindowRect& parent_rect,
544  const WindowRect& display_rect, WindowRect& rect) {
545  rect.left = parent_rect.left + 10;
546  rect.top = parent_rect.top + 10;
547  rect.width = child_size.width;
548  rect.height = child_size.height;
549  };
550 
551  TooltipWindowCreationRequest creation_request{
553  .view_min_width = 100,
554  .view_min_height = 50,
555  .view_max_width = 300,
556  .view_max_height = 200},
557  .parent = parent_window_handle,
558  .get_position_callback = position_callback};
559 
560  const int64_t tooltip_view_id =
562  engine_id(), &creation_request);
563 
564  HWND tooltip_window_handle =
566  engine_id(), tooltip_view_id);
567 
568  DWORD ex_style = GetWindowLong(tooltip_window_handle, GWL_EXSTYLE);
569  EXPECT_TRUE(ex_style & WS_EX_NOACTIVATE);
570 }
571 
572 TEST_F(WindowManagerTest, TooltipWindowDoesNotStealFocus) {
573  IsolateScope isolate_scope(isolate());
574 
575  const int64_t parent_view_id =
577  engine_id(), regular_creation_request());
578  const HWND parent_window_handle =
580  engine_id(), parent_view_id);
581 
582  // Give focus to the parent window
583  SetFocus(parent_window_handle);
584  HWND focused_before = GetFocus();
585 
586  auto position_callback =
587  [](const WindowSize& child_size, const WindowRect& parent_rect,
588  const WindowRect& display_rect, WindowRect& rect) {
589  rect.left = parent_rect.left + 10;
590  rect.top = parent_rect.top + 10;
591  rect.width = child_size.width;
592  rect.height = child_size.height;
593  };
594 
595  TooltipWindowCreationRequest creation_request{
597  .view_min_width = 100,
598  .view_min_height = 50,
599  .view_max_width = 300,
600  .view_max_height = 200},
601  .parent = parent_window_handle,
602  .get_position_callback = position_callback};
603 
604  const int64_t tooltip_view_id =
606  engine_id(), &creation_request);
607 
608  HWND tooltip_window_handle =
610  engine_id(), tooltip_view_id);
611 
612  // Verify focus remains with the parent window
613  HWND focused_after = GetFocus();
614  EXPECT_EQ(focused_before, focused_after);
615  EXPECT_NE(focused_after, tooltip_window_handle);
616 }
617 
618 TEST_F(WindowManagerTest, TooltipWindowReturnsNoActivateOnMouseClick) {
619  IsolateScope isolate_scope(isolate());
620 
621  const int64_t parent_view_id =
623  engine_id(), regular_creation_request());
624  const HWND parent_window_handle =
626  engine_id(), parent_view_id);
627 
628  auto position_callback =
629  [](const WindowSize& child_size, const WindowRect& parent_rect,
630  const WindowRect& display_rect, WindowRect& rect) {
631  rect.left = parent_rect.left + 10;
632  rect.top = parent_rect.top + 10;
633  rect.width = child_size.width;
634  rect.height = child_size.height;
635  };
636 
637  TooltipWindowCreationRequest creation_request{
639  .view_min_width = 100,
640  .view_min_height = 50,
641  .view_max_width = 300,
642  .view_max_height = 200},
643  .parent = parent_window_handle,
644  .get_position_callback = position_callback};
645 
646  const int64_t tooltip_view_id =
648  engine_id(), &creation_request);
649 
650  HWND tooltip_window_handle =
652  engine_id(), tooltip_view_id);
653 
654  // Send WM_MOUSEACTIVATE message to the tooltip window
655  LRESULT result = SendMessage(tooltip_window_handle, WM_MOUSEACTIVATE,
656  reinterpret_cast<WPARAM>(parent_window_handle),
657  MAKELPARAM(HTCLIENT, WM_LBUTTONDOWN));
658 
659  // Verify the tooltip returns MA_NOACTIVATE
660  EXPECT_EQ(result, MA_NOACTIVATE);
661 }
662 
663 // TODO(team-windows): Fix flakes. See:
664 // https://github.com/flutter/flutter/issues/177172
665 TEST_F(WindowManagerTest,
666  DISABLED_TooltipWindowUpdatesPositionOnViewSizeChange) {
667  IsolateScope isolate_scope(isolate());
668 
669  const int64_t parent_view_id =
671  engine_id(), regular_creation_request());
672  const HWND parent_window_handle =
674  engine_id(), parent_view_id);
675 
676  // Track the child size passed to the callback
677  static int callback_count = 0;
678  static int last_width = 0;
679  static int last_height = 0;
680 
681  auto position_callback =
682  [](const WindowSize& child_size, const WindowRect& parent_rect,
683  const WindowRect& display_rect, WindowRect& rect) {
684  callback_count++;
685  last_width = child_size.width;
686  last_height = child_size.height;
687 
688  rect.left = parent_rect.left + callback_count * 5;
689  rect.top = parent_rect.top + callback_count * 5;
690  rect.width = child_size.width;
691  rect.height = child_size.height;
692  };
693 
694  TooltipWindowCreationRequest creation_request{
696  .view_min_width = 100,
697  .view_min_height = 50,
698  .view_max_width = 300,
699  .view_max_height = 200},
700  .parent = parent_window_handle,
701  .get_position_callback = position_callback};
702 
703  // Reset callback tracking
704  callback_count = 0;
705  last_width = 0;
706  last_height = 0;
707 
708  const int64_t tooltip_view_id =
710  engine_id(), &creation_request);
711 
712  HWND tooltip_window_handle =
714  engine_id(), tooltip_view_id);
715 
716  // Get the view associated with the tooltip window
717  FlutterWindowsView* view =
718  engine()->GetViewFromTopLevelWindow(tooltip_window_handle);
719  ASSERT_NE(view, nullptr);
720 
721  // Get initial position
722  RECT initial_rect;
723  GetWindowRect(tooltip_window_handle, &initial_rect);
724  int initial_callback_count = callback_count;
725 
726  // Simulate a frame being generated with new dimensions
727  // This should trigger DidUpdateViewSize which calls UpdatePosition
728  view->OnFrameGenerated(150, 100);
729 
730  // Process any pending tasks to ensure the callback is executed
731  engine()->task_runner()->ProcessTasks();
732 
733  // Verify the callback was called again with the new dimensions
734  EXPECT_GT(callback_count, initial_callback_count);
735  EXPECT_EQ(last_width, 150);
736  EXPECT_EQ(last_height, 100);
737 
738  // Get new position and verify it changed
739  RECT new_rect;
740  GetWindowRect(tooltip_window_handle, &new_rect);
741 
742  // The position should have changed due to our callback logic
743  // (we offset by callback_count * 5)
744  EXPECT_NE(initial_rect.left, new_rect.left);
745  EXPECT_NE(initial_rect.top, new_rect.top);
746 }
747 
748 TEST_F(WindowManagerTest, CreatePopupWindow) {
749  IsolateScope isolate_scope(isolate());
750 
751  const int64_t parent_view_id =
753  engine_id(), regular_creation_request());
754  const HWND parent_window_handle =
756  engine_id(), parent_view_id);
757 
758  auto position_callback =
759  [](const WindowSize& child_size, const WindowRect& parent_rect,
760  const WindowRect& display_rect, WindowRect& rect) {
761  rect.left = parent_rect.left + 10;
762  rect.top = parent_rect.top + 10;
763  rect.width = child_size.width;
764  rect.height = child_size.height;
765  };
766 
767  PopupWindowCreationRequest creation_request{
769  .view_min_width = 100,
770  .view_min_height = 50,
771  .view_max_width = 300,
772  .view_max_height = 200},
773  .parent = parent_window_handle,
774  .get_position_callback = position_callback};
775 
776  const int64_t popup_view_id =
778  &creation_request);
779 
780  EXPECT_NE(popup_view_id, -1);
781  HWND popup_window_handle =
783  engine_id(), popup_view_id);
784  EXPECT_NE(popup_window_handle, nullptr);
785 }
786 
787 TEST_F(WindowManagerTest, PopupWindowHasNoActivateStyle) {
788  IsolateScope isolate_scope(isolate());
789 
790  const int64_t parent_view_id =
792  engine_id(), regular_creation_request());
793  const HWND parent_window_handle =
795  engine_id(), parent_view_id);
796 
797  auto position_callback =
798  [](const WindowSize& child_size, const WindowRect& parent_rect,
799  const WindowRect& display_rect, WindowRect& rect) {
800  rect.left = parent_rect.left + 10;
801  rect.top = parent_rect.top + 10;
802  rect.width = child_size.width;
803  rect.height = child_size.height;
804  };
805 
806  PopupWindowCreationRequest creation_request{
808  .view_min_width = 100,
809  .view_min_height = 50,
810  .view_max_width = 300,
811  .view_max_height = 200},
812  .parent = parent_window_handle,
813  .get_position_callback = position_callback};
814 
815  const int64_t popup_view_id =
817  &creation_request);
818 
819  HWND popup_window_handle =
821  engine_id(), popup_view_id);
822 
823  DWORD ex_style = GetWindowLong(popup_window_handle, GWL_EXSTYLE);
824  EXPECT_TRUE(ex_style & WS_EX_NOACTIVATE);
825 }
826 
827 TEST_F(WindowManagerTest, PopupWindowDoesNotStealFocus) {
828  IsolateScope isolate_scope(isolate());
829 
830  const int64_t parent_view_id =
832  engine_id(), regular_creation_request());
833  const HWND parent_window_handle =
835  engine_id(), parent_view_id);
836 
837  // Give focus to the parent window
838  SetFocus(parent_window_handle);
839  HWND focused_before = GetFocus();
840 
841  auto position_callback =
842  [](const WindowSize& child_size, const WindowRect& parent_rect,
843  const WindowRect& display_rect, WindowRect& rect) {
844  rect.left = parent_rect.left + 10;
845  rect.top = parent_rect.top + 10;
846  rect.width = child_size.width;
847  rect.height = child_size.height;
848  };
849 
850  PopupWindowCreationRequest creation_request{
852  .view_min_width = 100,
853  .view_min_height = 50,
854  .view_max_width = 300,
855  .view_max_height = 200},
856  .parent = parent_window_handle,
857  .get_position_callback = position_callback};
858 
859  const int64_t popup_view_id =
861  &creation_request);
862 
863  HWND popup_window_handle =
865  engine_id(), popup_view_id);
866 
867  // Verify focus remains with the parent window
868  HWND focused_after = GetFocus();
869  EXPECT_EQ(focused_before, focused_after);
870  EXPECT_NE(focused_after, popup_window_handle);
871 }
872 
873 // TODO(team-windows): Fix flakes. See:
874 // https://github.com/flutter/flutter/issues/177172
875 TEST_F(WindowManagerTest, DISABLED_PopupWindowUpdatesPositionOnViewSizeChange) {
876  IsolateScope isolate_scope(isolate());
877 
878  const int64_t parent_view_id =
880  engine_id(), regular_creation_request());
881  const HWND parent_window_handle =
883  engine_id(), parent_view_id);
884 
885  // Track the child size passed to the callback
886  static int callback_count = 0;
887  static int last_width = 0;
888  static int last_height = 0;
889 
890  auto position_callback =
891  [](const WindowSize& child_size, const WindowRect& parent_rect,
892  const WindowRect& display_rect, WindowRect& rect) {
893  callback_count++;
894  last_width = child_size.width;
895  last_height = child_size.height;
896 
897  rect.left = parent_rect.left + callback_count * 5;
898  rect.top = parent_rect.top + callback_count * 5;
899  rect.width = child_size.width;
900  rect.height = child_size.height;
901  };
902 
903  PopupWindowCreationRequest creation_request{
905  .view_min_width = 100,
906  .view_min_height = 50,
907  .view_max_width = 300,
908  .view_max_height = 200},
909  .parent = parent_window_handle,
910  .get_position_callback = position_callback};
911 
912  // Reset callback tracking
913  callback_count = 0;
914  last_width = 0;
915  last_height = 0;
916 
917  const int64_t popup_view_id =
919  &creation_request);
920 
921  HWND popup_window_handle =
923  engine_id(), popup_view_id);
924 
925  // Get the view associated with the popup window
926  FlutterWindowsView* view =
927  engine()->GetViewFromTopLevelWindow(popup_window_handle);
928  ASSERT_NE(view, nullptr);
929 
930  // Get initial position
931  RECT initial_rect;
932  GetWindowRect(popup_window_handle, &initial_rect);
933  int initial_callback_count = callback_count;
934 
935  // Simulate a frame being generated with new dimensions
936  // This should trigger DidUpdateViewSize which calls UpdatePosition
937  view->OnFrameGenerated(150, 100);
938 
939  // Process any pending tasks to ensure the callback is executed
940  engine()->task_runner()->ProcessTasks();
941 
942  // Verify the callback was called again with the new dimensions
943  EXPECT_GT(callback_count, initial_callback_count);
944  EXPECT_EQ(last_width, 150);
945  EXPECT_EQ(last_height, 100);
946 
947  // Get new position and verify it changed
948  RECT new_rect;
949  GetWindowRect(popup_window_handle, &new_rect);
950 
951  // The position should have changed due to our callback logic
952  // (we offset by callback_count * 5)
953  EXPECT_NE(initial_rect.left, new_rect.left);
954  EXPECT_NE(initial_rect.top, new_rect.top);
955 }
956 
957 TEST_F(WindowManagerTest, CreateRegularWindowSizedToContent) {
958  IsolateScope isolate_scope(isolate());
959 
960  RegularWindowCreationRequest creation_request{
962  .preferred_constraints = {.has_view_constraints = false},
963  .title = L"Sized To Content",
964  .sized_to_content = true,
965  .resizable = false};
966 
967  const int64_t view_id =
969  engine_id(), &creation_request);
970  EXPECT_GE(view_id, 1);
971 }
972 
973 // TODO(team-windows): Fix flakes. See:
974 // https://github.com/flutter/flutter/issues/177172
975 TEST_F(WindowManagerTest,
976  DISABLED_RegularWindowSizedToContentResizesToContent) {
977  IsolateScope isolate_scope(isolate());
978 
979  RegularWindowCreationRequest creation_request{
981  .preferred_constraints = {.has_view_constraints = false},
982  .title = L"Sized To Content",
983  .sized_to_content = true,
984  .resizable = false};
985 
986  const int64_t view_id =
988  engine_id(), &creation_request);
989  const HWND window_handle =
991  view_id);
992 
993  FlutterWindowsView* view = engine()->GetViewFromTopLevelWindow(window_handle);
994  ASSERT_NE(view, nullptr);
995 
996  view->OnFrameGenerated(300, 200);
997  engine()->task_runner()->ProcessTasks();
998 
999  ActualWindowSize size =
1001  EXPECT_EQ(size.width, 300);
1002  EXPECT_EQ(size.height, 200);
1003 }
1004 
1005 TEST_F(WindowManagerTest,
1006  RegularWindowSizedToContentNonResizableHasNoThickFrame) {
1007  IsolateScope isolate_scope(isolate());
1008 
1009  RegularWindowCreationRequest creation_request{
1011  .preferred_constraints = {.has_view_constraints = false},
1012  .title = L"Sized To Content Non-Resizable",
1013  .sized_to_content = true,
1014  .resizable = false};
1015 
1016  const int64_t view_id =
1018  engine_id(), &creation_request);
1019  const HWND window_handle =
1021  view_id);
1022 
1023  const LONG style = GetWindowLong(window_handle, GWL_STYLE);
1024  EXPECT_EQ(style & WS_THICKFRAME, 0L);
1025  EXPECT_EQ(style & WS_MAXIMIZEBOX, 0L);
1026 }
1027 
1028 TEST_F(WindowManagerTest, RegularWindowSizedToContentResizableHasThickFrame) {
1029  IsolateScope isolate_scope(isolate());
1030 
1031  RegularWindowCreationRequest creation_request{
1033  .preferred_constraints = {.has_view_constraints = false},
1034  .title = L"Sized To Content Resizable",
1035  .sized_to_content = true,
1036  .resizable = true};
1037 
1038  const int64_t view_id =
1040  engine_id(), &creation_request);
1041  const HWND window_handle =
1043  view_id);
1044 
1045  const LONG style = GetWindowLong(window_handle, GWL_STYLE);
1046  EXPECT_NE(style & WS_THICKFRAME, 0L);
1047  EXPECT_NE(style & WS_MAXIMIZEBOX, 0L);
1048 }
1049 
1050 // TODO(team-windows): Fix flakes. See:
1051 // https://github.com/flutter/flutter/issues/177172
1053  WindowManagerTest,
1054  DISABLED_RegularWindowSizedToContentResizableStopsTrackingAfterFirstFrame) {
1055  IsolateScope isolate_scope(isolate());
1056 
1057  RegularWindowCreationRequest creation_request{
1059  .preferred_constraints = {.has_view_constraints = false},
1060  .title = L"Sized To Content Resizable",
1061  .sized_to_content = true,
1062  .resizable = true};
1063 
1064  const int64_t view_id =
1066  engine_id(), &creation_request);
1067  const HWND window_handle =
1069  view_id);
1070 
1071  FlutterWindowsView* view = engine()->GetViewFromTopLevelWindow(window_handle);
1072  ASSERT_NE(view, nullptr);
1073 
1074  view->OnFrameGenerated(300, 200);
1075  engine()->task_runner()->ProcessTasks();
1076 
1077  EXPECT_FALSE(view->IsSizedToContent());
1078 }
1079 
1080 TEST_F(WindowManagerTest, CreateModelessDialogSizedToContent) {
1081  IsolateScope isolate_scope(isolate());
1082 
1083  DialogWindowCreationRequest creation_request{
1085  .preferred_constraints = {.has_view_constraints = false},
1086  .title = L"Modeless Dialog Sized To Content",
1087  .parent_or_null = nullptr,
1088  .sized_to_content = true,
1089  .resizable = false};
1090 
1091  const int64_t view_id =
1093  engine_id(), &creation_request);
1094  EXPECT_GE(view_id, 0);
1095 }
1096 
1097 TEST_F(WindowManagerTest, CreateModalDialogSizedToContent) {
1098  IsolateScope isolate_scope(isolate());
1099 
1100  const int64_t parent_view_id =
1102  engine_id(), regular_creation_request());
1103  const HWND parent_window_handle =
1105  engine_id(), parent_view_id);
1106 
1107  DialogWindowCreationRequest creation_request{
1109  .preferred_constraints = {.has_view_constraints = false},
1110  .title = L"Modal Dialog Sized To Content",
1111  .parent_or_null = parent_window_handle,
1112  .sized_to_content = true,
1113  .resizable = false};
1114 
1115  const int64_t view_id =
1117  engine_id(), &creation_request);
1118  EXPECT_GE(view_id, 0);
1119 }
1120 
1121 // TODO(team-windows): Fix flakes. See:
1122 // https://github.com/flutter/flutter/issues/177172
1123 TEST_F(WindowManagerTest, DISABLED_DialogWindowSizedToContentResizesToContent) {
1124  IsolateScope isolate_scope(isolate());
1125 
1126  DialogWindowCreationRequest creation_request{
1128  .preferred_constraints = {.has_view_constraints = false},
1129  .title = L"Dialog Sized To Content",
1130  .parent_or_null = nullptr,
1131  .sized_to_content = true,
1132  .resizable = false};
1133 
1134  const int64_t view_id =
1136  engine_id(), &creation_request);
1137  const HWND window_handle =
1139  view_id);
1140 
1141  FlutterWindowsView* view = engine()->GetViewFromTopLevelWindow(window_handle);
1142  ASSERT_NE(view, nullptr);
1143 
1144  view->OnFrameGenerated(300, 200);
1145  engine()->task_runner()->ProcessTasks();
1146 
1147  ActualWindowSize size =
1149  EXPECT_EQ(size.width, 300);
1150  EXPECT_EQ(size.height, 200);
1151 }
1152 
1153 TEST_F(WindowManagerTest,
1154  DialogWindowSizedToContentNonResizableHasNoThickFrame) {
1155  IsolateScope isolate_scope(isolate());
1156 
1157  DialogWindowCreationRequest creation_request{
1159  .preferred_constraints = {.has_view_constraints = false},
1160  .title = L"Dialog Sized To Content Non-Resizable",
1161  .parent_or_null = nullptr,
1162  .sized_to_content = true,
1163  .resizable = false};
1164 
1165  const int64_t view_id =
1167  engine_id(), &creation_request);
1168  const HWND window_handle =
1170  view_id);
1171 
1172  const LONG style = GetWindowLong(window_handle, GWL_STYLE);
1173  EXPECT_EQ(style & WS_THICKFRAME, 0L);
1174 }
1175 
1176 TEST_F(WindowManagerTest,
1177  OnPreEngineRestartDestroysWindowsWithoutDispatchingMessages) {
1178  IsolateScope isolate_scope(isolate());
1179 
1180  static bool received_message = false;
1181  WindowingInitRequest init_request{
1182  .on_message = [](WindowsMessage* message) { received_message = true; }};
1183  InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
1184 
1185  const int64_t first_view_id =
1187  engine_id(), regular_creation_request());
1188  const HWND first_window_handle =
1190  engine_id(), first_view_id);
1191  const int64_t second_view_id =
1193  engine_id(), regular_creation_request());
1194  const HWND second_window_handle =
1196  engine_id(), second_view_id);
1197 
1198  received_message = false;
1199  EngineModifier{engine()}.Restart();
1200 
1201  EXPECT_FALSE(received_message);
1202  EXPECT_FALSE(IsWindow(first_window_handle));
1203  EXPECT_FALSE(IsWindow(second_window_handle));
1204 }
1205 
1206 // Verifies that |OnEngineShutdown| destroys popup windows BEFORE clearing
1207 // |on_message_|, so the WM_DESTROY round-trip reaches the isolate. Without
1208 // this, |PopupWindowControllerWin32._destroyed| would never be set during
1209 // engine shutdown and queued FFI calls (e.g. updatePosition) would
1210 // dereference stale HostWindow pointers.
1211 TEST_F(WindowManagerTest, OnEngineShutdownDispatchesWmDestroyForPopupWindow) {
1212  IsolateScope isolate_scope(isolate());
1213 
1214  static std::vector<UINT> received_messages;
1215  received_messages.clear();
1216  WindowingInitRequest init_request{.on_message = [](WindowsMessage* message) {
1217  received_messages.push_back(message->message);
1218  }};
1219  InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
1220 
1221  const int64_t parent_view_id =
1223  engine_id(), regular_creation_request());
1224  const HWND parent_window_handle =
1226  engine_id(), parent_view_id);
1227 
1228  auto position_callback =
1229  [](const WindowSize& child_size, const WindowRect& parent_rect,
1230  const WindowRect& display_rect, WindowRect& rect) {
1231  rect.left = parent_rect.left + 10;
1232  rect.top = parent_rect.top + 10;
1233  rect.width = child_size.width;
1234  rect.height = child_size.height;
1235  };
1236 
1237  PopupWindowCreationRequest creation_request{
1239  .view_min_width = 100,
1240  .view_min_height = 50,
1241  .view_max_width = 300,
1242  .view_max_height = 200},
1243  .parent = parent_window_handle,
1244  .get_position_callback = position_callback};
1245 
1247  &creation_request);
1248 
1249  received_messages.clear();
1250  engine()->window_manager()->OnEngineShutdown();
1251 
1252  EXPECT_NE(std::find(received_messages.begin(), received_messages.end(),
1253  static_cast<UINT>(WM_DESTROY)),
1254  received_messages.end());
1255 }
1256 
1257 // Same as above for tooltips.
1258 TEST_F(WindowManagerTest, OnEngineShutdownDispatchesWmDestroyForTooltipWindow) {
1259  IsolateScope isolate_scope(isolate());
1260 
1261  static std::vector<UINT> received_messages;
1262  received_messages.clear();
1263  WindowingInitRequest init_request{.on_message = [](WindowsMessage* message) {
1264  received_messages.push_back(message->message);
1265  }};
1266  InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
1267 
1268  const int64_t parent_view_id =
1270  engine_id(), regular_creation_request());
1271  const HWND parent_window_handle =
1273  engine_id(), parent_view_id);
1274 
1275  auto position_callback =
1276  [](const WindowSize& child_size, const WindowRect& parent_rect,
1277  const WindowRect& display_rect, WindowRect& rect) {
1278  rect.left = parent_rect.left + 10;
1279  rect.top = parent_rect.top + 10;
1280  rect.width = child_size.width;
1281  rect.height = child_size.height;
1282  };
1283 
1284  TooltipWindowCreationRequest creation_request{
1286  .view_min_width = 100,
1287  .view_min_height = 50,
1288  .view_max_width = 300,
1289  .view_max_height = 200},
1290  .parent = parent_window_handle,
1291  .get_position_callback = position_callback};
1292 
1294  &creation_request);
1295 
1296  received_messages.clear();
1297  engine()->window_manager()->OnEngineShutdown();
1298 
1299  EXPECT_NE(std::find(received_messages.begin(), received_messages.end(),
1300  static_cast<UINT>(WM_DESTROY)),
1301  received_messages.end());
1302 }
1303 
1304 TEST_F(WindowManagerTest, CreateSatelliteWindow) {
1305  IsolateScope isolate_scope(isolate());
1306 
1307  const HWND parent_window_handle = CreateParentWindow();
1308  SatelliteWindowCreationRequest creation_request =
1309  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1310 
1311  const int64_t satellite_view_id =
1313  engine_id(), &creation_request);
1314 
1315  EXPECT_GE(satellite_view_id, 0);
1316  const HWND satellite_window_handle =
1318  engine_id(), satellite_view_id);
1319  EXPECT_NE(satellite_window_handle, nullptr);
1320 }
1321 
1322 TEST_F(WindowManagerTest, SatelliteWindowUsesPositionCallbackAfterFirstFrame) {
1323  IsolateScope isolate_scope(isolate());
1324 
1325  const HWND parent_window_handle = CreateParentWindow();
1326  SetWindowPos(parent_window_handle, nullptr, 200, 100, 0, 0,
1327  SWP_NOSIZE | SWP_NOZORDER);
1328 
1329  SatelliteWindowCreationRequest creation_request =
1330  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1331 
1332  const int64_t satellite_view_id =
1334  engine_id(), &creation_request);
1335  const HWND satellite_window_handle =
1337  engine_id(), satellite_view_id);
1338 
1339  FlutterWindowsView* view =
1340  engine()->GetViewFromTopLevelWindow(satellite_window_handle);
1341  ASSERT_NE(view, nullptr);
1342 
1343  // The positioner runs once the satellite has rendered its first frame, while
1344  // the window is still hidden.
1345  view->OnFramePresented();
1346  engine()->task_runner()->ProcessTasks();
1347 
1348  // |OffsetPositionCallback| places the satellite at the parent's client-area
1349  // origin, offset by (20, 30).
1350  RECT parent_client_rect;
1351  GetClientRect(parent_window_handle, &parent_client_rect);
1352  POINT parent_top_left = {parent_client_rect.left, parent_client_rect.top};
1353  ClientToScreen(parent_window_handle, &parent_top_left);
1354 
1355  // The positioner's origin applies to the window rectangle.
1356  RECT satellite_rect;
1357  GetWindowRect(satellite_window_handle, &satellite_rect);
1358 
1359  EXPECT_EQ(satellite_rect.left, parent_top_left.x + 20);
1360  EXPECT_EQ(satellite_rect.top, parent_top_left.y + 30);
1361 }
1362 
1363 TEST_F(WindowManagerTest, SatelliteWindowIsHiddenUntilFirstFrame) {
1364  IsolateScope isolate_scope(isolate());
1365 
1366  const HWND parent_window_handle = CreateParentWindow();
1367  SatelliteWindowCreationRequest creation_request =
1368  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1369 
1370  const HWND satellite_window_handle =
1372  engine_id(),
1374  engine_id(), &creation_request));
1375  ASSERT_NE(satellite_window_handle, nullptr);
1376 
1377  // The satellite is placed by the positioner before it is shown, so that the
1378  // move is never visible to the user.
1379  EXPECT_FALSE(IsWindowVisible(satellite_window_handle));
1380 
1381  FlutterWindowsView* view =
1382  engine()->GetViewFromTopLevelWindow(satellite_window_handle);
1383  ASSERT_NE(view, nullptr);
1384  view->OnFramePresented();
1385  engine()->task_runner()->ProcessTasks();
1386 
1387  EXPECT_TRUE(IsWindowVisible(satellite_window_handle));
1388 }
1389 
1390 TEST_F(WindowManagerTest, SatelliteWindowHandlesNullPositionCallback) {
1391  IsolateScope isolate_scope(isolate());
1392 
1393  const HWND parent_window_handle = CreateParentWindow();
1394  SatelliteWindowCreationRequest creation_request =
1395  SatelliteCreationRequest(parent_window_handle, nullptr);
1396 
1397  const int64_t satellite_view_id =
1399  engine_id(), &creation_request);
1400 
1401  EXPECT_GE(satellite_view_id, 0);
1403  engine_id(), satellite_view_id),
1404  nullptr);
1405 }
1406 
1407 TEST_F(WindowManagerTest, SatelliteWindowFollowsParentMovement) {
1408  IsolateScope isolate_scope(isolate());
1409 
1410  WindowingInitRequest init_request{
1411  .on_message = [](WindowsMessage* message) {}};
1412  InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
1413 
1414  const HWND parent_window_handle = CreateParentWindow();
1415  SetWindowPos(parent_window_handle, nullptr, 200, 100, 0, 0,
1416  SWP_NOSIZE | SWP_NOZORDER);
1417 
1418  SatelliteWindowCreationRequest creation_request =
1419  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1420  const HWND satellite_window_handle =
1422  engine_id(),
1424  engine_id(), &creation_request));
1425 
1426  RECT satellite_rect_before;
1427  GetWindowRect(satellite_window_handle, &satellite_rect_before);
1428 
1429  // Move the parent by (+50, +100). SetWindowPos dispatches
1430  // WM_WINDOWPOSCHANGED synchronously, so the satellite moves before it
1431  // returns.
1432  SetWindowPos(parent_window_handle, nullptr, 250, 200, 0, 0,
1433  SWP_NOSIZE | SWP_NOZORDER);
1434 
1435  RECT satellite_rect_after;
1436  GetWindowRect(satellite_window_handle, &satellite_rect_after);
1437 
1438  EXPECT_EQ(satellite_rect_after.left, satellite_rect_before.left + 50);
1439  EXPECT_EQ(satellite_rect_after.top, satellite_rect_before.top + 100);
1440 }
1441 
1442 TEST_F(WindowManagerTest, SatelliteWindowDoesNotMoveWhenParentOnlyResizes) {
1443  IsolateScope isolate_scope(isolate());
1444 
1445  WindowingInitRequest init_request{
1446  .on_message = [](WindowsMessage* message) {}};
1447  InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
1448 
1449  const HWND parent_window_handle = CreateParentWindow();
1450  SetWindowPos(parent_window_handle, nullptr, 200, 100, 0, 0,
1451  SWP_NOSIZE | SWP_NOZORDER);
1452 
1453  SatelliteWindowCreationRequest creation_request =
1454  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1455  const HWND satellite_window_handle =
1457  engine_id(),
1459  engine_id(), &creation_request));
1460 
1461  RECT satellite_rect_before;
1462  GetWindowRect(satellite_window_handle, &satellite_rect_before);
1463 
1464  SetWindowPos(parent_window_handle, nullptr, 0, 0, 640, 480,
1465  SWP_NOMOVE | SWP_NOZORDER);
1466 
1467  RECT satellite_rect_after;
1468  GetWindowRect(satellite_window_handle, &satellite_rect_after);
1469 
1470  EXPECT_EQ(satellite_rect_after.left, satellite_rect_before.left);
1471  EXPECT_EQ(satellite_rect_after.top, satellite_rect_before.top);
1472 }
1473 
1474 TEST_F(WindowManagerTest, SatelliteWindowKeepsOffsetAfterBeingMoved) {
1475  IsolateScope isolate_scope(isolate());
1476 
1477  WindowingInitRequest init_request{
1478  .on_message = [](WindowsMessage* message) {}};
1479  InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
1480 
1481  const HWND parent_window_handle = CreateParentWindow();
1482  SetWindowPos(parent_window_handle, nullptr, 200, 100, 0, 0,
1483  SWP_NOSIZE | SWP_NOZORDER);
1484 
1485  SatelliteWindowCreationRequest creation_request =
1486  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1487  const HWND satellite_window_handle =
1489  engine_id(),
1491  engine_id(), &creation_request));
1492 
1493  // Move the satellite itself, as the user would by dragging it.
1494  RECT satellite_rect;
1495  GetWindowRect(satellite_window_handle, &satellite_rect);
1496  SetWindowPos(satellite_window_handle, nullptr, satellite_rect.left + 40,
1497  satellite_rect.top + 60, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
1498 
1499  RECT satellite_rect_before;
1500  GetWindowRect(satellite_window_handle, &satellite_rect_before);
1501  ASSERT_EQ(satellite_rect_before.left, satellite_rect.left + 40);
1502  ASSERT_EQ(satellite_rect_before.top, satellite_rect.top + 60);
1503 
1504  // The satellite keeps its new offset from the parent.
1505  SetWindowPos(parent_window_handle, nullptr, 250, 200, 0, 0,
1506  SWP_NOSIZE | SWP_NOZORDER);
1507 
1508  RECT satellite_rect_after;
1509  GetWindowRect(satellite_window_handle, &satellite_rect_after);
1510  EXPECT_EQ(satellite_rect_after.left, satellite_rect_before.left + 50);
1511  EXPECT_EQ(satellite_rect_after.top, satellite_rect_before.top + 100);
1512 }
1513 
1514 TEST_F(WindowManagerTest, SatelliteWindowFollowsParentClientArea) {
1515  IsolateScope isolate_scope(isolate());
1516 
1517  WindowingInitRequest init_request{
1518  .on_message = [](WindowsMessage* message) {}};
1519  InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
1520 
1521  const HWND parent_window_handle = CreateParentWindow();
1522  SetWindowPos(parent_window_handle, nullptr, 200, 100, 0, 0,
1523  SWP_NOSIZE | SWP_NOZORDER);
1524 
1525  SatelliteWindowCreationRequest creation_request =
1526  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1527  const HWND satellite_window_handle =
1529  engine_id(),
1531  engine_id(), &creation_request));
1532 
1533  POINT parent_client_origin_before = {0, 0};
1534  ClientToScreen(parent_window_handle, &parent_client_origin_before);
1535  RECT satellite_rect_before;
1536  GetWindowRect(satellite_window_handle, &satellite_rect_before);
1537 
1538  // Removing the parent's caption moves its client area without moving the
1539  // window, much like a DPI change that rescales the title bar.
1540  SetWindowLongPtr(
1541  parent_window_handle, GWL_STYLE,
1542  GetWindowLongPtr(parent_window_handle, GWL_STYLE) & ~WS_CAPTION);
1543  SetWindowPos(parent_window_handle, nullptr, 0, 0, 0, 0,
1544  SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE |
1545  SWP_FRAMECHANGED);
1546 
1547  POINT parent_client_origin_after = {0, 0};
1548  ClientToScreen(parent_window_handle, &parent_client_origin_after);
1549  ASSERT_NE(parent_client_origin_after.y, parent_client_origin_before.y);
1550 
1551  RECT satellite_rect_after;
1552  GetWindowRect(satellite_window_handle, &satellite_rect_after);
1553  EXPECT_EQ(satellite_rect_after.left,
1554  satellite_rect_before.left +
1555  (parent_client_origin_after.x - parent_client_origin_before.x));
1556  EXPECT_EQ(satellite_rect_after.top,
1557  satellite_rect_before.top +
1558  (parent_client_origin_after.y - parent_client_origin_before.y));
1559 }
1560 
1561 TEST_F(WindowManagerTest, SatelliteWindowCannotBeMinimized) {
1562  IsolateScope isolate_scope(isolate());
1563 
1564  const HWND parent_window_handle = CreateParentWindow();
1565  SatelliteWindowCreationRequest creation_request =
1566  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1567 
1568  const HWND satellite_window_handle =
1570  engine_id(),
1572  engine_id(), &creation_request));
1573 
1574  SendMessage(satellite_window_handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
1575 
1576  EXPECT_FALSE(IsIconic(satellite_window_handle));
1577 }
1578 
1579 TEST_F(WindowManagerTest, SatelliteWindowCanReparent) {
1580  IsolateScope isolate_scope(isolate());
1581 
1582  WindowingInitRequest init_request{
1583  .on_message = [](WindowsMessage* message) {}};
1584  InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
1585 
1586  const HWND parent1_handle = CreateParentWindow();
1587  SetWindowPos(parent1_handle, nullptr, 100, 100, 0, 0,
1588  SWP_NOSIZE | SWP_NOZORDER);
1589  const HWND parent2_handle = CreateParentWindow();
1590  SetWindowPos(parent2_handle, nullptr, 500, 500, 0, 0,
1591  SWP_NOSIZE | SWP_NOZORDER);
1592 
1593  SatelliteWindowCreationRequest creation_request =
1594  SatelliteCreationRequest(parent1_handle, OffsetPositionCallback);
1595  const HWND satellite_handle =
1597  engine_id(),
1599  engine_id(), &creation_request));
1600 
1601  RECT satellite_rect_before;
1602  GetWindowRect(satellite_handle, &satellite_rect_before);
1603 
1604  // Reparenting must not move the satellite.
1606  parent2_handle);
1607 
1608  RECT satellite_rect_after;
1609  GetWindowRect(satellite_handle, &satellite_rect_after);
1610  EXPECT_EQ(satellite_rect_after.left, satellite_rect_before.left);
1611  EXPECT_EQ(satellite_rect_after.top, satellite_rect_before.top);
1612 
1613  // The satellite now follows the new parent...
1614  RECT parent2_rect;
1615  GetWindowRect(parent2_handle, &parent2_rect);
1616  SetWindowPos(parent2_handle, nullptr, parent2_rect.left + 30,
1617  parent2_rect.top + 40, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
1618 
1619  RECT satellite_rect_moved;
1620  GetWindowRect(satellite_handle, &satellite_rect_moved);
1621  EXPECT_EQ(satellite_rect_moved.left, satellite_rect_after.left + 30);
1622  EXPECT_EQ(satellite_rect_moved.top, satellite_rect_after.top + 40);
1623 
1624  // ...and no longer follows the old one.
1625  RECT parent1_rect;
1626  GetWindowRect(parent1_handle, &parent1_rect);
1627  SetWindowPos(parent1_handle, nullptr, parent1_rect.left + 70,
1628  parent1_rect.top + 80, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
1629 
1630  RECT satellite_rect_final;
1631  GetWindowRect(satellite_handle, &satellite_rect_final);
1632  EXPECT_EQ(satellite_rect_final.left, satellite_rect_moved.left);
1633  EXPECT_EQ(satellite_rect_final.top, satellite_rect_moved.top);
1634 }
1635 
1636 TEST_F(WindowManagerTest, CreateSatelliteWindowSizedToContent) {
1637  IsolateScope isolate_scope(isolate());
1638 
1639  const HWND parent_window_handle = CreateParentWindow();
1640  SetWindowPos(parent_window_handle, nullptr, 200, 100, 0, 0,
1641  SWP_NOSIZE | SWP_NOZORDER);
1642 
1643  SatelliteWindowCreationRequest creation_request =
1644  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1645  creation_request.preferred_size = {.has_preferred_view_size = false};
1646  creation_request.sized_to_content = true;
1647 
1648  const int64_t satellite_view_id =
1650  engine_id(), &creation_request);
1651  ASSERT_GE(satellite_view_id, 0);
1652  const HWND satellite_window_handle =
1654  engine_id(), satellite_view_id);
1655  ASSERT_NE(satellite_window_handle, nullptr);
1656 
1657  FlutterWindowsView* view =
1658  engine()->GetViewFromTopLevelWindow(satellite_window_handle);
1659  ASSERT_NE(view, nullptr);
1660  EXPECT_TRUE(view->IsSizedToContent());
1661 }
1662 
1663 TEST_F(WindowManagerTest,
1664  SatelliteWindowSizedToContentUsesPositionCallbackAfterFirstFrame) {
1665  IsolateScope isolate_scope(isolate());
1666 
1667  const HWND parent_window_handle = CreateParentWindow();
1668  SetWindowPos(parent_window_handle, nullptr, 200, 100, 0, 0,
1669  SWP_NOSIZE | SWP_NOZORDER);
1670 
1671  SatelliteWindowCreationRequest creation_request =
1672  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1673  creation_request.preferred_size = {.has_preferred_view_size = false};
1674  creation_request.sized_to_content = true;
1675 
1676  const HWND satellite_window_handle =
1678  engine_id(),
1680  engine_id(), &creation_request));
1681  ASSERT_NE(satellite_window_handle, nullptr);
1682 
1683  FlutterWindowsView* view =
1684  engine()->GetViewFromTopLevelWindow(satellite_window_handle);
1685  ASSERT_NE(view, nullptr);
1686 
1687  // A sized-to-content satellite does not know its size until the first frame,
1688  // so the positioner only runs once that frame has been generated.
1689  view->OnFrameGenerated(150, 100);
1690  engine()->task_runner()->ProcessTasks();
1691 
1692  RECT parent_client_rect;
1693  GetClientRect(parent_window_handle, &parent_client_rect);
1694  POINT parent_top_left = {parent_client_rect.left, parent_client_rect.top};
1695  ClientToScreen(parent_window_handle, &parent_top_left);
1696 
1697  // As on the creation path, the positioner's origin applies to the window
1698  // rectangle.
1699  RECT satellite_rect;
1700  GetWindowRect(satellite_window_handle, &satellite_rect);
1701  EXPECT_EQ(satellite_rect.left, parent_top_left.x + 20);
1702  EXPECT_EQ(satellite_rect.top, parent_top_left.y + 30);
1703 }
1704 
1705 TEST_F(WindowManagerTest, SatelliteWindowCannotBeReparentedToItself) {
1706  IsolateScope isolate_scope(isolate());
1707 
1708  WindowingInitRequest init_request{
1709  .on_message = [](WindowsMessage* message) {}};
1710  InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
1711 
1712  const HWND parent_window_handle = CreateParentWindow();
1713  SatelliteWindowCreationRequest creation_request =
1714  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1715  const HWND satellite_handle =
1717  engine_id(),
1719  engine_id(), &creation_request));
1720 
1721  // Anchoring a satellite to itself would make parent-movement propagation
1722  // recurse forever, so the request must be rejected.
1724  satellite_handle);
1725 
1726  RECT parent_rect;
1727  GetWindowRect(parent_window_handle, &parent_rect);
1728  RECT satellite_rect_before;
1729  GetWindowRect(satellite_handle, &satellite_rect_before);
1730 
1731  // The satellite still tracks its original parent.
1732  SetWindowPos(parent_window_handle, nullptr, parent_rect.left + 15,
1733  parent_rect.top + 25, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
1734 
1735  RECT satellite_rect_after;
1736  GetWindowRect(satellite_handle, &satellite_rect_after);
1737  EXPECT_EQ(satellite_rect_after.left, satellite_rect_before.left + 15);
1738  EXPECT_EQ(satellite_rect_after.top, satellite_rect_before.top + 25);
1739 }
1740 
1741 TEST_F(WindowManagerTest, SatelliteWindowCannotBeReparentedToItsOwnSatellite) {
1742  IsolateScope isolate_scope(isolate());
1743 
1744  WindowingInitRequest init_request{
1745  .on_message = [](WindowsMessage* message) {}};
1746  InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
1747 
1748  const HWND parent_window_handle = CreateParentWindow();
1749  SatelliteWindowCreationRequest outer_request =
1750  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1751  const HWND outer_handle =
1753  engine_id(),
1755  engine_id(), &outer_request));
1756 
1757  SatelliteWindowCreationRequest inner_request =
1758  SatelliteCreationRequest(outer_handle, OffsetPositionCallback);
1759  const HWND inner_handle =
1761  engine_id(),
1763  engine_id(), &inner_request));
1764 
1765  // |outer| already anchors |inner|, so anchoring |outer| to |inner| would
1766  // close a cycle and must be rejected.
1768  inner_handle);
1769 
1770  RECT parent_rect;
1771  GetWindowRect(parent_window_handle, &parent_rect);
1772  RECT outer_rect_before;
1773  GetWindowRect(outer_handle, &outer_rect_before);
1774  RECT inner_rect_before;
1775  GetWindowRect(inner_handle, &inner_rect_before);
1776 
1777  // Moving the root window still propagates down the chain exactly once.
1778  SetWindowPos(parent_window_handle, nullptr, parent_rect.left + 10,
1779  parent_rect.top + 20, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
1780 
1781  RECT outer_rect_after;
1782  GetWindowRect(outer_handle, &outer_rect_after);
1783  RECT inner_rect_after;
1784  GetWindowRect(inner_handle, &inner_rect_after);
1785  EXPECT_EQ(outer_rect_after.left, outer_rect_before.left + 10);
1786  EXPECT_EQ(outer_rect_after.top, outer_rect_before.top + 20);
1787  EXPECT_EQ(inner_rect_after.left, inner_rect_before.left + 10);
1788  EXPECT_EQ(inner_rect_after.top, inner_rect_before.top + 20);
1789 }
1790 
1791 TEST_F(WindowManagerTest, OnEngineShutdownDispatchesWmDestroyForSatellite) {
1792  IsolateScope isolate_scope(isolate());
1793 
1794  static std::vector<UINT> received_messages;
1795  received_messages.clear();
1796  WindowingInitRequest init_request{.on_message = [](WindowsMessage* message) {
1797  received_messages.push_back(message->message);
1798  }};
1799  InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
1800 
1801  const HWND parent_window_handle = CreateParentWindow();
1802  SatelliteWindowCreationRequest creation_request =
1803  SatelliteCreationRequest(parent_window_handle, OffsetPositionCallback);
1805  &creation_request);
1806 
1807  received_messages.clear();
1808  engine()->window_manager()->OnEngineShutdown();
1809 
1810  EXPECT_NE(std::find(received_messages.begin(), received_messages.end(),
1811  static_cast<UINT>(WM_DESTROY)),
1812  received_messages.end());
1813 }
1814 
1815 } // namespace testing
1816 } // namespace flutter
bool OnFrameGenerated(size_t width, size_t height)
HWND GetWindowHandle() const
Definition: host_window.cc:377
HostWindow * GetOwnerWindow() const
Definition: host_window.cc:923
static HostWindow * GetThisFromHandle(HWND hwnd)
Definition: host_window.cc:362
static Isolate Current()
Definition: isolate_scope.cc:9
Win32Message message
TEST_F(AccessibilityPluginTest, DirectAnnounceCall)
void(* GetWindowPositionCallback)(const WindowSize &child_size, const WindowRect &parent_rect, const WindowRect &display_rect, WindowRect &output_rect)
void(* on_message)(WindowsMessage *)
void InternalFlutterWindows_WindowManager_Initialize(int64_t engine_id, const flutter::WindowingInitRequest *request)
void InternalFlutterWindows_WindowManager_SetWindowConstraints(HWND hwnd, const flutter::WindowConstraints *constraints)
bool InternalFlutterWindows_WindowManager_GetFullscreen(HWND hwnd)
void InternalFlutterWindows_WindowManager_SetWindowSize(HWND hwnd, const flutter::WindowSizeRequest *size)
FlutterViewId InternalFlutterWindows_WindowManager_CreateRegularWindow(int64_t engine_id, const flutter::RegularWindowCreationRequest *request)
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreateDialogWindow(int64_t engine_id, const flutter::DialogWindowCreationRequest *request)
void InternalFlutterWindows_WindowManager_SetFullscreen(HWND hwnd, const flutter::FullscreenRequest *request)
flutter::ActualWindowSize InternalFlutterWindows_WindowManager_GetWindowContentSize(HWND hwnd)
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreateSatelliteWindow(int64_t engine_id, const flutter::SatelliteWindowCreationRequest *request)
FLUTTER_EXPORT void InternalFlutterWindows_WindowManager_SetSatelliteParent(HWND satellite_hwnd, HWND new_parent)
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreateTooltipWindow(int64_t engine_id, const flutter::TooltipWindowCreationRequest *request)
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreatePopupWindow(int64_t engine_id, const flutter::PopupWindowCreationRequest *request)
HWND InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(int64_t engine_id, FlutterViewId view_id)