|
9 | 9 | import XCTest
|
10 | 10 | @testable import KeyboardHelper
|
11 | 11 |
|
| 12 | +class ShowSpyDelegate : KeyboardNotificationDelegate { |
| 13 | + var kai : KeyboardAppearanceInfo? |
| 14 | + |
| 15 | + var expectation : XCTestExpectation? |
| 16 | + |
| 17 | + func keyboardWillDisappear(info: KeyboardAppearanceInfo) { |
| 18 | + |
| 19 | + } |
| 20 | + |
| 21 | + func keyboardWillAppear(info: KeyboardAppearanceInfo) { |
| 22 | + guard let expectation = expectation else { |
| 23 | + XCTFail("ShowSpyDelegate was not setup correctly. Missing XCTExpectation reference") |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + kai = info |
| 28 | + expectation.fulfill() |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +class HideSpyDelegate : KeyboardNotificationDelegate { |
| 33 | + var kai : KeyboardAppearanceInfo? |
| 34 | + |
| 35 | + var expectation : XCTestExpectation? |
| 36 | + |
| 37 | + func keyboardWillDisappear(info: KeyboardAppearanceInfo) { |
| 38 | + guard let expectation = expectation else { |
| 39 | + XCTFail("HideSpyDelegate was not setup correctly. Missing XCTExpectation reference") |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + kai = info |
| 44 | + expectation.fulfill() |
| 45 | + } |
| 46 | + |
| 47 | + func keyboardWillAppear(info: KeyboardAppearanceInfo) { |
| 48 | + |
| 49 | + } |
| 50 | +} |
| 51 | + |
12 | 52 | class KeyboardHelperTests: XCTestCase {
|
13 | 53 |
|
| 54 | + func testKeyboardShowDelegateMethodIsCalledAsync() { |
| 55 | + let spyDelegate = ShowSpyDelegate() |
| 56 | + let kh = KeyboardHelper(delegate: spyDelegate) |
| 57 | + |
| 58 | + let expectation = expectationWithDescription("KeyboardHelper calls the delegate as the result of receiving the show notification") |
| 59 | + spyDelegate.expectation = expectation |
| 60 | + |
| 61 | +// NSNotificationCenter.defaultCenter().postNotificationName(UIKeyboardWillShowNotification, object: kh) |
| 62 | + let notification : NSNotification |
| 63 | + if #available(iOS 9.0, *) { |
| 64 | + notification = NSNotification(name: UIKeyboardWillShowNotification, object: kh, userInfo:[ |
| 65 | + UIKeyboardAnimationCurveUserInfoKey : NSNumber(int: 7), |
| 66 | + UIKeyboardAnimationDurationUserInfoKey : NSNumber(double: 0.25), |
| 67 | + UIKeyboardFrameBeginUserInfoKey : NSValue(CGRect: CGRect(x: 0, y: 667, width: 375, height: 0)), |
| 68 | + UIKeyboardFrameEndUserInfoKey : NSValue(CGRect: CGRect(x: 0, y: 409, width: 375, height: 258)), |
| 69 | + UIKeyboardIsLocalUserInfoKey : NSNumber(bool: true) |
| 70 | + ]) |
| 71 | + } else { |
| 72 | + notification = NSNotification(name: UIKeyboardWillShowNotification, object: nil, userInfo:[ |
| 73 | + UIKeyboardAnimationCurveUserInfoKey : NSNumber(int: 7), |
| 74 | + UIKeyboardAnimationDurationUserInfoKey : NSNumber(double: 0.25), |
| 75 | + UIKeyboardFrameBeginUserInfoKey : NSValue(CGRect: CGRect(x: 0, y: 667, width: 375, height: 0)), |
| 76 | + UIKeyboardFrameEndUserInfoKey : NSValue(CGRect: CGRect(x: 0, y: 409, width: 375, height: 258)) |
| 77 | + ]) |
| 78 | + } |
| 79 | + |
| 80 | + NSNotificationCenter.defaultCenter().postNotification(notification) |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + waitForExpectationsWithTimeout(1) { error in |
| 85 | + if let error = error { |
| 86 | + XCTFail("waitForExpectationsWithTimeout errored: \(error)") |
| 87 | + } |
| 88 | + |
| 89 | + guard let result = spyDelegate.kai else { |
| 90 | + XCTFail("Expected delegate to be called") |
| 91 | + return |
| 92 | + } |
| 93 | + XCTAssertNotNil(result) |
| 94 | + XCTAssertEqual(result.animationCurve, UIViewAnimationCurve(rawValue: 7)) |
| 95 | + XCTAssertEqual(result.animationDuration, 0.25) |
| 96 | + XCTAssertEqual(result.beginFrame, CGRect(x: 0, y: 667, width: 375, height: 0)) |
| 97 | + XCTAssertEqual(result.endFrame, CGRect(x: 0, y: 409, width: 375, height: 258)) |
| 98 | + XCTAssertEqual(result.belongsToCurrentApp, true) |
| 99 | + |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + func testKeyboardHideDelegateMethodIsCalledAsync() { |
| 104 | + let spyDelegate = HideSpyDelegate() |
| 105 | + let kh = KeyboardHelper(delegate: spyDelegate) |
| 106 | + |
| 107 | + let expectation = expectationWithDescription("KeyboardHelper calls the delegate as the result of receiving the hide notification") |
| 108 | + spyDelegate.expectation = expectation |
| 109 | + |
| 110 | + NSNotificationCenter.defaultCenter().postNotificationName(UIKeyboardWillHideNotification, object: kh) |
| 111 | + |
| 112 | + |
| 113 | + waitForExpectationsWithTimeout(1) { error in |
| 114 | + if let error = error { |
| 115 | + XCTFail("waitForExpectationsWithTimeout errored: \(error)") |
| 116 | + } |
| 117 | + |
| 118 | + guard let result = spyDelegate.kai else { |
| 119 | + XCTFail("Expected delegate to be called") |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + XCTAssertNotNil(result) |
| 124 | + } |
| 125 | + } |
14 | 126 | }
|
0 commit comments