8 #import <OCMock/OCMock.h>
9 #import <XCTest/XCTest.h>
14 @property(nonatomic, copy) NSString*
channel;
15 @property(nonatomic, strong) NSData*
message;
16 @property(nonatomic, strong) NSMutableDictionary<NSString*, FlutterBinaryMessageHandler>*
handlers;
19 - (instancetype)init {
22 _handlers = [[NSMutableDictionary<NSString*, FlutterBinaryMessageHandler> alloc] init];
27 - (void)sendOnChannel:(NSString*)channel message:(NSData* _Nullable)message {
28 [
self sendOnChannel:channel message:message binaryReply:nil];
31 - (void)sendOnChannel:(NSString*)channel
32 message:(NSData* _Nullable)message
41 [
self.handlers setObject:handler forKey:channel];
55 - (void)testMethodInvoke {
56 NSString* channelName =
@"foo";
58 id codec = OCMProtocolMock(
@protocol(FlutterMethodCodec));
60 binaryMessenger:binaryMessenger
63 NSData* encodedMethodCall = [@"hey" dataUsingEncoding:NSUTF8StringEncoding];
64 OCMStub([codec encodeMethodCall:[OCMArg any]]).andReturn(encodedMethodCall);
66 OCMVerify([binaryMessenger sendOnChannel:channelName
message:encodedMethodCall]);
69 - (void)testMethodInvokeWithReply {
70 NSString* channelName =
@"foo";
72 id codec = OCMProtocolMock(
@protocol(FlutterMethodCodec));
74 binaryMessenger:binaryMessenger
77 NSData* encodedMethodCall = [@"hey" dataUsingEncoding:NSUTF8StringEncoding];
78 OCMStub([codec encodeMethodCall:[OCMArg any]]).andReturn(encodedMethodCall);
79 XCTestExpectation* didCallReply = [
self expectationWithDescription:@"didCallReply"];
80 OCMExpect([binaryMessenger sendOnChannel:channelName
82 binaryReply:[OCMArg checkWithBlock:^BOOL(
id obj) {
89 result:^(id _Nullable result) {
90 [didCallReply fulfill];
91 XCTAssertEqual(FlutterMethodNotImplemented, result);
93 OCMVerifyAll(binaryMessenger);
94 [
self waitForExpectationsWithTimeout:1.0 handler:nil];
97 - (void)testMethodMessageHandler {
98 NSString* channelName =
@"foo";
100 id codec = OCMProtocolMock(
@protocol(FlutterMethodCodec));
102 binaryMessenger:binaryMessenger
106 NSData* encodedMethodCall = [@"hey" dataUsingEncoding:NSUTF8StringEncoding];
107 OCMStub([codec encodeMethodCall:[OCMArg any]]).andReturn(encodedMethodCall);
112 OCMVerify([binaryMessenger setMessageHandlerOnChannel:channelName
113 binaryMessageHandler:[OCMArg isNotNil]]);
116 - (void)testCallMethodHandler {
117 NSString* channelName =
@"foo";
119 id codec = OCMProtocolMock(
@protocol(FlutterMethodCodec));
121 binaryMessenger:binaryMessenger
125 NSData* encodedMethodCall = [@"encoded" dataUsingEncoding:NSUTF8StringEncoding];
126 NSData* replyData = [@"reply" dataUsingEncoding:NSUTF8StringEncoding];
127 NSData* replyEnvelopeData = [@"reply-envelope" dataUsingEncoding:NSUTF8StringEncoding];
129 OCMStub([codec decodeMethodCall:encodedMethodCall]).andReturn(methodCall);
130 OCMStub([codec encodeSuccessEnvelope:replyData]).andReturn(replyEnvelopeData);
131 XCTestExpectation* didCallHandler = [
self expectationWithDescription:@"didCallHandler"];
132 XCTestExpectation* didCallReply = [
self expectationWithDescription:@"didCallReply"];
135 XCTAssertEqual(methodCall, call);
136 [didCallHandler fulfill];
140 binaryMessenger.
handlers[channelName](encodedMethodCall, ^(NSData* reply) {
141 [didCallReply fulfill];
142 XCTAssertEqual(replyEnvelopeData, reply);
144 [
self waitForExpectationsWithTimeout:1.0 handler:nil];
148 NSString* channelName =
@"flutter/test";
150 id codec = OCMProtocolMock(
@protocol(FlutterMethodCodec));
153 binaryMessenger:binaryMessenger
160 const unsigned char bytes[] = {7, 6, 114, 101, 115, 105, 122, 101, 12, 2,
161 7, 12, 102, 108, 117, 116, 116, 101, 114, 47,
162 116, 101, 115, 116, 3, 3, 0, 0, 0};
163 NSData* expectedMessage = [NSData dataWithBytes:bytes length:sizeof(bytes)];
165 OCMExpect([binaryMessenger sendOnChannel:
@"dev.flutter/channel-buffers" message:expectedMessage]);
167 OCMVerifyAll(binaryMessenger);
168 [binaryMessenger stopMocking];
171 - (void)testSetWarnsOnOverflow {
172 NSString* channelName =
@"flutter/test";
174 id codec = OCMProtocolMock(
@protocol(FlutterMethodCodec));
177 binaryMessenger:binaryMessenger
184 const unsigned char bytes[] = {7, 8, 111, 118, 101, 114, 102, 108, 111, 119, 12, 2, 7, 12,
185 102, 108, 117, 116, 116, 101, 114, 47, 116, 101, 115, 116, 1};
186 NSData* expectedMessage = [NSData dataWithBytes:bytes length:sizeof(bytes)];
188 OCMExpect([binaryMessenger sendOnChannel:
@"dev.flutter/channel-buffers" message:expectedMessage]);
190 OCMVerifyAll(binaryMessenger);
191 [binaryMessenger stopMocking];
194 - (void)testBasicMessageChannelCleanup {
195 NSString* channelName =
@"foo";
198 id codec = OCMProtocolMock(
@protocol(FlutterMethodCodec));
201 binaryMessenger:binaryMessenger
205 OCMStub([binaryMessenger setMessageHandlerOnChannel:channelName
206 binaryMessageHandler:[OCMArg any]])
207 .andReturn(connection);
209 OCMVerify([binaryMessenger setMessageHandlerOnChannel:channelName
210 binaryMessageHandler:[OCMArg isNotNil]]);
212 OCMVerify([binaryMessenger cleanUpConnection:connection]);
215 - (void)testMethodChannelCleanup {
216 NSString* channelName =
@"foo";
219 id codec = OCMProtocolMock(
@protocol(FlutterMethodCodec));
221 binaryMessenger:binaryMessenger
225 OCMStub([binaryMessenger setMessageHandlerOnChannel:channelName
226 binaryMessageHandler:[OCMArg any]])
227 .andReturn(connection);
233 OCMVerify([binaryMessenger setMessageHandlerOnChannel:channelName
234 binaryMessageHandler:[OCMArg isNotNil]]);
236 OCMVerify([binaryMessenger cleanUpConnection:connection]);
239 - (void)testBasicMessageChannelTaskQueue {
240 NSString* channelName =
@"foo";
243 id codec = OCMProtocolMock(
@protocol(FlutterMethodCodec));
247 binaryMessenger:binaryMessenger
249 taskQueue:taskQueue];
252 OCMStub([binaryMessenger setMessageHandlerOnChannel:channelName
253 binaryMessageHandler:[OCMArg any]
254 taskQueue:taskQueue])
255 .andReturn(connection);
257 OCMVerify([binaryMessenger setMessageHandlerOnChannel:channelName
258 binaryMessageHandler:[OCMArg isNotNil]
259 taskQueue:taskQueue]);
261 OCMVerify([binaryMessenger cleanUpConnection:connection]);
264 - (void)testBasicMessageChannelInvokeHandlerAfterChannelReleased {
265 NSString* channelName =
@"foo";
266 __block NSString* handlerMessage;
275 binaryMessenger:binaryMessenger
277 taskQueue:taskQueue];
282 OCMStub([binaryMessenger
283 setMessageHandlerOnChannel:channelName
284 binaryMessageHandler:[OCMArg checkWithBlock:^BOOL(
286 messageHandler = handler;
289 taskQueue:taskQueue])
290 .andReturn(connection);
291 OCMStub([codec decode:[OCMArg any]]).andReturn(
@"decoded message");
296 messageHandler([NSData data], ^(NSData* data){
298 XCTAssertEqualObjects(handlerMessage,
@"decoded message");
301 - (void)testMethodChannelInvokeHandlerAfterChannelReleased {
302 NSString* channelName =
@"foo";
308 id codec = OCMProtocolMock(
@protocol(FlutterMethodCodec));
311 binaryMessenger:binaryMessenger
313 taskQueue:taskQueue];
315 decodedMethodCall = call;
317 OCMStub([binaryMessenger
318 setMessageHandlerOnChannel:channelName
319 binaryMessageHandler:[OCMArg checkWithBlock:^BOOL(
321 messageHandler = handler;
324 taskQueue:taskQueue])
325 .andReturn(connection);
326 OCMStub([codec decodeMethodCall:[OCMArg any]])
331 messageHandler([NSData data], ^(NSData* data){
333 XCTAssertEqualObjects(decodedMethodCall.method,
@"decoded method call");
336 - (void)testMethodChannelTaskQueue {
337 NSString* channelName =
@"foo";
340 id codec = OCMProtocolMock(
@protocol(FlutterMethodCodec));
343 binaryMessenger:binaryMessenger
345 taskQueue:taskQueue];
349 OCMStub([binaryMessenger setMessageHandlerOnChannel:channelName
350 binaryMessageHandler:[OCMArg any]
351 taskQueue:taskQueue])
352 .andReturn(connection);
354 OCMVerify([binaryMessenger setMessageHandlerOnChannel:channelName
355 binaryMessageHandler:[OCMArg isNotNil]
356 taskQueue:taskQueue]);
358 OCMVerify([binaryMessenger cleanUpConnection:connection]);
361 - (void)testEventChannelTaskQueue {
362 NSString* channelName =
@"foo";
365 id codec = OCMProtocolMock(
@protocol(FlutterMethodCodec));
369 binaryMessenger:binaryMessenger
371 taskQueue:taskQueue];
373 OCMStub([binaryMessenger setMessageHandlerOnChannel:channelName
374 binaryMessageHandler:[OCMArg any]
375 taskQueue:taskQueue])
376 .andReturn(connection);
378 OCMVerify([binaryMessenger setMessageHandlerOnChannel:channelName
379 binaryMessageHandler:[OCMArg isNotNil]
380 taskQueue:taskQueue]);
382 OCMVerify([binaryMessenger cleanUpConnection:connection]);