Added unit tests for method channels. (flutter/engine#12373)
Added unit tests for method channels.
This commit is contained in:
@@ -715,6 +715,7 @@ FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterCha
|
||||
FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h
|
||||
FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h
|
||||
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterChannels.mm
|
||||
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterChannelsTest.m
|
||||
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm
|
||||
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodec.mm
|
||||
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodec_Internal.h
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#if !__has_feature(objc_arc)
|
||||
#error ARC must be enabled!
|
||||
#endif
|
||||
|
||||
#import <OCMock/OCMock.h>
|
||||
#import <XCTest/XCTest.h>
|
||||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
|
||||
|
||||
@interface MockBinaryMessenger : NSObject <FlutterBinaryMessenger>
|
||||
@property(nonatomic, copy) NSString* channel;
|
||||
@property(nonatomic, strong) NSData* message;
|
||||
@property(nonatomic, strong) NSMutableDictionary<NSString*, FlutterBinaryMessageHandler>* handlers;
|
||||
@end
|
||||
@implementation MockBinaryMessenger
|
||||
- (instancetype)init {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_handlers = [[NSMutableDictionary<NSString*, FlutterBinaryMessageHandler> alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)sendOnChannel:(NSString*)channel message:(NSData* _Nullable)message {
|
||||
[self sendOnChannel:channel message:message binaryReply:nil];
|
||||
}
|
||||
|
||||
- (void)sendOnChannel:(NSString*)channel
|
||||
message:(NSData* _Nullable)message
|
||||
binaryReply:(FlutterBinaryReply _Nullable)callback {
|
||||
self.channel = channel;
|
||||
self.message = message;
|
||||
}
|
||||
|
||||
- (void)setMessageHandlerOnChannel:(NSString*)channel
|
||||
binaryMessageHandler:(FlutterBinaryMessageHandler _Nullable)handler {
|
||||
[self.handlers setObject:handler forKey:channel];
|
||||
}
|
||||
@end
|
||||
|
||||
@interface FlutterChannelsTest : XCTestCase
|
||||
@end
|
||||
|
||||
@implementation FlutterChannelsTest
|
||||
|
||||
- (void)testMethodInvoke {
|
||||
NSString* channelName = @"foo";
|
||||
id binaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
id codec = OCMProtocolMock(@protocol(FlutterMethodCodec));
|
||||
FlutterMethodChannel* channel = [[FlutterMethodChannel alloc] initWithName:channelName
|
||||
binaryMessenger:binaryMessenger
|
||||
codec:codec];
|
||||
XCTAssertNotNil(channel);
|
||||
NSData* encodedMethodCall = [@"hey" dataUsingEncoding:NSUTF8StringEncoding];
|
||||
OCMStub([codec encodeMethodCall:[OCMArg any]]).andReturn(encodedMethodCall);
|
||||
[channel invokeMethod:@"foo" arguments:@[ @(1) ]];
|
||||
OCMVerify([binaryMessenger sendOnChannel:channelName message:encodedMethodCall]);
|
||||
}
|
||||
|
||||
- (void)testMethodInvokeWithReply {
|
||||
NSString* channelName = @"foo";
|
||||
id binaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
id codec = OCMProtocolMock(@protocol(FlutterMethodCodec));
|
||||
FlutterMethodChannel* channel = [[FlutterMethodChannel alloc] initWithName:channelName
|
||||
binaryMessenger:binaryMessenger
|
||||
codec:codec];
|
||||
XCTAssertNotNil(channel);
|
||||
NSData* encodedMethodCall = [@"hey" dataUsingEncoding:NSUTF8StringEncoding];
|
||||
OCMStub([codec encodeMethodCall:[OCMArg any]]).andReturn(encodedMethodCall);
|
||||
XCTestExpectation* didCallReply = [self expectationWithDescription:@"didCallReply"];
|
||||
OCMExpect([binaryMessenger sendOnChannel:channelName
|
||||
message:encodedMethodCall
|
||||
binaryReply:[OCMArg checkWithBlock:^BOOL(id obj) {
|
||||
FlutterBinaryReply reply = obj;
|
||||
reply(nil);
|
||||
return YES;
|
||||
}]]);
|
||||
[channel invokeMethod:@"foo"
|
||||
arguments:@[ @1 ]
|
||||
result:^(id _Nullable result) {
|
||||
[didCallReply fulfill];
|
||||
XCTAssertEqual(FlutterMethodNotImplemented, result);
|
||||
}];
|
||||
OCMVerifyAll(binaryMessenger);
|
||||
[self waitForExpectationsWithTimeout:1.0 handler:nil];
|
||||
}
|
||||
|
||||
- (void)testMethodMessageHandler {
|
||||
NSString* channelName = @"foo";
|
||||
id binaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
id codec = OCMProtocolMock(@protocol(FlutterMethodCodec));
|
||||
FlutterMethodChannel* channel = [[FlutterMethodChannel alloc] initWithName:channelName
|
||||
binaryMessenger:binaryMessenger
|
||||
codec:codec];
|
||||
XCTAssertNotNil(channel);
|
||||
|
||||
NSData* encodedMethodCall = [@"hey" dataUsingEncoding:NSUTF8StringEncoding];
|
||||
OCMStub([codec encodeMethodCall:[OCMArg any]]).andReturn(encodedMethodCall);
|
||||
FlutterMethodCallHandler handler =
|
||||
^(FlutterMethodCall* _Nonnull call, FlutterResult _Nonnull result) {
|
||||
NSLog(@"hey");
|
||||
};
|
||||
[channel setMethodCallHandler:handler];
|
||||
OCMVerify([binaryMessenger setMessageHandlerOnChannel:channelName
|
||||
binaryMessageHandler:[OCMArg isNotNil]]);
|
||||
}
|
||||
|
||||
- (void)testCallMethodHandler {
|
||||
NSString* channelName = @"foo";
|
||||
MockBinaryMessenger* binaryMessenger = [[MockBinaryMessenger alloc] init];
|
||||
id codec = OCMProtocolMock(@protocol(FlutterMethodCodec));
|
||||
FlutterMethodChannel* channel = [[FlutterMethodChannel alloc] initWithName:channelName
|
||||
binaryMessenger:binaryMessenger
|
||||
codec:codec];
|
||||
XCTAssertNotNil(channel);
|
||||
|
||||
NSData* encodedMethodCall = [@"encoded" dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSData* replyData = [@"reply" dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSData* replyEnvelopeData = [@"reply-envelope" dataUsingEncoding:NSUTF8StringEncoding];
|
||||
FlutterMethodCall* methodCall = [[FlutterMethodCall alloc] init];
|
||||
OCMStub([codec decodeMethodCall:encodedMethodCall]).andReturn(methodCall);
|
||||
OCMStub([codec encodeSuccessEnvelope:replyData]).andReturn(replyEnvelopeData);
|
||||
XCTestExpectation* didCallHandler = [self expectationWithDescription:@"didCallHandler"];
|
||||
XCTestExpectation* didCallReply = [self expectationWithDescription:@"didCallReply"];
|
||||
FlutterMethodCallHandler handler =
|
||||
^(FlutterMethodCall* _Nonnull call, FlutterResult _Nonnull result) {
|
||||
XCTAssertEqual(methodCall, call);
|
||||
[didCallHandler fulfill];
|
||||
result(replyData);
|
||||
};
|
||||
[channel setMethodCallHandler:handler];
|
||||
binaryMessenger.handlers[channelName](encodedMethodCall, ^(NSData* reply) {
|
||||
[didCallReply fulfill];
|
||||
XCTAssertEqual(replyEnvelopeData, reply);
|
||||
});
|
||||
[self waitForExpectationsWithTimeout:1.0 handler:nil];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
0D17A5C022D78FCD0057279F /* FlutterViewControllerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D17A5BF22D78FCD0057279F /* FlutterViewControllerTest.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
|
||||
0D1CE5D8233430F400E5D880 /* FlutterChannelsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D1CE5D7233430F400E5D880 /* FlutterChannelsTest.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
|
||||
0D4C3FB022DF9F5300A67C70 /* FlutterPluginAppLifeCycleDelegateTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D4C3FAF22DF9F5300A67C70 /* FlutterPluginAppLifeCycleDelegateTest.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
|
||||
0D52D3BD22C566D50011DEBD /* FlutterBinaryMessengerRelayTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0D52D3B622C566D50011DEBD /* FlutterBinaryMessengerRelayTest.mm */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
|
||||
0D6AB6B622BB05E100EEE540 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D6AB6B522BB05E100EEE540 /* AppDelegate.m */; };
|
||||
@@ -75,6 +76,7 @@
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
0D17A5BF22D78FCD0057279F /* FlutterViewControllerTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FlutterViewControllerTest.m; sourceTree = "<group>"; };
|
||||
0D1CE5D7233430F400E5D880 /* FlutterChannelsTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FlutterChannelsTest.m; sourceTree = "<group>"; };
|
||||
0D4C3FAF22DF9F5300A67C70 /* FlutterPluginAppLifeCycleDelegateTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FlutterPluginAppLifeCycleDelegateTest.m; sourceTree = "<group>"; };
|
||||
0D52D3B622C566D50011DEBD /* FlutterBinaryMessengerRelayTest.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FlutterBinaryMessengerRelayTest.mm; sourceTree = "<group>"; };
|
||||
0D6AB6B122BB05E100EEE540 /* IosUnitTests.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = IosUnitTests.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
@@ -113,6 +115,15 @@
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
0D1CE5D62334309900E5D880 /* Source-Common */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
0D1CE5D7233430F400E5D880 /* FlutterChannelsTest.m */,
|
||||
);
|
||||
name = "Source-Common";
|
||||
path = ../../../../shell/platform/darwin/common/framework/Source;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
0D6AB6A822BB05E100EEE540 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -151,6 +162,7 @@
|
||||
0D6AB6CC22BB05E200EEE540 /* Tests */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
0D1CE5D62334309900E5D880 /* Source-Common */,
|
||||
0D6AB71722BC336100EEE540 /* OCMock.xcodeproj */,
|
||||
0D6AB6E622BB409F00EEE540 /* Source */,
|
||||
0D6AB6CF22BB05E200EEE540 /* Info.plist */,
|
||||
@@ -376,6 +388,7 @@
|
||||
files = (
|
||||
0D6AB6EB22BB40E700EEE540 /* FlutterEngineTest.mm in Sources */,
|
||||
0D17A5C022D78FCD0057279F /* FlutterViewControllerTest.m in Sources */,
|
||||
0D1CE5D8233430F400E5D880 /* FlutterChannelsTest.m in Sources */,
|
||||
0D52D3BD22C566D50011DEBD /* FlutterBinaryMessengerRelayTest.mm in Sources */,
|
||||
0D4C3FB022DF9F5300A67C70 /* FlutterPluginAppLifeCycleDelegateTest.m in Sources */,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user