Skip to content

Commit d9bfa19

Browse files
committed
Add some test methods to test the hidden animation.
1 parent 2e0ad5c commit d9bfa19

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed

TZStackViewTests/TZStackViewTests.swift

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,4 +1054,205 @@ class TZStackViewTests: TZStackViewTestCase {
10541054

10551055
verifyArrangedSubviewConsistency()
10561056
}
1057+
1058+
// If you are not animating the hidden property, the hidden property should be set immediately
1059+
func testNonAnimatingHidden() {
1060+
let uiTestView = uiStackView.arrangedSubviews.last!
1061+
let tzTestView = tzStackView.arrangedSubviews.last!
1062+
1063+
let expectation = expectationWithDescription("delay")
1064+
1065+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1066+
uiTestView.hidden = true
1067+
tzTestView.hidden = true
1068+
1069+
XCTAssert(uiTestView.hidden)
1070+
XCTAssert(tzTestView.hidden)
1071+
XCTAssert(uiTestView.layer.hidden)
1072+
XCTAssert(tzTestView.layer.hidden)
1073+
}
1074+
1075+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1076+
expectation.fulfill()
1077+
}
1078+
1079+
waitForExpectationsWithTimeout(100, handler: nil)
1080+
}
1081+
1082+
// If you are not animating the hidden property, the hidden property should be set immediately
1083+
func testNonAnimatingHiddenWithOther() {
1084+
let uiTestView = uiStackView.arrangedSubviews.last!
1085+
let tzTestView = tzStackView.arrangedSubviews.last!
1086+
1087+
let expectation = expectationWithDescription("delay")
1088+
1089+
uiTestView.backgroundColor = UIColor.clearColor()
1090+
tzTestView.backgroundColor = UIColor.clearColor()
1091+
UIView.animateWithDuration(2) {
1092+
uiTestView.backgroundColor = UIColor.greenColor()
1093+
tzTestView.backgroundColor = UIColor.greenColor()
1094+
}
1095+
1096+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1097+
uiTestView.hidden = true
1098+
tzTestView.hidden = true
1099+
1100+
XCTAssert(uiTestView.hidden)
1101+
XCTAssert(tzTestView.hidden)
1102+
XCTAssert(uiTestView.layer.hidden)
1103+
XCTAssert(tzTestView.layer.hidden)
1104+
}
1105+
1106+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1107+
expectation.fulfill()
1108+
}
1109+
1110+
waitForExpectationsWithTimeout(100, handler: nil)
1111+
}
1112+
1113+
func testAnimatingHidden() {
1114+
let uiTestView = uiStackView.arrangedSubviews.last!
1115+
let tzTestView = tzStackView.arrangedSubviews.last!
1116+
1117+
let expectation = expectationWithDescription("delay")
1118+
1119+
UIView.animateWithDuration(1) {
1120+
uiTestView.hidden = true
1121+
tzTestView.hidden = true
1122+
1123+
// Note uiTestView.hidden == true, tzTestView.hidden == false
1124+
1125+
// The presentation should not be hidden.
1126+
XCTAssert(!uiTestView.layer.hidden)
1127+
XCTAssert(!tzTestView.layer.hidden)
1128+
}
1129+
1130+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1131+
XCTAssert(uiTestView.hidden)
1132+
XCTAssert(tzTestView.hidden)
1133+
XCTAssert(uiTestView.layer.hidden)
1134+
XCTAssert(tzTestView.layer.hidden)
1135+
}
1136+
1137+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1138+
expectation.fulfill()
1139+
}
1140+
1141+
waitForExpectationsWithTimeout(100, handler: nil)
1142+
}
1143+
1144+
func testAnimatingHiddenWithOther() {
1145+
let uiTestView = uiStackView.arrangedSubviews.last!
1146+
let tzTestView = tzStackView.arrangedSubviews.last!
1147+
1148+
let expectation = expectationWithDescription("delay")
1149+
1150+
UIView.animateWithDuration(1) {
1151+
uiTestView.hidden = true
1152+
tzTestView.hidden = true
1153+
}
1154+
1155+
uiTestView.backgroundColor = UIColor.clearColor()
1156+
tzTestView.backgroundColor = UIColor.clearColor()
1157+
UIView.animateWithDuration(2) {
1158+
uiTestView.backgroundColor = UIColor.greenColor()
1159+
tzTestView.backgroundColor = UIColor.greenColor()
1160+
}
1161+
1162+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1163+
// The view should be hidden after the hidden animation completes even if there are still other animations
1164+
XCTAssert(uiTestView.hidden)
1165+
XCTAssert(tzTestView.hidden)
1166+
XCTAssert(uiTestView.layer.hidden)
1167+
XCTAssert(tzTestView.layer.hidden)
1168+
}
1169+
1170+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1171+
expectation.fulfill()
1172+
}
1173+
1174+
waitForExpectationsWithTimeout(100, handler: nil)
1175+
}
1176+
1177+
// The completion callback of an animation should be called
1178+
func testHiddenAnimationCallback() {
1179+
let uiTestView = uiStackView.arrangedSubviews.last!
1180+
let tzTestView = tzStackView.arrangedSubviews.last!
1181+
1182+
let expectation = expectationWithDescription("delay")
1183+
1184+
var uiCompletionCalled = false
1185+
var tzCompletionCalled = false
1186+
1187+
UIView.animateWithDuration(1,
1188+
animations: {
1189+
uiTestView.hidden = true
1190+
}, completion: { _ in
1191+
uiCompletionCalled = true
1192+
})
1193+
1194+
UIView.animateWithDuration(1,
1195+
animations: {
1196+
tzTestView.hidden = true
1197+
}, completion: { _ in
1198+
tzCompletionCalled = true
1199+
})
1200+
1201+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1202+
XCTAssert(uiCompletionCalled)
1203+
XCTAssert(tzCompletionCalled)
1204+
}
1205+
1206+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1207+
expectation.fulfill()
1208+
}
1209+
1210+
waitForExpectationsWithTimeout(100, handler: nil)
1211+
}
1212+
1213+
// The completion callback of an animation should be called when the animation is canceled
1214+
func testHiddenAnimationCallbackCancel() {
1215+
let uiTestView = uiStackView.arrangedSubviews.last!
1216+
let tzTestView = tzStackView.arrangedSubviews.last!
1217+
1218+
let expectation = expectationWithDescription("delay")
1219+
1220+
var uiCompletionCalled = false
1221+
var tzCompletionCalled = false
1222+
1223+
UIView.animateWithDuration(1,
1224+
animations: {
1225+
uiTestView.hidden = true
1226+
}, completion: { finished in
1227+
uiCompletionCalled = true
1228+
XCTAssert(!finished)
1229+
})
1230+
1231+
UIView.animateWithDuration(1,
1232+
animations: {
1233+
tzTestView.hidden = true
1234+
}, completion: { finished in
1235+
tzCompletionCalled = true
1236+
XCTAssert(!finished)
1237+
})
1238+
1239+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1240+
// this will cancel the animation
1241+
self.uiStackView.removeFromSuperview()
1242+
self.tzStackView.removeFromSuperview()
1243+
}
1244+
1245+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.7 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1246+
XCTAssert(uiCompletionCalled)
1247+
XCTAssert(tzCompletionCalled)
1248+
XCTAssert(uiTestView.hidden)
1249+
XCTAssert(tzTestView.hidden)
1250+
}
1251+
1252+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
1253+
expectation.fulfill()
1254+
}
1255+
1256+
waitForExpectationsWithTimeout(100, handler: nil)
1257+
}
10571258
}

0 commit comments

Comments
 (0)