Flutter Engine
The Flutter Engine
Functions
fl_method_codec_test.cc File Reference
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_codec.h"
#include <cstring>
#include "flutter/shell/platform/linux/fl_method_codec_private.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_message_codec.h"
#include "gtest/gtest.h"

Go to the source code of this file.

Functions

 G_DECLARE_FINAL_TYPE (FlTestMethodCodec, fl_test_method_codec, FL, TEST_METHOD_CODEC, FlMethodCodec) struct _FlTestMethodCodec
 
 G_DEFINE_TYPE (FlTestMethodCodec, fl_test_method_codec, fl_method_codec_get_type()) static gchar *message_to_text(GBytes *message)
 
static GBytes * text_to_message (const gchar *text)
 
static GBytes * fl_test_codec_encode_method_call (FlMethodCodec *codec, const gchar *name, FlValue *args, GError **error)
 
static gboolean fl_test_codec_decode_method_call (FlMethodCodec *codec, GBytes *message, gchar **name, FlValue **args, GError **error)
 
static GBytes * fl_test_codec_encode_success_envelope (FlMethodCodec *codec, FlValue *result, GError **error)
 
static GBytes * fl_test_codec_encode_error_envelope (FlMethodCodec *codec, const gchar *code, const gchar *message, FlValue *details, GError **error)
 
static FlMethodResponse * fl_test_codec_decode_response (FlMethodCodec *codec, GBytes *message, GError **error)
 
static void fl_test_method_codec_class_init (FlTestMethodCodecClass *klass)
 
static void fl_test_method_codec_init (FlTestMethodCodec *self)
 
static FlTestMethodCodec * fl_test_method_codec_new ()
 
 TEST (FlMethodCodecTest, EncodeMethodCall)
 
 TEST (FlMethodCodecTest, EncodeMethodCallEmptyName)
 
 TEST (FlMethodCodecTest, EncodeMethodCallArgs)
 
 TEST (FlMethodCodecTest, EncodeMethodCallError)
 
 TEST (FlMethodCodecTest, DecodeMethodCall)
 
 TEST (FlMethodCodecTest, EncodeSuccessEnvelope)
 
 TEST (FlMethodCodecTest, EncodeSuccessEnvelopeEmpty)
 
 TEST (FlMethodCodecTest, EncodeSuccessEnvelopeError)
 
 TEST (FlMethodCodecTest, EncodeErrorEnvelopeNoMessageOrDetails)
 
 TEST (FlMethodCodecTest, EncodeErrorEnvelopeMessage)
 
 TEST (FlMethodCodecTest, EncodeErrorEnvelopeDetails)
 
 TEST (FlMethodCodecTest, EncodeErrorEnvelopeMessageAndDetails)
 
 TEST (FlMethodCodecTest, DecodeResponseSuccess)
 
 TEST (FlMethodCodecTest, DecodeResponseNotImplemented)
 
 TEST (FlMethodCodecTest, DecodeResponseCodecError)
 
 TEST (FlMethodCodecTest, DecodeResponseError)
 

Function Documentation

◆ fl_test_codec_decode_method_call()

static gboolean fl_test_codec_decode_method_call ( FlMethodCodec *  codec,
GBytes *  message,
gchar **  name,
FlValue **  args,
GError **  error 
)
static

Definition at line 65 of file fl_method_codec_test.cc.

69 {
70 EXPECT_TRUE(FL_IS_TEST_METHOD_CODEC(codec));
71
72 g_autofree gchar* m = message_to_text(message);
73
74 if (strcmp(m, "error") == 0) {
76 "ERROR");
77 return FALSE;
78 } else {
79 *name = g_strdup(m);
81 return TRUE;
82 }
83}
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static gchar * message_to_text(GBytes *message)
@ FL_MESSAGE_CODEC_ERROR_FAILED
#define FL_MESSAGE_CODEC_ERROR
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:251
Win32Message message
return FALSE
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678

◆ fl_test_codec_decode_response()

static FlMethodResponse * fl_test_codec_decode_response ( FlMethodCodec *  codec,
GBytes *  message,
GError **  error 
)
static

Definition at line 142 of file fl_method_codec_test.cc.

144 {
145 EXPECT_TRUE(FL_IS_TEST_METHOD_CODEC(codec));
146
147 g_autofree gchar* m = message_to_text(message);
148 if (strcmp(m, "codec-error") == 0) {
150 "ERROR");
151 return nullptr;
152 } else if (strcmp(m, "error") == 0) {
153 g_autoptr(FlValue) details = fl_value_new_int(42);
154 return FL_METHOD_RESPONSE(
155 fl_method_error_response_new("code", "message", details));
156 } else {
157 g_autoptr(FlValue) result = fl_value_new_string(m);
158 return FL_METHOD_RESPONSE(fl_method_success_response_new(result));
159 }
160}
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
GAsyncResult * result
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:262
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42

◆ fl_test_codec_encode_error_envelope()

static GBytes * fl_test_codec_encode_error_envelope ( FlMethodCodec *  codec,
const gchar *  code,
const gchar *  message,
FlValue details,
GError **  error 
)
static

Definition at line 106 of file fl_method_codec_test.cc.

110 {
111 EXPECT_TRUE(FL_IS_TEST_METHOD_CODEC(codec));
112
113 if (details != nullptr && fl_value_get_type(details) != FL_VALUE_TYPE_INT) {
115 "ERROR");
116 return nullptr;
117 }
118
119 g_autofree gchar* text = nullptr;
120 if (message == nullptr) {
121 if (details == nullptr ||
123 text = g_strdup_printf("Error_%s()", code);
124 } else {
125 text = g_strdup_printf("Error_%s(%" G_GINT64_FORMAT ")", code,
126 fl_value_get_int(details));
127 }
128 } else {
129 if (details == nullptr ||
131 text = g_strdup_printf("Error_%s(%s)", code, message);
132 } else {
133 text = g_strdup_printf("Error_%s(%s,%" G_GINT64_FORMAT ")", code, message,
134 fl_value_get_int(details));
135 }
136 }
137
138 return text_to_message(text);
139}
static GBytes * text_to_message(const gchar *text)
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:668
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:65
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:67
std::u16string text

◆ fl_test_codec_encode_method_call()

static GBytes * fl_test_codec_encode_method_call ( FlMethodCodec *  codec,
const gchar *  name,
FlValue args,
GError **  error 
)
static

Definition at line 43 of file fl_method_codec_test.cc.

46 {
47 EXPECT_TRUE(FL_IS_TEST_METHOD_CODEC(codec));
48
49 g_autofree gchar* text = nullptr;
50 if (args == nullptr || fl_value_get_type(args) == FL_VALUE_TYPE_NULL) {
51 text = g_strdup_printf("%s()", name);
53 text = g_strdup_printf("%s(%" G_GINT64_FORMAT ")", name,
55 } else {
57 "ERROR");
58 return nullptr;
59 }
60
61 return text_to_message(text);
62}

◆ fl_test_codec_encode_success_envelope()

static GBytes * fl_test_codec_encode_success_envelope ( FlMethodCodec *  codec,
FlValue result,
GError **  error 
)
static

Definition at line 86 of file fl_method_codec_test.cc.

88 {
89 EXPECT_TRUE(FL_IS_TEST_METHOD_CODEC(codec));
90
91 g_autofree gchar* text = nullptr;
92 if (result == nullptr || fl_value_get_type(result) == FL_VALUE_TYPE_NULL) {
93 text = g_strdup("(null)");
95 text = g_strdup_printf("%" G_GINT64_FORMAT, fl_value_get_int(result));
96 } else {
98 "ERROR");
99 return nullptr;
100 }
101
102 return text_to_message(text);
103}

◆ fl_test_method_codec_class_init()

static void fl_test_method_codec_class_init ( FlTestMethodCodecClass *  klass)
static

Definition at line 162 of file fl_method_codec_test.cc.

162 {
163 FL_METHOD_CODEC_CLASS(klass)->encode_method_call =
165 FL_METHOD_CODEC_CLASS(klass)->decode_method_call =
167 FL_METHOD_CODEC_CLASS(klass)->encode_success_envelope =
169 FL_METHOD_CODEC_CLASS(klass)->encode_error_envelope =
171 FL_METHOD_CODEC_CLASS(klass)->decode_response = fl_test_codec_decode_response;
172}
static GBytes * fl_test_codec_encode_success_envelope(FlMethodCodec *codec, FlValue *result, GError **error)
static GBytes * fl_test_codec_encode_error_envelope(FlMethodCodec *codec, const gchar *code, const gchar *message, FlValue *details, GError **error)
static gboolean fl_test_codec_decode_method_call(FlMethodCodec *codec, GBytes *message, gchar **name, FlValue **args, GError **error)
static GBytes * fl_test_codec_encode_method_call(FlMethodCodec *codec, const gchar *name, FlValue *args, GError **error)
static FlMethodResponse * fl_test_codec_decode_response(FlMethodCodec *codec, GBytes *message, GError **error)

◆ fl_test_method_codec_init()

static void fl_test_method_codec_init ( FlTestMethodCodec *  self)
static

Definition at line 174 of file fl_method_codec_test.cc.

174{}

◆ fl_test_method_codec_new()

static FlTestMethodCodec * fl_test_method_codec_new ( )
static

Definition at line 176 of file fl_method_codec_test.cc.

176 {
177 return FL_TEST_METHOD_CODEC(
178 g_object_new(fl_test_method_codec_get_type(), nullptr));
179}

◆ G_DECLARE_FINAL_TYPE()

G_DECLARE_FINAL_TYPE ( FlTestMethodCodec  ,
fl_test_method_codec  ,
FL  ,
TEST_METHOD_CODEC  ,
FlMethodCodec   
)

Definition at line 13 of file fl_method_codec_test.cc.

21 {
22 FlMethodCodec parent_instance;
23};

◆ G_DEFINE_TYPE()

G_DEFINE_TYPE ( FlTestMethodCodec  ,
fl_test_method_codec  ,
fl_method_codec_get_type()   
)

Definition at line 25 of file fl_method_codec_test.cc.

30 {
31 size_t data_length;
32 const gchar* data =
33 static_cast<const gchar*>(g_bytes_get_data(message, &data_length));
34 return g_strndup(data, data_length);
35}
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63

◆ TEST() [1/16]

TEST ( FlMethodCodecTest  ,
DecodeMethodCall   
)

Definition at line 233 of file fl_method_codec_test.cc.

233 {
234 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
235
236 g_autoptr(GBytes) message = text_to_message("foo");
237
238 g_autofree gchar* name = nullptr;
239 g_autoptr(FlValue) args = nullptr;
240 g_autoptr(GError) error = nullptr;
242 FL_METHOD_CODEC(codec), message, &name, &args, &error);
243 EXPECT_EQ(error, nullptr);
245
246 EXPECT_STREQ(name, "foo");
248}
gboolean fl_method_codec_decode_method_call(FlMethodCodec *self, GBytes *message, gchar **name, FlValue **args, GError **error)
static FlTestMethodCodec * fl_test_method_codec_new()

◆ TEST() [2/16]

TEST ( FlMethodCodecTest  ,
DecodeResponseCodecError   
)

Definition at line 374 of file fl_method_codec_test.cc.

374 {
375 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
376
377 g_autoptr(GBytes) message = text_to_message("codec-error");
378
379 g_autoptr(GError) error = nullptr;
380 g_autoptr(FlMethodResponse) response =
381 fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
384 EXPECT_EQ(response, nullptr);
385}
FlMethodResponse * fl_method_codec_decode_response(FlMethodCodec *self, GBytes *message, GError **error)

◆ TEST() [3/16]

TEST ( FlMethodCodecTest  ,
DecodeResponseError   
)

Definition at line 387 of file fl_method_codec_test.cc.

387 {
388 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
389
390 g_autoptr(GBytes) message = text_to_message("error");
391
392 g_autoptr(GError) error = nullptr;
393 g_autoptr(FlMethodResponse) response =
394 fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
395 EXPECT_EQ(error, nullptr);
396 EXPECT_NE(response, nullptr);
397 ASSERT_TRUE(FL_METHOD_ERROR_RESPONSE(response));
398 EXPECT_STREQ(
399 fl_method_error_response_get_code(FL_METHOD_ERROR_RESPONSE(response)),
400 "code");
401 EXPECT_STREQ(
402 fl_method_error_response_get_message(FL_METHOD_ERROR_RESPONSE(response)),
403 "message");
404 FlValue* details =
405 fl_method_error_response_get_details(FL_METHOD_ERROR_RESPONSE(response));
406 ASSERT_NE(details, nullptr);
407 ASSERT_EQ(fl_value_get_type(details), FL_VALUE_TYPE_INT);
408 EXPECT_EQ(fl_value_get_int(details), 42);
409}
G_MODULE_EXPORT const gchar * fl_method_error_response_get_message(FlMethodErrorResponse *self)
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)

◆ TEST() [4/16]

TEST ( FlMethodCodecTest  ,
DecodeResponseNotImplemented   
)

Definition at line 361 of file fl_method_codec_test.cc.

361 {
362 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
363
364 g_autoptr(GBytes) message = g_bytes_new(nullptr, 0);
365
366 g_autoptr(GError) error = nullptr;
367 g_autoptr(FlMethodResponse) response =
368 fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
369 EXPECT_EQ(error, nullptr);
370 EXPECT_NE(response, nullptr);
371 ASSERT_TRUE(FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response));
372}

◆ TEST() [5/16]

TEST ( FlMethodCodecTest  ,
DecodeResponseSuccess   
)

Definition at line 343 of file fl_method_codec_test.cc.

343 {
344 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
345
346 g_autoptr(GBytes) message = text_to_message("echo");
347
348 g_autoptr(GError) error = nullptr;
349 g_autoptr(FlMethodResponse) response =
350 fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
351 EXPECT_EQ(error, nullptr);
352 EXPECT_NE(response, nullptr);
353 ASSERT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
355 FL_METHOD_SUCCESS_RESPONSE(response));
356 ASSERT_NE(result, nullptr);
358 EXPECT_STREQ(fl_value_get_string(result), "echo");
359}
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:69

◆ TEST() [6/16]

TEST ( FlMethodCodecTest  ,
EncodeErrorEnvelopeDetails   
)

Definition at line 315 of file fl_method_codec_test.cc.

315 {
316 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
317
318 g_autoptr(FlValue) details = fl_value_new_int(42);
319 g_autoptr(GError) error = nullptr;
321 FL_METHOD_CODEC(codec), "code", nullptr, details, &error);
322 EXPECT_EQ(error, nullptr);
323 EXPECT_NE(message, nullptr);
324
325 g_autofree gchar* message_text = message_to_text(message);
326 EXPECT_STREQ(message_text, "Error_code(42)");
327}
GBytes * fl_method_codec_encode_error_envelope(FlMethodCodec *self, const gchar *code, const gchar *message, FlValue *details, GError **error)

◆ TEST() [7/16]

TEST ( FlMethodCodecTest  ,
EncodeErrorEnvelopeMessage   
)

Definition at line 302 of file fl_method_codec_test.cc.

302 {
303 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
304
305 g_autoptr(GError) error = nullptr;
307 FL_METHOD_CODEC(codec), "code", "message", nullptr, &error);
308 EXPECT_EQ(error, nullptr);
309 EXPECT_NE(message, nullptr);
310
311 g_autofree gchar* message_text = message_to_text(message);
312 EXPECT_STREQ(message_text, "Error_code(message)");
313}

◆ TEST() [8/16]

TEST ( FlMethodCodecTest  ,
EncodeErrorEnvelopeMessageAndDetails   
)

Definition at line 329 of file fl_method_codec_test.cc.

329 {
330 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
331
332 g_autoptr(FlValue) details = fl_value_new_int(42);
333 g_autoptr(GError) error = nullptr;
335 FL_METHOD_CODEC(codec), "code", "message", details, &error);
336 EXPECT_EQ(error, nullptr);
337 EXPECT_NE(message, nullptr);
338
339 g_autofree gchar* message_text = message_to_text(message);
340 EXPECT_STREQ(message_text, "Error_code(message,42)");
341}

◆ TEST() [9/16]

TEST ( FlMethodCodecTest  ,
EncodeErrorEnvelopeNoMessageOrDetails   
)

Definition at line 289 of file fl_method_codec_test.cc.

289 {
290 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
291
292 g_autoptr(GError) error = nullptr;
294 FL_METHOD_CODEC(codec), "code", nullptr, nullptr, &error);
295 EXPECT_EQ(error, nullptr);
296 EXPECT_NE(message, nullptr);
297
298 g_autofree gchar* message_text = message_to_text(message);
299 EXPECT_STREQ(message_text, "Error_code()");
300}

◆ TEST() [10/16]

TEST ( FlMethodCodecTest  ,
EncodeMethodCall   
)

Definition at line 181 of file fl_method_codec_test.cc.

181 {
182 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
183
184 g_autoptr(GError) error = nullptr;
186 FL_METHOD_CODEC(codec), "foo", nullptr, &error);
187 EXPECT_EQ(error, nullptr);
188 EXPECT_NE(message, nullptr);
189
190 g_autofree gchar* message_text = message_to_text(message);
191 EXPECT_STREQ(message_text, "foo()");
192}
GBytes * fl_method_codec_encode_method_call(FlMethodCodec *self, const gchar *name, FlValue *args, GError **error)

◆ TEST() [11/16]

TEST ( FlMethodCodecTest  ,
EncodeMethodCallArgs   
)

Definition at line 207 of file fl_method_codec_test.cc.

207 {
208 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
209
210 g_autoptr(FlValue) args = fl_value_new_int(42);
211 g_autoptr(GError) error = nullptr;
213 FL_METHOD_CODEC(codec), "foo", args, &error);
214 EXPECT_EQ(error, nullptr);
215 EXPECT_NE(message, nullptr);
216
217 g_autofree gchar* message_text = message_to_text(message);
218 EXPECT_STREQ(message_text, "foo(42)");
219}

◆ TEST() [12/16]

TEST ( FlMethodCodecTest  ,
EncodeMethodCallEmptyName   
)

Definition at line 194 of file fl_method_codec_test.cc.

194 {
195 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
196
197 g_autoptr(GError) error = nullptr;
199 FL_METHOD_CODEC(codec), "", nullptr, &error);
200 EXPECT_EQ(error, nullptr);
201 EXPECT_NE(message, nullptr);
202
203 g_autofree gchar* message_text = message_to_text(message);
204 EXPECT_STREQ(message_text, "()");
205}

◆ TEST() [13/16]

TEST ( FlMethodCodecTest  ,
EncodeMethodCallError   
)

Definition at line 221 of file fl_method_codec_test.cc.

221 {
222 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
223
224 g_autoptr(FlValue) args = fl_value_new_bool(FALSE);
225 g_autoptr(GError) error = nullptr;
227 FL_METHOD_CODEC(codec), "foo", args, &error);
228 EXPECT_EQ(message, nullptr);
231}
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:255

◆ TEST() [14/16]

TEST ( FlMethodCodecTest  ,
EncodeSuccessEnvelope   
)

Definition at line 250 of file fl_method_codec_test.cc.

250 {
251 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
252
253 g_autoptr(FlValue) result = fl_value_new_int(42);
254 g_autoptr(GError) error = nullptr;
256 FL_METHOD_CODEC(codec), result, &error);
257 EXPECT_EQ(error, nullptr);
258 EXPECT_NE(message, nullptr);
259
260 g_autofree gchar* message_text = message_to_text(message);
261 EXPECT_STREQ(message_text, "42");
262}
GBytes * fl_method_codec_encode_success_envelope(FlMethodCodec *self, FlValue *result, GError **error)

◆ TEST() [15/16]

TEST ( FlMethodCodecTest  ,
EncodeSuccessEnvelopeEmpty   
)

Definition at line 264 of file fl_method_codec_test.cc.

264 {
265 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
266
267 g_autoptr(GError) error = nullptr;
269 FL_METHOD_CODEC(codec), nullptr, &error);
270 EXPECT_EQ(error, nullptr);
271 EXPECT_NE(message, nullptr);
272
273 g_autofree gchar* message_text = message_to_text(message);
274 EXPECT_STREQ(message_text, "(null)");
275}

◆ TEST() [16/16]

TEST ( FlMethodCodecTest  ,
EncodeSuccessEnvelopeError   
)

Definition at line 277 of file fl_method_codec_test.cc.

277 {
278 g_autoptr(FlTestMethodCodec) codec = fl_test_method_codec_new();
279
280 g_autoptr(FlValue) result = fl_value_new_string("X");
281 g_autoptr(GError) error = nullptr;
283 FL_METHOD_CODEC(codec), result, &error);
284 EXPECT_EQ(message, nullptr);
287}

◆ text_to_message()

static GBytes * text_to_message ( const gchar *  text)
static

Definition at line 38 of file fl_method_codec_test.cc.

38 {
39 return g_bytes_new(text, strlen(text));
40}