Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit 87914c8

Browse files
committed
Merge pull request #6 from nodes-ios/delegateTests
Added tests for the KeyboardHelper
2 parents 5c90b78 + f214583 commit 87914c8

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

KeyboardHelper/Classes/KeyboardHelper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public class KeyboardHelper {
5353
delegate = nil
5454
}
5555

56-
private func keyboardWillAppear(note: NSNotification) {
56+
dynamic private func keyboardWillAppear(note: NSNotification) {
5757
let info = KeyboardAppearanceInfo(notification: note)
5858
self.delegate?.keyboardWillAppear(info)
5959
}
6060

61-
private func keyboardWillDisappear(note: NSNotification) {
61+
dynamic private func keyboardWillDisappear(note: NSNotification) {
6262
let info = KeyboardAppearanceInfo(notification: note)
6363
self.delegate?.keyboardWillDisappear(info)
6464
}

KeyboardHelperTests/Tests/KeyboardAppearanceInfoTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import KeyboardHelper
1212
class KeyboardAppearanceInfoTests: XCTestCase {
1313

1414
var apperanceInfo: KeyboardAppearanceInfo!
15+
var defaultsAppearanceInfo : KeyboardAppearanceInfo!
1516

1617
override func setUp() {
1718
super.setUp()
@@ -24,41 +25,55 @@ class KeyboardAppearanceInfoTests: XCTestCase {
2425
UIKeyboardAnimationCurveUserInfoKey: UIViewAnimationCurve.EaseOut.rawValue,
2526
]
2627

28+
var defaultParsingUserInfo: [String: AnyObject] = [
29+
UIKeyboardFrameBeginUserInfoKey: "bla",
30+
UIKeyboardFrameEndUserInfoKey: "bla",
31+
UIKeyboardAnimationDurationUserInfoKey: NSValue(CGRect: CGRect(x: 100, y: 100, width: 100, height: 100)),
32+
UIKeyboardAnimationCurveUserInfoKey: "bla",
33+
]
34+
2735
if #available(iOS 9.0, *) {
2836
testUserInfo[UIKeyboardIsLocalUserInfoKey] = false
37+
defaultParsingUserInfo[UIKeyboardIsLocalUserInfoKey] = "this shouldn't work"
2938
} else {
3039
print("UIKeyboardIsLocalUserInfoKey is not available before iOS9.")
3140
}
3241

3342
// Fake the notification
3443
let note = NSNotification(name: UIKeyboardWillShowNotification, object: nil, userInfo: testUserInfo)
3544
apperanceInfo = KeyboardAppearanceInfo(notification: note)
45+
defaultsAppearanceInfo = KeyboardAppearanceInfo(notification: NSNotification(name: UIKeyboardWillShowNotification, object: nil, userInfo: defaultParsingUserInfo))
3646
}
3747

3848
func testBeginFrame() {
3949
XCTAssertEqual(apperanceInfo.beginFrame, CGRect(x: 100, y: 100, width: 100, height: 100),
4050
"Parsing beginFrame from keyboard appearance info failed.")
51+
XCTAssertEqual(defaultsAppearanceInfo.beginFrame, CGRectZero)
4152
}
4253

4354
func testEndFrame() {
4455
XCTAssertEqual(apperanceInfo.endFrame, CGRect(x: 200, y: 200, width: 200, height: 200),
4556
"Parsing endFrame from keyboard appearance info failed.")
57+
XCTAssertEqual(defaultsAppearanceInfo.endFrame, CGRectZero)
4658
}
4759

4860
@available(iOS 9.0, *)
4961
func testBelongsToCurrentApp() {
5062
XCTAssertEqual(apperanceInfo.belongsToCurrentApp, false,
5163
"Parsing belongsToCurrentApp from keyboard appearance info failed.")
64+
XCTAssertEqual(defaultsAppearanceInfo.belongsToCurrentApp, true)
5265
}
5366

5467
func testAnimationDuration() {
5568
XCTAssertEqual(apperanceInfo.animationDuration, Double(3),
5669
"Parsing animationDuration from keyboard appearance info failed.")
70+
XCTAssertEqual(defaultsAppearanceInfo.animationDuration, 0.25)
5771
}
5872

5973
func testAnimationCurve() {
6074
XCTAssertEqual(apperanceInfo.animationCurve, UIViewAnimationCurve(rawValue: 2),
6175
"Parsing animationCurve from keyboard appearance info failed.")
76+
XCTAssertEqual(defaultsAppearanceInfo.animationCurve, UIViewAnimationCurve.EaseInOut)
6277
}
6378

6479
func testAnimateAlong() {

KeyboardHelperTests/Tests/KeyboardHelperTests.swift

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,118 @@
99
import XCTest
1010
@testable import KeyboardHelper
1111

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+
1252
class KeyboardHelperTests: XCTestCase {
1353

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+
}
14126
}

0 commit comments

Comments
 (0)