-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBridgeTests.m
183 lines (158 loc) · 6.83 KB
/
BridgeTests.m
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// BridgeTests.m
// WKWebViewJavascriptBridge
//
// Created by Pieter De Baets on 18/04/2015.
// Copyright (c) 2015 marcuswestin. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "WebViewJavascriptBridge.h"
#import "AppDelegate.h"
static NSString *const echoHandler = @"echoHandler";
@interface BridgeTests : XCTestCase
@end
@interface TestWebPageLoadDelegate : NSObject<UIWebViewDelegate, WKNavigationDelegate>
@property XCTestExpectation* expectation;
@end
@implementation BridgeTests {
UIWebView *_uiWebView;
WKWebView *_wkWebView;
NSMutableArray* _retains;
}
- (void)setUp {
[super setUp];
UIViewController *rootVC = [[(AppDelegate *)[[UIApplication sharedApplication] delegate] window] rootViewController];
CGRect frame = rootVC.view.bounds;
frame.size.height /= 2;
_uiWebView = [[UIWebView alloc] initWithFrame:frame];
_uiWebView.backgroundColor = [UIColor blueColor];
[rootVC.view addSubview:_uiWebView];
frame.origin.y += frame.size.height;
_wkWebView = [[WKWebView alloc] initWithFrame:frame];
_wkWebView.backgroundColor = [UIColor redColor];
[rootVC.view addSubview:_wkWebView];
_retains = [NSMutableArray array];
}
- (void)tearDown {
[super tearDown];
[_uiWebView removeFromSuperview];
[_wkWebView removeFromSuperview];
}
- (WebViewJavascriptBridge*)bridgeForWebView:(id)webView {
WebViewJavascriptBridge* bridge = [WebViewJavascriptBridge bridgeForWebView:webView];
[_retains addObject:bridge];
return bridge;
}
static void loadEchoSample(id webView) {
NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"echo" withExtension:@"html"]];
[(UIWebView*)webView loadRequest:request];
}
const NSTimeInterval timeoutSec = 5;
- (void)testEchoHandler {
[self classSpecificTestEchoHandler:_uiWebView];
[self classSpecificTestEchoHandler:_wkWebView];
[self waitForExpectationsWithTimeout:timeoutSec handler:NULL];
}
- (void)classSpecificTestEchoHandler:(id)webView {
WebViewJavascriptBridge *bridge = [self bridgeForWebView:webView];
XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
[bridge callHandler:echoHandler data:@"testEchoHandler" responseCallback:^(id responseData) {
XCTAssertEqualObjects(responseData, @"testEchoHandler");
[callbackInvocked fulfill];
}];
loadEchoSample(webView);
}
- (void)testEchoHandlerAfterSetup {
[self classSpecificTestEchoHandlerAfterSetup:_uiWebView];
[self classSpecificTestEchoHandlerAfterSetup:_wkWebView];
[self waitForExpectationsWithTimeout:timeoutSec handler:NULL];
}
- (void)classSpecificTestEchoHandlerAfterSetup:(id)webView {
WebViewJavascriptBridge *bridge = [self bridgeForWebView:webView];
XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
loadEchoSample(webView);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 150 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
[bridge callHandler:echoHandler data:@"testEchoHandler" responseCallback:^(id responseData) {
XCTAssertEqualObjects(responseData, @"testEchoHandler");
[callbackInvocked fulfill];
}];
});
}
- (void)testObjectEncoding {
[self classSpecificTestObjectEncoding:_uiWebView];
[self classSpecificTestObjectEncoding:_wkWebView];
[self waitForExpectationsWithTimeout:timeoutSec handler:NULL];
}
- (void)classSpecificTestObjectEncoding:(id)webView {
WebViewJavascriptBridge *bridge = [self bridgeForWebView:webView];
void (^echoObject)(id) = ^void(id object) {
XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
[bridge callHandler:echoHandler data:object responseCallback:^(id responseData) {
XCTAssertEqualObjects(responseData, object);
[callbackInvocked fulfill];
}];
};
echoObject(@"A string sent over the wire");
echoObject(@"A string with '\"'/\\");
echoObject(@[ @1, @2, @3 ]);
echoObject(@{ @"a" : @1, @"b" : @2 });
loadEchoSample(webView);
}
- (void)testJavascriptReceiveResponse {
[self classSpecificTestJavascriptReceiveResponse:_uiWebView];
[self classSpecificTestJavascriptReceiveResponse:_wkWebView];
[self waitForExpectationsWithTimeout:timeoutSec handler:NULL];
}
- (void)classSpecificTestJavascriptReceiveResponse:(id)webView {
WebViewJavascriptBridge *bridge = [self bridgeForWebView:webView];
loadEchoSample(webView);
XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
[bridge registerHandler:@"objcEchoToJs" handler:^(id data, WVJBResponseCallback responseCallback) {
responseCallback(data);
}];
[bridge callHandler:@"jsRcvResponseTest" data:nil responseCallback:^(id responseData) {
XCTAssertEqualObjects(responseData, @"Response from JS");
[callbackInvocked fulfill];
}];
}
- (void)testJavascriptReceiveResponseWithoutSafetyTimeout {
[self classSpecificTestJavascriptReceiveResponseWithoutSafetyTimeout:_uiWebView];
[self classSpecificTestJavascriptReceiveResponseWithoutSafetyTimeout:_wkWebView];
[self waitForExpectationsWithTimeout:timeoutSec handler:NULL];
}
- (void)classSpecificTestJavascriptReceiveResponseWithoutSafetyTimeout:(id)webView {
WebViewJavascriptBridge *bridge = [self bridgeForWebView:webView];
[bridge disableJavscriptAlertBoxSafetyTimeout];
loadEchoSample(webView);
XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
[bridge registerHandler:@"objcEchoToJs" handler:^(id data, WVJBResponseCallback responseCallback) {
responseCallback(data);
}];
[bridge callHandler:@"jsRcvResponseTest" data:nil responseCallback:^(id responseData) {
XCTAssertEqualObjects(responseData, @"Response from JS");
[callbackInvocked fulfill];
}];
}
- (void)testWebpageLoad {
[self classSpecificTestWebpageLoad:_uiWebView];
[self classSpecificTestWebpageLoad:_wkWebView];
[self waitForExpectationsWithTimeout:timeoutSec handler:NULL];
}
- (void)classSpecificTestWebpageLoad:(id)webView {
WebViewJavascriptBridge* bridge = [self bridgeForWebView:webView];
TestWebPageLoadDelegate* delegate = [TestWebPageLoadDelegate new];
delegate.expectation = [self expectationWithDescription:@"Webpage loaded"];
[_retains addObject:delegate];
[bridge setWebViewDelegate:delegate];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://example.com"]];
[(UIWebView*)webView loadRequest:request];
}
@end
@implementation TestWebPageLoadDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.expectation fulfill];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[self.expectation fulfill];
}
@end