| 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 "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
6 #include "flutter/shell/platform/windows/testing/windows_test.h"
7 #include "flutter/shell/platform/windows/window_manager.h"
8 #include "gtest/gtest.h"
9
10 namespace flutter {
11 namespace testing {
12
13 namespace {
14
15 class WindowManagerTest : public WindowsTest {
16 public:
17 WindowManagerTest() = default;
18 virtual ~WindowManagerTest() = default;
19
20 protected:
21 void SetUp() override {
22 auto& context = GetContext();
23 FlutterWindowsEngineBuilder builder(context);
24
25 engine_ = builder.Build();
26 ASSERT_TRUE(engine_);
27
28 engine_->SetRootIsolateCreateCallback(context.GetRootIsolateCallback());
29 ASSERT_TRUE(engine_->Run("testWindowController"));
30
31 bool signalled = false;
32 context.AddNativeFunction(
33 "Signal", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
34 isolate_ = flutter::Isolate::Current();
35 signalled = true;
36 }));
37 while (!signalled) {
38 engine_->task_runner()->ProcessTasks();
39 }
40 }
41
42 void TearDown() override { engine_->Stop(); }
43
44 int64_t engine_id() { return reinterpret_cast<int64_t>(engine_.get()); }
45 flutter::Isolate& isolate() { return *isolate_; }
46 WindowCreationRequest* creation_request() { return &creation_request_; }
47
48 private:
49 std::unique_ptr<FlutterWindowsEngine> engine_;
50 std::optional<flutter::Isolate> isolate_;
51 WindowCreationRequest creation_request_{
52 .content_size =
53 {
54 .has_preferred_view_size = true,
55 .preferred_view_width = 800,
56 .preferred_view_height = 600,
57 },
58 };
59
60 FML_DISALLOW_COPY_AND_ASSIGN(WindowManagerTest);
61 };
62
63 } // namespace
64
65 TEST_F(WindowManagerTest, WindowingInitialize) {
66 IsolateScope isolate_scope(isolate());
67
68 static bool received_message = false;
69 WindowingInitRequest init_request{
70 .on_message = [](WindowsMessage* message) { received_message = true; }};
71
72 InternalFlutterWindows_WindowManager_Initialize(engine_id(), &init_request);
73 const int64_t view_id =
74 InternalFlutterWindows_WindowManager_CreateRegularWindow(
75 engine_id(), creation_request());
76 DestroyWindow(InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(
77 engine_id(), view_id));
78
79 EXPECT_TRUE(received_message);
80 }
81
82 TEST_F(WindowManagerTest, HasTopLevelWindows) {
83 IsolateScope isolate_scope(isolate());
84
85 bool has_top_level_windows =
86 InternalFlutterWindows_WindowManager_HasTopLevelWindows(engine_id());
87 EXPECT_FALSE(has_top_level_windows);
88
89 InternalFlutterWindows_WindowManager_CreateRegularWindow(engine_id(),
90 creation_request());
91 has_top_level_windows =
92 InternalFlutterWindows_WindowManager_HasTopLevelWindows(engine_id());
93 EXPECT_TRUE(has_top_level_windows);
94 }
95
96 TEST_F(WindowManagerTest, CreateRegularWindow) {
97 IsolateScope isolate_scope(isolate());
98
99 const int64_t view_id =
100 InternalFlutterWindows_WindowManager_CreateRegularWindow(
101 engine_id(), creation_request());
102 EXPECT_EQ(view_id, 0);
103 }
104
105 TEST_F(WindowManagerTest, GetWindowHandle) {
106 IsolateScope isolate_scope(isolate());
107
108 const int64_t view_id =
109 InternalFlutterWindows_WindowManager_CreateRegularWindow(
110 engine_id(), creation_request());
111 const HWND window_handle =
112 InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(engine_id(),
113 view_id);
114 EXPECT_NE(window_handle, nullptr);
115 }
116
117 TEST_F(WindowManagerTest, GetWindowSize) {
118 IsolateScope isolate_scope(isolate());
119
120 const int64_t view_id =
121 InternalFlutterWindows_WindowManager_CreateRegularWindow(
122 engine_id(), creation_request());
123 const HWND window_handle =
124 InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(engine_id(),
125 view_id);
126
127 FlutterWindowSize size =
128 InternalFlutterWindows_WindowManager_GetWindowContentSize(window_handle);
129
130 EXPECT_EQ(size.width, creation_request()->content_size.preferred_view_width);
131 EXPECT_EQ(size.height,
132 creation_request()->content_size.preferred_view_height);
133 }
134
135 TEST_F(WindowManagerTest, SetWindowSize) {
136 IsolateScope isolate_scope(isolate());
137
138 const int64_t view_id =
139 InternalFlutterWindows_WindowManager_CreateRegularWindow(
140 engine_id(), creation_request());
141 const HWND window_handle =
142 InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(engine_id(),
143 view_id);
144
145 WindowSizing requestedSize{
146 .has_preferred_view_size = true,
147 .preferred_view_width = 640,
148 .preferred_view_height = 480,
149 };
150 InternalFlutterWindows_WindowManager_SetWindowContentSize(window_handle,
151 &requestedSize);
152
153 FlutterWindowSize actual_size =
154 InternalFlutterWindows_WindowManager_GetWindowContentSize(window_handle);
155 EXPECT_EQ(actual_size.width, 640);
156 EXPECT_EQ(actual_size.height, 480);
157 }
158
159 } // namespace testing
160 } // namespace flutter
flutter::Isolate
Definition: isolate_scope.h:15
flutter::Isolate::Current
static Isolate Current()
Definition: isolate_scope.cc:9
flutter::IsolateScope
Definition: isolate_scope.h:33
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter::testing::TEST_F
TEST_F(CompositorOpenGLTest, CreateBackingStore)
Definition: compositor_opengl_unittests.cc:139
flutter
Definition: accessibility_bridge_windows.cc:11
FlutterWindowSize
Definition: window_manager.h:120
FlutterWindowSize::height
double height
Definition: window_manager.h:122
FlutterWindowSize::width
double width
Definition: window_manager.h:121
flutter::WindowSizing
Definition: window_manager.h:36
flutter::WindowSizing::has_preferred_view_size
bool has_preferred_view_size
Definition: window_manager.h:37
flutter::WindowingInitRequest
Definition: window_manager.h:47
flutter::WindowingInitRequest::on_message
void(* on_message)(WindowsMessage *)
Definition: window_manager.h:48
flutter::WindowsMessage
Definition: window_manager.h:26
InternalFlutterWindows_WindowManager_Initialize
void InternalFlutterWindows_WindowManager_Initialize(int64_t engine_id, const flutter::WindowingInitRequest *request)
Definition: window_manager.cc:101
InternalFlutterWindows_WindowManager_HasTopLevelWindows
bool InternalFlutterWindows_WindowManager_HasTopLevelWindows(int64_t engine_id)
Definition: window_manager.cc:109
InternalFlutterWindows_WindowManager_CreateRegularWindow
FlutterViewId InternalFlutterWindows_WindowManager_CreateRegularWindow(int64_t engine_id, const flutter::WindowCreationRequest *request)
Definition: window_manager.cc:116
InternalFlutterWindows_WindowManager_GetWindowContentSize
FlutterWindowSize InternalFlutterWindows_WindowManager_GetWindowContentSize(HWND hwnd)
Definition: window_manager.cc:137
InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle
HWND InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(int64_t engine_id, FlutterViewId view_id)
Definition: window_manager.cc:124
InternalFlutterWindows_WindowManager_SetWindowContentSize
void InternalFlutterWindows_WindowManager_SetWindowContentSize(HWND hwnd, const flutter::WindowSizing *size)
Definition: window_manager.cc:151
window_manager.h