| 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/public/flutter_linux/fl_string_codec.h"
7 #include "flutter/shell/platform/linux/testing/fl_test.h"
8 #include "flutter/shell/platform/linux/testing/mock_binary_messenger.h"
9 #include "flutter/shell/platform/linux/testing/mock_window.h"
10
11 #include "gtest/gtest.h"
12
13 // Matches if a FlValue is a the supplied string.
14 class FlValueStringMatcher {
15 public:
16 using is_gtest_matcher = void;
17
18 explicit FlValueStringMatcher(::testing::Matcher<std::string> value)
19 : value_(std::move(value)) {}
20
21 bool MatchAndExplain(GBytes* data,
22 ::testing::MatchResultListener* result_listener) const {
23 g_autoptr(FlStringCodec) codec = fl_string_codec_new();
24 g_autoptr(GError) error = nullptr;
25 g_autoptr(FlValue) value =
26 fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), data, &error);
27 if (value == nullptr) {
28 *result_listener << ::testing::PrintToString(error->message);
29 return false;
30 }
31 if (!value_.MatchAndExplain(fl_value_get_string(value), result_listener)) {
32 *result_listener << " where the value doesn't match: \"" << value << "\"";
33 return false;
34 }
35 return true;
36 }
37
38 void DescribeTo(std::ostream* os) const {
39 *os << "value ";
40 value_.DescribeTo(os);
41 }
42
43 void DescribeNegationTo(std::ostream* os) const {
44 *os << "value ";
45 value_.DescribeNegationTo(os);
46 }
47
48 private:
49 ::testing::Matcher<std::string> value_;
50 };
51
52 ::testing::Matcher<GBytes*> LifecycleString(const std::string& value) {
53 return FlValueStringMatcher(::testing::StrEq(value));
54 }
55
56 TEST(FlWindowStateMonitorTest, GainFocus) {
57 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
58 ::testing::NiceMock<flutter::testing::MockWindow> mock_window;
59
60 gtk_init(0, nullptr);
61
62 EXPECT_CALL(mock_window, gdk_window_get_state)
63 .WillOnce(::testing::Return(static_cast<GdkWindowState>(0)));
64 EXPECT_CALL(messenger, fl_binary_messenger_send_on_channel(
65 ::testing::Eq<FlBinaryMessenger*>(messenger),
66 ::testing::StrEq("flutter/lifecycle"),
67 LifecycleString("AppLifecycleState.resumed"),
68 ::testing::_, ::testing::_, ::testing::_));
69
70 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
71 gtk_widget_show(GTK_WIDGET(window));
72 g_autoptr(FlWindowStateMonitor) monitor =
73 fl_window_state_monitor_new(messenger, window);
74
75 GdkEvent event = {
76 .window_state = {.new_window_state = GDK_WINDOW_STATE_FOCUSED}};
77 gboolean handled;
78 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
79 }
80
81 TEST(FlWindowStateMonitorTest, LoseFocus) {
82 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
83 ::testing::NiceMock<flutter::testing::MockWindow> mock_window;
84
85 gtk_init(0, nullptr);
86
87 EXPECT_CALL(mock_window, gdk_window_get_state)
88 .WillOnce(::testing::Return(GDK_WINDOW_STATE_FOCUSED));
89 EXPECT_CALL(messenger, fl_binary_messenger_send_on_channel(
90 ::testing::Eq<FlBinaryMessenger*>(messenger),
91 ::testing::StrEq("flutter/lifecycle"),
92 LifecycleString("AppLifecycleState.inactive"),
93 ::testing::_, ::testing::_, ::testing::_));
94
95 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
96 gtk_widget_show(GTK_WIDGET(window));
97 g_autoptr(FlWindowStateMonitor) monitor =
98 fl_window_state_monitor_new(messenger, window);
99
100 GdkEvent event = {
101 .window_state = {.new_window_state = static_cast<GdkWindowState>(0)}};
102 gboolean handled;
103 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
104 }
105
106 TEST(FlWindowStateMonitorTest, EnterIconified) {
107 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
108 ::testing::NiceMock<flutter::testing::MockWindow> mock_window;
109
110 gtk_init(0, nullptr);
111
112 EXPECT_CALL(mock_window, gdk_window_get_state)
113 .WillOnce(::testing::Return(static_cast<GdkWindowState>(0)));
114 EXPECT_CALL(messenger, fl_binary_messenger_send_on_channel(
115 ::testing::Eq<FlBinaryMessenger*>(messenger),
116 ::testing::StrEq("flutter/lifecycle"),
117 LifecycleString("AppLifecycleState.hidden"),
118 ::testing::_, ::testing::_, ::testing::_));
119
120 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
121 gtk_widget_show(GTK_WIDGET(window));
122 g_autoptr(FlWindowStateMonitor) monitor =
123 fl_window_state_monitor_new(messenger, window);
124
125 GdkEvent event = {
126 .window_state = {.new_window_state = GDK_WINDOW_STATE_ICONIFIED}};
127 gboolean handled;
128 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
129 }
130
131 TEST(FlWindowStateMonitorTest, LeaveIconified) {
132 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
133 ::testing::NiceMock<flutter::testing::MockWindow> mock_window;
134
135 gtk_init(0, nullptr);
136
137 EXPECT_CALL(mock_window, gdk_window_get_state)
138 .WillOnce(::testing::Return(GDK_WINDOW_STATE_ICONIFIED));
139 EXPECT_CALL(messenger, fl_binary_messenger_send_on_channel(
140 ::testing::Eq<FlBinaryMessenger*>(messenger),
141 ::testing::StrEq("flutter/lifecycle"),
142 LifecycleString("AppLifecycleState.inactive"),
143 ::testing::_, ::testing::_, ::testing::_));
144
145 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
146 gtk_widget_show(GTK_WIDGET(window));
147 g_autoptr(FlWindowStateMonitor) monitor =
148 fl_window_state_monitor_new(messenger, window);
149
150 GdkEvent event = {
151 .window_state = {.new_window_state = static_cast<GdkWindowState>(0)}};
152 gboolean handled;
153 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
154 }
155
156 TEST(FlWindowStateMonitorTest, LeaveIconifiedFocused) {
157 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
158 ::testing::NiceMock<flutter::testing::MockWindow> mock_window;
159
160 gtk_init(0, nullptr);
161
162 EXPECT_CALL(mock_window, gdk_window_get_state)
163 .WillOnce(::testing::Return(GDK_WINDOW_STATE_ICONIFIED));
164 EXPECT_CALL(messenger, fl_binary_messenger_send_on_channel(
165 ::testing::Eq<FlBinaryMessenger*>(messenger),
166 ::testing::StrEq("flutter/lifecycle"),
167 LifecycleString("AppLifecycleState.resumed"),
168 ::testing::_, ::testing::_, ::testing::_));
169
170 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
171 gtk_widget_show(GTK_WIDGET(window));
172 g_autoptr(FlWindowStateMonitor) monitor =
173 fl_window_state_monitor_new(messenger, window);
174
175 GdkEvent event = {
176 .window_state = {.new_window_state = static_cast<GdkWindowState>(
177 GDK_WINDOW_STATE_FOCUSED)}};
178 gboolean handled;
179 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
180 }
181
182 TEST(FlWindowStateMonitorTest, EnterWithdrawn) {
183 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
184 ::testing::NiceMock<flutter::testing::MockWindow> mock_window;
185
186 gtk_init(0, nullptr);
187
188 EXPECT_CALL(mock_window, gdk_window_get_state)
189 .WillOnce(::testing::Return(static_cast<GdkWindowState>(0)));
190 EXPECT_CALL(messenger, fl_binary_messenger_send_on_channel(
191 ::testing::Eq<FlBinaryMessenger*>(messenger),
192 ::testing::StrEq("flutter/lifecycle"),
193 LifecycleString("AppLifecycleState.hidden"),
194 ::testing::_, ::testing::_, ::testing::_));
195
196 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
197 gtk_widget_show(GTK_WIDGET(window));
198 g_autoptr(FlWindowStateMonitor) monitor =
199 fl_window_state_monitor_new(messenger, window);
200
201 GdkEvent event = {
202 .window_state = {.new_window_state = GDK_WINDOW_STATE_WITHDRAWN}};
203 gboolean handled;
204 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
205 }
206
207 TEST(FlWindowStateMonitorTest, LeaveWithdrawn) {
208 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
209 ::testing::NiceMock<flutter::testing::MockWindow> mock_window;
210
211 gtk_init(0, nullptr);
212
213 EXPECT_CALL(mock_window, gdk_window_get_state)
214 .WillOnce(::testing::Return(GDK_WINDOW_STATE_WITHDRAWN));
215 EXPECT_CALL(messenger, fl_binary_messenger_send_on_channel(
216 ::testing::Eq<FlBinaryMessenger*>(messenger),
217 ::testing::StrEq("flutter/lifecycle"),
218 LifecycleString("AppLifecycleState.inactive"),
219 ::testing::_, ::testing::_, ::testing::_));
220
221 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
222 gtk_widget_show(GTK_WIDGET(window));
223 g_autoptr(FlWindowStateMonitor) monitor =
224 fl_window_state_monitor_new(messenger, window);
225
226 GdkEvent event = {
227 .window_state = {.new_window_state = static_cast<GdkWindowState>(0)}};
228 gboolean handled;
229 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
230 }
231
232 TEST(FlWindowStateMonitorTest, LeaveWithdrawnFocused) {
233 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
234 ::testing::NiceMock<flutter::testing::MockWindow> mock_window;
235
236 gtk_init(0, nullptr);
237
238 EXPECT_CALL(mock_window, gdk_window_get_state)
239 .WillOnce(::testing::Return(GDK_WINDOW_STATE_WITHDRAWN));
240 EXPECT_CALL(messenger, fl_binary_messenger_send_on_channel(
241 ::testing::Eq<FlBinaryMessenger*>(messenger),
242 ::testing::StrEq("flutter/lifecycle"),
243 LifecycleString("AppLifecycleState.resumed"),
244 ::testing::_, ::testing::_, ::testing::_));
245
246 GtkWindow* window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
247 gtk_widget_show(GTK_WIDGET(window));
248 g_autoptr(FlWindowStateMonitor) monitor =
249 fl_window_state_monitor_new(messenger, window);
250
251 GdkEvent event = {
252 .window_state = {.new_window_state = static_cast<GdkWindowState>(
253 GDK_WINDOW_STATE_FOCUSED)}};
254 gboolean handled;
255 g_signal_emit_by_name(window, "window-state-event", &event, &handled);
256 }
event
FlKeyEvent * event
Definition: fl_key_channel_responder.cc:118
FlValueStringMatcher::DescribeTo
void DescribeTo(std::ostream *os) const
Definition: fl_window_state_monitor_test.cc:38
FlValueStringMatcher::MatchAndExplain
bool MatchAndExplain(GBytes *data, ::testing::MatchResultListener *result_listener) const
Definition: fl_window_state_monitor_test.cc:21
FlValueStringMatcher::DescribeNegationTo
void DescribeNegationTo(std::ostream *os) const
Definition: fl_window_state_monitor_test.cc:43
fl_string_codec.h
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_value_get_string
const G_MODULE_EXPORT gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
LifecycleString
::testing::Matcher< GBytes * > LifecycleString(const std::string &value)
Definition: fl_window_state_monitor_test.cc:52
fl_message_codec_decode_message
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
Definition: fl_message_codec.cc:33
TEST
TEST(FlWindowStateMonitorTest, GainFocus)
Definition: fl_window_state_monitor_test.cc:56
FlValueStringMatcher
Definition: fl_window_state_monitor_test.cc:14
fl_window_state_monitor_new
FlWindowStateMonitor * fl_window_state_monitor_new(FlBinaryMessenger *messenger, GtkWindow *window)
Definition: fl_window_state_monitor.cc:104
FlValueStringMatcher::is_gtest_matcher
void is_gtest_matcher
Definition: fl_window_state_monitor_test.cc:16
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
fl_window_state_monitor.h
fl_string_codec_new
G_MODULE_EXPORT FlStringCodec * fl_string_codec_new()
Definition: fl_string_codec.cc:53
FlValueStringMatcher::FlValueStringMatcher
FlValueStringMatcher(::testing::Matcher< std::string > value)
Definition: fl_window_state_monitor_test.cc:18
value
uint8_t value
Definition: fl_standard_message_codec.cc:36
fl_binary_messenger_send_on_channel
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_binary_messenger.cc:443