This repository was archived by the owner on Sep 14, 2023. It is now read-only.
forked from flutter-team-archive/plugins
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFWFScriptMessageHandlerHostApiTests.m
More file actions
87 lines (71 loc) · 3.51 KB
/
Copy pathFWFScriptMessageHandlerHostApiTests.m
File metadata and controls
87 lines (71 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 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.
@import Flutter;
@import XCTest;
@import webview_flutter_wkwebview;
#import <OCMock/OCMock.h>
@interface FWFScriptMessageHandlerHostApiTests : XCTestCase
@end
@implementation FWFScriptMessageHandlerHostApiTests
/**
* Creates a partially mocked FWFScriptMessageHandler and adds it to instanceManager.
*
* @param instanceManager Instance manager to add the delegate to.
* @param identifier Identifier for the delegate added to the instanceManager.
*
* @return A mock FWFScriptMessageHandler.
*/
- (id)mockHandlerWithManager:(FWFInstanceManager *)instanceManager identifier:(long)identifier {
FWFScriptMessageHandler *handler = [[FWFScriptMessageHandler alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];
[instanceManager addDartCreatedInstance:handler withIdentifier:0];
return OCMPartialMock(handler);
}
/**
* Creates a mock FWFScriptMessageHandlerFlutterApiImpl with instanceManager.
*
* @param instanceManager Instance manager passed to the Flutter API.
*
* @return A mock FWFScriptMessageHandlerFlutterApiImpl.
*/
- (id)mockFlutterApiWithManager:(FWFInstanceManager *)instanceManager {
FWFScriptMessageHandlerFlutterApiImpl *flutterAPI = [[FWFScriptMessageHandlerFlutterApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];
return OCMPartialMock(flutterAPI);
}
- (void)testCreateWithIdentifier {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
FWFScriptMessageHandlerHostApiImpl *hostAPI = [[FWFScriptMessageHandlerHostApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];
FlutterError *error;
[hostAPI createWithIdentifier:@0 error:&error];
FWFScriptMessageHandler *scriptMessageHandler =
(FWFScriptMessageHandler *)[instanceManager instanceForIdentifier:0];
XCTAssertTrue([scriptMessageHandler conformsToProtocol:@protocol(WKScriptMessageHandler)]);
XCTAssertNil(error);
}
- (void)testDidReceiveScriptMessageForHandler {
FWFInstanceManager *instanceManager = [[FWFInstanceManager alloc] init];
FWFScriptMessageHandler *mockHandler = [self mockHandlerWithManager:instanceManager identifier:0];
FWFScriptMessageHandlerFlutterApiImpl *mockFlutterAPI =
[self mockFlutterApiWithManager:instanceManager];
OCMStub([mockHandler scriptMessageHandlerAPI]).andReturn(mockFlutterAPI);
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[instanceManager addDartCreatedInstance:userContentController withIdentifier:1];
WKScriptMessage *mockScriptMessage = OCMClassMock([WKScriptMessage class]);
OCMStub([mockScriptMessage name]).andReturn(@"name");
OCMStub([mockScriptMessage body]).andReturn(@"message");
[mockHandler userContentController:userContentController
didReceiveScriptMessage:mockScriptMessage];
OCMVerify([mockFlutterAPI
didReceiveScriptMessageForHandlerWithIdentifier:@0
userContentControllerIdentifier:@1
message:[OCMArg isKindOfClass:[FWFWKScriptMessageData
class]]
completion:OCMOCK_ANY]);
}
@end