Flutter Engine
flutter_standard_codec_unittest.mm File Reference
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h"
#include "gtest/gtest.h"

Go to the source code of this file.

Functions

void checkEncodeDecode (id value, NSData *expectedEncoding)
 
void checkEncodeDecode (id value)
 
 TEST (FlutterStandardCodec, CanDecodeZeroLength)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeNil)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeNSNull)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeYes)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeNo)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeUInt8)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeUInt16)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeUInt32)
 
 TEST (FlutterStandardCodec, CanEncodeUInt64)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeSInt8)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeSInt16)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeSInt32)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeSInt64)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeFloat32)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeFloat64)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeString)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeStringWithNonAsciiCodePoint)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeStringWithNonBMPCodePoint)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeArray)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeDictionary)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeByteArray)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeNSData)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeInt32Array)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeInt64Array)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeFloat32Array)
 
 TEST (FlutterStandardCodec, CanEncodeAndDecodeFloat64Array)
 
 TEST (FlutterStandardCodec, HandlesMethodCallsWithNilArguments)
 
 TEST (FlutterStandardCodec, HandlesMethodCallsWithSingleArgument)
 
 TEST (FlutterStandardCodec, HandlesMethodCallsWithArgumentList)
 
 TEST (FlutterStandardCodec, HandlesSuccessEnvelopesWithNilResult)
 
 TEST (FlutterStandardCodec, HandlesSuccessEnvelopesWithSingleResult)
 
 TEST (FlutterStandardCodec, HandlesSuccessEnvelopesWithResultMap)
 
 TEST (FlutterStandardCodec, HandlesErrorEnvelopes)
 

Function Documentation

◆ checkEncodeDecode() [1/2]

void checkEncodeDecode ( id  value,
NSData *  expectedEncoding 
)

Definition at line 9 of file flutter_standard_codec_unittest.mm.

References value.

Referenced by TEST().

9  {
11  NSData* encoded = [codec encode:value];
12  if (expectedEncoding == nil)
13  ASSERT_TRUE(encoded == nil);
14  else
15  ASSERT_TRUE([encoded isEqual:expectedEncoding]);
16  id decoded = [codec decode:encoded];
17  if (value == nil || value == [NSNull null])
18  ASSERT_TRUE(decoded == nil);
19  else
20  ASSERT_TRUE([value isEqual:decoded]);
21 }
uint8_t value

◆ checkEncodeDecode() [2/2]

void checkEncodeDecode ( id  value)

Definition at line 23 of file flutter_standard_codec_unittest.mm.

References value.

23  {
25  NSData* encoded = [codec encode:value];
26  id decoded = [codec decode:encoded];
27  if (value == nil || value == [NSNull null])
28  ASSERT_TRUE(decoded == nil);
29  else
30  ASSERT_TRUE([value isEqual:decoded]);
31 }
uint8_t value

◆ TEST() [1/33]

TEST ( FlutterStandardCodec  ,
CanDecodeZeroLength   
)

Definition at line 33 of file flutter_standard_codec_unittest.mm.

33  {
35  id decoded = [codec decode:[NSData data]];
36  ASSERT_TRUE(decoded == nil);
37 }

◆ TEST() [2/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeNil   
)

Definition at line 39 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode().

39  {
40  checkEncodeDecode(nil, nil);
41 }
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [3/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeNSNull   
)

Definition at line 43 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

43  {
44  uint8_t bytes[1] = {0x00};
45  checkEncodeDecode([NSNull null], [NSData dataWithBytes:bytes length:1]);
46 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [4/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeYes   
)

Definition at line 48 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

48  {
49  uint8_t bytes[1] = {0x01};
50  checkEncodeDecode(@YES, [NSData dataWithBytes:bytes length:1]);
51 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [5/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeNo   
)

Definition at line 53 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

53  {
54  uint8_t bytes[1] = {0x02};
55  checkEncodeDecode(@NO, [NSData dataWithBytes:bytes length:1]);
56 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [6/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeUInt8   
)

Definition at line 58 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

58  {
59  uint8_t bytes[5] = {0x03, 0xfe, 0x00, 0x00, 0x00};
60  UInt8 value = 0xfe;
61  checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:5]);
62 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [7/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeUInt16   
)

Definition at line 64 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

64  {
65  uint8_t bytes[5] = {0x03, 0xdc, 0xfe, 0x00, 0x00};
66  UInt16 value = 0xfedc;
67  checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:5]);
68 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [8/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeUInt32   
)

Definition at line 70 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

70  {
71  uint8_t bytes[9] = {0x04, 0x09, 0xba, 0xdc, 0xfe, 0x00, 0x00, 0x00, 0x00};
72  UInt32 value = 0xfedcba09;
73  checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:9]);
74 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [9/33]

TEST ( FlutterStandardCodec  ,
CanEncodeUInt64   
)

Definition at line 76 of file flutter_standard_codec_unittest.mm.

References length.

76  {
78  UInt64 u64 = 0xfffffffffffffffa;
79  uint8_t bytes[9] = {0x04, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
80  NSData* encoded = [codec encode:@(u64)];
81  ASSERT_TRUE([encoded isEqual:[NSData dataWithBytes:bytes length:9]]);
82 }
size_t length

◆ TEST() [10/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeSInt8   
)

Definition at line 84 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

84  {
85  uint8_t bytes[5] = {0x03, 0xfe, 0xff, 0xff, 0xff};
86  SInt8 value = 0xfe;
87  checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:5]);
88 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [11/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeSInt16   
)

Definition at line 90 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

90  {
91  uint8_t bytes[5] = {0x03, 0xdc, 0xfe, 0xff, 0xff};
92  SInt16 value = 0xfedc;
93  checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:5]);
94 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [12/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeSInt32   
)

Definition at line 96 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

96  {
97  uint8_t bytes[5] = {0x03, 0x78, 0x56, 0x34, 0x12};
98  checkEncodeDecode(@(0x12345678), [NSData dataWithBytes:bytes length:5]);
99 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [13/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeSInt64   
)

Definition at line 101 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

101  {
102  uint8_t bytes[9] = {0x04, 0xef, 0xcd, 0xab, 0x90, 0x78, 0x56, 0x34, 0x12};
103  checkEncodeDecode(@(0x1234567890abcdef), [NSData dataWithBytes:bytes length:9]);
104 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [14/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeFloat32   
)

Definition at line 106 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

106  {
107  uint8_t bytes[16] = {0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108  0x00, 0x00, 0x00, 0x60, 0xfb, 0x21, 0x09, 0x40};
109  checkEncodeDecode(@3.1415927f, [NSData dataWithBytes:bytes length:16]);
110 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [15/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeFloat64   
)

Definition at line 112 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

112  {
113  uint8_t bytes[16] = {0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114  0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40};
115  checkEncodeDecode(@3.14159265358979311599796346854, [NSData dataWithBytes:bytes length:16]);
116 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [16/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeString   
)

Definition at line 118 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

118  {
119  uint8_t bytes[13] = {0x07, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
120  0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64};
121  checkEncodeDecode(@"hello world", [NSData dataWithBytes:bytes length:13]);
122 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [17/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeStringWithNonAsciiCodePoint   
)

Definition at line 124 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

124  {
125  uint8_t bytes[7] = {0x07, 0x05, 0x68, 0xe2, 0x98, 0xba, 0x77};
126  checkEncodeDecode(@"h\u263Aw", [NSData dataWithBytes:bytes length:7]);
127 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [18/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeStringWithNonBMPCodePoint   
)

Definition at line 129 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

129  {
130  uint8_t bytes[8] = {0x07, 0x06, 0x68, 0xf0, 0x9f, 0x98, 0x82, 0x77};
131  checkEncodeDecode(@"h\U0001F602w", [NSData dataWithBytes:bytes length:8]);
132 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [19/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeArray   
)

Definition at line 134 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and value.

134  {
135  NSArray* value = @[ [NSNull null], @"hello", @3.14, @47, @{@42 : @"nested"} ];
136  checkEncodeDecode(value);
137 }
uint8_t value
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [20/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeDictionary   
)

Definition at line 139 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and value.

139  {
140  NSDictionary* value =
141  @{@"a" : @3.14,
142  @"b" : @47,
143  [NSNull null] : [NSNull null],
144  @3.14 : @[ @"nested" ]};
145  checkEncodeDecode(value);
146 }
uint8_t value
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [21/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeByteArray   
)

Definition at line 148 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

148  {
149  uint8_t bytes[4] = {0xBA, 0x5E, 0xBA, 0x11};
150  NSData* data = [NSData dataWithBytes:bytes length:4];
151  FlutterStandardTypedData* value = [FlutterStandardTypedData typedDataWithBytes:data];
152  checkEncodeDecode(value);
153 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [22/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeNSData   
)

Definition at line 155 of file flutter_standard_codec_unittest.mm.

References length.

155  {
157  uint8_t bytes[4] = {0xBA, 0x5E, 0xBA, 0x11};
158  NSData* data = [NSData dataWithBytes:bytes length:4];
159  FlutterStandardTypedData* standardData = [FlutterStandardTypedData typedDataWithBytes:data];
160 
161  NSData* encoded = [codec encode:data];
162  ASSERT_TRUE([encoded isEqual:[codec encode:standardData]]);
163 }
size_t length

◆ TEST() [23/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeInt32Array   
)

Definition at line 165 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

165  {
166  uint8_t bytes[8] = {0xBA, 0x5E, 0xBA, 0x11, 0xff, 0xff, 0xff, 0xff};
167  NSData* data = [NSData dataWithBytes:bytes length:8];
168  FlutterStandardTypedData* value = [FlutterStandardTypedData typedDataWithInt32:data];
169  checkEncodeDecode(value);
170 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [24/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeInt64Array   
)

Definition at line 172 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

172  {
173  uint8_t bytes[8] = {0xBA, 0x5E, 0xBA, 0x11, 0xff, 0xff, 0xff, 0xff};
174  NSData* data = [NSData dataWithBytes:bytes length:8];
175  FlutterStandardTypedData* value = [FlutterStandardTypedData typedDataWithInt64:data];
176  checkEncodeDecode(value);
177 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [25/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeFloat32Array   
)

Definition at line 179 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

179  {
180  uint8_t bytes[8] = {0xd8, 0x0f, 0x49, 0x40, 0x00, 0x00, 0x7a, 0x44};
181  NSData* data = [NSData dataWithBytes:bytes length:8];
182  FlutterStandardTypedData* value = [FlutterStandardTypedData typedDataWithFloat32:data];
183  checkEncodeDecode(value);
184 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [26/33]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeFloat64Array   
)

Definition at line 186 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

186  {
187  uint8_t bytes[16] = {0xBA, 0x5E, 0xBA, 0x11, 0xff, 0xff, 0xff, 0xff,
188  0xBA, 0x5E, 0xBA, 0x11, 0xff, 0xff, 0xff, 0xff};
189  NSData* data = [NSData dataWithBytes:bytes length:16];
190  FlutterStandardTypedData* value = [FlutterStandardTypedData typedDataWithFloat64:data];
191  checkEncodeDecode(value);
192 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [27/33]

TEST ( FlutterStandardCodec  ,
HandlesMethodCallsWithNilArguments   
)

Definition at line 194 of file flutter_standard_codec_unittest.mm.

194  {
195  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
196  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"hello" arguments:nil];
197  NSData* encoded = [codec encodeMethodCall:call];
198  FlutterMethodCall* decoded = [codec decodeMethodCall:encoded];
199  ASSERT_TRUE([decoded isEqual:call]);
200 }

◆ TEST() [28/33]

TEST ( FlutterStandardCodec  ,
HandlesMethodCallsWithSingleArgument   
)

Definition at line 202 of file flutter_standard_codec_unittest.mm.

202  {
203  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
204  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"hello" arguments:@42];
205  NSData* encoded = [codec encodeMethodCall:call];
206  FlutterMethodCall* decoded = [codec decodeMethodCall:encoded];
207  ASSERT_TRUE([decoded isEqual:call]);
208 }

◆ TEST() [29/33]

TEST ( FlutterStandardCodec  ,
HandlesMethodCallsWithArgumentList   
)

Definition at line 210 of file flutter_standard_codec_unittest.mm.

210  {
211  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
212  NSArray* arguments = @[ @42, @"world" ];
213  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"hello"
214  arguments:arguments];
215  NSData* encoded = [codec encodeMethodCall:call];
216  FlutterMethodCall* decoded = [codec decodeMethodCall:encoded];
217  ASSERT_TRUE([decoded isEqual:call]);
218 }

◆ TEST() [30/33]

TEST ( FlutterStandardCodec  ,
HandlesSuccessEnvelopesWithNilResult   
)

Definition at line 220 of file flutter_standard_codec_unittest.mm.

220  {
221  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
222  NSData* encoded = [codec encodeSuccessEnvelope:nil];
223  id decoded = [codec decodeEnvelope:encoded];
224  ASSERT_TRUE(decoded == nil);
225 }

◆ TEST() [31/33]

TEST ( FlutterStandardCodec  ,
HandlesSuccessEnvelopesWithSingleResult   
)

Definition at line 227 of file flutter_standard_codec_unittest.mm.

227  {
228  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
229  NSData* encoded = [codec encodeSuccessEnvelope:@42];
230  id decoded = [codec decodeEnvelope:encoded];
231  ASSERT_TRUE([decoded isEqual:@42]);
232 }

◆ TEST() [32/33]

TEST ( FlutterStandardCodec  ,
HandlesSuccessEnvelopesWithResultMap   
)

Definition at line 234 of file flutter_standard_codec_unittest.mm.

References result.

234  {
235  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
236  NSDictionary* result = @{@"a" : @42, @42 : @"a"};
237  NSData* encoded = [codec encodeSuccessEnvelope:result];
238  id decoded = [codec decodeEnvelope:encoded];
239  ASSERT_TRUE([decoded isEqual:result]);
240 }
GAsyncResult * result

◆ TEST() [33/33]

TEST ( FlutterStandardCodec  ,
HandlesErrorEnvelopes   
)

Definition at line 242 of file flutter_standard_codec_unittest.mm.

References error.

242  {
243  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
244  NSDictionary* details = @{@"a" : @42, @42 : @"a"};
245  FlutterError* error = [FlutterError errorWithCode:@"errorCode"
246  message:@"something failed"
247  details:details];
248  NSData* encoded = [codec encodeErrorEnvelope:error];
249  id decoded = [codec decodeEnvelope:encoded];
250  ASSERT_TRUE([decoded isEqual:error]);
251 }
const uint8_t uint32_t uint32_t GError ** error