| Flutter Linux Embedder |

fl_window_state_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
5 #include "flutter/shell/platform/linux/fl_window_state_monitor.h"
6 #include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
7 #include "flutter/shell/platform/linux/public/flutter_linux/fl_string_codec.h"
8 #include "flutter/shell/platform/linux/testing/fl_mock_binary_messenger.h"
9 #include "flutter/shell/platform/linux/testing/mock_gtk.h"
10
11 #include "gtest/gtest.h"
12
13 TEST(FlWindowStateMonitorTest, GainFocus) {
14 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
15 ::testing::NiceMock<flutter::testing::MockGtk> mock_gtk;
16
17 gtk_init(0, nullptr);
18
19 EXPECT_CALL(mock_gtk, gdk_window_get_state)
20 .WillOnce(::testing::Return(static_cast<GdkWindowState>(0)));
21
22 gboolean called = TRUE;
23 fl_mock_binary_messenger_set_string_message_channel(
24 messenger, "flutter/lifecycle",
25 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
26 gpointer user_data) {
27 gboolean* called = static_cast<gboolean*>(user_data);
28 *called = TRUE;
29 EXPECT_STREQ(fl_value_get_string(message), "AppLifecycleState.resumed");
30 return fl_value_new_string("");
31 },
32 &called);
33
34 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
35 gtk_widget_show(GTK_WIDGET(window));
36 g_autoptr(FlWindowStateMonitor) monitor =
37 fl_window_state_monitor_new(FL_BINARY_MESSENGER(messenger), window);
38
39 GdkEvent event = {
40 .window_state = {.new_window_state = GDK_WINDOW_STATE_FOCUSED}};
41 gboolean handled;
42 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
43 EXPECT_TRUE(called);
44
45 fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
46 }
47
48 TEST(FlWindowStateMonitorTest, LoseFocus) {
49 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
50 ::testing::NiceMock<flutter::testing::MockGtk> mock_gtk;
51
52 gtk_init(0, nullptr);
53
54 EXPECT_CALL(mock_gtk, gdk_window_get_state)
55 .WillOnce(::testing::Return(GDK_WINDOW_STATE_FOCUSED));
56 gboolean called = TRUE;
57 fl_mock_binary_messenger_set_string_message_channel(
58 messenger, "flutter/lifecycle",
59 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
60 gpointer user_data) {
61 gboolean* called = static_cast<gboolean*>(user_data);
62 *called = TRUE;
63 EXPECT_STREQ(fl_value_get_string(message),
64 "AppLifecycleState.inactive");
65 return fl_value_new_string("");
66 },
67 &called);
68
69 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
70 gtk_widget_show(GTK_WIDGET(window));
71 g_autoptr(FlWindowStateMonitor) monitor =
72 fl_window_state_monitor_new(FL_BINARY_MESSENGER(messenger), window);
73
74 GdkEvent event = {
75 .window_state = {.new_window_state = static_cast<GdkWindowState>(0)}};
76 gboolean handled;
77 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
78 EXPECT_TRUE(called);
79
80 fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
81 }
82
83 TEST(FlWindowStateMonitorTest, EnterIconified) {
84 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
85 ::testing::NiceMock<flutter::testing::MockGtk> mock_gtk;
86
87 gtk_init(0, nullptr);
88
89 EXPECT_CALL(mock_gtk, gdk_window_get_state)
90 .WillOnce(::testing::Return(static_cast<GdkWindowState>(0)));
91 gboolean called = TRUE;
92 fl_mock_binary_messenger_set_string_message_channel(
93 messenger, "flutter/lifecycle",
94 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
95 gpointer user_data) {
96 gboolean* called = static_cast<gboolean*>(user_data);
97 *called = TRUE;
98 EXPECT_STREQ(fl_value_get_string(message), "AppLifecycleState.hidden");
99 return fl_value_new_string("");
100 },
101 &called);
102
103 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
104 gtk_widget_show(GTK_WIDGET(window));
105 g_autoptr(FlWindowStateMonitor) monitor =
106 fl_window_state_monitor_new(FL_BINARY_MESSENGER(messenger), window);
107
108 GdkEvent event = {
109 .window_state = {.new_window_state = GDK_WINDOW_STATE_ICONIFIED}};
110 gboolean handled;
111 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
112 EXPECT_TRUE(called);
113
114 fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
115 }
116
117 TEST(FlWindowStateMonitorTest, LeaveIconified) {
118 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
119 ::testing::NiceMock<flutter::testing::MockGtk> mock_gtk;
120
121 gtk_init(0, nullptr);
122
123 EXPECT_CALL(mock_gtk, gdk_window_get_state)
124 .WillOnce(::testing::Return(GDK_WINDOW_STATE_ICONIFIED));
125 gboolean called = TRUE;
126 fl_mock_binary_messenger_set_string_message_channel(
127 messenger, "flutter/lifecycle",
128 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
129 gpointer user_data) {
130 gboolean* called = static_cast<gboolean*>(user_data);
131 *called = TRUE;
132 EXPECT_STREQ(fl_value_get_string(message),
133 "AppLifecycleState.inactive");
134 return fl_value_new_string("");
135 },
136 &called);
137
138 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
139 gtk_widget_show(GTK_WIDGET(window));
140 g_autoptr(FlWindowStateMonitor) monitor =
141 fl_window_state_monitor_new(FL_BINARY_MESSENGER(messenger), window);
142
143 GdkEvent event = {
144 .window_state = {.new_window_state = static_cast<GdkWindowState>(0)}};
145 gboolean handled;
146 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
147 EXPECT_TRUE(called);
148
149 fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
150 }
151
152 TEST(FlWindowStateMonitorTest, LeaveIconifiedFocused) {
153 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
154 ::testing::NiceMock<flutter::testing::MockGtk> mock_gtk;
155
156 gtk_init(0, nullptr);
157
158 EXPECT_CALL(mock_gtk, gdk_window_get_state)
159 .WillOnce(::testing::Return(GDK_WINDOW_STATE_ICONIFIED));
160 gboolean called = TRUE;
161 fl_mock_binary_messenger_set_string_message_channel(
162 messenger, "flutter/lifecycle",
163 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
164 gpointer user_data) {
165 gboolean* called = static_cast<gboolean*>(user_data);
166 *called = TRUE;
167 EXPECT_STREQ(fl_value_get_string(message), "AppLifecycleState.resumed");
168 return fl_value_new_string("");
169 },
170 &called);
171
172 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
173 gtk_widget_show(GTK_WIDGET(window));
174 g_autoptr(FlWindowStateMonitor) monitor =
175 fl_window_state_monitor_new(FL_BINARY_MESSENGER(messenger), window);
176
177 GdkEvent event = {
178 .window_state = {.new_window_state = static_cast<GdkWindowState>(
179 GDK_WINDOW_STATE_FOCUSED)}};
180 gboolean handled;
181 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
182 EXPECT_TRUE(called);
183
184 fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
185 }
186
187 TEST(FlWindowStateMonitorTest, EnterWithdrawn) {
188 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
189 ::testing::NiceMock<flutter::testing::MockGtk> mock_gtk;
190
191 gtk_init(0, nullptr);
192
193 EXPECT_CALL(mock_gtk, gdk_window_get_state)
194 .WillOnce(::testing::Return(static_cast<GdkWindowState>(0)));
195 gboolean called = TRUE;
196 fl_mock_binary_messenger_set_string_message_channel(
197 messenger, "flutter/lifecycle",
198 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
199 gpointer user_data) {
200 gboolean* called = static_cast<gboolean*>(user_data);
201 *called = TRUE;
202 EXPECT_STREQ(fl_value_get_string(message), "AppLifecycleState.hidden");
203 return fl_value_new_string("");
204 },
205 &called);
206
207 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
208 gtk_widget_show(GTK_WIDGET(window));
209 g_autoptr(FlWindowStateMonitor) monitor =
210 fl_window_state_monitor_new(FL_BINARY_MESSENGER(messenger), window);
211
212 GdkEvent event = {
213 .window_state = {.new_window_state = GDK_WINDOW_STATE_WITHDRAWN}};
214 gboolean handled;
215 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
216 EXPECT_TRUE(called);
217
218 fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
219 }
220
221 TEST(FlWindowStateMonitorTest, LeaveWithdrawn) {
222 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
223 ::testing::NiceMock<flutter::testing::MockGtk> mock_gtk;
224
225 gtk_init(0, nullptr);
226
227 EXPECT_CALL(mock_gtk, gdk_window_get_state)
228 .WillOnce(::testing::Return(GDK_WINDOW_STATE_WITHDRAWN));
229 gboolean called = TRUE;
230 fl_mock_binary_messenger_set_string_message_channel(
231 messenger, "flutter/lifecycle",
232 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
233 gpointer user_data) {
234 gboolean* called = static_cast<gboolean*>(user_data);
235 *called = TRUE;
236 EXPECT_STREQ(fl_value_get_string(message),
237 "AppLifecycleState.inactive");
238 return fl_value_new_string("");
239 },
240 &called);
241
242 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
243 gtk_widget_show(GTK_WIDGET(window));
244 g_autoptr(FlWindowStateMonitor) monitor =
245 fl_window_state_monitor_new(FL_BINARY_MESSENGER(messenger), window);
246
247 GdkEvent event = {
248 .window_state = {.new_window_state = static_cast<GdkWindowState>(0)}};
249 gboolean handled;
250 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
251 EXPECT_TRUE(called);
252
253 fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
254 }
255
256 TEST(FlWindowStateMonitorTest, LeaveWithdrawnFocused) {
257 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
258 ::testing::NiceMock<flutter::testing::MockGtk> mock_gtk;
259
260 gtk_init(0, nullptr);
261
262 EXPECT_CALL(mock_gtk, gdk_window_get_state)
263 .WillOnce(::testing::Return(GDK_WINDOW_STATE_WITHDRAWN));
264 gboolean called = TRUE;
265 fl_mock_binary_messenger_set_string_message_channel(
266 messenger, "flutter/lifecycle",
267 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
268 gpointer user_data) {
269 gboolean* called = static_cast<gboolean*>(user_data);
270 *called = TRUE;
271 EXPECT_STREQ(fl_value_get_string(message), "AppLifecycleState.resumed");
272 return fl_value_new_string("");
273 },
274 &called);
275
276 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
277 gtk_widget_show(GTK_WIDGET(window));
278 g_autoptr(FlWindowStateMonitor) monitor =
279 fl_window_state_monitor_new(FL_BINARY_MESSENGER(messenger), window);
280
281 GdkEvent event = {
282 .window_state = {.new_window_state = static_cast<GdkWindowState>(
283 GDK_WINDOW_STATE_FOCUSED)}};
284 gboolean handled;
285 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
286 EXPECT_TRUE(called);
287
288 fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
289 }
window
return window
Definition: fl_application.cc:41
fl_binary_messenger_shutdown
void fl_binary_messenger_shutdown(FlBinaryMessenger *self)
Definition: fl_binary_messenger.cc:500
fl_binary_messenger_private.h
g_autoptr
g_autoptr(GMutexLocker) locker
TRUE
return TRUE
Definition: fl_compositor_software.cc:68
user_data
G_BEGIN_DECLS G_MODULE_EXPORT FlValue gpointer user_data
Definition: fl_event_channel.h:90
fl_string_codec.h
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
fl_value_get_string
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_window_state_monitor_new
FlWindowStateMonitor * fl_window_state_monitor_new(FlBinaryMessenger *messenger, GtkWindow *window)
Definition: fl_window_state_monitor.cc:104
fl_window_state_monitor.h
TEST
TEST(FlWindowStateMonitorTest, GainFocus)
Definition: fl_window_state_monitor_test.cc:13