6#include "gtest/gtest.h"
14TEST(FlMethodChannelTest, InvokeMethod) {
15 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
20 [](FlMockBinaryMessenger* messenger, GTask* task,
const gchar*
name,
22 EXPECT_STREQ(
name,
"Test");
32 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
37 [](GObject*
object, GAsyncResult* result, gpointer
user_data) {
42 EXPECT_NE(response,
nullptr);
43 EXPECT_EQ(
error,
nullptr);
46 EXPECT_NE(r,
nullptr);
47 EXPECT_EQ(
error,
nullptr);
52 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
56 g_main_loop_run(loop);
60TEST(FlMethodChannelTest, InvokeMethodNullptrArgsMessage) {
61 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
66 [](FlMockBinaryMessenger* messenger, GTask* task,
const gchar*
name,
68 EXPECT_STREQ(
name,
"Test");
76 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
79 channel,
"Test",
nullptr,
nullptr,
80 [](GObject*
object, GAsyncResult* result, gpointer
user_data) {
85 EXPECT_NE(response,
nullptr);
86 EXPECT_EQ(
error,
nullptr);
89 EXPECT_NE(r,
nullptr);
90 EXPECT_EQ(
error,
nullptr);
93 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
97 g_main_loop_run(loop);
101TEST(FlMethodChannelTest, InvokeMethodError) {
102 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
107 [](FlMockBinaryMessenger* messenger, GTask* task,
const gchar*
name,
109 EXPECT_STREQ(
name,
"Test");
111 return FL_METHOD_RESPONSE(
118 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
121 channel,
"Test",
nullptr,
nullptr,
122 [](GObject*
object, GAsyncResult* result, gpointer
user_data) {
127 EXPECT_NE(response,
nullptr);
128 EXPECT_EQ(
error,
nullptr);
130 EXPECT_TRUE(FL_IS_METHOD_ERROR_RESPONSE(response));
132 FL_METHOD_ERROR_RESPONSE(response)),
135 FL_METHOD_ERROR_RESPONSE(response)),
138 FL_METHOD_ERROR_RESPONSE(response));
139 EXPECT_NE(details,
nullptr);
143 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
147 g_main_loop_run(loop);
151TEST(FlMethodChannelTest, InvokeMethodNotImplemented) {
152 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
157 [](FlMockBinaryMessenger* messenger, GTask* task,
const gchar*
name,
159 EXPECT_STREQ(
name,
"Test");
166 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
169 channel,
"Test",
nullptr,
nullptr,
170 [](GObject*
object, GAsyncResult* result, gpointer
user_data) {
175 EXPECT_NE(response,
nullptr);
176 EXPECT_EQ(
error,
nullptr);
178 EXPECT_TRUE(FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response));
180 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
184 g_main_loop_run(loop);
188TEST(FlMethodChannelTest, InvokeMethodFailure) {
189 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
196 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
199 channel,
"Test",
nullptr,
nullptr,
200 [](GObject*
object, GAsyncResult* result, gpointer
user_data) {
205 EXPECT_EQ(response,
nullptr);
206 EXPECT_NE(
error,
nullptr);
208 EXPECT_EQ(
error->code, 42);
209 EXPECT_STREQ(
error->message,
"ERROR");
211 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
215 g_main_loop_run(loop);
219TEST(FlMethodChannelTest, ReceiveMethodCallRespondSuccess) {
224 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
239 EXPECT_EQ(
error,
nullptr);
245 gboolean called = FALSE;
247 messenger,
"test",
"Test",
args,
248 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
250 gboolean* called =
static_cast<gboolean*
>(
user_data);
253 EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
255 FL_METHOD_SUCCESS_RESPONSE(response));
264TEST(FlMethodChannelTest, ReceiveMethodCallRespondError) {
269 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
284 EXPECT_EQ(
error,
nullptr);
290 gboolean called = FALSE;
292 messenger,
"test",
"Test",
args,
293 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
295 gboolean* called =
static_cast<gboolean*
>(
user_data);
298 EXPECT_TRUE(FL_IS_METHOD_ERROR_RESPONSE(response));
300 FL_METHOD_ERROR_RESPONSE(response)),
303 FL_METHOD_ERROR_RESPONSE(response)),
306 FL_METHOD_ERROR_RESPONSE(response));
315TEST(FlMethodChannelTest, ReceiveMethodCallRespondNotImplemented) {
320 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
334 EXPECT_EQ(
error,
nullptr);
340 gboolean called = FALSE;
342 messenger,
"test",
"Test",
args,
343 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
345 gboolean* called =
static_cast<gboolean*
>(
user_data);
348 EXPECT_TRUE(FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response));
361struct _TestMethodCodec {
362 FlMethodCodec parent_instance;
364 FlStandardMethodCodec* wrapped_codec;
367G_DEFINE_TYPE(TestMethodCodec, test_method_codec, fl_method_codec_get_type())
370 TestMethodCodec*
self = TEST_METHOD_CODEC(
object);
372 g_clear_object(&
self->wrapped_codec);
374 G_OBJECT_CLASS(test_method_codec_parent_class)->dispose(
object);
382 EXPECT_TRUE(TEST_IS_METHOD_CODEC(codec));
383 TestMethodCodec*
self = TEST_METHOD_CODEC(codec);
394 EXPECT_TRUE(TEST_IS_METHOD_CODEC(codec));
395 TestMethodCodec*
self = TEST_METHOD_CODEC(codec);
424 EXPECT_TRUE(TEST_IS_METHOD_CODEC(codec));
425 TestMethodCodec*
self = TEST_METHOD_CODEC(codec);
432 FL_METHOD_CODEC_CLASS(klass)->encode_method_call =
434 FL_METHOD_CODEC_CLASS(klass)->decode_method_call =
436 FL_METHOD_CODEC_CLASS(klass)->encode_success_envelope =
438 FL_METHOD_CODEC_CLASS(klass)->encode_error_envelope =
440 FL_METHOD_CODEC_CLASS(klass)->decode_response =
449 return TEST_METHOD_CODEC(g_object_new(test_method_codec_get_type(),
nullptr));
454TEST(FlMethodChannelTest, ReceiveMethodCallRespondSuccessError) {
459 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
460 gboolean called = FALSE;
465 gboolean* called =
static_cast<gboolean*
>(
user_data);
469 g_autoptr(GError) response_error =
nullptr;
472 EXPECT_NE(response_error,
nullptr);
473 EXPECT_STREQ(response_error->message,
"Unsupported type");
482 messenger,
"test",
"Test",
nullptr,
483 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
492TEST(FlMethodChannelTest, ReceiveMethodCallRespondErrorError) {
497 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
498 gboolean called = FALSE;
503 gboolean* called =
static_cast<gboolean*
>(
user_data);
507 g_autoptr(GError) response_error =
nullptr;
509 details, &response_error));
510 EXPECT_NE(response_error,
nullptr);
511 EXPECT_STREQ(response_error->message,
"Unsupported type");
517 nullptr,
nullptr,
nullptr);
529TEST(FlMethodChannelTest, ReplaceADisposedMethodChannel) {
536 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
542 int* first_count =
static_cast<int*
>(
user_data);
548 &first_count,
nullptr);
551 messenger,
"test",
"Test",
nullptr,
552 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
555 EXPECT_EQ(first_count, 1);
558 g_object_unref(channel1);
562 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
563 int second_count = 0;
568 int* second_count =
static_cast<int*
>(
user_data);
574 &second_count,
nullptr);
577 messenger,
"test",
"Test",
nullptr,
578 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
581 EXPECT_EQ(first_count, 1);
582 EXPECT_EQ(second_count, 1);
592TEST(FlMethodChannelTest, DisposeAReplacedMethodChannel) {
599 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
605 int* first_count =
static_cast<int*
>(
user_data);
611 &first_count,
nullptr);
614 messenger,
"test",
"Test",
nullptr,
615 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
618 EXPECT_EQ(first_count, 1);
622 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
623 int second_count = 0;
628 int* second_count =
static_cast<int*
>(
user_data);
634 &second_count,
nullptr);
637 messenger,
"test",
"Test",
nullptr,
638 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
641 EXPECT_EQ(first_count, 1);
642 EXPECT_EQ(second_count, 1);
645 g_object_unref(channel1);
648 messenger,
"test",
"Test",
nullptr,
649 [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
652 EXPECT_EQ(first_count, 1);
653 EXPECT_EQ(second_count, 2);
657TEST(FlMethodChannelTest, CustomType) {
658 g_autoptr(GMainLoop) loop = g_main_loop_new(
nullptr, 0);
663 [](FlMockBinaryMessenger* messenger, GTask* task,
const gchar*
name,
671 FL_BINARY_MESSENGER(messenger),
"test", FL_METHOD_CODEC(codec));
676 [](GObject*
object, GAsyncResult* result, gpointer
user_data) {
681 EXPECT_EQ(response,
nullptr);
682 EXPECT_NE(
error,
nullptr);
683 EXPECT_STREQ(
error->message,
"Custom value not implemented");
685 g_main_loop_quit(
static_cast<GMainLoop*
>(
user_data));
689 g_main_loop_run(loop);
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
g_autoptr(GMutexLocker) locker
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
@ FL_MESSAGE_CODEC_ERROR_FAILED
#define FL_MESSAGE_CODEC_ERROR
G_MODULE_EXPORT gboolean fl_method_call_respond_error(FlMethodCall *self, const gchar *code, const gchar *message, FlValue *details, GError **error)
G_MODULE_EXPORT const gchar * fl_method_call_get_name(FlMethodCall *self)
G_MODULE_EXPORT gboolean fl_method_call_respond_success(FlMethodCall *self, FlValue *result, GError **error)
G_MODULE_EXPORT gboolean fl_method_call_respond_not_implemented(FlMethodCall *self, GError **error)
G_MODULE_EXPORT FlValue * fl_method_call_get_args(FlMethodCall *self)
G_MODULE_EXPORT FlMethodResponse * fl_method_channel_invoke_method_finish(FlMethodChannel *self, GAsyncResult *result, GError **error)
G_MODULE_EXPORT FlMethodChannel * fl_method_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
G_MODULE_EXPORT void fl_method_channel_invoke_method(FlMethodChannel *self, const gchar *method, FlValue *args, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
G_MODULE_EXPORT void fl_method_channel_set_method_call_handler(FlMethodChannel *self, FlMethodChannelMethodCallHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall * method_call
TEST(FlMethodChannelTest, InvokeMethod)
static GBytes * test_method_codec_encode_method_call(FlMethodCodec *codec, const gchar *name, FlValue *args, GError **error)
static void test_method_codec_dispose(GObject *object)
TestMethodCodec * test_method_codec_new()
static GBytes * test_method_codec_encode_success_envelope(FlMethodCodec *codec, FlValue *result, GError **error)
static FlMethodResponse * test_method_codec_decode_response(FlMethodCodec *codec, GBytes *message, GError **error)
static gboolean test_method_codec_decode_method_call(FlMethodCodec *codec, GBytes *message, gchar **name, FlValue **args, GError **error)
static void test_method_codec_class_init(TestMethodCodecClass *klass)
static void test_method_codec_init(TestMethodCodec *self)
static GBytes * test_method_codec_encode_error_envelope(FlMethodCodec *codec, const gchar *code, const gchar *message, FlValue *details, GError **error)
G_DECLARE_FINAL_TYPE(TestMethodCodec, test_method_codec, TEST, METHOD_CODEC, FlMethodCodec) struct _TestMethodCodec
FlMethodResponse * fl_method_codec_decode_response(FlMethodCodec *self, GBytes *message, GError **error)
gboolean fl_method_codec_decode_method_call(FlMethodCodec *self, GBytes *message, gchar **name, FlValue **args, GError **error)
GBytes * fl_method_codec_encode_method_call(FlMethodCodec *self, const gchar *name, FlValue *args, GError **error)
G_MODULE_EXPORT FlValue * fl_method_response_get_result(FlMethodResponse *self, GError **error)
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)
G_MODULE_EXPORT const gchar * fl_method_error_response_get_message(FlMethodErrorResponse *self)
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
G_MODULE_EXPORT FlValue * fl_method_error_response_get_details(FlMethodErrorResponse *self)
G_MODULE_EXPORT const gchar * fl_method_error_response_get_code(FlMethodErrorResponse *self)
G_MODULE_EXPORT FlMethodNotImplementedResponse * fl_method_not_implemented_response_new()
void fl_mock_binary_messenger_invoke_standard_method(FlMockBinaryMessenger *self, const gchar *channel, const char *name, FlValue *args, FlMockBinaryMessengerMethodCallback callback, gpointer user_data)
void fl_mock_binary_messenger_set_standard_method_channel(FlMockBinaryMessenger *self, const gchar *channel, FlMockBinaryMessengerMethodChannelHandler handler, gpointer user_data)
void fl_mock_binary_messenger_set_error_channel(FlMockBinaryMessenger *self, const gchar *channel, gint code, const gchar *message)
FlMockBinaryMessenger * fl_mock_binary_messenger_new()
G_BEGIN_DECLS GBytes * message
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT FlStandardMethodCodec * fl_standard_method_codec_new()
G_MODULE_EXPORT FlValue * fl_value_new_custom(int type, gconstpointer value, GDestroyNotify destroy_notify)
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
typedefG_BEGIN_DECLS struct _FlValue FlValue