Flutter Engine
win32_window_proc_delegate_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/win32_window_proc_delegate_manager.h"
6 #include "gtest/gtest.h"
7 
8 namespace flutter {
9 namespace testing {
10 
11 namespace {
12 
13 using TestWindowProcDelegate = std::function<std::optional<
14  LRESULT>(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)>;
15 
16 // A FlutterDesktopWindowProcCallback that forwards to a std::function provided
17 // as user_data.
18 bool TestWindowProcCallback(HWND hwnd,
19  UINT message,
20  WPARAM wparam,
21  LPARAM lparam,
22  void* user_data,
23  LRESULT* result) {
24  TestWindowProcDelegate& delegate =
25  *static_cast<TestWindowProcDelegate*>(user_data);
26  auto delegate_result = delegate(hwnd, message, wparam, lparam);
27  if (delegate_result) {
28  *result = *delegate_result;
29  }
30  return delegate_result.has_value();
31 }
32 
33 // Same as the above, but with a different address, to test multiple
34 // registration.
35 bool TestWindowProcCallback2(HWND hwnd,
36  UINT message,
37  WPARAM wparam,
38  LPARAM lparam,
39  void* user_data,
40  LRESULT* result) {
41  return TestWindowProcCallback(hwnd, message, wparam, lparam, user_data,
42  result);
43 }
44 
45 } // namespace
46 
47 TEST(Win32WindowProcDelegateManagerTest, CallsCorrectly) {
49  HWND dummy_hwnd;
50 
51  bool called = false;
52  TestWindowProcDelegate delegate = [&called, &dummy_hwnd](
53  HWND hwnd, UINT message, WPARAM wparam,
54  LPARAM lparam) {
55  called = true;
56  EXPECT_EQ(hwnd, dummy_hwnd);
57  EXPECT_EQ(message, 2);
58  EXPECT_EQ(wparam, 3);
59  EXPECT_EQ(lparam, 4);
60  return std::optional<LRESULT>();
61  };
62  manager.RegisterTopLevelWindowProcDelegate(TestWindowProcCallback, &delegate);
63  auto result = manager.OnTopLevelWindowProc(dummy_hwnd, 2, 3, 4);
64 
65  EXPECT_TRUE(called);
66  EXPECT_FALSE(result);
67 }
68 
69 TEST(Win32WindowProcDelegateManagerTest, ReplacementRegister) {
71 
72  bool called_a = false;
73  TestWindowProcDelegate delegate_a =
74  [&called_a](HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
75  called_a = true;
76  return std::optional<LRESULT>();
77  };
78  bool called_b = false;
79  TestWindowProcDelegate delegate_b =
80  [&called_b](HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
81  called_b = true;
82  return std::optional<LRESULT>();
83  };
84  manager.RegisterTopLevelWindowProcDelegate(TestWindowProcCallback,
85  &delegate_a);
86  // The function pointer is the same, so this should replace, not add.
87  manager.RegisterTopLevelWindowProcDelegate(TestWindowProcCallback,
88  &delegate_b);
89  manager.OnTopLevelWindowProc(nullptr, 0, 0, 0);
90 
91  EXPECT_FALSE(called_a);
92  EXPECT_TRUE(called_b);
93 }
94 
95 TEST(Win32WindowProcDelegateManagerTest, RegisterMultiple) {
97 
98  bool called_a = false;
99  TestWindowProcDelegate delegate_a =
100  [&called_a](HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
101  called_a = true;
102  return std::optional<LRESULT>();
103  };
104  bool called_b = false;
105  TestWindowProcDelegate delegate_b =
106  [&called_b](HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
107  called_b = true;
108  return std::optional<LRESULT>();
109  };
110  manager.RegisterTopLevelWindowProcDelegate(TestWindowProcCallback,
111  &delegate_a);
112  // Function pointer is different, so both should be called.
113  manager.RegisterTopLevelWindowProcDelegate(TestWindowProcCallback2,
114  &delegate_b);
115  manager.OnTopLevelWindowProc(nullptr, 0, 0, 0);
116 
117  EXPECT_TRUE(called_a);
118  EXPECT_TRUE(called_b);
119 }
120 
121 TEST(Win32WindowProcDelegateManagerTest, ConflictingDelegates) {
123 
124  bool called_a = false;
125  TestWindowProcDelegate delegate_a =
126  [&called_a](HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
127  called_a = true;
128  return std::optional<LRESULT>(1);
129  };
130  bool called_b = false;
131  TestWindowProcDelegate delegate_b =
132  [&called_b](HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
133  called_b = true;
134  return std::optional<LRESULT>(1);
135  };
136  manager.RegisterTopLevelWindowProcDelegate(TestWindowProcCallback,
137  &delegate_a);
138  manager.RegisterTopLevelWindowProcDelegate(TestWindowProcCallback2,
139  &delegate_b);
140  auto result = manager.OnTopLevelWindowProc(nullptr, 0, 0, 0);
141 
142  EXPECT_TRUE(result);
143  // Exactly one of the handlers should be called since each will claim to have
144  // handled the message. Which one is unspecified, since the calling order is
145  // unspecified.
146  EXPECT_TRUE(called_a || called_b);
147  EXPECT_NE(called_a, called_b);
148 }
149 
150 TEST(Win32WindowProcDelegateManagerTest, Unregister) {
152 
153  bool called = false;
154  TestWindowProcDelegate delegate = [&called](HWND hwnd, UINT message,
155  WPARAM wparam, LPARAM lparam) {
156  called = true;
157  return std::optional<LRESULT>();
158  };
159  manager.RegisterTopLevelWindowProcDelegate(TestWindowProcCallback, &delegate);
160  manager.UnregisterTopLevelWindowProcDelegate(TestWindowProcCallback);
161  auto result = manager.OnTopLevelWindowProc(nullptr, 0, 0, 0);
162 
163  EXPECT_FALSE(result);
164  EXPECT_FALSE(called);
165 }
166 
167 } // namespace testing
168 } // namespace flutter
std::optional< LRESULT > OnTopLevelWindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Dart_NativeFunction function
Definition: fuchsia.cc:51
G_BEGIN_DECLS FlValue gpointer user_data
void UnregisterTopLevelWindowProcDelegate(FlutterDesktopWindowProcCallback delegate)
void RegisterTopLevelWindowProcDelegate(FlutterDesktopWindowProcCallback delegate, void *user_data)
TEST(EmbeddedViewParams, GetBoundingRectAfterMutationsWithNoMutations)