Skip to content

Commit

Permalink
Run tests for both UIWebView and WKWebView. Done in a pretty ghetto w…
Browse files Browse the repository at this point in the history
…ay. Much better would be to bundle WK/UI bridges into a single class, which can handle both webview types
  • Loading branch information
marcuswestin committed Feb 2, 2016
1 parent 810806f commit b59c15f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 61 deletions.
4 changes: 4 additions & 0 deletions Tests/WebViewJavascriptBridge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
2C3E7C641C5A928700A1E322 /* WebViewJavascriptBridge_JS.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C3E7C5E1C5A928700A1E322 /* WebViewJavascriptBridge_JS.m */; };
2C3E7C651C5A928700A1E322 /* WebViewJavascriptBridgeBase.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C3E7C601C5A928700A1E322 /* WebViewJavascriptBridgeBase.m */; };
2C3E7C661C5A928700A1E322 /* WKWebViewJavascriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C3E7C621C5A928700A1E322 /* WKWebViewJavascriptBridge.m */; };
2C864FFD1C60FC8A00954B70 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2C864FFC1C60FC8A00954B70 /* WebKit.framework */; };
3D99867E1AE2A3B2001DDA2C /* echo.html in Resources */ = {isa = PBXBuildFile; fileRef = 3D99867D1AE2A3B2001DDA2C /* echo.html */; };
3D9E5F2F1AE288E5009D1C36 /* BridgeTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3D9E5F2E1AE288E5009D1C36 /* BridgeTests.m */; };
3DCCF7DB1AE28C2900CE7C51 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 3DCCF7DA1AE28C2900CE7C51 /* main.m */; };
Expand Down Expand Up @@ -39,6 +40,7 @@
2C3E7C601C5A928700A1E322 /* WebViewJavascriptBridgeBase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WebViewJavascriptBridgeBase.m; sourceTree = "<group>"; };
2C3E7C611C5A928700A1E322 /* WKWebViewJavascriptBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKWebViewJavascriptBridge.h; sourceTree = "<group>"; };
2C3E7C621C5A928700A1E322 /* WKWebViewJavascriptBridge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WKWebViewJavascriptBridge.m; sourceTree = "<group>"; };
2C864FFC1C60FC8A00954B70 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
3D0FE4751AE2886500BB4104 /* WebViewJavascriptBridgeTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = WebViewJavascriptBridgeTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3D0FE47B1AE2886500BB4104 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3D99867D1AE2A3B2001DDA2C /* echo.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = echo.html; path = WebViewJavascriptBridgeTests/echo.html; sourceTree = SOURCE_ROOT; };
Expand All @@ -56,6 +58,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
2C864FFD1C60FC8A00954B70 /* WebKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -89,6 +92,7 @@
3D0FE4611AE2886400BB4104 = {
isa = PBXGroup;
children = (
2C864FFC1C60FC8A00954B70 /* WebKit.framework */,
2C35E9751C5A7F8E0093FB29 /* Default-568h@2x.png */,
2C3E7C5A1C5A928700A1E322 /* WebViewJavascriptBridge */,
3D0FE4791AE2886500BB4104 /* WebViewJavascriptBridgeTests */,
Expand Down
143 changes: 83 additions & 60 deletions Tests/WebViewJavascriptBridgeTests/BridgeTests.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// BridgeTests.m
// WebViewJavascriptBridge
// WKWebViewJavascriptBridge
//
// Created by Pieter De Baets on 18/04/2015.
// Copyright (c) 2015 marcuswestin. All rights reserved.
Expand All @@ -9,6 +9,7 @@
#import <XCTest/XCTest.h>

#import "WebViewJavascriptBridge.h"
#import "WKWebViewJavascriptBridge.h"
#import "AppDelegate.h"

static NSString *const echoHandler = @"echoHandler";
Expand All @@ -18,92 +19,114 @@ @interface BridgeTests : XCTestCase
@end

@implementation BridgeTests {
UIWebView *_webView;
UIWebView *_uiWebView;
WKWebView *_wkWebView;
}

- (void)setUp
{
[super setUp];

UIViewController *rootVC = [[(AppDelegate *)[[UIApplication sharedApplication] delegate] window] rootViewController];
_webView = [[UIWebView alloc] initWithFrame:rootVC.view.bounds];
[rootVC.view addSubview:_webView];
- (void)setUp {
[super setUp];

UIViewController *rootVC = [[(AppDelegate *)[[UIApplication sharedApplication] delegate] window] rootViewController];
_uiWebView = [[UIWebView alloc] initWithFrame:rootVC.view.bounds];
CGRect wkFrame = _uiWebView.frame;
wkFrame.origin.y += _uiWebView.frame.size.height;
_wkWebView = [[WKWebView alloc] initWithFrame:wkFrame];
[rootVC.view addSubview:_wkWebView];
}

- (void)tearDown
{
[super tearDown];
[_webView removeFromSuperview];
- (void)tearDown {
[super tearDown];
[_uiWebView removeFromSuperview];
[_wkWebView removeFromSuperview];
}

static void loadEchoSample(UIWebView *webView)
{
NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"echo" withExtension:@"html"]];
[webView loadRequest:request];
static void loadEchoSample(id webView) {
NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"echo" withExtension:@"html"]];
[(UIWebView*)webView loadRequest:request];
}

- (void)testInitialization
{
XCTestExpectation *startup = [self expectationWithDescription:@"Startup completed"];
WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge bridgeForWebView:_webView];
- (void)testInitialization {
[self classSpecificTestInitialization:[WebViewJavascriptBridge class] webView:_uiWebView];
[self classSpecificTestInitialization:[WKWebViewJavascriptBridge class] webView:_wkWebView];
[self waitForExpectationsWithTimeout:10 handler:NULL];
}
- (void)classSpecificTestInitialization:(Class)cls webView:(id)webView {
XCTestExpectation *startup = [self expectationWithDescription:@"Startup completed"];
WebViewJavascriptBridge *bridge = [self bridgeForCls:cls webView:webView];
[bridge registerHandler:@"Greet" handler:^(id data, WVJBResponseCallback responseCallback) {
XCTAssertEqualObjects(data, @"Hello world");
[startup fulfill];
}];
XCTAssertNotNil(bridge);

loadEchoSample(_webView);
[self waitForExpectationsWithTimeout:1 handler:NULL];
XCTAssertNotNil(bridge);

loadEchoSample(webView);
}

- (void)testEchoHandler
{
WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge bridgeForWebView:_webView];

XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
[bridge callHandler:echoHandler data:@"testEchoHandler" responseCallback:^(id responseData) {
XCTAssertEqualObjects(responseData, @"testEchoHandler");
[callbackInvocked fulfill];
}];

loadEchoSample(_webView);
[self waitForExpectationsWithTimeout:1 handler:NULL];
- (void)testEchoHandler {
[self classSpecificTestEchoHandler:[WebViewJavascriptBridge class] webView:_uiWebView];
[self classSpecificTestEchoHandler:[WKWebViewJavascriptBridge class] webView:_wkWebView];
[self waitForExpectationsWithTimeout:10 handler:NULL];
}
- (void)classSpecificTestEchoHandler:(Class)cls webView:(id)webView {
WebViewJavascriptBridge *bridge = [self bridgeForCls:cls webView:webView];

XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
[bridge callHandler:echoHandler data:@"testEchoHandler" responseCallback:^(id responseData) {
XCTAssertEqualObjects(responseData, @"testEchoHandler");
[callbackInvocked fulfill];
}];

loadEchoSample(webView);
}

- (void)testEchoHandlerAfterSetup
{
WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge bridgeForWebView:_webView];
- (void)testEchoHandlerAfterSetup {
[self classSpecificTestEchoHandlerAfterSetup:[WebViewJavascriptBridge class] webView:_uiWebView];
[self classSpecificTestEchoHandlerAfterSetup:[WKWebViewJavascriptBridge class] webView:_wkWebView];
[self waitForExpectationsWithTimeout:10 handler:NULL];
}
- (void)classSpecificTestEchoHandlerAfterSetup:(Class)cls webView:(id)webView {
WebViewJavascriptBridge *bridge = [self bridgeForCls:cls webView:webView];

XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
loadEchoSample(_webView);
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];
}];
});
[self waitForExpectationsWithTimeout:1 handler:NULL];
}

- (void)testObjectEncoding
{
WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge 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];
}];
};
- (void)testObjectEncoding {
[self classSpecificTestObjectEncoding:[WebViewJavascriptBridge class] webView:_uiWebView];
[self classSpecificTestObjectEncoding:[WKWebViewJavascriptBridge class] webView:_wkWebView];
[self waitForExpectationsWithTimeout:10 handler:NULL];
}
- (void)classSpecificTestObjectEncoding:(Class)cls webView:(id)webView {
WebViewJavascriptBridge *bridge = [self bridgeForCls:cls webView: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);
}

echoObject(@"A string sent over the wire");
echoObject(@"A string with '\"'/\\");
echoObject(@[ @1, @2, @3 ]);
echoObject(@{ @"a" : @1, @"b" : @2 });

loadEchoSample(_webView);
[self waitForExpectationsWithTimeout:1 handler:NULL];
- (WebViewJavascriptBridge*)bridgeForCls:(Class)cls webView:(id)webView {
if (cls == [WebViewJavascriptBridge class]) {
return [WebViewJavascriptBridge bridgeForWebView:webView];
} else {
return (WebViewJavascriptBridge*)[WKWebViewJavascriptBridge bridgeForWebView:_wkWebView];
}
}

@end
2 changes: 1 addition & 1 deletion WebViewJavascriptBridge/WKWebViewJavascriptBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copyright (c) 2014 @LokiMeyburg. All rights reserved.
//

#if (__MAC_OS_X_VERSION_MAX_ALLOWED > __MAC_10_9 || __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1)
#if (__MAC_OS_X_VERSION_MAX_ALLOWED > __MAC_10_9 || __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_1)
#define supportsWKWebKit
#endif

Expand Down

0 comments on commit b59c15f

Please sign in to comment.