Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
typealias Helper = UnitTestHelper
class UnitTestHelper {
//MARK: - Accessing top view controllers
private(set) class var rootViewController: UIViewController? {
set {
UIApplication.sharedApplication().keyWindow?.rootViewController = newValue
}
get {
return UIApplication.sharedApplication().keyWindow?.rootViewController
}
}
class var rootNavigationController: UINavigationController? {
return rootViewController as? UINavigationController
}
class var topViewController: UIViewController? {
guard let root = rootViewController else { return nil }
return topViewControllerFromViewController(root)
}
private class func topViewControllerFromViewController(viewController: UIViewController)
-> UIViewController {
if let presentedViewController = viewController.presentedViewController {
return topViewControllerFromViewController(presentedViewController)
}
if let topFromNavVC = (viewController as? UINavigationController)?.topViewController {
return topViewControllerFromViewController(topFromNavVC)
}
return viewController
}
//MARK: Presenting view controllers
class func attachViewControllerToWindow(vc: UIViewController) {
rootViewController = vc
}
class func presentViewControllerModally(vc: UIViewController) {
let root = UIViewController()
rootViewController = root
root.presentViewController(vc, animated: false, completion: nil)
}
class func presentModallyInNavigatoinController(vc: UIViewController) {
let root = UIViewController()
rootViewController = root
root.presentViewController(UINavigationController(rootViewController: vc), animated: false, completion: nil)
}
class func presentInNavigationController(vc: UIViewController) {
let root = UINavigationController()
rootViewController = root
root.pushViewController(vc, animated: false)
}
//MARK: Dismissing view controllers
class func popFromNavigationController(vc: UIViewController) {
guard let navVC = rootViewController as? UINavigationController,
topVC = navVC.topViewController
where vc == topVC else { return }
navVC.popViewControllerAnimated(false)
}
class func dissmissModalViewController(vc: UIViewController) {
vc.dismissViewControllerAnimated(false, completion: nil)
}
class func detachViewControllerFromWindow(vc: UIViewController) {
if rootViewController == vc {
rootViewController = nil
}
}
//MARK: Sending actions to objects
class func sendAction(action: Selector, target: AnyObject?, from sender: AnyObject, forEvent event: UIEvent? = nil) {
UIApplication.sharedApplication().sendAction(action, to: target, from: sender, forEvent: event)
}
class func tap(button: UIButton?) {
guard let button = button else { return }
for target in button.allTargets() {
for action in button.actionsForTarget(target, forControlEvent: .TouchUpInside) ?? [] {
sendAction(Selector(action), target: target, from: button)
}
}
}
class func tap(button: UIBarButtonItem?) {
guard let button = button else { return }
sendAction(button.action, target: button.target, from: button)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.