Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#import <XCTest/XCTest.h>
@interface XCTestCase (Private)
- (void)_enqueueFailureWithDescription:(id)arg1 inFile:(id)arg2 atLine:(unsigned long long)arg3 expected:(_Bool)arg4;
@end
@interface UniversalLinksUITests : XCTestCase
@property (assign, nonatomic) BOOL ignoreFailure;
@end
@implementation UniversalLinksUITests
- (void)setUp {
[super setUp];
self.ignoreFailure = NO;
self.continueAfterFailure = NO;
[[[XCUIApplication alloc] init] launch];
}
- (void)tearDown {
[super tearDown];
}
- (void)_enqueueFailureWithDescription:(id)arg1 inFile:(id)arg2 atLine:(unsigned long long)arg3 expected:(_Bool)arg4 {
if (self.ignoreFailure) {
return;
}
[super _enqueueFailureWithDescription:arg1 inFile:arg2 atLine:arg3 expected:arg4];
}
- (void)testDeprecateLaunchFail {
// 既にlaunchしている状態でlaunchを実行すると例外を投げるかのテスト
// 内部でXCTFail(もしくはそれと近いメソッド)が実行されているためPrivate APIを上書きして
// テスト自体は成功するようにしている
self.ignoreFailure = YES;
XCTAssertThrows([[[XCUIApplication alloc] init] launch]);
}
- (void)testUniversalLink {
XCUIApplication *const app = [[XCUIApplication alloc] init];
sleep(5); // WebViewの読み込み完了を待つ
// UIWebView内のボタンをタップする
NSString *const openAppText = @"アプリで開く";
[[app.links matchingIdentifier:openAppText].staticTexts[openAppText] tap];
sleep(5); // アプリが切り替わる処理の完了を待つ
// アプリが切り替わっていない状態でlaunchを呼ぶと例外を投げる
// 例外が投げられなかった=アプリが切り替わった=Universal Linksが動作したと見なす
XCTAssertNoThrow([app launch]);
}
@end
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.loadRequest(NSURLRequest(URL: NSURL(string: "https://m.sumally.com/p/1")!))
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return request.URL?.scheme.rangeOfString("^https?", options: .RegularExpressionSearch) != nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment