Skip to content

Commit

Permalink
Making it easier to use text fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Sargent committed Jul 31, 2015
1 parent 4704923 commit 1fc55dd
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 60 deletions.
85 changes: 78 additions & 7 deletions Example/Tests/LKAlertControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class LKAlertControllerTests: XCTestCase {
let theTitle = "Alert Title"
let theMessage = "Alert Message"

LKAlertController.overrideShowForTesting { (style, title, message, actions) -> Void in
LKAlertController.overrideShowForTesting { (style, title, message, actions, fields) -> Void in
XCTAssertEqual(style, theStyle, "The style was incorrect")
if let title = title {
XCTAssertEqual(title, theTitle, "The title was incorrect")
Expand All @@ -101,7 +101,7 @@ class LKAlertControllerTests: XCTestCase {
func testShow() {
let expectation = expectationWithDescription("Show override")

LKAlertController.overrideShowForTesting { (style, title, message, actions) -> Void in
LKAlertController.overrideShowForTesting { (style, title, message, actions, fields) -> Void in
XCTAssertEqual(actions.count, 0, "Incorrect number of buttons")
XCTAssertEqual(style, UIAlertControllerStyle.Alert, "Incorrect Style of the alert")

Expand All @@ -115,7 +115,7 @@ class LKAlertControllerTests: XCTestCase {
func testShowAnimated() {
let expectation = expectationWithDescription("Show override")

LKAlertController.overrideShowForTesting { (style, title, message, actions) -> Void in
LKAlertController.overrideShowForTesting { (style, title, message, actions, fields) -> Void in
XCTAssertEqual(actions.count, 0, "Incorrect number of buttons")
XCTAssertEqual(style, UIAlertControllerStyle.Alert, "Incorrect Style of the alert")

Expand All @@ -130,7 +130,7 @@ class LKAlertControllerTests: XCTestCase {
func testShowAnimatedCompletion() {
let expectation = expectationWithDescription("Show override")

LKAlertController.overrideShowForTesting { (style, title, message, actions) -> Void in
LKAlertController.overrideShowForTesting { (style, title, message, actions, fields) -> Void in
XCTAssertEqual(actions.count, 0, "Incorrect number of buttons")
XCTAssertEqual(style, UIAlertControllerStyle.Alert, "Incorrect Style of the alert")

Expand Down Expand Up @@ -232,7 +232,7 @@ class AlertTests: XCTestCase {
func testShow() {
let expectation = expectationWithDescription("Show override")

LKAlertController.overrideShowForTesting { (style, title, message, actions) -> Void in
LKAlertController.overrideShowForTesting { (style, title, message, actions, fields) -> Void in
XCTAssertEqual(actions.count, 1, "Incorrect number of buttons")
XCTAssertEqual(style, UIAlertControllerStyle.Alert, "Incorrect Style of the alert")

Expand All @@ -254,7 +254,7 @@ class AlertTests: XCTestCase {
func testShowOkay() {
let expectation = expectationWithDescription("Show override")

LKAlertController.overrideShowForTesting { (style, title, message, actions) -> Void in
LKAlertController.overrideShowForTesting { (style, title, message, actions, fields) -> Void in
XCTAssertEqual(actions.count, 1, "Incorrect number of buttons")

if let button = actions.first as? UIAlertAction {
Expand All @@ -272,6 +272,77 @@ class AlertTests: XCTestCase {

waitForExpectationsWithTimeout(0.5, handler: nil)
}

func testAddTextField() {
let expectation = expectationWithDescription("Show override")

LKAlertController.overrideShowForTesting { (style, title, message, actions, fields) -> Void in
XCTAssertNotNil(fields, "Fields was nil")
if fields != nil {
XCTAssertEqual(fields!.count, 1, "Incorrect number of fields")
}

if let field = fields?.first as? UITextField, placeholder = field.placeholder {
XCTAssertEqual(placeholder, "the placeholder", "incorrect placeholder")
}
else {
XCTFail("No text field")
}

expectation.fulfill()
}

var textField = UITextField()
textField.placeholder = "the placeholder"

Alert(message: "text field alert").addTextField(&textField).showOkay()

waitForExpectationsWithTimeout(0.5, handler: nil)
}

func testAddMultipleField() {
let expectation = expectationWithDescription("Show override")

LKAlertController.overrideShowForTesting { (style, title, message, actions, fields) -> Void in
XCTAssertNotNil(fields, "Fields was nil")
if fields != nil {
XCTAssertEqual(fields!.count, 2, "Incorrect number of fields")
}

if let first = fields?.first as? UITextField,
second = fields?.last as? UITextField {

XCTAssertNotNil(first.placeholder, "placeholder nil")
if first.placeholder != nil {
XCTAssertEqual(first.placeholder!, "username", "incorrect placeholder")
}
XCTAssertEqual(first.text, "user", "incorrect text")

XCTAssertNotNil(second.placeholder, "placeholder nil")
if second.placeholder != nil {
XCTAssertEqual(second.placeholder!, "password", "incorrect placeholder")
}
XCTAssertTrue(second.secureTextEntry, "Secure text entry not enabled")
}
else {
XCTFail("No text field")
}

expectation.fulfill()
}

var first = UITextField()
first.placeholder = "username"
first.text = "user"

var second = UITextField()
second.placeholder = "password"
second.secureTextEntry = true

Alert(message: "text field alert").addTextField(&first).addTextField(&second).showOkay()

waitForExpectationsWithTimeout(0.5, handler: nil)
}
}


Expand Down Expand Up @@ -362,7 +433,7 @@ class ActionSheetTests: XCTestCase {
func testShow() {
let expectation = expectationWithDescription("Show override")

LKAlertController.overrideShowForTesting { (style, title, message, actions) -> Void in
LKAlertController.overrideShowForTesting { (style, title, message, actions, fields) -> Void in
XCTAssertEqual(actions.count, 1, "Incorrect number of buttons")
XCTAssertEqual(style, UIAlertControllerStyle.ActionSheet, "Incorrect Style of the alert")

Expand Down
75 changes: 22 additions & 53 deletions Pod/Classes/LKAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class LKAlertController {
internal var alertController: UIAlertController

/** Internal static variable to store the override the show method for testing purposes */
internal static var alertTester: ((style: UIAlertControllerStyle, title: String?, message: String?, actions: [AnyObject]) -> Void)? = nil
internal static var alertTester: ((style: UIAlertControllerStyle, title: String?, message: String?, actions: [AnyObject], fields: [AnyObject]?) -> Void)? = nil

/** Title of the alert controller */
internal var title: String? {
Expand Down Expand Up @@ -52,25 +52,6 @@ public class LKAlertController {
alertController = UIAlertController(title: nil, message: nil, preferredStyle: style)
}

/**
Add a text field to the controller
:param: placeholder Placeholder text for text field
:param: secureText Secure text entry for text field
*/
internal func addTextField(placeholder: String?, secureText: Bool?) -> Alert {
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
if let placeholder = placeholder {
textField.placeholder = placeholder
}
if let secureText = secureText {
textField.secureTextEntry = true
}
}

return self as! Alert
}

/**
Add a new button to the controller.
Expand Down Expand Up @@ -116,7 +97,7 @@ public class LKAlertController {
public func show(#animated: Bool, completion: (() -> Void)?) {
//Override for testing
if let alertTester = LKAlertController.alertTester {
alertTester(style: alertController.preferredStyle, title: title, message: message, actions: alertController.actions)
alertTester(style: alertController.preferredStyle, title: title, message: message, actions: alertController.actions, fields: alertController.textFields)
LKAlertController.alertTester = nil
}
//Present the alert
Expand Down Expand Up @@ -154,7 +135,7 @@ public class LKAlertController {
/**
Override the show function with a closure for using with your unit tests
*/
public class func overrideShowForTesting(callback: ((style: UIAlertControllerStyle, title: String?, message: String?, actions: [AnyObject]) -> Void)?) {
public class func overrideShowForTesting(callback: ((style: UIAlertControllerStyle, title: String?, message: String?, actions: [AnyObject], fields: [AnyObject]?) -> Void)?) {
alertTester = callback
}
}
Expand Down Expand Up @@ -204,37 +185,6 @@ public class Alert: LKAlertController {
self.message = message
}

/**
Add a text field to the controller. It will not have any placeholder text nor secure text entry
*/
public func addTextField() -> Alert {
super.addTextField(nil, secureText: nil)

return self
}

/**
Add a text field with placeholder text to the controller
:param: placeholder Placeholder text for text field
*/
public func addTextField(placeholder: String) -> Alert {
super.addTextField(placeholder, secureText: nil)

return self
}

/**
Add a text field with secure text entry to the controller
:param: secureText Secure text entry for text field
*/
public func addTextField(secureText: Bool) -> Alert {
super.addTextField(nil, secureText: secureText)

return self
}

/**
Add a new button to the alert. It will not have an action and will have the Cancel style
Expand All @@ -255,6 +205,25 @@ public class Alert: LKAlertController {
return super.addAction(title, style: style, handler: handler) as! Alert
}

/**
Add a text field to the controller
:param: textField textField to add to the alert (must be a var, not let)
*/
public func addTextField(inout textField: UITextField) -> Alert {
alertController.addTextFieldWithConfigurationHandler { (tf: UITextField!) -> Void in
tf.text = textField.text
tf.placeholder = textField.placeholder
tf.font = textField.font
tf.textColor = textField.textColor
tf.secureTextEntry = textField.secureTextEntry

textField = tf
}

return self
}

/**
Shortcut method for adding an Okay button and showing the alert
*/
Expand Down

0 comments on commit 1fc55dd

Please sign in to comment.