|
8 | 8 |
|
9 | 9 | #import <XCTest/XCTest.h> |
10 | 10 |
|
| 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 | + |
11 | 17 | @interface BridgeTests : XCTestCase |
12 | 18 |
|
13 | 19 | @end |
14 | 20 |
|
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 | +} |
16 | 72 |
|
17 | | -- (void)testInit |
| 73 | +- (void)testEchoHandler |
18 | 74 | { |
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]; |
20 | 106 | } |
21 | 107 |
|
22 | 108 | @end |
0 commit comments