Flutter Linux Embedder
fl_window_monitor_test.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 
6 
7 #include "flutter/shell/platform/linux/testing/fl_test_gtk_logs.h"
8 #include "flutter/shell/platform/linux/testing/linux_test.h"
9 #include "flutter/shell/platform/linux/testing/mock_gtk.h"
10 #include "gtest/gtest.h"
11 
12 namespace {
13 
14 // The monitor reports using function pointers that have no user data, so the
15 // calls they make are recorded here.
16 struct MonitorCalls {
17  int configure = 0;
18  int state_changed = 0;
19  int is_active_notify = 0;
20  int title_notify = 0;
21  int moved_to_rect = 0;
22  int close = 0;
23  int destroy = 0;
24  GdkRectangle rect = {};
25 };
26 
27 MonitorCalls calls;
28 
29 void on_configure() {
30  calls.configure++;
31 }
32 
33 void on_state_changed() {
34  calls.state_changed++;
35 }
36 
37 void on_is_active_notify() {
38  calls.is_active_notify++;
39 }
40 
41 void on_title_notify() {
42  calls.title_notify++;
43 }
44 
45 void on_moved_to_rect(int x, int y, int width, int height) {
46  calls.moved_to_rect++;
47  calls.rect = {x, y, width, height};
48 }
49 
50 void on_close() {
51  calls.close++;
52 }
53 
54 void on_destroy() {
55  calls.destroy++;
56 }
57 
58 FlWindowMonitor* monitor_new(GtkWindow* window) {
59  return fl_window_monitor_new(window, on_configure, on_state_changed,
60  on_is_active_notify, on_title_notify,
61  on_moved_to_rect, on_close, on_destroy);
62 }
63 
64 // GdkWindow is abstract, so a concrete subclass is required to make instances
65 // that signals can be connected to. GdkWindow expects to be created by GDK with
66 // a backend behind it, so its dispose and finalize methods are replaced with
67 // ones that don't touch that state.
68 //
69 // The G_DEFINE_TYPE macros can't be used to do this, as they need to place a
70 // GdkWindow inside the instance structure and GDK doesn't make that structure
71 // public. The size of the structure is instead requested from the type system
72 // at runtime.
73 void test_gdk_window_dispose(GObject* object) {}
74 
75 void test_gdk_window_finalize(GObject* object) {
76  G_OBJECT_CLASS(g_type_class_peek(G_TYPE_OBJECT))->finalize(object);
77 }
78 
79 void test_gdk_window_class_init(gpointer klass, gpointer data) {
80  G_OBJECT_CLASS(klass)->dispose = test_gdk_window_dispose;
81  G_OBJECT_CLASS(klass)->finalize = test_gdk_window_finalize;
82 }
83 
84 GType test_gdk_window_get_type() {
85  static gsize type = 0;
86  if (g_once_init_enter(&type)) {
87  GTypeQuery query;
88  g_type_query(GDK_TYPE_WINDOW, &query);
89  g_once_init_leave(
90  &type, g_type_register_static_simple(
91  GDK_TYPE_WINDOW, "TestGdkWindow", query.class_size,
92  test_gdk_window_class_init, query.instance_size, nullptr,
93  static_cast<GTypeFlags>(0)));
94  }
95  return type;
96 }
97 
98 GdkWindow* test_gdk_window_new() {
99  return GDK_WINDOW(g_object_new(test_gdk_window_get_type(), nullptr));
100 }
101 
102 // Checks if @monitor is listening to @name on @instance.
103 bool has_handler(gpointer instance,
104  const gchar* name,
105  FlWindowMonitor* monitor) {
106  GSignalMatchType match =
107  static_cast<GSignalMatchType>(G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DATA);
108  guint signal_id = g_signal_lookup(name, G_OBJECT_TYPE(instance));
109  return g_signal_handler_find(instance, match, signal_id, 0, nullptr, nullptr,
110  monitor) != 0;
111 }
112 
113 } // namespace
114 
115 using flutter::testing::fl_has_received_gtk_log_level;
116 using flutter::testing::fl_reset_received_gtk_log_levels;
117 
118 class FlWindowMonitorTest : public flutter::testing::LinuxTest {
119  protected:
120  void SetUp() override {
121  fl_reset_received_gtk_log_levels();
122  calls = MonitorCalls();
123  }
124 
125  ::testing::NiceMock<flutter::testing::MockGtk> mock_gtk;
126 };
127 
128 // The moved-to-rect signal is emitted by the GdkWindow, which does not exist
129 // until the window is realized. The monitor has to wait for that to happen.
130 TEST_F(FlWindowMonitorTest, ConnectsToRealize) {
131  g_autoptr(GtkWindow) window =
132  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
133  ASSERT_FALSE(gtk_widget_get_realized(GTK_WIDGET(window)));
134 
135  g_autoptr(FlWindowMonitor) monitor = monitor_new(window);
136 
137  EXPECT_TRUE(has_handler(window, "realize", monitor));
138 }
139 
140 // Unrealizing destroys the GdkWindow and the moved-to-rect connection with it,
141 // so the monitor has to stay connected to realize even if the window is already
142 // realized when it is created.
143 TEST_F(FlWindowMonitorTest, ConnectsToRealizeWhenAlreadyRealized) {
144  g_autoptr(GtkWindow) window =
145  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
146  gtk_widget_set_realized(GTK_WIDGET(window), TRUE);
147 
148  g_autoptr(FlWindowMonitor) monitor = monitor_new(window);
149 
150  EXPECT_TRUE(has_handler(window, "realize", monitor));
151  // The window has no GdkWindow to connect moved-to-rect to yet, which is not
152  // an error.
153  EXPECT_FALSE(fl_has_received_gtk_log_level(G_LOG_LEVEL_CRITICAL));
154 
155  gtk_widget_set_realized(GTK_WIDGET(window), FALSE);
156 }
157 
158 // A window that is already realized has a GdkWindow, so moved-to-rect can be
159 // connected immediately.
160 TEST_F(FlWindowMonitorTest, ConnectsMovedToRectWhenAlreadyRealized) {
161  g_autoptr(GtkWindow) window =
162  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
163  gtk_widget_set_realized(GTK_WIDGET(window), TRUE);
164  g_autoptr(GdkWindow) gdk_window = test_gdk_window_new();
165  EXPECT_CALL(mock_gtk, gtk_widget_get_window)
166  .WillRepeatedly(::testing::Return(gdk_window));
167 
168  g_autoptr(FlWindowMonitor) monitor = monitor_new(window);
169 
170  EXPECT_TRUE(has_handler(gdk_window, "moved-to-rect", monitor));
171 
172  gtk_widget_set_realized(GTK_WIDGET(window), FALSE);
173 }
174 
175 // A window that is not yet realized has no GdkWindow, so moved-to-rect can only
176 // be connected once it is realized.
177 TEST_F(FlWindowMonitorTest, ConnectsMovedToRectOnRealize) {
178  g_autoptr(GtkWindow) window =
179  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
180  g_autoptr(GdkWindow) gdk_window = test_gdk_window_new();
181 
182  g_autoptr(FlWindowMonitor) monitor = monitor_new(window);
183  EXPECT_FALSE(has_handler(gdk_window, "moved-to-rect", monitor));
184 
185  // The GdkWindow is created when the window is realized.
186  EXPECT_CALL(mock_gtk, gtk_widget_get_window)
187  .WillRepeatedly(::testing::Return(gdk_window));
188  g_signal_emit_by_name(window, "realize");
189 
190  EXPECT_TRUE(has_handler(gdk_window, "moved-to-rect", monitor));
191 }
192 
193 // The window reports changes to its size, position or stacking with the
194 // configure-event signal.
196  g_autoptr(GtkWindow) window =
197  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
198  g_autoptr(FlWindowMonitor) monitor = monitor_new(window);
199 
200  GdkEventConfigure event = {};
201  event.type = GDK_CONFIGURE;
202  gboolean result = FALSE;
203  g_signal_emit_by_name(window, "configure-event", &event, &result);
204 
205  EXPECT_EQ(calls.configure, 1);
206 }
207 
208 // The window reports changes to its state with the window-state-event signal.
209 TEST_F(FlWindowMonitorTest, StateChanged) {
210  g_autoptr(GtkWindow) window =
211  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
212  g_autoptr(FlWindowMonitor) monitor = monitor_new(window);
213 
214  GdkEventWindowState event = {};
215  event.type = GDK_WINDOW_STATE;
216  event.changed_mask = GDK_WINDOW_STATE_MAXIMIZED;
217  event.new_window_state = GDK_WINDOW_STATE_MAXIMIZED;
218  gboolean result = FALSE;
219  g_signal_emit_by_name(window, "window-state-event", &event, &result);
220 
221  EXPECT_EQ(calls.state_changed, 1);
222 }
223 
224 TEST_F(FlWindowMonitorTest, IsActiveNotify) {
225  g_autoptr(GtkWindow) window =
226  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
227  g_autoptr(FlWindowMonitor) monitor = monitor_new(window);
228 
229  g_object_notify(G_OBJECT(window), "is-active");
230 
231  EXPECT_EQ(calls.is_active_notify, 1);
232 }
233 
235  g_autoptr(GtkWindow) window =
236  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
237  g_autoptr(FlWindowMonitor) monitor = monitor_new(window);
238 
239  g_object_notify(G_OBJECT(window), "title");
240 
241  EXPECT_EQ(calls.title_notify, 1);
242 }
243 
244 // Popup windows are told where they ended up by the moved-to-rect signal.
246  g_autoptr(GtkWindow) window =
247  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
248  gtk_widget_set_realized(GTK_WIDGET(window), TRUE);
249  g_autoptr(GdkWindow) gdk_window = test_gdk_window_new();
250  EXPECT_CALL(mock_gtk, gtk_widget_get_window)
251  .WillRepeatedly(::testing::Return(gdk_window));
252  g_autoptr(FlWindowMonitor) monitor = monitor_new(window);
253 
254  GdkRectangle flipped_rect = {1, 2, 3, 4};
255  GdkRectangle final_rect = {5, 6, 7, 8};
256  g_signal_emit_by_name(gdk_window, "moved-to-rect", &flipped_rect, &final_rect,
257  FALSE, FALSE);
258 
259  EXPECT_EQ(calls.moved_to_rect, 1);
260  EXPECT_EQ(calls.rect.x, 5);
261  EXPECT_EQ(calls.rect.y, 6);
262  EXPECT_EQ(calls.rect.width, 7);
263  EXPECT_EQ(calls.rect.height, 8);
264 
265  gtk_widget_set_realized(GTK_WIDGET(window), FALSE);
266 }
267 
268 // The backend is not always able to report where the window ended up, in which
269 // case there is nothing to report.
270 TEST_F(FlWindowMonitorTest, MovedToRectWithoutFinalRect) {
271  g_autoptr(GtkWindow) window =
272  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
273  gtk_widget_set_realized(GTK_WIDGET(window), TRUE);
274  g_autoptr(GdkWindow) gdk_window = test_gdk_window_new();
275  EXPECT_CALL(mock_gtk, gtk_widget_get_window)
276  .WillRepeatedly(::testing::Return(gdk_window));
277  g_autoptr(FlWindowMonitor) monitor = monitor_new(window);
278 
279  GdkRectangle flipped_rect = {1, 2, 3, 4};
280  g_signal_emit_by_name(gdk_window, "moved-to-rect", &flipped_rect, nullptr,
281  FALSE, FALSE);
282 
283  EXPECT_EQ(calls.moved_to_rect, 0);
284 
285  gtk_widget_set_realized(GTK_WIDGET(window), FALSE);
286 }
287 
288 // Closing is requested with the delete-event signal. The window is not
289 // destroyed in response, as Flutter decides if the close is allowed.
291  g_autoptr(GtkWindow) window =
292  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
293  g_autoptr(FlWindowMonitor) monitor = monitor_new(window);
294 
295  GdkEvent event = {};
296  event.type = GDK_DELETE;
297  gboolean result = FALSE;
298  g_signal_emit_by_name(window, "delete-event", &event, &result);
299 
300  EXPECT_EQ(calls.close, 1);
301  EXPECT_TRUE(result);
302  EXPECT_EQ(calls.destroy, 0);
303 }
304 
306  g_autoptr(GtkWindow) window =
307  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
308  g_autoptr(FlWindowMonitor) monitor = monitor_new(window);
309 
310  g_signal_emit_by_name(window, "destroy");
311 
312  EXPECT_EQ(calls.destroy, 1);
313 }
314 
315 // The monitor keeps the window alive for as long as it is monitoring it, and
316 // releases it when it stops.
317 TEST_F(FlWindowMonitorTest, ReferencesWindow) {
318  g_autoptr(GtkWindow) window =
319  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
320  guint ref_count = G_OBJECT(window)->ref_count;
321 
322  FlWindowMonitor* monitor = monitor_new(window);
323  EXPECT_EQ(G_OBJECT(window)->ref_count, ref_count + 1);
324 
325  g_object_unref(monitor);
326  EXPECT_EQ(G_OBJECT(window)->ref_count, ref_count);
327 }
328 
329 // A destroyed monitor stops reporting, rather than using its callbacks after
330 // the Dart object they belong to has gone.
331 TEST_F(FlWindowMonitorTest, StopsReportingWhenDestroyed) {
332  g_autoptr(GtkWindow) window =
333  GTK_WINDOW(g_object_ref_sink(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
334  FlWindowMonitor* monitor = monitor_new(window);
335  g_object_unref(monitor);
336 
337  GdkEventConfigure event = {};
338  event.type = GDK_CONFIGURE;
339  gboolean result = FALSE;
340  g_signal_emit_by_name(window, "configure-event", &event, &result);
341  g_object_notify(G_OBJECT(window), "title");
342 
343  EXPECT_EQ(calls.configure, 0);
344  EXPECT_EQ(calls.title_notify, 0);
345 }
::testing::NiceMock< flutter::testing::MockGtk > mock_gtk
g_autoptr(FlEngine) engine
const uint8_t uint32_t uint32_t * height
const uint8_t uint32_t * width
static void configure(const gchar *input_action, gboolean enable_delta_model, FlTextInputType input_type, GtkInputPurpose im_purpose, GtkInputHints im_hints, gpointer user_data)
G_MODULE_EXPORT FlWindowMonitor * fl_window_monitor_new(GtkWindow *window, void(*on_configure)(void), void(*on_state_changed)(void), void(*on_is_active_notify)(void), void(*on_title_notify)(void), void(*on_moved_to_rect)(int, int, int, int), void(*on_close)(void), void(*on_destroy)(void))
TEST_F(FlWindowMonitorTest, ConnectsToRealize)