| Flutter Linux Embedder |

fl_event_channel_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 // Included first as it collides with the X11 headers.
6 #include "gtest/gtest.h"
7
8 #include "flutter/shell/platform/linux/public/flutter_linux/fl_event_channel.h"
9 #include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
10 #include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"
11 #include "flutter/shell/platform/linux/testing/fl_mock_binary_messenger.h"
12
13 // Checks we detect a listen event.
14 TEST(FlEventChannelTest, Listen) {
15 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
16 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
17 g_autoptr(FlEventChannel) channel = fl_event_channel_new(
18 FL_BINARY_MESSENGER(messenger), "test", FL_METHOD_CODEC(codec));
19 fl_event_channel_set_stream_handlers(
20 channel,
21 [](FlEventChannel* channel, FlValue* args, gpointer user_data) {
22 EXPECT_EQ(fl_value_get_type(args), FL_VALUE_TYPE_NULL);
23
24 return static_cast<FlMethodErrorResponse*>(nullptr);
25 },
26 nullptr, nullptr, nullptr);
27
28 gboolean called = FALSE;
29 fl_mock_binary_messenger_invoke_standard_method(
30 messenger, "test", "listen", nullptr,
31 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
32 gpointer user_data) {
33 gboolean* called = static_cast<gboolean*>(user_data);
34 *called = TRUE;
35
36 EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
37 FlValue* result = fl_method_success_response_get_result(
38 FL_METHOD_SUCCESS_RESPONSE(response));
39 EXPECT_EQ(fl_value_get_type(result), FL_VALUE_TYPE_NULL);
40 },
41 &called);
42 EXPECT_TRUE(called);
43 }
44
45 // Checks we can generate a listen exception.
46 TEST(FlEventChannelTest, ListenException) {
47 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
48 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
49 g_autoptr(FlEventChannel) channel = fl_event_channel_new(
50 FL_BINARY_MESSENGER(messenger), "test", FL_METHOD_CODEC(codec));
51 fl_event_channel_set_stream_handlers(
52 channel,
53 [](FlEventChannel* channel, FlValue* args, gpointer user_data) {
54 return fl_method_error_response_new("LISTEN-ERROR",
55 "LISTEN-ERROR-MESSAGE", nullptr);
56 },
57 nullptr, nullptr, nullptr);
58
59 gboolean called = FALSE;
60 fl_mock_binary_messenger_invoke_standard_method(
61 messenger, "test", "listen", nullptr,
62 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
63 gpointer user_data) {
64 gboolean* called = static_cast<gboolean*>(user_data);
65 *called = TRUE;
66
67 EXPECT_TRUE(FL_IS_METHOD_ERROR_RESPONSE(response));
68 EXPECT_STREQ(fl_method_error_response_get_code(
69 FL_METHOD_ERROR_RESPONSE(response)),
70 "LISTEN-ERROR");
71 EXPECT_STREQ(fl_method_error_response_get_message(
72 FL_METHOD_ERROR_RESPONSE(response)),
73 "LISTEN-ERROR-MESSAGE");
74 },
75 &called);
76 EXPECT_TRUE(called);
77 }
78
79 // Checks we detect a cancel event.
80 TEST(FlEventChannelTest, Cancel) {
81 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
82 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
83 g_autoptr(FlEventChannel) channel = fl_event_channel_new(
84 FL_BINARY_MESSENGER(messenger), "test", FL_METHOD_CODEC(codec));
85 fl_event_channel_set_stream_handlers(
86 channel, nullptr,
87 [](FlEventChannel* channel, FlValue* args, gpointer user_data) {
88 EXPECT_EQ(fl_value_get_type(args), FL_VALUE_TYPE_NULL);
89
90 return static_cast<FlMethodErrorResponse*>(nullptr);
91 },
92 nullptr, nullptr);
93
94 fl_mock_binary_messenger_invoke_standard_method(
95 messenger, "test", "listen", nullptr,
96 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
97 gpointer user_data) {},
98 nullptr);
99 gboolean called = FALSE;
100 fl_mock_binary_messenger_invoke_standard_method(
101 messenger, "test", "cancel", nullptr,
102 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
103 gpointer user_data) {
104 gboolean* called = static_cast<gboolean*>(user_data);
105 *called = TRUE;
106
107 EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
108 FlValue* result = fl_method_success_response_get_result(
109 FL_METHOD_SUCCESS_RESPONSE(response));
110 EXPECT_EQ(fl_value_get_type(result), FL_VALUE_TYPE_NULL);
111 },
112 &called);
113 EXPECT_TRUE(called);
114 }
115
116 // Checks we can generate a cancel exception.
117 TEST(FlEventChannelTest, CancelException) {
118 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
119 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
120 g_autoptr(FlEventChannel) channel = fl_event_channel_new(
121 FL_BINARY_MESSENGER(messenger), "test", FL_METHOD_CODEC(codec));
122 fl_event_channel_set_stream_handlers(
123 channel, nullptr,
124 [](FlEventChannel* channel, FlValue* args, gpointer user_data) {
125 return fl_method_error_response_new("CANCEL-ERROR",
126 "CANCEL-ERROR-MESSAGE", nullptr);
127 },
128 nullptr, nullptr);
129
130 fl_mock_binary_messenger_invoke_standard_method(
131 messenger, "test", "listen", nullptr,
132 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
133 gpointer user_data) {},
134 nullptr);
135 gboolean called = FALSE;
136 fl_mock_binary_messenger_invoke_standard_method(
137 messenger, "test", "cancel", nullptr,
138 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
139 gpointer user_data) {
140 gboolean* called = static_cast<gboolean*>(user_data);
141 *called = TRUE;
142
143 EXPECT_TRUE(FL_IS_METHOD_ERROR_RESPONSE(response));
144 EXPECT_STREQ(fl_method_error_response_get_code(
145 FL_METHOD_ERROR_RESPONSE(response)),
146 "CANCEL-ERROR");
147 EXPECT_STREQ(fl_method_error_response_get_message(
148 FL_METHOD_ERROR_RESPONSE(response)),
149 "CANCEL-ERROR-MESSAGE");
150 },
151 &called);
152 EXPECT_TRUE(called);
153 }
154
155 // Checks args are passed to listen/cancel.
156 TEST(FlEventChannelTest, Args) {
157 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
158 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
159 g_autoptr(FlEventChannel) channel = fl_event_channel_new(
160 FL_BINARY_MESSENGER(messenger), "test", FL_METHOD_CODEC(codec));
161 int call_count = 0;
162 fl_event_channel_set_stream_handlers(
163 channel,
164 [](FlEventChannel* channel, FlValue* args, gpointer user_data) {
165 int* call_count = static_cast<int*>(user_data);
166 EXPECT_EQ(*call_count, 0);
167 (*call_count)++;
168
169 g_autoptr(FlValue) expected_args = fl_value_new_string("LISTEN-ARGS");
170 EXPECT_TRUE(fl_value_equal(args, expected_args));
171
172 return static_cast<FlMethodErrorResponse*>(nullptr);
173 },
174 [](FlEventChannel* channel, FlValue* args, gpointer user_data) {
175 int* call_count = static_cast<int*>(user_data);
176 EXPECT_EQ(*call_count, 1);
177 (*call_count)++;
178
179 g_autoptr(FlValue) expected_args = fl_value_new_string("CANCEL-ARGS");
180 EXPECT_TRUE(fl_value_equal(args, expected_args));
181
182 return static_cast<FlMethodErrorResponse*>(nullptr);
183 },
184 &call_count, nullptr);
185
186 g_autoptr(FlValue) listen_args = fl_value_new_string("LISTEN-ARGS");
187 fl_mock_binary_messenger_invoke_standard_method(
188 messenger, "test", "listen", listen_args,
189 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
190 gpointer user_data) {},
191 nullptr);
192 g_autoptr(FlValue) cancel_args = fl_value_new_string("CANCEL-ARGS");
193 fl_mock_binary_messenger_invoke_standard_method(
194 messenger, "test", "cancel", cancel_args,
195 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
196 gpointer user_data) {},
197 nullptr);
198
199 EXPECT_EQ(call_count, 2);
200 }
201
202 // Checks can send events.
203 TEST(FlEventChannelTest, SendEvents) {
204 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
205 int event_count = 0;
206 fl_mock_binary_messenger_set_standard_event_channel(
207 messenger, "test",
208 [](FlMockBinaryMessenger* messenger, FlValue* event, gpointer user_data) {
209 int* event_count = static_cast<int*>(user_data);
210
211 EXPECT_EQ(fl_value_get_type(event), FL_VALUE_TYPE_INT);
212 EXPECT_EQ(fl_value_get_int(event), *event_count);
213
214 (*event_count)++;
215 },
216 [](FlMockBinaryMessenger* messenger, const gchar* code,
217 const gchar* message, FlValue* details, gpointer user_data) {},
218 &event_count);
219
220 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
221 g_autoptr(FlEventChannel) channel = fl_event_channel_new(
222 FL_BINARY_MESSENGER(messenger), "test", FL_METHOD_CODEC(codec));
223 fl_event_channel_set_stream_handlers(
224 channel,
225 [](FlEventChannel* channel, FlValue* args, gpointer user_data) {
226 // Send some events.
227 for (int i = 0; i < 5; i++) {
228 g_autoptr(FlValue) event = fl_value_new_int(i);
229 g_autoptr(GError) error = nullptr;
230 EXPECT_TRUE(fl_event_channel_send(channel, event, nullptr, &error));
231 EXPECT_EQ(error, nullptr);
232 }
233
234 return static_cast<FlMethodErrorResponse*>(nullptr);
235 },
236 nullptr, nullptr, nullptr);
237
238 fl_mock_binary_messenger_invoke_standard_method(
239 messenger, "test", "listen", nullptr,
240 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
241 gpointer user_data) {},
242 nullptr);
243 fl_mock_binary_messenger_invoke_standard_method(
244 messenger, "test", "cancel", nullptr,
245 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
246 gpointer user_data) {},
247 nullptr);
248
249 EXPECT_EQ(event_count, 5);
250 }
251
252 // Check can register an event channel with the same name as one previously
253 // used.
254 TEST(FlEventChannelTest, ReuseChannel) {
255 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
256 int event_count = 0;
257 fl_mock_binary_messenger_set_standard_event_channel(
258 messenger, "test",
259 [](FlMockBinaryMessenger* messenger, FlValue* event, gpointer user_data) {
260 int* event_count = static_cast<int*>(user_data);
261
262 EXPECT_EQ(fl_value_get_type(event), FL_VALUE_TYPE_INT);
263 EXPECT_EQ(fl_value_get_int(event), *event_count);
264
265 (*event_count)++;
266 },
267 [](FlMockBinaryMessenger* messenger, const gchar* code,
268 const gchar* message, FlValue* details, gpointer user_data) {},
269 &event_count);
270
271 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
272 FlEventChannel* channel1 = fl_event_channel_new(
273 FL_BINARY_MESSENGER(messenger), "test", FL_METHOD_CODEC(codec));
274 fl_event_channel_set_stream_handlers(
275 channel1,
276 [](FlEventChannel* channel, FlValue* args, gpointer user_data) {
277 // Send some events.
278 for (int i = 0; i < 5; i++) {
279 g_autoptr(FlValue) event = fl_value_new_int(100 + i);
280 g_autoptr(GError) error = nullptr;
281 EXPECT_TRUE(fl_event_channel_send(channel, event, nullptr, &error));
282 EXPECT_EQ(error, nullptr);
283 }
284
285 return static_cast<FlMethodErrorResponse*>(nullptr);
286 },
287 nullptr, nullptr, nullptr);
288
289 // Remove this channel
290 g_object_unref(channel1);
291
292 // Register a second channel with the same name.
293 g_autoptr(FlEventChannel) channel2 = fl_event_channel_new(
294 FL_BINARY_MESSENGER(messenger), "test", FL_METHOD_CODEC(codec));
295 fl_event_channel_set_stream_handlers(
296 channel2,
297 [](FlEventChannel* channel, FlValue* args, gpointer user_data) {
298 // Send some events.
299 for (int i = 0; i < 5; i++) {
300 g_autoptr(FlValue) event = fl_value_new_int(i);
301 g_autoptr(GError) error = nullptr;
302 EXPECT_TRUE(fl_event_channel_send(channel, event, nullptr, &error));
303 EXPECT_EQ(error, nullptr);
304 }
305
306 return static_cast<FlMethodErrorResponse*>(nullptr);
307 },
308 nullptr, nullptr, nullptr);
309
310 fl_mock_binary_messenger_invoke_standard_method(
311 messenger, "test", "listen", nullptr,
312 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
313 gpointer user_data) {},
314 nullptr);
315 fl_mock_binary_messenger_invoke_standard_method(
316 messenger, "test", "cancel", nullptr,
317 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
318 gpointer user_data) {},
319 nullptr);
320
321 EXPECT_EQ(event_count, 5);
322 }
323
324 // Check can register an event channel replacing an existing one.
325 TEST(FlEventChannelTest, ReplaceChannel) {
326 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
327 int event_count = 0;
328 fl_mock_binary_messenger_set_standard_event_channel(
329 messenger, "test",
330 [](FlMockBinaryMessenger* messenger, FlValue* event, gpointer user_data) {
331 int* event_count = static_cast<int*>(user_data);
332
333 EXPECT_EQ(fl_value_get_type(event), FL_VALUE_TYPE_INT);
334 EXPECT_EQ(fl_value_get_int(event), *event_count);
335
336 (*event_count)++;
337 },
338 [](FlMockBinaryMessenger* messenger, const gchar* code,
339 const gchar* message, FlValue* details, gpointer user_data) {},
340 &event_count);
341
342 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
343 FlEventChannel* channel1 = fl_event_channel_new(
344 FL_BINARY_MESSENGER(messenger), "test", FL_METHOD_CODEC(codec));
345 fl_event_channel_set_stream_handlers(
346 channel1,
347 [](FlEventChannel* channel, FlValue* args, gpointer user_data) {
348 // Send some events.
349 for (int i = 0; i < 5; i++) {
350 g_autoptr(FlValue) event = fl_value_new_int(100 + i);
351 g_autoptr(GError) error = nullptr;
352 EXPECT_TRUE(fl_event_channel_send(channel, event, nullptr, &error));
353 EXPECT_EQ(error, nullptr);
354 }
355
356 return static_cast<FlMethodErrorResponse*>(nullptr);
357 },
358 nullptr, nullptr, nullptr);
359
360 // Register a second channel with the same name.
361 g_autoptr(FlEventChannel) channel2 = fl_event_channel_new(
362 FL_BINARY_MESSENGER(messenger), "test", FL_METHOD_CODEC(codec));
363 fl_event_channel_set_stream_handlers(
364 channel2,
365 [](FlEventChannel* channel, FlValue* args, gpointer user_data) {
366 // Send some events.
367 for (int i = 0; i < 5; i++) {
368 g_autoptr(FlValue) event = fl_value_new_int(i);
369 g_autoptr(GError) error = nullptr;
370 EXPECT_TRUE(fl_event_channel_send(channel, event, nullptr, &error));
371 EXPECT_EQ(error, nullptr);
372 }
373
374 return static_cast<FlMethodErrorResponse*>(nullptr);
375 },
376 nullptr, nullptr, nullptr);
377
378 fl_mock_binary_messenger_invoke_standard_method(
379 messenger, "test", "listen", nullptr,
380 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
381 gpointer user_data) {},
382 nullptr);
383 fl_mock_binary_messenger_invoke_standard_method(
384 messenger, "test", "cancel", nullptr,
385 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
386 gpointer user_data) {},
387 nullptr);
388
389 EXPECT_EQ(event_count, 5);
390 }
fl_method_error_response_new
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)
Definition: fl_method_response.cc:144
fl_standard_method_codec_new
G_MODULE_EXPORT FlStandardMethodCodec * fl_standard_method_codec_new()
Definition: fl_standard_method_codec.cc:291
fl_method_channel.h
i
int i
Definition: fl_socket_accessible.cc:18
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_value_new_int
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:262
fl_event_channel_send
G_MODULE_EXPORT gboolean fl_event_channel_send(FlEventChannel *self, FlValue *event, GCancellable *cancellable, GError **error)
Definition: fl_event_channel.cc:196
fl_method_error_response_get_message
const G_MODULE_EXPORT gchar * fl_method_error_response_get_message(FlMethodErrorResponse *self)
Definition: fl_method_response.cc:166
FL_VALUE_TYPE_NULL
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:64
fl_value_get_int
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:668
user_data
G_BEGIN_DECLS G_MODULE_EXPORT FlValue gpointer user_data
Definition: fl_event_channel.h:90
TEST
TEST(FlEventChannelTest, Listen)
Definition: fl_event_channel_test.cc:14
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
fl_event_channel_new
G_MODULE_EXPORT FlEventChannel * fl_event_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
Definition: fl_event_channel.cc:159
fl_method_success_response_get_result
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
Definition: fl_method_response.cc:138
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_event_channel.h
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:471
fl_standard_method_codec.h
FL_VALUE_TYPE_INT
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:66
fl_event_channel_set_stream_handlers
G_MODULE_EXPORT void fl_event_channel_set_stream_handlers(FlEventChannel *self, FlEventChannelHandler listen_handler, FlEventChannelHandler cancel_handler, gpointer user_data, GDestroyNotify destroy_notify)
Definition: fl_event_channel.cc:181
fl_method_error_response_get_code
const G_MODULE_EXPORT gchar * fl_method_error_response_get_code(FlMethodErrorResponse *self)
Definition: fl_method_response.cc:160
args
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
Definition: fl_event_channel.h:89
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276