Flutter Engine
The Flutter Engine
flutter_codecs_unittest.mm
Go to the documentation of this file.
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#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h"
6
7#include "gtest/gtest.h"
8
10
11TEST(FlutterStringCodec, CanEncodeAndDecodeNil) {
12 FlutterStringCodec* codec = [FlutterStringCodec sharedInstance];
13 ASSERT_TRUE([codec encode:nil] == nil);
14 ASSERT_TRUE([codec decode:nil] == nil);
15}
16
17TEST(FlutterStringCodec, CanEncodeAndDecodeEmptyString) {
18 FlutterStringCodec* codec = [FlutterStringCodec sharedInstance];
19 ASSERT_TRUE([[codec encode:@""] isEqualTo:[NSData data]]);
20 ASSERT_TRUE([[codec decode:[NSData data]] isEqualTo:@""]);
21}
22
23TEST(FlutterStringCodec, CanEncodeAndDecodeAsciiString) {
24 NSString* value = @"hello world";
25 FlutterStringCodec* codec = [FlutterStringCodec sharedInstance];
26 NSData* encoded = [codec encode:value];
27 NSString* decoded = [codec decode:encoded];
28 ASSERT_TRUE([value isEqualTo:decoded]);
29}
30
31TEST(FlutterStringCodec, CanEncodeAndDecodeNonAsciiString) {
32 NSString* value = @"hello \u263A world";
33 FlutterStringCodec* codec = [FlutterStringCodec sharedInstance];
34 NSData* encoded = [codec encode:value];
35 NSString* decoded = [codec decode:encoded];
36 ASSERT_TRUE([value isEqualTo:decoded]);
37}
38
39TEST(FlutterStringCodec, CanEncodeAndDecodeNonBMPString) {
40 NSString* value = @"hello \U0001F602 world";
41 FlutterStringCodec* codec = [FlutterStringCodec sharedInstance];
42 NSData* encoded = [codec encode:value];
43 NSString* decoded = [codec decode:encoded];
44 ASSERT_TRUE([value isEqualTo:decoded]);
45}
46
47TEST(FlutterJSONCodec, ThrowsOnInvalidEncode) {
48 NSString* value = [[NSString alloc] initWithBytes:"\xdf\xff"
49 length:2
50 encoding:NSUTF16StringEncoding];
51 FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance];
52 EXPECT_EXIT([codec encode:value], testing::KilledBySignal(SIGABRT), "failed to convert to UTF8");
53}
54
55TEST(FlutterJSONCodec, CanDecodeZeroLength) {
56 FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance];
57 ASSERT_TRUE([codec decode:[NSData data]] == nil);
58}
59
60TEST(FlutterJSONCodec, ThrowsOnInvalidDecode) {
61 NSString* value = @"{{{";
62 FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance];
63 EXPECT_EXIT([codec decode:[value dataUsingEncoding:value.fastestEncoding]],
64 testing::KilledBySignal(SIGABRT), "No string key for value in object around line 1");
65}
66
67TEST(FlutterJSONCodec, CanEncodeAndDecodeNil) {
68 FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance];
69 ASSERT_TRUE([codec encode:nil] == nil);
70 ASSERT_TRUE([codec decode:nil] == nil);
71}
72
73TEST(FlutterJSONCodec, CanEncodeAndDecodeArray) {
74 NSArray* value = @[ [NSNull null], @"hello", @3.14, @47, @{@"a" : @"nested"} ];
75 FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance];
76 NSData* encoded = [codec encode:value];
77 NSArray* decoded = [codec decode:encoded];
78 ASSERT_TRUE([value isEqualTo:decoded]);
79}
80
81TEST(FlutterJSONCodec, CanEncodeAndDecodeDictionary) {
82 NSDictionary* value = @{@"a" : @3.14, @"b" : @47, @"c" : [NSNull null], @"d" : @[ @"nested" ]};
83 FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance];
84 NSData* encoded = [codec encode:value];
85 NSDictionary* decoded = [codec decode:encoded];
86 ASSERT_TRUE([value isEqualTo:decoded]);
87}
static void encode(uint8_t output[16], const uint32_t input[4])
Definition: SkMD5.cpp:240
uint8_t value
FLUTTER_ASSERT_ARC TEST(FlutterStringCodec, CanEncodeAndDecodeNil)
size_t length
static DecodeResult decode(std::string path)
Definition: png_codec.cpp:124
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63