-
Notifications
You must be signed in to change notification settings - Fork 62
/
DodoToolbarTests.swift
228 lines (162 loc) · 5.98 KB
/
DodoToolbarTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import UIKit
import XCTest
class DodoToolbarTests: XCTestCase {
var obj: DodoToolbar!
var superview: UIView!
var animationShowCompleted = false
var animationHideCompleted = false
var buttonDelegate: ButtonViewDelegateMock!
override func setUp() {
super.setUp()
buttonDelegate = ButtonViewDelegateMock()
DodoPresets.resetAll()
obj = DodoToolbar()
obj.buttonViewDelegate = buttonDelegate
superview = UIView()
// Mock animation
animationShowCompleted = false
animationHideCompleted = false
obj.style.bar.animationShow = { view, duration, locationTop, onComplete in
onComplete()
self.animationShowCompleted = true
}
obj.style.bar.animationHide = { view, duration, locationTop, onComplete in
onComplete()
self.animationHideCompleted = true
}
DodoStyle.resetDefaultStyles()
}
// MARK: - Show
func testShow() {
obj.show(inSuperview: superview, withMessage: "hello")
let view = superview.subviews[0] as? DodoToolbar
XCTAssert(view != nil)
XCTAssert(animationShowCompleted)
}
// MARK: - Hide
func testHide() {
superview.addSubview(obj)
var completeHandlerCalled = false
obj.hide({
completeHandlerCalled = true
})
XCTAssert(superview.subviews.isEmpty)
XCTAssert(animationHideCompleted)
XCTAssert(completeHandlerCalled)
}
func testHideAnimationIsDoneOnceWhenHideIsCalledTwice() {
superview.addSubview(obj)
var completeHandlerCalled = false
obj.hide({
completeHandlerCalled = true
})
animationHideCompleted = false
completeHandlerCalled = false
obj.hide({
completeHandlerCalled = true
})
XCTAssert(superview.subviews.isEmpty)
XCTAssertFalse(animationHideCompleted)
XCTAssertFalse(completeHandlerCalled)
}
// MARK: - Show message
func testUpdateMessage() {
obj.show(inSuperview: superview, withMessage: "Hello World!")
let labels = sabToolbar(superview)!.subviews.filter { $0 is UILabel }.map { $0 as! UILabel }
let label = labels[0]
XCTAssertEqual("Hello World!", label.text!)
}
// MARK: - Styling the bar
func testUseDefaultStyle() {
DodoBarDefaultStyles.cornerRadius = 13
obj.show(inSuperview: superview, withMessage: "Hello World!")
let toolbar = sabToolbar(superview)!
XCTAssertEqual(13, toolbar.layer.cornerRadius)
}
func testUsePresetStyle() {
DodoBarDefaultStyles.cornerRadius = 13
DodoPresets.success.style.bar.cornerRadius = 16
obj.style.parent = DodoPresets.success.style
obj.show(inSuperview: superview, withMessage: "Hello World!")
let toolbar = sabToolbar(superview)!
XCTAssertEqual(16, toolbar.layer.cornerRadius)
}
func testSetItemStyle() {
DodoBarDefaultStyles.cornerRadius = 13
DodoPresets.success.style.bar.cornerRadius = 16
obj.style.parent = DodoPresets.success.style
obj.style.bar.cornerRadius = 17
obj.show(inSuperview: superview, withMessage: "Hello World!")
let toolbar = sabToolbar(superview)!
XCTAssertEqual(17, toolbar.layer.cornerRadius)
}
// MARK: - Create buttons
func testCreateLeftButton() {
let image = TestBundle.image("67px.png")!
obj.style.leftButton.image = image
obj.style.rightButton.image = nil
obj.show(inSuperview: superview, withMessage: "hello")
let buttons = sabButtons(superview)
XCTAssertEqual(2, buttons.count)
XCTAssertEqual(67, buttons[0].image!.size.width)
XCTAssert(buttons[1].image == nil)
}
func testCreateRightButton() {
let image = TestBundle.image("96px.png")!
obj.style.leftButton.image = nil
obj.style.rightButton.image = image
obj.show(inSuperview: superview, withMessage: "hello")
let buttons = sabButtons(superview)
XCTAssertEqual(2, buttons.count)
XCTAssert(buttons[0].image == nil)
XCTAssertEqual(96, buttons[1].image!.size.width)
}
func testCreateBothRightAndLeftButtons() {
let imageLeft = TestBundle.image("67px.png")!
let imageRight = TestBundle.image("96px.png")!
obj.style.leftButton.image = imageLeft
obj.style.rightButton.image = imageRight
obj.show(inSuperview: superview, withMessage: "hello")
let buttons = sabButtons(superview)
XCTAssertEqual(2, buttons.count)
XCTAssertEqual(67, buttons[0].image!.size.width)
XCTAssertEqual(96, buttons[1].image!.size.width)
}
func testCreateLeftButtonWithIcon() {
obj.style.leftButton.icon = DodoIcons.close
obj.style.rightButton.image = nil
obj.show(inSuperview: superview, withMessage: "hello")
let buttons = sabButtons(superview)
XCTAssertEqual(2, buttons.count)
XCTAssertEqual(256, buttons[0].image!.size.width)
}
func testRunTapCallbackClosures() {
var tappedLeftButton = false
obj.style.leftButton.icon = DodoIcons.close
obj.style.leftButton.onTap = { tappedLeftButton = true }
var tappedRightButton = false
obj.style.rightButton.onTap = { tappedRightButton = true }
obj.show(inSuperview: superview, withMessage: "hello")
let buttons = sabButtons(superview)
buttons[0].onTap?.didTap(UITapGestureRecognizer())
buttons[1].onTap?.didTap(UITapGestureRecognizer())
XCTAssert(tappedLeftButton)
XCTAssert(tappedRightButton)
}
func testCreateNoButtonsWhenImagesAreNotSupplied() {
obj.style.leftButton.image = nil
obj.style.rightButton.image = nil
obj.show(inSuperview: superview, withMessage: "hello")
let buttons = sabButtons(superview)
XCTAssert(buttons.isEmpty)
}
// MARK: - Use button with a bundled icon
func testUseBundledIcon() {
obj.style.leftButton.icon = DodoIcons.close
obj.show(inSuperview: superview, withMessage: "hello")
let buttons = sabButtons(superview)
XCTAssertEqual(2, buttons.count)
XCTAssertEqual(256, buttons[0].image!.size.width)
XCTAssert(buttons[1].image == nil)
}
}