Skip to content

Commit e8996ba

Browse files
committed
Some basic tests
1 parent 86feab4 commit e8996ba

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

WebViewJavascriptBridge.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
3D0FE4761AE2886500BB4104 /* libWebViewJavascriptBridge.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3D0FE46A1AE2886400BB4104 /* libWebViewJavascriptBridge.a */; };
11+
3D99867E1AE2A3B2001DDA2C /* echo.html in Resources */ = {isa = PBXBuildFile; fileRef = 3D99867D1AE2A3B2001DDA2C /* echo.html */; };
1112
3D9E5F2D1AE2888D009D1C36 /* WebViewJavascriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 3D9E5F2C1AE2888D009D1C36 /* WebViewJavascriptBridge.m */; };
1213
3D9E5F2F1AE288E5009D1C36 /* BridgeTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3D9E5F2E1AE288E5009D1C36 /* BridgeTests.m */; };
1314
3DCCF7DB1AE28C2900CE7C51 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 3DCCF7DA1AE28C2900CE7C51 /* main.m */; };
@@ -49,6 +50,7 @@
4950
3D0FE46A1AE2886400BB4104 /* libWebViewJavascriptBridge.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libWebViewJavascriptBridge.a; sourceTree = BUILT_PRODUCTS_DIR; };
5051
3D0FE4751AE2886500BB4104 /* WebViewJavascriptBridgeTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = WebViewJavascriptBridgeTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5152
3D0FE47B1AE2886500BB4104 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
53+
3D99867D1AE2A3B2001DDA2C /* echo.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = echo.html; path = WebViewJavascriptBridgeTests/echo.html; sourceTree = SOURCE_ROOT; };
5254
3D9E5F2B1AE2888D009D1C36 /* WebViewJavascriptBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebViewJavascriptBridge.h; sourceTree = "<group>"; };
5355
3D9E5F2C1AE2888D009D1C36 /* WebViewJavascriptBridge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WebViewJavascriptBridge.m; sourceTree = "<group>"; };
5456
3D9E5F2E1AE288E5009D1C36 /* BridgeTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BridgeTests.m; sourceTree = "<group>"; };
@@ -148,6 +150,7 @@
148150
3DCCF7D81AE28C2900CE7C51 /* Supporting Files */ = {
149151
isa = PBXGroup;
150152
children = (
153+
3D99867D1AE2A3B2001DDA2C /* echo.html */,
151154
3DCCF7D91AE28C2900CE7C51 /* Info.plist */,
152155
3DCCF7DA1AE28C2900CE7C51 /* main.m */,
153156
3D9E5F301AE28A5E009D1C36 /* WebViewJavascriptBridge.js.txt */,
@@ -273,6 +276,7 @@
273276
buildActionMask = 2147483647;
274277
files = (
275278
3DCCF8001AE28FF600CE7C51 /* WebViewJavascriptBridge.js.txt in Resources */,
279+
3D99867E1AE2A3B2001DDA2C /* echo.html in Resources */,
276280
);
277281
runOnlyForDeploymentPostprocessing = 0;
278282
};

WebViewJavascriptBridgeTests/BridgeTests.m

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,101 @@
88

99
#import <XCTest/XCTest.h>
1010

11+
#import "WebViewJavascriptBridge.h"
12+
#import "AppDelegate.h"
13+
14+
static NSString *const echoHandler = @"echoHandler";
15+
static const WVJBHandler nullHandler = ^(id data, WVJBResponseCallback callback) {};
16+
1117
@interface BridgeTests : XCTestCase
1218

1319
@end
1420

15-
@implementation BridgeTests
21+
@implementation BridgeTests {
22+
UIWebView *_webView;
23+
}
24+
25+
- (void)setUp
26+
{
27+
[super setUp];
28+
29+
UIViewController *rootVC = [[(AppDelegate *)[[UIApplication sharedApplication] delegate] window] rootViewController];
30+
_webView = [[UIWebView alloc] initWithFrame:rootVC.view.bounds];
31+
[rootVC.view addSubview:_webView];
32+
}
33+
34+
- (void)tearDown
35+
{
36+
[super tearDown];
37+
[_webView removeFromSuperview];
38+
}
39+
40+
static void loadEchoSample(UIWebView *webView)
41+
{
42+
NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"echo" withExtension:@"html"]];
43+
[webView loadRequest:request];
44+
}
45+
46+
- (void)testInitialization
47+
{
48+
XCTestExpectation *startup = [self expectationWithDescription:@"Startup completed"];
49+
WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge bridgeForWebView:_webView handler:^(id data, WVJBResponseCallback responseCallback) {
50+
XCTAssertEqualObjects(data, @"Hello world");
51+
[startup fulfill];
52+
}];
53+
XCTAssertNotNil(bridge);
54+
55+
loadEchoSample(_webView);
56+
[self waitForExpectationsWithTimeout:10 handler:NULL];
57+
}
58+
59+
- (void)testResponseHandler
60+
{
61+
WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge bridgeForWebView:_webView handler:nullHandler];
62+
63+
XCTestExpectation *callbackInvoked = [self expectationWithDescription:@"Callback invoked"];
64+
[bridge send:@"testResponseHandler" responseCallback:^(id responseData) {
65+
XCTAssertEqualObjects(responseData, @"testResponseHandler");
66+
[callbackInvoked fulfill];
67+
}];
68+
69+
loadEchoSample(_webView);
70+
[self waitForExpectationsWithTimeout:10 handler:NULL];
71+
}
1672

17-
- (void)testInit
73+
- (void)testEchoHandler
1874
{
19-
XCTAssert(YES);
75+
WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge bridgeForWebView:_webView handler:nullHandler];
76+
77+
XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
78+
[bridge callHandler:echoHandler data:@"testEchoHandler" responseCallback:^(id responseData) {
79+
XCTAssertEqualObjects(responseData, @"testEchoHandler");
80+
[callbackInvocked fulfill];
81+
}];
82+
83+
loadEchoSample(_webView);
84+
[self waitForExpectationsWithTimeout:10 handler:NULL];
85+
}
86+
87+
- (void)testObjectEncoding
88+
{
89+
WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge bridgeForWebView:_webView handler:nullHandler];
90+
91+
void (^echoObject)(id) = ^void(id object) {
92+
XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
93+
[bridge callHandler:echoHandler data:object responseCallback:^(id responseData) {
94+
XCTAssertEqualObjects(responseData, object);
95+
[callbackInvocked fulfill];
96+
}];
97+
};
98+
99+
echoObject(@"A string sent over the wire");
100+
echoObject(@"A string with '\"'/\\");
101+
echoObject(@[ @1, @2, @3 ]);
102+
echoObject(@{ @"a" : @1, @"b" : @2 });
103+
104+
loadEchoSample(_webView);
105+
[self waitForExpectationsWithTimeout:10 handler:NULL];
20106
}
21107

22108
@end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!doctype html>
2+
<html><head>
3+
</head><body>
4+
<p>WebViewJavascriptBridgeTests - echo.html</p>
5+
<script>
6+
function connectWebViewJavascriptBridge(callback) {
7+
if (window.WebViewJavascriptBridge) {
8+
callback(WebViewJavascriptBridge)
9+
} else {
10+
document.addEventListener('WebViewJavascriptBridgeReady', function() {
11+
callback(WebViewJavascriptBridge)
12+
}, false)
13+
}
14+
}
15+
16+
connectWebViewJavascriptBridge(function(bridge) {
17+
bridge.init(function(message, responseCallback) {
18+
if(responseCallback) {
19+
responseCallback(message);
20+
}
21+
})
22+
bridge.send('Hello world')
23+
24+
bridge.registerHandler('echoHandler', function(data, responseCallback) {
25+
responseCallback(data)
26+
})
27+
})
28+
</script>
29+
</body></html>

0 commit comments

Comments
 (0)