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