Skip to content

Commit ad4fedc

Browse files
committed
Add tests
1 parent b45badc commit ad4fedc

File tree

2 files changed

+294
-0
lines changed

2 files changed

+294
-0
lines changed

TZStackView.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
5F50EAD959E8ACC5929DBD75 /* NSLayoutConstraintExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB41AF691B294B8E003DB902 /* NSLayoutConstraintExtension.swift */; };
1515
5F50EF474D670FC33E8E80EA /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5F50ED5A43FBFC32B9B9E1AA /* Images.xcassets */; };
1616
7E44D3921C906B1800A3D266 /* TZFuncAnimationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E44D3911C906B1800A3D266 /* TZFuncAnimationDelegate.swift */; };
17+
7E44D3941C906C3F00A3D266 /* HidingAnimationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E44D3931C906C3F00A3D266 /* HidingAnimationTests.swift */; };
1718
A45441C21B9B6D71002452BA /* TZStackView.h in Headers */ = {isa = PBXBuildFile; fileRef = A45441C11B9B6D71002452BA /* TZStackView.h */; settings = {ATTRIBUTES = (Public, ); }; };
1819
A45441C61B9B6D71002452BA /* TZStackView.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A45441BF1B9B6D71002452BA /* TZStackView.framework */; };
1920
A45441C71B9B6D71002452BA /* TZStackView.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = A45441BF1B9B6D71002452BA /* TZStackView.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
@@ -69,6 +70,7 @@
6970
5F50EF54F01A3A6938C6CEA1 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7071
5F50EFD0C46B7C7F989F10E1 /* TZStackViewTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TZStackViewTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
7172
7E44D3911C906B1800A3D266 /* TZFuncAnimationDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TZFuncAnimationDelegate.swift; sourceTree = "<group>"; };
73+
7E44D3931C906C3F00A3D266 /* HidingAnimationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HidingAnimationTests.swift; sourceTree = "<group>"; };
7274
A45441BF1B9B6D71002452BA /* TZStackView.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TZStackView.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7375
A45441C11B9B6D71002452BA /* TZStackView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TZStackView.h; sourceTree = "<group>"; };
7476
A45441C31B9B6D71002452BA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -130,6 +132,7 @@
130132
isa = PBXGroup;
131133
children = (
132134
5F50EDEB0947F99E67140FC6 /* TZStackViewTests.swift */,
135+
7E44D3931C906C3F00A3D266 /* HidingAnimationTests.swift */,
133136
DB5B70841B2A1963006043BD /* TestView.swift */,
134137
DB5B70861B2B8816006043BD /* TZStackViewTestCase.swift */,
135138
5F50E05A91CC731E5AFD4E94 /* Supporting Files */,
@@ -329,6 +332,7 @@
329332
DB5B70871B2B8816006043BD /* TZStackViewTestCase.swift in Sources */,
330333
DB5B70851B2A1963006043BD /* TestView.swift in Sources */,
331334
5F50EAD959E8ACC5929DBD75 /* NSLayoutConstraintExtension.swift in Sources */,
335+
7E44D3941C906C3F00A3D266 /* HidingAnimationTests.swift in Sources */,
332336
);
333337
runOnlyForDeploymentPostprocessing = 0;
334338
};
Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
//
2+
// HidingAnimationTests.swift
3+
// TZStackView
4+
//
5+
// Created by CosynPa on 3/6/16.
6+
// Copyright © 2016 Tom van Zummeren. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import UIKit
11+
import XCTest
12+
import TZStackView
13+
14+
class HidingAnimationTests: TZStackViewTestCase {
15+
var uiTestView: UIView!
16+
var tzTestView: UIView!
17+
18+
override func createTestViews() -> [UIView] {
19+
var views = [UIView]()
20+
for i in 0 ..< 5 {
21+
views.append(TestView(index: i, size: CGSize(width: 100 * (i + 1), height: 100 * (i + 1))))
22+
}
23+
return views
24+
}
25+
26+
override func setUp() {
27+
super.setUp()
28+
uiTestView = uiStackView.arrangedSubviews.last!
29+
tzTestView = tzStackView.arrangedSubviews.last!
30+
}
31+
32+
// If you are not animating the hidden property, the hidden property should be set immediately
33+
func testNonAnimatingHidden() {
34+
let expectation = expectationWithDescription("delay")
35+
36+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
37+
self.uiTestView.hidden = true
38+
self.tzTestView.hidden = true
39+
40+
XCTAssert(self.uiTestView.hidden)
41+
XCTAssert(self.tzTestView.hidden)
42+
XCTAssert(self.uiTestView.layer.hidden)
43+
XCTAssert(self.tzTestView.layer.hidden)
44+
}
45+
46+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
47+
expectation.fulfill()
48+
}
49+
50+
waitForExpectationsWithTimeout(100, handler: nil)
51+
}
52+
53+
// If you are not animating the hidden property, the hidden property should be set immediately
54+
func testNonAnimatingHiddenWithOther() {
55+
let expectation = expectationWithDescription("delay")
56+
57+
uiTestView.backgroundColor = UIColor.clearColor()
58+
tzTestView.backgroundColor = UIColor.clearColor()
59+
UIView.animateWithDuration(2) {
60+
self.uiTestView.backgroundColor = UIColor.greenColor()
61+
self.tzTestView.backgroundColor = UIColor.greenColor()
62+
}
63+
64+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
65+
self.uiTestView.hidden = true
66+
self.tzTestView.hidden = true
67+
68+
XCTAssert(self.uiTestView.hidden)
69+
XCTAssert(self.tzTestView.hidden)
70+
XCTAssert(self.uiTestView.layer.hidden)
71+
XCTAssert(self.tzTestView.layer.hidden)
72+
}
73+
74+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
75+
expectation.fulfill()
76+
}
77+
78+
waitForExpectationsWithTimeout(100, handler: nil)
79+
}
80+
81+
func animationHiddenWithDelay(delay: NSTimeInterval) {
82+
let expectation = expectationWithDescription("delay")
83+
84+
let duration = 1.0
85+
86+
UIView.animateWithDuration(duration, delay: delay, options: [],
87+
animations: { () -> Void in
88+
self.uiTestView.hidden = true
89+
self.tzTestView.hidden = true
90+
91+
// Note uiTestView.hidden == true, tzTestView.hidden == false
92+
93+
// The presentation should not be hidden.
94+
XCTAssert(!self.uiTestView.layer.hidden)
95+
XCTAssert(!self.tzTestView.layer.hidden)
96+
97+
}, completion: { _ in
98+
XCTAssert(self.uiTestView.hidden)
99+
XCTAssert(self.tzTestView.hidden)
100+
XCTAssert(self.uiTestView.layer.hidden)
101+
XCTAssert(self.tzTestView.layer.hidden)
102+
})
103+
104+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64((duration + delay + 0.2) * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
105+
XCTAssert(self.uiTestView.hidden)
106+
XCTAssert(self.tzTestView.hidden)
107+
XCTAssert(self.uiTestView.layer.hidden)
108+
XCTAssert(self.tzTestView.layer.hidden)
109+
}
110+
111+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64((duration + delay + 0.4) * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
112+
expectation.fulfill()
113+
}
114+
115+
waitForExpectationsWithTimeout(100, handler: nil)
116+
}
117+
118+
func testAnimatingHidden() {
119+
animationHiddenWithDelay(0)
120+
}
121+
122+
func testAnimatingHiddenWithDelay() {
123+
animationHiddenWithDelay(1)
124+
}
125+
126+
func testAnimatingHiddenWithOther() {
127+
let expectation = expectationWithDescription("delay")
128+
129+
UIView.animateWithDuration(1) {
130+
self.uiTestView.hidden = true
131+
self.tzTestView.hidden = true
132+
}
133+
134+
uiTestView.backgroundColor = UIColor.clearColor()
135+
tzTestView.backgroundColor = UIColor.clearColor()
136+
UIView.animateWithDuration(2) {
137+
self.uiTestView.backgroundColor = UIColor.greenColor()
138+
self.tzTestView.backgroundColor = UIColor.greenColor()
139+
}
140+
141+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
142+
// The view should be hidden after the hiding animation completes even if there are still other animations
143+
XCTAssert(self.uiTestView.hidden)
144+
XCTAssert(self.tzTestView.hidden)
145+
XCTAssert(self.uiTestView.layer.hidden)
146+
XCTAssert(self.tzTestView.layer.hidden)
147+
}
148+
149+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
150+
expectation.fulfill()
151+
}
152+
153+
waitForExpectationsWithTimeout(100, handler: nil)
154+
}
155+
156+
// The completion callback of an animation should be called
157+
func testHidingAnimationCallback() {
158+
let expectation = expectationWithDescription("delay")
159+
160+
var uiCompletionCalled = false
161+
var tzCompletionCalled = false
162+
163+
UIView.animateWithDuration(1,
164+
animations: {
165+
self.uiTestView.hidden = true
166+
}, completion: { _ in
167+
uiCompletionCalled = true
168+
})
169+
170+
UIView.animateWithDuration(1,
171+
animations: {
172+
self.tzTestView.hidden = true
173+
}, completion: { _ in
174+
tzCompletionCalled = true
175+
})
176+
177+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
178+
XCTAssert(uiCompletionCalled)
179+
XCTAssert(tzCompletionCalled)
180+
}
181+
182+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
183+
expectation.fulfill()
184+
}
185+
186+
waitForExpectationsWithTimeout(100, handler: nil)
187+
}
188+
189+
// The completion callback of an animation should be called when the animation is canceled
190+
func testHidingAnimationCallbackCancel() {
191+
let expectation = expectationWithDescription("delay")
192+
193+
var uiCompletionCalled = false
194+
var tzCompletionCalled = false
195+
196+
UIView.animateWithDuration(1,
197+
animations: {
198+
self.uiTestView.hidden = true
199+
}, completion: { finished in
200+
uiCompletionCalled = true
201+
XCTAssert(!finished)
202+
})
203+
204+
UIView.animateWithDuration(1,
205+
animations: {
206+
self.tzTestView.hidden = true
207+
}, completion: { finished in
208+
tzCompletionCalled = true
209+
XCTAssert(!finished)
210+
})
211+
212+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
213+
// This will cancel the animation
214+
self.uiStackView.removeFromSuperview()
215+
self.tzStackView.removeFromSuperview()
216+
}
217+
218+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.7 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
219+
XCTAssert(uiCompletionCalled)
220+
XCTAssert(tzCompletionCalled)
221+
XCTAssert(self.uiTestView.hidden)
222+
XCTAssert(self.tzTestView.hidden)
223+
}
224+
225+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
226+
expectation.fulfill()
227+
}
228+
229+
waitForExpectationsWithTimeout(100, handler: nil)
230+
}
231+
232+
// When set the hidden property in the middle of an animation, the hidden property should be updated eventually
233+
func hidingAnimationSetAgainFirstHidden(firstHidden: Bool, withAnimation: Bool) {
234+
let expectation = expectationWithDescription("delay")
235+
236+
uiTestView.hidden = firstHidden
237+
tzTestView.hidden = firstHidden
238+
239+
UIView.animateWithDuration(2) {
240+
self.uiTestView.hidden = !firstHidden
241+
self.tzTestView.hidden = !firstHidden
242+
}
243+
244+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
245+
if !withAnimation {
246+
self.uiTestView.hidden = firstHidden
247+
self.tzTestView.hidden = firstHidden
248+
} else {
249+
UIView.animateWithDuration(2) {
250+
self.uiTestView.hidden = firstHidden
251+
self.tzTestView.hidden = firstHidden
252+
253+
// Animating, the presentation is not hidden
254+
XCTAssert(!self.uiTestView.layer.hidden)
255+
XCTAssert(!self.tzTestView.layer.hidden)
256+
}
257+
}
258+
259+
// Note, here we don't expect the hidden property to be the right value even when without animation,
260+
}
261+
262+
let endTime = !withAnimation ? 2.2 : 3.2
263+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(endTime * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
264+
XCTAssert(self.uiTestView.hidden == firstHidden)
265+
XCTAssert(self.tzTestView.hidden == firstHidden)
266+
}
267+
268+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64((endTime + 0.2) * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
269+
expectation.fulfill()
270+
}
271+
272+
waitForExpectationsWithTimeout(100, handler: nil)
273+
}
274+
275+
func testHidingAnimationSetAgainFirstNotHidden() {
276+
hidingAnimationSetAgainFirstHidden(false, withAnimation: false)
277+
}
278+
279+
func testHidingAnimationSetAgainFirstHidden() {
280+
hidingAnimationSetAgainFirstHidden(true, withAnimation: false)
281+
}
282+
283+
func testHidingAnimationSetAgainFirstNotHiddenWithAnimation() {
284+
hidingAnimationSetAgainFirstHidden(false, withAnimation: true)
285+
}
286+
287+
func testHidingAnimationSetAgainFirstHiddenWithAnimation() {
288+
hidingAnimationSetAgainFirstHidden(true, withAnimation: true)
289+
}
290+
}

0 commit comments

Comments
 (0)