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, 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, 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/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeNil   
)

Definition at line 33 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode().

33  {
34  checkEncodeDecode(nil, nil);
35 }
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [2/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeNSNull   
)

Definition at line 37 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

37  {
38  uint8_t bytes[1] = {0x00};
39  checkEncodeDecode([NSNull null], [NSData dataWithBytes:bytes length:1]);
40 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [3/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeYes   
)

Definition at line 42 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

42  {
43  uint8_t bytes[1] = {0x01};
44  checkEncodeDecode(@YES, [NSData dataWithBytes:bytes length:1]);
45 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [4/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeNo   
)

Definition at line 47 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

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

◆ TEST() [5/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeUInt8   
)

Definition at line 52 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

52  {
53  uint8_t bytes[5] = {0x03, 0xfe, 0x00, 0x00, 0x00};
54  UInt8 value = 0xfe;
55  checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:5]);
56 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [6/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeUInt16   
)

Definition at line 58 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

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

◆ TEST() [7/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeUInt32   
)

Definition at line 64 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

64  {
65  uint8_t bytes[9] = {0x04, 0x09, 0xba, 0xdc, 0xfe, 0x00, 0x00, 0x00, 0x00};
66  UInt32 value = 0xfedcba09;
67  checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:9]);
68 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [8/31]

TEST ( FlutterStandardCodec  ,
CanEncodeUInt64   
)

Definition at line 70 of file flutter_standard_codec_unittest.mm.

References length.

70  {
72  UInt64 u64 = 0xfffffffffffffffa;
73  uint8_t bytes[9] = {0x04, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
74  NSData* encoded = [codec encode:@(u64)];
75  ASSERT_TRUE([encoded isEqual:[NSData dataWithBytes:bytes length:9]]);
76 }
size_t length

◆ TEST() [9/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeSInt8   
)

Definition at line 78 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

78  {
79  uint8_t bytes[5] = {0x03, 0xfe, 0xff, 0xff, 0xff};
80  SInt8 value = 0xfe;
81  checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:5]);
82 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [10/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeSInt16   
)

Definition at line 84 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

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

◆ TEST() [11/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeSInt32   
)

Definition at line 90 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

90  {
91  uint8_t bytes[5] = {0x03, 0x78, 0x56, 0x34, 0x12};
92  checkEncodeDecode(@(0x12345678), [NSData dataWithBytes:bytes length:5]);
93 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [12/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeSInt64   
)

Definition at line 95 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

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

◆ TEST() [13/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeFloat32   
)

Definition at line 100 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

100  {
101  uint8_t bytes[16] = {0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102  0x00, 0x00, 0x00, 0x60, 0xfb, 0x21, 0x09, 0x40};
103  checkEncodeDecode(@3.1415927f, [NSData dataWithBytes:bytes length:16]);
104 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [14/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeFloat64   
)

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  0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40};
109  checkEncodeDecode(@3.14159265358979311599796346854, [NSData dataWithBytes:bytes length:16]);
110 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [15/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeString   
)

Definition at line 112 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

112  {
113  uint8_t bytes[13] = {0x07, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
114  0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64};
115  checkEncodeDecode(@"hello world", [NSData dataWithBytes:bytes length:13]);
116 }
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [16/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeStringWithNonAsciiCodePoint   
)

Definition at line 118 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

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

◆ TEST() [17/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeStringWithNonBMPCodePoint   
)

Definition at line 123 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and length.

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

◆ TEST() [18/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeArray   
)

Definition at line 128 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and value.

128  {
129  NSArray* value = @[ [NSNull null], @"hello", @3.14, @47, @{@42 : @"nested"} ];
130  checkEncodeDecode(value);
131 }
uint8_t value
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [19/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeDictionary   
)

Definition at line 133 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), and value.

133  {
134  NSDictionary* value =
135  @{@"a" : @3.14,
136  @"b" : @47,
137  [NSNull null] : [NSNull null],
138  @3.14 : @[ @"nested" ]};
139  checkEncodeDecode(value);
140 }
uint8_t value
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [20/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeByteArray   
)

Definition at line 142 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

142  {
143  uint8_t bytes[4] = {0xBA, 0x5E, 0xBA, 0x11};
144  NSData* data = [NSData dataWithBytes:bytes length:4];
145  FlutterStandardTypedData* value = [FlutterStandardTypedData typedDataWithBytes:data];
146  checkEncodeDecode(value);
147 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [21/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeNSData   
)

Definition at line 149 of file flutter_standard_codec_unittest.mm.

References length.

149  {
151  uint8_t bytes[4] = {0xBA, 0x5E, 0xBA, 0x11};
152  NSData* data = [NSData dataWithBytes:bytes length:4];
153  FlutterStandardTypedData* standardData = [FlutterStandardTypedData typedDataWithBytes:data];
154 
155  NSData* encoded = [codec encode:data];
156  ASSERT_TRUE([encoded isEqual:[codec encode:standardData]]);
157 }
size_t length

◆ TEST() [22/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeInt32Array   
)

Definition at line 159 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

159  {
160  uint8_t bytes[8] = {0xBA, 0x5E, 0xBA, 0x11, 0xff, 0xff, 0xff, 0xff};
161  NSData* data = [NSData dataWithBytes:bytes length:8];
162  FlutterStandardTypedData* value = [FlutterStandardTypedData typedDataWithInt32:data];
163  checkEncodeDecode(value);
164 }
uint8_t value
size_t length
void checkEncodeDecode(id value, NSData *expectedEncoding)

◆ TEST() [23/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeInt64Array   
)

Definition at line 166 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

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

◆ TEST() [24/31]

TEST ( FlutterStandardCodec  ,
CanEncodeAndDecodeFloat64Array   
)

Definition at line 173 of file flutter_standard_codec_unittest.mm.

References checkEncodeDecode(), length, and value.

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

◆ TEST() [25/31]

TEST ( FlutterStandardCodec  ,
HandlesMethodCallsWithNilArguments   
)

Definition at line 181 of file flutter_standard_codec_unittest.mm.

181  {
182  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
183  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"hello" arguments:nil];
184  NSData* encoded = [codec encodeMethodCall:call];
185  FlutterMethodCall* decoded = [codec decodeMethodCall:encoded];
186  ASSERT_TRUE([decoded isEqual:call]);
187 }

◆ TEST() [26/31]

TEST ( FlutterStandardCodec  ,
HandlesMethodCallsWithSingleArgument   
)

Definition at line 189 of file flutter_standard_codec_unittest.mm.

189  {
190  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
191  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"hello" arguments:@42];
192  NSData* encoded = [codec encodeMethodCall:call];
193  FlutterMethodCall* decoded = [codec decodeMethodCall:encoded];
194  ASSERT_TRUE([decoded isEqual:call]);
195 }

◆ TEST() [27/31]

TEST ( FlutterStandardCodec  ,
HandlesMethodCallsWithArgumentList   
)

Definition at line 197 of file flutter_standard_codec_unittest.mm.

197  {
198  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
199  NSArray* arguments = @[ @42, @"world" ];
200  FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"hello"
201  arguments:arguments];
202  NSData* encoded = [codec encodeMethodCall:call];
203  FlutterMethodCall* decoded = [codec decodeMethodCall:encoded];
204  ASSERT_TRUE([decoded isEqual:call]);
205 }

◆ TEST() [28/31]

TEST ( FlutterStandardCodec  ,
HandlesSuccessEnvelopesWithNilResult   
)

Definition at line 207 of file flutter_standard_codec_unittest.mm.

207  {
208  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
209  NSData* encoded = [codec encodeSuccessEnvelope:nil];
210  id decoded = [codec decodeEnvelope:encoded];
211  ASSERT_TRUE(decoded == nil);
212 }

◆ TEST() [29/31]

TEST ( FlutterStandardCodec  ,
HandlesSuccessEnvelopesWithSingleResult   
)

Definition at line 214 of file flutter_standard_codec_unittest.mm.

214  {
215  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
216  NSData* encoded = [codec encodeSuccessEnvelope:@42];
217  id decoded = [codec decodeEnvelope:encoded];
218  ASSERT_TRUE([decoded isEqual:@42]);
219 }

◆ TEST() [30/31]

TEST ( FlutterStandardCodec  ,
HandlesSuccessEnvelopesWithResultMap   
)

Definition at line 221 of file flutter_standard_codec_unittest.mm.

221  {
222  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
223  NSDictionary* result = @{@"a" : @42, @42 : @"a"};
224  NSData* encoded = [codec encodeSuccessEnvelope:result];
225  id decoded = [codec decodeEnvelope:encoded];
226  ASSERT_TRUE([decoded isEqual:result]);
227 }

◆ TEST() [31/31]

TEST ( FlutterStandardCodec  ,
HandlesErrorEnvelopes   
)

Definition at line 229 of file flutter_standard_codec_unittest.mm.

References error.

229  {
230  FlutterStandardMethodCodec* codec = [FlutterStandardMethodCodec sharedInstance];
231  NSDictionary* details = @{@"a" : @42, @42 : @"a"};
232  FlutterError* error = [FlutterError errorWithCode:@"errorCode"
233  message:@"something failed"
234  details:details];
235  NSData* encoded = [codec encodeErrorEnvelope:error];
236  id decoded = [codec decodeEnvelope:encoded];
237  ASSERT_TRUE([decoded isEqual:error]);
238 }
FlMethodResponse GError ** error