Flutter Linux Embedder
fl_json_method_codec_test.cc File Reference

Go to the source code of this file.

Functions

static gchar * message_to_text (GBytes *message)
 
static GBytes * text_to_message (const gchar *text)
 
static gchar * encode_method_call (const gchar *name, FlValue *args)
 
static gchar * encode_success_envelope (FlValue *result)
 
static gchar * encode_error_envelope (const gchar *error_code, const gchar *error_message, FlValue *details)
 
static void decode_method_call (const char *text, gchar **name, FlValue **args)
 
static void decode_error_method_call (const char *text, GQuark domain, gint code)
 
static void decode_response_with_success (const char *text, FlValue *result)
 
static void decode_response_with_error (const char *text, const gchar *code, const gchar *error_message, FlValue *details)
 
static void decode_error_response (const char *text, GQuark domain, gint code)
 
 TEST (FlJsonMethodCodecTest, EncodeMethodCallNullptrArgs)
 
 TEST (FlJsonMethodCodecTest, EncodeMethodCallNullArgs)
 
 TEST (FlJsonMethodCodecTest, EncodeMethodCallStringArgs)
 
 TEST (FlJsonMethodCodecTest, EncodeMethodCallListArgs)
 
 TEST (FlJsonMethodCodecTest, DecodeMethodCallNoArgs)
 
 TEST (FlJsonMethodCodecTest, DecodeMethodCallNullArgs)
 
 TEST (FlJsonMethodCodecTest, DecodeMethodCallStringArgs)
 
 TEST (FlJsonMethodCodecTest, DecodeMethodCallListArgs)
 
 TEST (FlJsonMethodCodecTest, DecodeMethodCallNoData)
 
 TEST (FlJsonMethodCodecTest, DecodeMethodCallNoMethodOrArgs)
 
 TEST (FlJsonMethodCodecTest, DecodeMethodCallInvalidJson)
 
 TEST (FlJsonMethodCodecTest, DecodeMethodCallWrongType)
 
 TEST (FlJsonMethodCodecTest, DecodeMethodCallNoMethod)
 
 TEST (FlJsonMethodCodecTest, DecodeMethodCallNoTerminator)
 
 TEST (FlJsonMethodCodecTest, DecodeMethodCallExtraData)
 
 TEST (FlJsonMethodCodecTest, EncodeSuccessEnvelopeNullptr)
 
 TEST (FlJsonMethodCodecTest, EncodeSuccessEnvelopeNull)
 
 TEST (FlJsonMethodCodecTest, EncodeSuccessEnvelopeString)
 
 TEST (FlJsonMethodCodecTest, EncodeSuccessEnvelopeList)
 

Function Documentation

◆ decode_error_method_call()

static void decode_error_method_call ( const char *  text,
GQuark  domain,
gint  code 
)
static

Definition at line 77 of file fl_json_method_codec_test.cc.

79  {
80  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
81  g_autoptr(GBytes) data = text_to_message(text);
82  g_autoptr(GError) error = nullptr;
83  g_autofree gchar* name = nullptr;
84  g_autoptr(FlValue) args = nullptr;
86  FL_METHOD_CODEC(codec), data, &name, &args, &error);
87  EXPECT_FALSE(result);
88  EXPECT_EQ(name, nullptr);
89  EXPECT_EQ(args, nullptr);
90  EXPECT_TRUE(g_error_matches(error, domain, code));
91 }

References args, error, fl_json_method_codec_new(), fl_method_codec_decode_method_call(), result, and text_to_message().

Referenced by TEST().

◆ decode_error_response()

static void decode_error_response ( const char *  text,
GQuark  domain,
gint  code 
)
static

Definition at line 146 of file fl_json_method_codec_test.cc.

146  {
147  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
148  g_autoptr(GBytes) message = text_to_message(text);
149  g_autoptr(GError) error = nullptr;
150  g_autoptr(FlMethodResponse) response =
151  fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
152  EXPECT_EQ(response, nullptr);
153  EXPECT_TRUE(g_error_matches(error, domain, code));
154 }

References error, fl_json_method_codec_new(), fl_method_codec_decode_response(), and text_to_message().

◆ decode_method_call()

static void decode_method_call ( const char *  text,
gchar **  name,
FlValue **  args 
)
static

Definition at line 66 of file fl_json_method_codec_test.cc.

66  {
67  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
68  g_autoptr(GBytes) data = text_to_message(text);
69  g_autoptr(GError) error = nullptr;
71  FL_METHOD_CODEC(codec), data, name, args, &error);
72  EXPECT_TRUE(result);
73  EXPECT_EQ(error, nullptr);
74 }

References args, error, fl_json_method_codec_new(), fl_method_codec_decode_method_call(), result, and text_to_message().

Referenced by TEST().

◆ decode_response_with_error()

static void decode_response_with_error ( const char *  text,
const gchar *  code,
const gchar *  error_message,
FlValue details 
)
static

Definition at line 110 of file fl_json_method_codec_test.cc.

113  {
114  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
115  g_autoptr(GBytes) message = text_to_message(text);
116  g_autoptr(GError) error = nullptr;
117  g_autoptr(FlMethodResponse) response =
118  fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
119  ASSERT_NE(response, nullptr);
120  EXPECT_EQ(error, nullptr);
121  ASSERT_TRUE(FL_IS_METHOD_ERROR_RESPONSE(response));
122  EXPECT_STREQ(
123  fl_method_error_response_get_code(FL_METHOD_ERROR_RESPONSE(response)),
124  code);
125  if (error_message == nullptr) {
127  FL_METHOD_ERROR_RESPONSE(response)),
128  nullptr);
129  } else {
131  FL_METHOD_ERROR_RESPONSE(response)),
132  error_message);
133  }
134  if (details == nullptr) {
136  FL_METHOD_ERROR_RESPONSE(response)),
137  nullptr);
138  } else {
140  FL_METHOD_ERROR_RESPONSE(response)),
141  details));
142  }
143 }

References error, fl_json_method_codec_new(), fl_method_codec_decode_response(), fl_method_error_response_get_code(), fl_method_error_response_get_details(), fl_method_error_response_get_message(), fl_value_equal(), and text_to_message().

◆ decode_response_with_success()

static void decode_response_with_success ( const char *  text,
FlValue result 
)
static

Definition at line 94 of file fl_json_method_codec_test.cc.

94  {
95  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
96  g_autoptr(GBytes) message = text_to_message(text);
97  g_autoptr(GError) error = nullptr;
98  g_autoptr(FlMethodResponse) response =
99  fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
100  ASSERT_NE(response, nullptr);
101  EXPECT_EQ(error, nullptr);
102  ASSERT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
104  FL_METHOD_SUCCESS_RESPONSE(response)),
105  result));
106 }

References error, fl_json_method_codec_new(), fl_method_codec_decode_response(), fl_method_success_response_get_result(), fl_value_equal(), result, and text_to_message().

◆ encode_error_envelope()

static gchar* encode_error_envelope ( const gchar *  error_code,
const gchar *  error_message,
FlValue details 
)
static

Definition at line 52 of file fl_json_method_codec_test.cc.

54  {
55  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
56  g_autoptr(GError) error = nullptr;
57  g_autoptr(GBytes) message = fl_method_codec_encode_error_envelope(
58  FL_METHOD_CODEC(codec), error_code, error_message, details, &error);
59  EXPECT_NE(message, nullptr);
60  EXPECT_EQ(error, nullptr);
61 
62  return message_to_text(message);
63 }

References error, fl_json_method_codec_new(), fl_method_codec_encode_error_envelope(), and message_to_text().

◆ encode_method_call()

static gchar* encode_method_call ( const gchar *  name,
FlValue args 
)
static

Definition at line 28 of file fl_json_method_codec_test.cc.

28  {
29  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
30  g_autoptr(GError) error = nullptr;
31  g_autoptr(GBytes) message = fl_method_codec_encode_method_call(
32  FL_METHOD_CODEC(codec), name, args, &error);
33  EXPECT_NE(message, nullptr);
34  EXPECT_EQ(error, nullptr);
35 
36  return message_to_text(message);
37 }

References args, error, fl_json_method_codec_new(), fl_method_codec_encode_method_call(), and message_to_text().

Referenced by TEST().

◆ encode_success_envelope()

static gchar* encode_success_envelope ( FlValue result)
static

Definition at line 40 of file fl_json_method_codec_test.cc.

40  {
41  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
42  g_autoptr(GError) error = nullptr;
43  g_autoptr(GBytes) message = fl_method_codec_encode_success_envelope(
44  FL_METHOD_CODEC(codec), result, &error);
45  EXPECT_NE(message, nullptr);
46  EXPECT_EQ(error, nullptr);
47 
48  return message_to_text(message);
49 }

References error, fl_json_method_codec_new(), fl_method_codec_encode_success_envelope(), message_to_text(), and result.

Referenced by TEST().

◆ message_to_text()

static gchar* message_to_text ( GBytes *  message)
static

Definition at line 15 of file fl_json_method_codec_test.cc.

15  {
16  size_t data_length;
17  const gchar* data =
18  static_cast<const gchar*>(g_bytes_get_data(message, &data_length));
19  return g_strndup(data, data_length);
20 }

Referenced by encode_error_envelope(), encode_method_call(), encode_success_envelope(), fl_test_codec_decode_method_call(), fl_test_codec_decode_response(), and TEST().

◆ TEST() [1/19]

TEST ( FlJsonMethodCodecTest  ,
DecodeMethodCallExtraData   
)

◆ TEST() [2/19]

TEST ( FlJsonMethodCodecTest  ,
DecodeMethodCallInvalidJson   
)

◆ TEST() [3/19]

TEST ( FlJsonMethodCodecTest  ,
DecodeMethodCallListArgs   
)

Definition at line 206 of file fl_json_method_codec_test.cc.

206  {
207  g_autofree gchar* name = nullptr;
208  g_autoptr(FlValue) args = nullptr;
209  decode_method_call("{\"method\":\"hello\",\"args\":[\"count\",42]}", &name,
210  &args);
211  EXPECT_STREQ(name, "hello");
213  EXPECT_EQ(fl_value_get_length(args), static_cast<size_t>(2));
214 
216  ASSERT_EQ(fl_value_get_type(arg0), FL_VALUE_TYPE_STRING);
217  EXPECT_STREQ(fl_value_get_string(arg0), "count");
218 
220  ASSERT_EQ(fl_value_get_type(arg1), FL_VALUE_TYPE_INT);
221  EXPECT_EQ(fl_value_get_int(arg1), 42);
222 }

References args, decode_method_call(), fl_value_get_int(), fl_value_get_length(), fl_value_get_list_value(), fl_value_get_string(), fl_value_get_type(), FL_VALUE_TYPE_INT, FL_VALUE_TYPE_LIST, and FL_VALUE_TYPE_STRING.

◆ TEST() [4/19]

TEST ( FlJsonMethodCodecTest  ,
DecodeMethodCallNoArgs   
)

Definition at line 181 of file fl_json_method_codec_test.cc.

181  {
182  g_autofree gchar* name = nullptr;
183  g_autoptr(FlValue) args = nullptr;
184  decode_method_call("{\"method\":\"hello\"}", &name, &args);
185  EXPECT_STREQ(name, "hello");
186  ASSERT_EQ(args, nullptr);
187 }

References args, and decode_method_call().

◆ TEST() [5/19]

TEST ( FlJsonMethodCodecTest  ,
DecodeMethodCallNoData   
)

◆ TEST() [6/19]

TEST ( FlJsonMethodCodecTest  ,
DecodeMethodCallNoMethod   
)

◆ TEST() [7/19]

TEST ( FlJsonMethodCodecTest  ,
DecodeMethodCallNoMethodOrArgs   
)

◆ TEST() [8/19]

TEST ( FlJsonMethodCodecTest  ,
DecodeMethodCallNoTerminator   
)

◆ TEST() [9/19]

TEST ( FlJsonMethodCodecTest  ,
DecodeMethodCallNullArgs   
)

Definition at line 189 of file fl_json_method_codec_test.cc.

189  {
190  g_autofree gchar* name = nullptr;
191  g_autoptr(FlValue) args = nullptr;
192  decode_method_call("{\"method\":\"hello\",\"args\":null}", &name, &args);
193  EXPECT_STREQ(name, "hello");
195 }

References args, decode_method_call(), fl_value_get_type(), and FL_VALUE_TYPE_NULL.

◆ TEST() [10/19]

TEST ( FlJsonMethodCodecTest  ,
DecodeMethodCallStringArgs   
)

Definition at line 197 of file fl_json_method_codec_test.cc.

197  {
198  g_autofree gchar* name = nullptr;
199  g_autoptr(FlValue) args = nullptr;
200  decode_method_call("{\"method\":\"hello\",\"args\":\"world\"}", &name, &args);
201  EXPECT_STREQ(name, "hello");
203  EXPECT_STREQ(fl_value_get_string(args), "world");
204 }

References args, decode_method_call(), fl_value_get_string(), fl_value_get_type(), and FL_VALUE_TYPE_STRING.

◆ TEST() [11/19]

TEST ( FlJsonMethodCodecTest  ,
DecodeMethodCallWrongType   
)

◆ TEST() [12/19]

TEST ( FlJsonMethodCodecTest  ,
EncodeMethodCallListArgs   
)

Definition at line 173 of file fl_json_method_codec_test.cc.

173  {
174  g_autoptr(FlValue) args = fl_value_new_list();
177  g_autofree gchar* text = encode_method_call("hello", args);
178  EXPECT_STREQ(text, "{\"method\":\"hello\",\"args\":[\"count\",42]}");
179 }

References args, encode_method_call(), fl_value_append_take(), fl_value_new_int(), fl_value_new_list(), and fl_value_new_string().

◆ TEST() [13/19]

TEST ( FlJsonMethodCodecTest  ,
EncodeMethodCallNullArgs   
)

Definition at line 161 of file fl_json_method_codec_test.cc.

161  {
162  g_autoptr(FlValue) value = fl_value_new_null();
163  g_autofree gchar* text = encode_method_call("hello", value);
164  EXPECT_STREQ(text, "{\"method\":\"hello\",\"args\":null}");
165 }

References encode_method_call(), fl_value_new_null(), and value.

◆ TEST() [14/19]

TEST ( FlJsonMethodCodecTest  ,
EncodeMethodCallNullptrArgs   
)

Definition at line 156 of file fl_json_method_codec_test.cc.

156  {
157  g_autofree gchar* text = encode_method_call("hello", nullptr);
158  EXPECT_STREQ(text, "{\"method\":\"hello\",\"args\":null}");
159 }

References encode_method_call().

◆ TEST() [15/19]

TEST ( FlJsonMethodCodecTest  ,
EncodeMethodCallStringArgs   
)

Definition at line 167 of file fl_json_method_codec_test.cc.

167  {
168  g_autoptr(FlValue) args = fl_value_new_string("world");
169  g_autofree gchar* text = encode_method_call("hello", args);
170  EXPECT_STREQ(text, "{\"method\":\"hello\",\"args\":\"world\"}");
171 }

References args, encode_method_call(), and fl_value_new_string().

◆ TEST() [16/19]

TEST ( FlJsonMethodCodecTest  ,
EncodeSuccessEnvelopeList   
)

◆ TEST() [17/19]

TEST ( FlJsonMethodCodecTest  ,
EncodeSuccessEnvelopeNull   
)

Definition at line 266 of file fl_json_method_codec_test.cc.

266  {
267  g_autoptr(FlValue) result = fl_value_new_null();
268  g_autofree gchar* text = encode_success_envelope(result);
269  EXPECT_STREQ(text, "[null]");
270 }

References encode_success_envelope(), fl_value_new_null(), and result.

◆ TEST() [18/19]

TEST ( FlJsonMethodCodecTest  ,
EncodeSuccessEnvelopeNullptr   
)

Definition at line 261 of file fl_json_method_codec_test.cc.

261  {
262  g_autofree gchar* text = encode_success_envelope(nullptr);
263  EXPECT_STREQ(text, "[null]");
264 }

References encode_success_envelope().

◆ TEST() [19/19]

TEST ( FlJsonMethodCodecTest  ,
EncodeSuccessEnvelopeString   
)

Definition at line 272 of file fl_json_method_codec_test.cc.

272  {
273  g_autoptr(FlValue) result = fl_value_new_string("hello");
274  g_autofree gchar* text = encode_success_envelope(result);
275  EXPECT_STREQ(text, "[\"hello\"]");
276 }

References encode_success_envelope(), fl_value_new_string(), and result.

◆ text_to_message()

static GBytes* text_to_message ( const gchar *  text)
static

Definition at line 23 of file fl_json_method_codec_test.cc.

23  {
24  return g_bytes_new(text, strlen(text));
25 }

Referenced by decode_error_method_call(), decode_error_response(), decode_method_call(), decode_response_with_error(), and decode_response_with_success().

fl_json_method_codec_new
G_MODULE_EXPORT FlJsonMethodCodec * fl_json_method_codec_new()
Definition: fl_json_method_codec.cc:205
fl_method_codec_encode_method_call
GBytes * fl_method_codec_encode_method_call(FlMethodCodec *self, const gchar *name, FlValue *args, GError **error)
Definition: fl_method_codec.cc:16
fl_value_new_list
G_MODULE_EXPORT FlValue * fl_value_new_list()
Definition: fl_value.cc:349
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_value_new_null
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:251
FL_VALUE_TYPE_LIST
@ FL_VALUE_TYPE_LIST
Definition: fl_value.h:74
fl_value_new_int
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:262
fl_value_get_string
const G_MODULE_EXPORT gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
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:65
fl_value_get_int
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:668
decode_error_method_call
static void decode_error_method_call(const char *text, GQuark domain, gint code)
Definition: fl_json_method_codec_test.cc:77
decode_method_call
static void decode_method_call(const char *text, gchar **name, FlValue **args)
Definition: fl_json_method_codec_test.cc:66
text_to_message
static GBytes * text_to_message(const gchar *text)
Definition: fl_json_method_codec_test.cc:23
encode_success_envelope
static gchar * encode_success_envelope(FlValue *result)
Definition: fl_json_method_codec_test.cc:40
fl_method_error_response_get_details
G_MODULE_EXPORT FlValue * fl_method_error_response_get_details(FlMethodErrorResponse *self)
Definition: fl_method_response.cc:172
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
fl_value_get_list_value
G_MODULE_EXPORT FlValue * fl_value_get_list_value(FlValue *self, size_t index)
Definition: fl_value.cc:776
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:69
message_to_text
static gchar * message_to_text(GBytes *message)
Definition: fl_json_method_codec_test.cc:15
fl_method_codec_decode_method_call
gboolean fl_method_codec_decode_method_call(FlMethodCodec *self, GBytes *message, gchar **name, FlValue **args, GError **error)
Definition: fl_method_codec.cc:27
fl_method_success_response_get_result
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
Definition: fl_method_response.cc:138
encode_method_call
static gchar * encode_method_call(const gchar *name, FlValue *args)
Definition: fl_json_method_codec_test.cc:28
FL_MESSAGE_CODEC_ERROR_FAILED
@ FL_MESSAGE_CODEC_ERROR_FAILED
Definition: fl_message_codec.h:34
fl_value_append_take
G_MODULE_EXPORT void fl_value_append_take(FlValue *self, FlValue *value)
Definition: fl_value.cc:600
fl_method_codec_encode_success_envelope
GBytes * fl_method_codec_encode_success_envelope(FlMethodCodec *self, FlValue *result, GError **error)
Definition: fl_method_codec.cc:41
fl_value_get_length
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:724
fl_method_codec_decode_response
FlMethodResponse * fl_method_codec_decode_response(FlMethodCodec *self, GBytes *message, GError **error)
Definition: fl_method_codec.cc:62
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:471
FL_VALUE_TYPE_INT
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:67
result
GAsyncResult * result
Definition: fl_text_input_plugin.cc:106
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_JSON_MESSAGE_CODEC_ERROR
#define FL_JSON_MESSAGE_CODEC_ERROR
Definition: fl_json_message_codec.h:27
fl_method_codec_encode_error_envelope
GBytes * fl_method_codec_encode_error_envelope(FlMethodCodec *self, const gchar *code, const gchar *message, FlValue *details, GError **error)
Definition: fl_method_codec.cc:50
FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON
@ FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON
Definition: fl_json_message_codec.h:32
value
uint8_t value
Definition: fl_standard_message_codec.cc:36
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
FL_MESSAGE_CODEC_ERROR
#define FL_MESSAGE_CODEC_ERROR
Definition: fl_message_codec.h:30