6 #include "gtest/gtest.h"
14 #include "flutter/shell/platform/linux/testing/fl_test.h"
15 #include "flutter/shell/platform/linux/testing/mock_renderer.h"
22 g_autoptr(GError)
error =
nullptr;
25 EXPECT_NE(response,
nullptr);
26 EXPECT_EQ(
error,
nullptr);
29 EXPECT_NE(r,
nullptr);
30 EXPECT_EQ(
error,
nullptr);
35 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
39 TEST(FlMethodChannelTest, InvokeMethod) {
40 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
46 messenger,
"test/standard-method", FL_METHOD_CODEC(codec));
53 g_main_loop_run(loop);
61 g_autoptr(GError)
error =
nullptr;
64 EXPECT_NE(response,
nullptr);
65 EXPECT_EQ(
error,
nullptr);
68 EXPECT_NE(r,
nullptr);
69 EXPECT_EQ(
error,
nullptr);
72 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
76 TEST(FlMethodChannelTest, InvokeMethodNullptrArgsMessage) {
77 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
83 messenger,
"test/standard-method", FL_METHOD_CODEC(codec));
89 g_main_loop_run(loop);
97 g_autoptr(GError)
error =
nullptr;
100 EXPECT_NE(response,
nullptr);
101 EXPECT_EQ(
error,
nullptr);
103 EXPECT_TRUE(FL_IS_METHOD_ERROR_RESPONSE(response));
112 EXPECT_NE(details,
nullptr);
116 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
120 TEST(FlMethodChannelTest, InvokeMethodError) {
121 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
127 messenger,
"test/standard-method", FL_METHOD_CODEC(codec));
137 g_main_loop_run(loop);
145 g_autoptr(GError)
error =
nullptr;
148 EXPECT_NE(response,
nullptr);
149 EXPECT_EQ(
error,
nullptr);
151 EXPECT_TRUE(FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response));
153 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
157 TEST(FlMethodChannelTest, InvokeMethodNotImplemented) {
158 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
164 messenger,
"test/standard-method", FL_METHOD_CODEC(codec));
170 g_main_loop_run(loop);
178 g_autoptr(GError)
error =
nullptr;
181 EXPECT_EQ(response,
nullptr);
182 EXPECT_NE(
error,
nullptr);
184 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
188 TEST(FlMethodChannelTest, InvokeMethodFailure) {
189 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
194 g_autoptr(FlMethodChannel) channel =
201 g_main_loop_run(loop);
216 g_autoptr(GError)
error =
nullptr;
218 EXPECT_EQ(
error,
nullptr);
224 FlBinaryMessenger* messenger,
225 const gchar* channel,
227 FlBinaryMessengerResponseHandle* response_handle,
230 g_autoptr(GError)
error =
nullptr;
231 g_autoptr(FlMethodResponse) response =
233 EXPECT_NE(response,
nullptr);
234 EXPECT_EQ(
error,
nullptr);
236 EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
238 FL_METHOD_SUCCESS_RESPONSE(response));
245 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
249 TEST(FlMethodChannelTest, ReceiveMethodCallRespondSuccess) {
250 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
256 messenger,
"test/standard-method", FL_METHOD_CODEC(codec));
274 g_main_loop_run(loop);
289 g_autoptr(GError)
error =
nullptr;
292 EXPECT_EQ(
error,
nullptr);
298 FlBinaryMessenger* messenger,
299 const gchar* channel,
301 FlBinaryMessengerResponseHandle* response_handle,
304 g_autoptr(GError)
error =
nullptr;
305 g_autoptr(FlMethodResponse) response =
307 EXPECT_NE(response,
nullptr);
308 EXPECT_EQ(
error,
nullptr);
310 EXPECT_TRUE(FL_IS_METHOD_ERROR_RESPONSE(response));
325 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
329 TEST(FlMethodChannelTest, ReceiveMethodCallRespondError) {
330 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
336 messenger,
"test/standard-method", FL_METHOD_CODEC(codec));
354 g_main_loop_run(loop);
368 g_autoptr(GError)
error =
nullptr;
370 EXPECT_EQ(
error,
nullptr);
376 FlBinaryMessenger* messenger,
377 const gchar* channel,
379 FlBinaryMessengerResponseHandle* response_handle,
382 g_autoptr(GError)
error =
nullptr;
383 g_autoptr(FlMethodResponse) response =
385 EXPECT_NE(response,
nullptr);
386 EXPECT_EQ(
error,
nullptr);
388 EXPECT_TRUE(FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response));
393 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
397 TEST(FlMethodChannelTest, ReceiveMethodCallRespondNotImplemented) {
398 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
404 messenger,
"test/standard-method", FL_METHOD_CODEC(codec));
422 g_main_loop_run(loop);
432 struct _TestMethodCodec {
433 FlMethodCodec parent_instance;
435 FlStandardMethodCodec* wrapped_codec;
438 G_DEFINE_TYPE(TestMethodCodec, test_method_codec, fl_method_codec_get_type())
441 TestMethodCodec*
self = TEST_METHOD_CODEC(
object);
443 g_clear_object(&self->wrapped_codec);
445 G_OBJECT_CLASS(test_method_codec_parent_class)->dispose(
object);
453 EXPECT_TRUE(TEST_IS_METHOD_CODEC(codec));
454 TestMethodCodec*
self = TEST_METHOD_CODEC(codec);
456 FL_METHOD_CODEC(self->wrapped_codec), name,
args,
error);
465 EXPECT_TRUE(TEST_IS_METHOD_CODEC(codec));
466 TestMethodCodec*
self = TEST_METHOD_CODEC(codec);
468 FL_METHOD_CODEC(self->wrapped_codec), message, name,
args,
error);
483 const gchar* message,
495 EXPECT_TRUE(TEST_IS_METHOD_CODEC(codec));
496 TestMethodCodec*
self = TEST_METHOD_CODEC(codec);
503 FL_METHOD_CODEC_CLASS(klass)->encode_method_call =
505 FL_METHOD_CODEC_CLASS(klass)->decode_method_call =
507 FL_METHOD_CODEC_CLASS(klass)->encode_success_envelope =
509 FL_METHOD_CODEC_CLASS(klass)->encode_error_envelope =
511 FL_METHOD_CODEC_CLASS(klass)->decode_response =
520 return TEST_METHOD_CODEC(g_object_new(test_method_codec_get_type(),
nullptr));
529 g_autoptr(GError) response_error =
nullptr;
532 EXPECT_NE(response_error,
nullptr);
537 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
542 TEST(FlMethodChannelTest, ReceiveMethodCallRespondSuccessError) {
543 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
549 messenger,
"test/standard-method", FL_METHOD_CODEC(codec));
562 g_main_loop_run(loop);
571 g_autoptr(GError) response_error =
nullptr;
573 details, &response_error));
574 EXPECT_NE(response_error,
nullptr);
579 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
584 TEST(FlMethodChannelTest, ReceiveMethodCallRespondErrorError) {
585 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
591 messenger,
"test/standard-method", FL_METHOD_CODEC(codec));
604 g_main_loop_run(loop);
616 gpointer raw_user_data) {
622 g_autoptr(GError)
error =
nullptr;
624 EXPECT_EQ(
error,
nullptr);
636 TEST(FlMethodChannelTest, ReplaceADisposedMethodChannel) {
637 const char* method_name =
"test/standard-method";
640 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
655 FlMethodChannel* channel1 =
658 &user_data1,
nullptr);
662 g_main_loop_run(loop);
663 EXPECT_EQ(user_data1.count, 101);
666 g_object_unref(channel1);
673 g_autoptr(FlMethodChannel) channel2 =
676 &user_data2,
nullptr);
680 g_main_loop_run(loop);
682 EXPECT_EQ(user_data1.count, 101);
683 EXPECT_EQ(user_data2.count, 101);
693 TEST(FlMethodChannelTest, DisposeAReplacedMethodChannel) {
694 const char* method_name =
"test/standard-method";
697 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
712 FlMethodChannel* channel1 =
715 &user_data1,
nullptr);
719 g_main_loop_run(loop);
720 EXPECT_EQ(user_data1.count, 101);
727 g_autoptr(FlMethodChannel) channel2 =
730 &user_data2,
nullptr);
734 g_main_loop_run(loop);
735 EXPECT_EQ(user_data1.count, 101);
736 EXPECT_EQ(user_data2.count, 101);
739 g_object_unref(channel1);
743 g_main_loop_run(loop);
744 EXPECT_EQ(user_data1.count, 101);
745 EXPECT_EQ(user_data2.count, 102);