Skip to content

Commit

Permalink
Add some test methods to test the hidden animation.
Browse files Browse the repository at this point in the history
  • Loading branch information
CosynPa committed Mar 5, 2016
1 parent be6971f commit 9ed35f1
Showing 1 changed file with 201 additions and 0 deletions.
201 changes: 201 additions & 0 deletions TZStackViewTests/TZStackViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1054,4 +1054,205 @@ class TZStackViewTests: TZStackViewTestCase {

verifyArrangedSubviewConsistency()
}

// If you are not animating the hidden property, the hidden property should be set immediately
func testNonAnimatingHidden() {
let uiTestView = uiStackView.arrangedSubviews.last!
let tzTestView = tzStackView.arrangedSubviews.last!

let expectation = expectationWithDescription("delay")

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
uiTestView.hidden = true
tzTestView.hidden = true

XCTAssert(uiTestView.hidden)
XCTAssert(tzTestView.hidden)
XCTAssert(uiTestView.layer.hidden)
XCTAssert(tzTestView.layer.hidden)
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
expectation.fulfill()
}

waitForExpectationsWithTimeout(100, handler: nil)
}

// If you are not animating the hidden property, the hidden property should be set immediately
func testNonAnimatingHiddenWithOther() {
let uiTestView = uiStackView.arrangedSubviews.last!
let tzTestView = tzStackView.arrangedSubviews.last!

let expectation = expectationWithDescription("delay")

uiTestView.backgroundColor = UIColor.clearColor()
tzTestView.backgroundColor = UIColor.clearColor()
UIView.animateWithDuration(2) {
uiTestView.backgroundColor = UIColor.greenColor()
tzTestView.backgroundColor = UIColor.greenColor()
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
uiTestView.hidden = true
tzTestView.hidden = true

XCTAssert(uiTestView.hidden)
XCTAssert(tzTestView.hidden)
XCTAssert(uiTestView.layer.hidden)
XCTAssert(tzTestView.layer.hidden)
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
expectation.fulfill()
}

waitForExpectationsWithTimeout(100, handler: nil)
}

func testAnimatingHidden() {
let uiTestView = uiStackView.arrangedSubviews.last!
let tzTestView = tzStackView.arrangedSubviews.last!

let expectation = expectationWithDescription("delay")

UIView.animateWithDuration(1) {
uiTestView.hidden = true
tzTestView.hidden = true

// Note uiTestView.hidden == true, tzTestView.hidden == false

// The presentation should not be hidden.
XCTAssert(!uiTestView.layer.hidden)
XCTAssert(!tzTestView.layer.hidden)
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
XCTAssert(uiTestView.hidden)
XCTAssert(tzTestView.hidden)
XCTAssert(uiTestView.layer.hidden)
XCTAssert(tzTestView.layer.hidden)
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
expectation.fulfill()
}

waitForExpectationsWithTimeout(100, handler: nil)
}

func testAnimatingHiddenWithOther() {
let uiTestView = uiStackView.arrangedSubviews.last!
let tzTestView = tzStackView.arrangedSubviews.last!

let expectation = expectationWithDescription("delay")

UIView.animateWithDuration(1) {
uiTestView.hidden = true
tzTestView.hidden = true
}

uiTestView.backgroundColor = UIColor.clearColor()
tzTestView.backgroundColor = UIColor.clearColor()
UIView.animateWithDuration(2) {
uiTestView.backgroundColor = UIColor.greenColor()
tzTestView.backgroundColor = UIColor.greenColor()
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
// The view should be hidden after the hiding animation completes even if there are still other animations
XCTAssert(uiTestView.hidden)
XCTAssert(tzTestView.hidden)
XCTAssert(uiTestView.layer.hidden)
XCTAssert(tzTestView.layer.hidden)
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
expectation.fulfill()
}

waitForExpectationsWithTimeout(100, handler: nil)
}

// The completion callback of an animation should be called
func testHidingAnimationCallback() {
let uiTestView = uiStackView.arrangedSubviews.last!
let tzTestView = tzStackView.arrangedSubviews.last!

let expectation = expectationWithDescription("delay")

var uiCompletionCalled = false
var tzCompletionCalled = false

UIView.animateWithDuration(1,
animations: {
uiTestView.hidden = true
}, completion: { _ in
uiCompletionCalled = true
})

UIView.animateWithDuration(1,
animations: {
tzTestView.hidden = true
}, completion: { _ in
tzCompletionCalled = true
})

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
XCTAssert(uiCompletionCalled)
XCTAssert(tzCompletionCalled)
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
expectation.fulfill()
}

waitForExpectationsWithTimeout(100, handler: nil)
}

// The completion callback of an animation should be called when the animation is canceled
func testHidingAnimationCallbackCancel() {
let uiTestView = uiStackView.arrangedSubviews.last!
let tzTestView = tzStackView.arrangedSubviews.last!

let expectation = expectationWithDescription("delay")

var uiCompletionCalled = false
var tzCompletionCalled = false

UIView.animateWithDuration(1,
animations: {
uiTestView.hidden = true
}, completion: { finished in
uiCompletionCalled = true
XCTAssert(!finished)
})

UIView.animateWithDuration(1,
animations: {
tzTestView.hidden = true
}, completion: { finished in
tzCompletionCalled = true
XCTAssert(!finished)
})

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
// this will cancel the animation
self.uiStackView.removeFromSuperview()
self.tzStackView.removeFromSuperview()
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.7 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
XCTAssert(uiCompletionCalled)
XCTAssert(tzCompletionCalled)
XCTAssert(uiTestView.hidden)
XCTAssert(tzTestView.hidden)
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
expectation.fulfill()
}

waitForExpectationsWithTimeout(100, handler: nil)
}
}

0 comments on commit 9ed35f1

Please sign in to comment.