From 0027d03af9be29fb1a291a692ac99aeb76c277ea Mon Sep 17 00:00:00 2001 From: Mikkel Nygaard Ravn Date: Wed, 22 Mar 2017 15:48:59 +0100 Subject: [PATCH] Revert "Improved value coverage of Flutter standard codec. Added unit tests. (#3493)" (flutter/engine#3496) This reverts commit 3f0d2dc586be84675683e9eee607c2b28bad75eb. --- .../ios/framework/Source/FlutterChannels.mm | 175 +++++------ .../framework/Source/FlutterStandardCodec.mm | 74 +---- .../Source/flutter_standard_codec_unittest.mm | 275 ++++++------------ 3 files changed, 172 insertions(+), 352 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterChannels.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterChannels.mm index a15e4edcd9..0f6362861e 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterChannels.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterChannels.mm @@ -38,33 +38,33 @@ } - (void)sendMessage:(id)message { - [_messenger sendBinaryMessage:[_codec encode:message] channelName:_name]; + [_messenger sendBinaryMessage:[_codec encode:message] + channelName:_name]; } - (void)sendMessage:(id)message replyHandler:(FlutterReplyHandler)handler { - FlutterBinaryReplyHandler replyHandler = ^(NSData* reply) { - if (handler) - handler([_codec decode:reply]); - }; [_messenger sendBinaryMessage:[_codec encode:message] channelName:_name - binaryReplyHandler:replyHandler]; + binaryReplyHandler:^(NSData* reply) { + if (handler) + handler([_codec decode:reply]); + }]; } - (void)setMessageHandler:(FlutterMessageHandler)handler { - if (!handler) { + if (handler) { + [_messenger setBinaryMessageHandlerOnChannel:_name + binaryMessageHandler:^( + NSData* message, + FlutterBinaryReplyHandler replyHandler) { + handler([_codec decode:message], ^(id reply) { + replyHandler([_codec encode:reply]); + }); + }]; + } else { [_messenger setBinaryMessageHandlerOnChannel:_name binaryMessageHandler:nil]; - return; } - FlutterBinaryMessageHandler messageHandler = - ^(NSData* message, FlutterBinaryReplyHandler replyHandler) { - handler([_codec decode:message], ^(id reply) { - replyHandler([_codec encode:reply]); - }); - }; - [_messenger setBinaryMessageHandlerOnChannel:_name - binaryMessageHandler:messageHandler]; } @end @@ -82,7 +82,6 @@ - (instancetype)initWithCode:(NSString*)code message:(NSString*)message details:(id)details { - NSAssert(code, @"Code cannot be nil"); if (self = [super init]) { _code = [code retain]; _message = [message retain]; @@ -97,23 +96,6 @@ [_details release]; [super dealloc]; } - -- (BOOL)isEqual:(id)object { - if (self == object) - return YES; - if (![object isKindOfClass:[FlutterError class]]) - return NO; - FlutterError* other = (FlutterError*)object; - return [self.code isEqual:other.code] && - ((!self.message && !other.message) || - [self.message isEqual:other.message]) && - ((!self.details && !other.details) || - [self.details isEqual:other.details]); -} - -- (NSUInteger)hash { - return [self.code hash] ^ [self.message hash] ^ [self.details hash]; -} @end @implementation FlutterMethodCall @@ -124,7 +106,6 @@ } - (instancetype)initWithMethodName:(NSString*)method arguments:(id)arguments { - NSAssert(method, @"Method name cannot be nil"); if (self = [super init]) { _method = [method retain]; _arguments = [arguments retain]; @@ -137,21 +118,6 @@ [_arguments release]; [super dealloc]; } - -- (BOOL)isEqual:(id)object { - if (self == object) - return YES; - if (![object isKindOfClass:[FlutterMethodCall class]]) - return NO; - FlutterMethodCall* other = (FlutterMethodCall*)object; - return [self.method isEqual:[other method]] && - ((!self.arguments && !other.arguments) || - [self.arguments isEqual:other.arguments]); -} - -- (NSUInteger)hash { - return [self.method hash] ^ [self.arguments hash]; -} @end @implementation FlutterMethodChannel { @@ -187,48 +153,37 @@ } - (void)invokeMethod:(NSString*)method arguments:(id)arguments { - FlutterMethodCall* methodCall = - [FlutterMethodCall methodCallWithMethodName:method arguments:arguments]; - NSData* message = [_codec encodeMethodCall:methodCall]; - [_messenger sendBinaryMessage:message channelName:_name]; + [_messenger sendBinaryMessage:[_codec encodeMethodCall:[FlutterMethodCall methodCallWithMethodName:method arguments:arguments]] + channelName:_name]; } - (void)invokeMethod:(NSString*)method arguments:(id)arguments resultReceiver:(FlutterResultReceiver)resultReceiver { - FlutterMethodCall* methodCall = - [FlutterMethodCall methodCallWithMethodName:method arguments:arguments]; - NSData* message = [_codec encodeMethodCall:methodCall]; - FlutterBinaryReplyHandler replyHandler = ^(NSData* reply) { - if (resultReceiver) { - FlutterError* flutterError = nil; - id result = [_codec decodeEnvelope:reply error:&flutterError]; - resultReceiver(result, flutterError); - } - }; - [_messenger sendBinaryMessage:message - channelName:_name - binaryReplyHandler:replyHandler]; + [_messenger sendBinaryMessage:[_codec encodeMethodCall:[FlutterMethodCall methodCallWithMethodName:method arguments:arguments]] + channelName:_name + binaryReplyHandler:^(NSData* reply) { + if (resultReceiver) { + FlutterError* flutterError = nil; + id result = [_codec decodeEnvelope:reply error:&flutterError]; + resultReceiver(result, flutterError); + } + }]; } - (void)setMethodCallHandler:(FlutterMethodCallHandler)handler { - if (!handler) { - [_messenger setBinaryMessageHandlerOnChannel:_name - binaryMessageHandler:nil]; - return; - } - FlutterBinaryMessageHandler messageHandler = - ^(NSData* message, FlutterBinaryReplyHandler reply) { - FlutterMethodCall* call = [_codec decodeMethodCall:message]; - handler(call, ^(id result, FlutterError* error) { - if (error) - reply([_codec encodeErrorEnvelope:error]); - else - reply([_codec encodeSuccessEnvelope:result]); - }); - }; - [_messenger setBinaryMessageHandlerOnChannel:_name - binaryMessageHandler:messageHandler]; + [_messenger + setBinaryMessageHandlerOnChannel:_name + binaryMessageHandler:^(NSData* message, + FlutterBinaryReplyHandler reply) { + FlutterMethodCall* call = [_codec decodeMethodCall:message]; + handler(call, ^(id result, FlutterError* error) { + if (error) + reply([_codec encodeErrorEnvelope:error]); + else + reply([_codec encodeSuccessEnvelope:result]); + }); + }]; } - (void)setStreamHandler:(FlutterStreamHandler)handler { @@ -237,29 +192,33 @@ binaryMessageHandler:nil]; return; } - FlutterBinaryMessageHandler messageHandler = ^( - NSData* message, FlutterBinaryReplyHandler reply) { - FlutterMethodCall* call = [_codec decodeMethodCall:message]; - FlutterResultReceiver resultReceiver = ^(id result, FlutterError* error) { - if (error) - reply([_codec encodeErrorEnvelope:error]); - else - reply([_codec encodeSuccessEnvelope:nil]); - }; - FlutterEventReceiver eventReceiver = - ^(id event, FlutterError* error, BOOL done) { - if (error) - [_messenger sendBinaryMessage:[_codec encodeErrorEnvelope:error] - channelName:_name]; - else if (done) - [_messenger sendBinaryMessage:[NSData data] channelName:_name]; - else - [_messenger sendBinaryMessage:[_codec encodeSuccessEnvelope:event] - channelName:_name]; - }; - handler(call, resultReceiver, eventReceiver); - }; - [_messenger setBinaryMessageHandlerOnChannel:_name - binaryMessageHandler:messageHandler]; + [_messenger + setBinaryMessageHandlerOnChannel:_name + binaryMessageHandler:^(NSData* message, + FlutterBinaryReplyHandler reply) { + FlutterMethodCall* call = [_codec decodeMethodCall:message]; + handler( + call, + ^(id result, FlutterError* error) { + if (error) + reply([_codec encodeErrorEnvelope:error]); + else + reply([_codec encodeSuccessEnvelope:nil]); + }, + ^(id event, FlutterError* error, BOOL done) { + if (error) + [_messenger + sendBinaryMessage:[_codec + encodeErrorEnvelope:error] + channelName:_name]; + else if (done) + [_messenger sendBinaryMessage:[NSData data] + channelName:_name]; + else + [_messenger sendBinaryMessage: + [_codec encodeSuccessEnvelope:event] + channelName:_name]; + }); + }]; } @end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterStandardCodec.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterStandardCodec.mm index c8e22a78fd..d110251cea 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterStandardCodec.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterStandardCodec.mm @@ -89,20 +89,19 @@ case 0: { result = [reader readValue]; NSAssert(![reader hasMore], @"Corrupted standard envelope"); - } break; + } + break; case 1: { id code = [reader readValue]; id message = [reader readValue]; id details = [reader readValue]; NSAssert(![reader hasMore], @"Corrupted standard envelope"); - NSAssert([code isKindOfClass:[NSString class]], - @"Invalid standard envelope"); - NSAssert(message == nil || [message isKindOfClass:[NSString class]], - @"Invalid standard envelope"); - *error = - [FlutterError errorWithCode:code message:message details:details]; + NSAssert([code isKindOfClass:[NSString class]], @"Invalid standard envelope"); + NSAssert(message == nil || [message isKindOfClass:[NSString class]], @"Invalid standard envelope"); + *error = [FlutterError errorWithCode:code message:message details:details]; result = nil; - } break; + } + break; } return result; } @@ -145,7 +144,6 @@ using namespace shell; - (instancetype)initWithData:(NSData*)data type:(FlutterStandardDataType)type { UInt8 elementSize = elementSizeForFlutterStandardDataType(type); - NSAssert(data, @"Data cannot be nil"); NSAssert(data.length % elementSize == 0, @"Data must contain integral number of elements"); if (self = [super init]) { @@ -161,20 +159,6 @@ using namespace shell; [_data release]; [super dealloc]; } - -- (BOOL)isEqual:(id)object { - if (self == object) - return YES; - if (![object isKindOfClass:[FlutterStandardTypedData class]]) - return NO; - FlutterStandardTypedData* other = (FlutterStandardTypedData*)object; - return self.type == other.type && self.elementCount == other.elementCount && - [self.data isEqual:other.data]; -} - -- (NSUInteger)hash { - return [self.data hash] ^ self.type; -} @end @implementation FlutterStandardBigInteger @@ -183,7 +167,6 @@ using namespace shell; } - (instancetype)initWithHex:(NSString*)hex { - NSAssert(hex, @"Hex cannot be nil"); if (self = [super init]) { _hex = [hex retain]; } @@ -194,19 +177,6 @@ using namespace shell; [_hex release]; [super dealloc]; } - -- (BOOL)isEqual:(id)object { - if (self == object) - return YES; - if (![object isKindOfClass:[FlutterStandardBigInteger class]]) - return NO; - FlutterStandardBigInteger* other = (FlutterStandardBigInteger*)object; - return [self.hex isEqual:other.hex]; -} - -- (NSUInteger)hash { - return [self.hex hash]; -} @end #pragma mark - Writer and reader of standard codec @@ -272,37 +242,24 @@ using namespace shell; } else if ([value isKindOfClass:[NSNumber class]]) { NSNumber* number = value; const char* type = [number objCType]; - if ([self isBool:number type:type]) { + if (strcmp(type, @encode(BOOL)) == 0) { BOOL b = number.boolValue; [self writeByte:(b ? FlutterStandardFieldTrue : FlutterStandardFieldFalse)]; - } else if (strcmp(type, @encode(signed int)) == 0 || - strcmp(type, @encode(signed short)) == 0 || - strcmp(type, @encode(unsigned short)) == 0 || - strcmp(type, @encode(signed char)) == 0 || - strcmp(type, @encode(unsigned char)) == 0) { + } else if (strcmp(type, @encode(int)) == 0) { SInt32 n = number.intValue; [self writeByte:FlutterStandardFieldInt32]; [_data appendBytes:(UInt8*)&n length:4]; - } else if (strcmp(type, @encode(signed long)) == 0 || - strcmp(type, @encode(unsigned int)) == 0) { + } else if (strcmp(type, @encode(long)) == 0) { SInt64 n = number.longValue; [self writeByte:FlutterStandardFieldInt64]; [_data appendBytes:(UInt8*)&n length:8]; - } else if (strcmp(type, @encode(double)) == 0 || - strcmp(type, @encode(float)) == 0) { + } else if (strcmp(type, @encode(double)) == 0) { Float64 f = number.doubleValue; [self writeByte:FlutterStandardFieldFloat64]; [_data appendBytes:(UInt8*)&f length:8]; - } else if (strcmp(type, @encode(unsigned long)) == 0 || - strcmp(type, @encode(signed long long)) == 0 || - strcmp(type, @encode(unsigned long long)) == 0) { - NSString* hex = - [NSString stringWithFormat:@"%llx", number.unsignedLongLongValue]; - [self writeByte:FlutterStandardFieldIntHex]; - [self writeUTF8:hex]; } else { - NSLog(@"Unsupported value: %@ of type %s", value, type); + NSLog(@"Unsupported value: %@", value); NSAssert(NO, @"Unsupported value for standard codec"); } } else if ([value isKindOfClass:[NSString class]]) { @@ -335,15 +292,10 @@ using namespace shell; [self writeValue:[dict objectForKey:key]]; } } else { - NSLog(@"Unsupported value: %@ of type %@", value, [value class]); + NSLog(@"Unsupported value: %@", value); NSAssert(NO, @"Unsupported value for standard codec"); } } - -- (BOOL)isBool:(NSNumber*)number type:(const char*)type { - return strcmp(type, @encode(signed char)) == 0 && - [NSStringFromClass([number class]) isEqual:@"__NSCFBoolean"]; -} @end @implementation FlutterStandardReader { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/flutter_standard_codec_unittest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/flutter_standard_codec_unittest.mm index bb0543e922..81e2f79de9 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/flutter_standard_codec_unittest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/flutter_standard_codec_unittest.mm @@ -5,137 +5,89 @@ #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterCodecs.h" #include "gtest/gtest.h" -void checkEncodeDecode(id value, NSData* expectedEncoding) { - FlutterStandardMessageCodec* codec = - [FlutterStandardMessageCodec sharedInstance]; - NSData* encoded = [codec encode:value]; - ASSERT_TRUE([encoded isEqual:expectedEncoding]); - id decoded = [codec decode:encoded]; - if (value == nil || value == [NSNull null]) - ASSERT_EQ(decoded, nil); - else - ASSERT_TRUE([value isEqual:decoded]); -} - -void checkEncodeDecode(id value) { - FlutterStandardMessageCodec* codec = - [FlutterStandardMessageCodec sharedInstance]; - NSData* encoded = [codec encode:value]; - id decoded = [codec decode:encoded]; - if (value == nil || value == [NSNull null]) - ASSERT_EQ(decoded, nil); - else - ASSERT_TRUE([value isEqual:decoded]); -} - TEST(FlutterStandardCodec, CanEncodeAndDecodeNil) { - char bytes[1] = {0x00}; - checkEncodeDecode(nil, [NSData dataWithBytes:bytes length:1]); + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:nil]; + id decoded = [codec decode:encoded]; + ASSERT_TRUE(decoded == nil); } TEST(FlutterStandardCodec, CanEncodeAndDecodeNSNull) { - char bytes[1] = {0x00}; - checkEncodeDecode([NSNull null], [NSData dataWithBytes:bytes length:1]); -} - -TEST(FlutterStandardCodec, CanEncodeAndDecodeYes) { - char bytes[1] = {0x01}; - checkEncodeDecode(@YES, [NSData dataWithBytes:bytes length:1]); -} - -TEST(FlutterStandardCodec, CanEncodeAndDecodeNo) { - char bytes[1] = {0x02}; - checkEncodeDecode(@NO, [NSData dataWithBytes:bytes length:1]); -} - -TEST(FlutterStandardCodec, CanEncodeAndDecodeUInt8) { - char bytes[5] = {0x03, 0xfe, 0x00, 0x00, 0x00}; - UInt8 value = 0xfe; - checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:5]); -} - -TEST(FlutterStandardCodec, CanEncodeAndDecodeUInt16) { - char bytes[5] = {0x03, 0xdc, 0xfe, 0x00, 0x00}; - UInt16 value = 0xfedc; - checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:5]); -} - -TEST(FlutterStandardCodec, CanEncodeAndDecodeUInt32) { - char bytes[9] = {0x04, 0x09, 0xba, 0xdc, 0xfe, 0x00, 0x00, 0x00, 0x00}; - UInt32 value = 0xfedcba09; - checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:9]); -} - -TEST(FlutterStandardCodec, CanEncodeAndDecodeUInt64AsHexString) { FlutterStandardMessageCodec* codec = [FlutterStandardMessageCodec sharedInstance]; - UInt64 u64 = 0xfffffffffffffffa; - NSData* encoded = [codec encode:@(u64)]; - FlutterStandardBigInteger* decoded = [codec decode:encoded]; - ASSERT_TRUE([decoded.hex isEqual:@"fffffffffffffffa"]); + NSData* encoded = [codec encode:[NSNull null]]; + id decoded = [codec decode:encoded]; + ASSERT_TRUE(decoded == nil); } -TEST(FlutterStandardCodec, CanEncodeAndDecodeSInt8) { - char bytes[5] = {0x03, 0xfe, 0xff, 0xff, 0xff}; - SInt8 value = 0xfe; - checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:5]); +TEST(FlutterStandardCodec, CanEncodeAndDecodeInt32) { + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:@-78]; + NSNumber* decoded = [codec decode:encoded]; + ASSERT_TRUE([@-78 isEqualTo:decoded]); } -TEST(FlutterStandardCodec, CanEncodeAndDecodeSInt16) { - char bytes[5] = {0x03, 0xdc, 0xfe, 0xff, 0xff}; - SInt16 value = 0xfedc; - checkEncodeDecode(@(value), [NSData dataWithBytes:bytes length:5]); -} - -TEST(FlutterStandardCodec, CanEncodeAndDecodeSInt32) { - char bytes[5] = {0x03, 0x78, 0x56, 0x34, 0x12}; - checkEncodeDecode(@(0x12345678), [NSData dataWithBytes:bytes length:5]); -} - -TEST(FlutterStandardCodec, CanEncodeAndDecodeSInt64) { - char bytes[9] = {0x04, 0xef, 0xcd, 0xab, 0x90, 0x78, 0x56, 0x34, 0x12}; - checkEncodeDecode(@(0x1234567890abcdef), - [NSData dataWithBytes:bytes length:9]); -} - -TEST(FlutterStandardCodec, CanEncodeAndDecodeBigInteger) { - FlutterStandardBigInteger* value = [FlutterStandardBigInteger - bigIntegerWithHex:@"-abcdef0123456789abcdef01234567890"]; - checkEncodeDecode(value); -} - -TEST(FlutterStandardCodec, CanEncodeAndDecodeFloat32) { - char bytes[9] = {0x06, 0x00, 0x00, 0x00, 0x60, 0xfb, 0x21, 0x09, 0x40}; - checkEncodeDecode(@3.1415927f, [NSData dataWithBytes:bytes length:9]); +TEST(FlutterStandardCodec, CanEncodeAndDecodeInt64) { + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:@78000000001]; + NSNumber* decoded = [codec decode:encoded]; + ASSERT_TRUE([@78000000001 isEqualTo:decoded]); } TEST(FlutterStandardCodec, CanEncodeAndDecodeFloat64) { - char bytes[9] = {0x06, 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}; - checkEncodeDecode(@3.14159265358979311599796346854, - [NSData dataWithBytes:bytes length:9]); + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:@3.14]; + NSNumber* decoded = [codec decode:encoded]; + ASSERT_TRUE([@3.14 isEqualTo:decoded]); } TEST(FlutterStandardCodec, CanEncodeAndDecodeString) { - char bytes[13] = {0x07, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, - 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64}; - checkEncodeDecode(@"hello world", [NSData dataWithBytes:bytes length:13]); + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:@"hello world"]; + NSString* decoded = [codec decode:encoded]; + ASSERT_TRUE([@"hello world" isEqualTo:decoded]); } TEST(FlutterStandardCodec, CanEncodeAndDecodeStringWithNonAsciiCodePoint) { - char bytes[7] = {0x07, 0x05, 0x68, 0xe2, 0x98, 0xba, 0x77}; - checkEncodeDecode(@"h\u263Aw", [NSData dataWithBytes:bytes length:7]); + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:@"hello \u263A world"]; + NSString* decoded = [codec decode:encoded]; + ASSERT_TRUE([@"hello \u263A world" isEqualTo:decoded]); } TEST(FlutterStandardCodec, CanEncodeAndDecodeStringWithNonBMPCodePoint) { - char bytes[8] = {0x07, 0x06, 0x68, 0xf0, 0x9f, 0x98, 0x82, 0x77}; - checkEncodeDecode(@"h\U0001F602w", [NSData dataWithBytes:bytes length:8]); + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:@"hello \U0001F602 world"]; + NSString* decoded = [codec decode:encoded]; + ASSERT_TRUE([@"hello \U0001F602 world" isEqualTo:decoded]); +} + +TEST(FlutterStandardCodec, CanEncodeAndDecodeBigInteger) { + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = + [codec encode:[FlutterStandardBigInteger + bigIntegerWithHex:@"-abcdef120902390239021321abfdec"]]; + FlutterStandardBigInteger* decoded = [codec decode:encoded]; + ASSERT_TRUE([@"-abcdef120902390239021321abfdec" isEqualTo:decoded.hex]); } TEST(FlutterStandardCodec, CanEncodeAndDecodeArray) { NSArray* value = @[ [NSNull null], @"hello", @3.14, @47, @{ @42 : @"nested" } ]; - checkEncodeDecode(value); + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:value]; + NSArray* decoded = [codec decode:encoded]; + ASSERT_TRUE([value isEqualTo:decoded]); } TEST(FlutterStandardCodec, CanEncodeAndDecodeDictionary) { @@ -145,7 +97,11 @@ TEST(FlutterStandardCodec, CanEncodeAndDecodeDictionary) { [NSNull null] : [NSNull null], @3.14 : @[ @"nested" ] }; - checkEncodeDecode(value); + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:value]; + NSDictionary* decoded = [codec decode:encoded]; + ASSERT_TRUE([value isEqualTo:decoded]); } TEST(FlutterStandardCodec, CanEncodeAndDecodeByteArray) { @@ -153,7 +109,14 @@ TEST(FlutterStandardCodec, CanEncodeAndDecodeByteArray) { NSData* data = [NSData dataWithBytes:bytes length:4]; FlutterStandardTypedData* value = [FlutterStandardTypedData typedDataWithBytes:data]; - checkEncodeDecode(value); + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:value]; + FlutterStandardTypedData* decoded = [codec decode:encoded]; + ASSERT_TRUE(decoded.type == FlutterStandardDataTypeUInt8); + ASSERT_TRUE(decoded.elementCount == 4); + ASSERT_TRUE(decoded.elementSize == 1); + ASSERT_TRUE([data isEqualTo:decoded.data]); } TEST(FlutterStandardCodec, CanEncodeAndDecodeInt32Array) { @@ -161,7 +124,14 @@ TEST(FlutterStandardCodec, CanEncodeAndDecodeInt32Array) { NSData* data = [NSData dataWithBytes:bytes length:8]; FlutterStandardTypedData* value = [FlutterStandardTypedData typedDataWithInt32:data]; - checkEncodeDecode(value); + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:value]; + FlutterStandardTypedData* decoded = [codec decode:encoded]; + ASSERT_TRUE(decoded.type == FlutterStandardDataTypeInt32); + ASSERT_TRUE(decoded.elementCount == 2); + ASSERT_TRUE(decoded.elementSize == 4); + ASSERT_TRUE([data isEqualTo:decoded.data]); } TEST(FlutterStandardCodec, CanEncodeAndDecodeInt64Array) { @@ -169,7 +139,14 @@ TEST(FlutterStandardCodec, CanEncodeAndDecodeInt64Array) { NSData* data = [NSData dataWithBytes:bytes length:8]; FlutterStandardTypedData* value = [FlutterStandardTypedData typedDataWithInt64:data]; - checkEncodeDecode(value); + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:value]; + FlutterStandardTypedData* decoded = [codec decode:encoded]; + ASSERT_TRUE(decoded.type == FlutterStandardDataTypeInt64); + ASSERT_TRUE(decoded.elementCount == 1); + ASSERT_TRUE(decoded.elementSize == 8); + ASSERT_TRUE([data isEqualTo:decoded.data]); } TEST(FlutterStandardCodec, CanEncodeAndDecodeFloat64Array) { @@ -178,80 +155,12 @@ TEST(FlutterStandardCodec, CanEncodeAndDecodeFloat64Array) { NSData* data = [NSData dataWithBytes:bytes length:16]; FlutterStandardTypedData* value = [FlutterStandardTypedData typedDataWithFloat64:data]; - checkEncodeDecode(value); -} - -TEST(FlutterStandardCodec, HandlesMethodCallsWithNilArguments) { - FlutterStandardMethodCodec* codec = - [FlutterStandardMethodCodec sharedInstance]; - FlutterMethodCall* call = - [FlutterMethodCall methodCallWithMethodName:@"hello" arguments:nil]; - NSData* encoded = [codec encodeMethodCall:call]; - FlutterMethodCall* decoded = [codec decodeMethodCall:encoded]; - ASSERT_TRUE([decoded isEqual:call]); -} - -TEST(FlutterStandardCodec, HandlesMethodCallsWithSingleArgument) { - FlutterStandardMethodCodec* codec = - [FlutterStandardMethodCodec sharedInstance]; - FlutterMethodCall* call = - [FlutterMethodCall methodCallWithMethodName:@"hello" arguments:@42]; - NSData* encoded = [codec encodeMethodCall:call]; - FlutterMethodCall* decoded = [codec decodeMethodCall:encoded]; - ASSERT_TRUE([decoded isEqual:call]); -} - -TEST(FlutterStandardCodec, HandlesMethodCallsWithArgumentList) { - FlutterStandardMethodCodec* codec = - [FlutterStandardMethodCodec sharedInstance]; - NSArray* arguments = @[ @42, @"world" ]; - FlutterMethodCall* call = - [FlutterMethodCall methodCallWithMethodName:@"hello" arguments:arguments]; - NSData* encoded = [codec encodeMethodCall:call]; - FlutterMethodCall* decoded = [codec decodeMethodCall:encoded]; - ASSERT_TRUE([decoded isEqual:call]); -} - -TEST(FlutterStandardCodec, HandlesSuccessEnvelopesWithNilResult) { - FlutterStandardMethodCodec* codec = - [FlutterStandardMethodCodec sharedInstance]; - NSData* encoded = [codec encodeSuccessEnvelope:nil]; - FlutterError* error = nil; - id decoded = [codec decodeEnvelope:encoded error:&error]; - ASSERT_EQ(error, nil); - ASSERT_EQ(decoded, nil); -} - -TEST(FlutterStandardCodec, HandlesSuccessEnvelopesWithSingleResult) { - FlutterStandardMethodCodec* codec = - [FlutterStandardMethodCodec sharedInstance]; - NSData* encoded = [codec encodeSuccessEnvelope:@42]; - FlutterError* decodedError = nil; - id decodedResult = [codec decodeEnvelope:encoded error:&decodedError]; - ASSERT_EQ(decodedError, nil); - ASSERT_EQ(decodedResult, @42); -} - -TEST(FlutterStandardCodec, HandlesSuccessEnvelopesWithResultMap) { - FlutterStandardMethodCodec* codec = - [FlutterStandardMethodCodec sharedInstance]; - NSDictionary* result = @{ @"a" : @42, @42 : @"a" }; - NSData* encoded = [codec encodeSuccessEnvelope:result]; - FlutterError* decodedError = nil; - id decodedResult = [codec decodeEnvelope:encoded error:&decodedError]; - ASSERT_TRUE([decodedResult isEqual:result]); -} - -TEST(FlutterStandardCodec, HandlesErrorEnvelopes) { - FlutterStandardMethodCodec* codec = - [FlutterStandardMethodCodec sharedInstance]; - NSDictionary* details = @{ @"a" : @42, @42 : @"a" }; - FlutterError* error = [FlutterError errorWithCode:@"errorCode" - message:@"something failed" - details:details]; - NSData* encoded = [codec encodeErrorEnvelope:error]; - FlutterError* decodedError = nil; - id decodedResult = [codec decodeEnvelope:encoded error:&decodedError]; - ASSERT_EQ(decodedResult, nil); - ASSERT_TRUE([decodedError isEqual:error]); + FlutterStandardMessageCodec* codec = + [FlutterStandardMessageCodec sharedInstance]; + NSData* encoded = [codec encode:value]; + FlutterStandardTypedData* decoded = [codec decode:encoded]; + ASSERT_TRUE(decoded.type == FlutterStandardDataTypeFloat64); + ASSERT_TRUE(decoded.elementCount == 2); + ASSERT_TRUE(decoded.elementSize == 8); + ASSERT_TRUE([data isEqualTo:decoded.data]); }