forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for method channels. (flutter#12373)
Added unit tests for method channels.
- Loading branch information
Showing
3 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
shell/platform/darwin/common/framework/Source/FlutterChannelsTest.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters