|
| 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 | +#include "flutter/shell/platform/linux/public/flutter_linux/fl_binary_codec.h" |
| 6 | +#include "flutter/shell/platform/linux/fl_test.h" |
| 7 | +#include "gtest/gtest.h" |
| 8 | + |
| 9 | +static gchar* encode_message(FlValue* value) { |
| 10 | + g_autoptr(FlBinaryCodec) codec = fl_binary_codec_new(); |
| 11 | + g_autoptr(GError) error = nullptr; |
| 12 | + g_autoptr(GBytes) message = |
| 13 | + fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error); |
| 14 | + EXPECT_NE(message, nullptr); |
| 15 | + EXPECT_EQ(error, nullptr); |
| 16 | + |
| 17 | + return bytes_to_hex_string(message); |
| 18 | +} |
| 19 | + |
| 20 | +static void encode_message_error(FlValue* value, GQuark domain, int code) { |
| 21 | + g_autoptr(FlBinaryCodec) codec = fl_binary_codec_new(); |
| 22 | + g_autoptr(GError) error = nullptr; |
| 23 | + g_autoptr(GBytes) message = |
| 24 | + fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error); |
| 25 | + EXPECT_EQ(message, nullptr); |
| 26 | + EXPECT_TRUE(g_error_matches(error, domain, code)); |
| 27 | +} |
| 28 | + |
| 29 | +static FlValue* decode_message(const char* hex_string) { |
| 30 | + g_autoptr(FlBinaryCodec) codec = fl_binary_codec_new(); |
| 31 | + g_autoptr(GBytes) data = hex_string_to_bytes(hex_string); |
| 32 | + g_autoptr(GError) error = nullptr; |
| 33 | + g_autoptr(FlValue) value = |
| 34 | + fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), data, &error); |
| 35 | + EXPECT_EQ(error, nullptr); |
| 36 | + EXPECT_NE(value, nullptr); |
| 37 | + return fl_value_ref(value); |
| 38 | +} |
| 39 | + |
| 40 | +TEST(FlBinaryCodecTest, EncodeData) { |
| 41 | + uint8_t data[] = {0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF}; |
| 42 | + g_autoptr(FlValue) value = fl_value_uint8_list_new(data, 6); |
| 43 | + g_autofree gchar* hex_string = encode_message(value); |
| 44 | + EXPECT_STREQ(hex_string, "000102fdfeff"); |
| 45 | +} |
| 46 | + |
| 47 | +TEST(FlBinaryCodecTest, EncodeEmpty) { |
| 48 | + g_autoptr(FlValue) value = fl_value_uint8_list_new(nullptr, 0); |
| 49 | + g_autofree gchar* hex_string = encode_message(value); |
| 50 | + EXPECT_STREQ(hex_string, ""); |
| 51 | +} |
| 52 | + |
| 53 | +TEST(FlBinaryCodecTest, EncodeNULL) { |
| 54 | + encode_message_error(nullptr, FL_MESSAGE_CODEC_ERROR, |
| 55 | + FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE); |
| 56 | +} |
| 57 | + |
| 58 | +TEST(FlBinaryCodecTest, EncodeUnknownType) { |
| 59 | + g_autoptr(FlValue) value = fl_value_null_new(); |
| 60 | + encode_message_error(value, FL_MESSAGE_CODEC_ERROR, |
| 61 | + FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE); |
| 62 | +} |
| 63 | + |
| 64 | +TEST(FlBinaryCodecTest, DecodeData) { |
| 65 | + g_autoptr(FlValue) value = decode_message("000102fdfeff"); |
| 66 | + ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST); |
| 67 | + ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(6)); |
| 68 | + EXPECT_EQ(fl_value_get_uint8_list(value)[0], 0x00); |
| 69 | + EXPECT_EQ(fl_value_get_uint8_list(value)[1], 0x01); |
| 70 | + EXPECT_EQ(fl_value_get_uint8_list(value)[2], 0x02); |
| 71 | + EXPECT_EQ(fl_value_get_uint8_list(value)[3], 0xFD); |
| 72 | + EXPECT_EQ(fl_value_get_uint8_list(value)[4], 0xFE); |
| 73 | + EXPECT_EQ(fl_value_get_uint8_list(value)[5], 0xFF); |
| 74 | +} |
| 75 | + |
| 76 | +TEST(FlBinaryCodecTest, DecodeEmpty) { |
| 77 | + g_autoptr(FlValue) value = decode_message(""); |
| 78 | + ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST); |
| 79 | + ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(0)); |
| 80 | +} |
0 commit comments