Skip to content

Instantly share code, notes, and snippets.

@fatuhoku
Created November 26, 2015 21:02
Show Gist options
  • Save fatuhoku/d1b35828a1651abdd4cb to your computer and use it in GitHub Desktop.
Save fatuhoku/d1b35828a1651abdd4cb to your computer and use it in GitHub Desktop.
// Taken from: https://github.com/jeffh/TestingInSwift/blob/2d011f227f0ca1552680eba08f213792660ea32d/RandomAppTests/Support/UISupport.swift
// All of these functions here are inspired/ported from PivotalCoreKit
// https://github.com/pivotal/PivotalCoreKit
import UIKit
import Nimble
// emulates a tap on a bar button item. Does not support customViews
//func tap(barButtonItem: UIBarButtonItem) {
// SelectorProxy(target: barButtonItem.target).performAction(barButtonItem.action, withObject: barButtonItem)
//}
// emulates a button tap. Does not support gesture recognizers
// not used here, but just to demonstrate it.
func tap(button: UIButton) {
button.sendActionsForControlEvents(.TouchUpInside)
}
// emulates a tap on a tableView cell. Conforms to the delegate call-chain
// for selecting cells (not deselecting).
//
// It's enough to get the job done, but you might want to look at
// PivotalCoreKit for a more proper implementation.
func tap(cell: UITableViewCell, file: String=__FILE__, line: UInt=__LINE__) {
var parentView = cell.superview
var tableView: UITableView?
// find the tableView we belong to
while parentView != nil{
if let parentView = parentView as? UITableView {
tableView = parentView
break
} else if let view = parentView {
parentView = view.superview
}
}
if let tableView = tableView {
if let indexPath = tableView.indexPathForCell(cell) {
var newIndexPath: NSIndexPath?
if tableView.delegate?.respondsToSelector("tableView:willSelectRowAtIndexPath:") == true {
newIndexPath = tableView.delegate?.tableView!(tableView, willSelectRowAtIndexPath: indexPath)
} else {
newIndexPath = indexPath
}
if let newIndexPath = newIndexPath {
tableView.selectRowAtIndexPath(newIndexPath, animated: false, scrollPosition: .Middle)
tableView.delegate?.tableView?(tableView, didSelectRowAtIndexPath: newIndexPath)
}
return
}
}
fail("Could not find table view for cell: \(cell)", file: file, line: line)
}
// Short-hand for fetching a cell from a tableView by scrolling to it.
func tableCellAt(tableView: UITableView, indexPath: NSIndexPath)
-> UITableViewCell {
tableView.layoutIfNeeded()
tableView.scrollToRowAtIndexPath(indexPath,
atScrollPosition: UITableViewScrollPosition.Middle,
animated: false)
return tableView.cellForRowAtIndexPath(indexPath)!
}
// Short-hand for fetching a cell from a tableView by scrolling to it.
func tableCellAt(tableView: UITableView, row: Int, section: Int = 0) -> UITableViewCell {
let indexPath = NSIndexPath(forRow: row, inSection: section)
return tableCellAt(tableView, indexPath: indexPath)
}
// Short-hand for fetching a cell from a tableView by scrolling to it.
func cellAt(collectionView: UICollectionView, indexPath: NSIndexPath)
-> UICollectionViewCell {
collectionView.layoutIfNeeded()
collectionView.scrollToItemAtIndexPath(indexPath,
atScrollPosition: [.CenteredHorizontally, .CenteredVertically],
animated: false)
return collectionView.cellForItemAtIndexPath(indexPath)!
}
// Short-hand for fetching a cell from a collectionView by scrolling to it.
func cellAt(collectionView: UICollectionView, item: Int, section: Int = 0) -> UICollectionViewCell {
let indexPath = NSIndexPath(forItem: item, inSection: section)
return cellAt(collectionView, indexPath: indexPath)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment