Skip to content

Instantly share code, notes, and snippets.

@chuganzy
Last active March 18, 2016 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuganzy/05e7a5033c9f9f65d45c to your computer and use it in GitHub Desktop.
Save chuganzy/05e7a5033c9f9f65d45c to your computer and use it in GitHub Desktop.
#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