|
| 1 | +// |
| 2 | +// UIView+AnimationTests.swift |
| 3 | +// YCoreUI |
| 4 | +// |
| 5 | +// Created by Mark Pospesel on 3/31/23. |
| 6 | +// Copyright © 2023 Y Media Labs. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import XCTest |
| 10 | +@testable import YCoreUI |
| 11 | + |
| 12 | +final class UIViewAnimationTests: XCTestCase { |
| 13 | + func test_regular_deliversAnimation() throws { |
| 14 | + defer { SpyView.reset() } |
| 15 | + // Given |
| 16 | + let duration = CGFloat(Int.random(in: 1...5)) / 10.0 |
| 17 | + let delay = CGFloat(Int.random(in: 1...5)) / 10.0 |
| 18 | + let options = try XCTUnwrap(getOptions().randomElement()) |
| 19 | + let sut = Animation( |
| 20 | + duration: duration, |
| 21 | + delay: delay, |
| 22 | + curve: .regular(options: options) |
| 23 | + ) |
| 24 | + var isAnimationBlockCalled = false |
| 25 | + var isCompletionBlockCalled = false |
| 26 | + |
| 27 | + // When |
| 28 | + SpyView.animate( |
| 29 | + with: sut |
| 30 | + ) { |
| 31 | + isAnimationBlockCalled = true |
| 32 | + } completion: { _ in |
| 33 | + isCompletionBlockCalled = true |
| 34 | + } |
| 35 | + |
| 36 | + // Then |
| 37 | + XCTAssertEqual(SpyView.lastAnimation, sut) |
| 38 | + XCTAssertTrue(isAnimationBlockCalled) |
| 39 | + XCTAssertTrue(isCompletionBlockCalled) |
| 40 | + } |
| 41 | + |
| 42 | + func test_spring_deliversAnimation() throws { |
| 43 | + defer { SpyView.reset() } |
| 44 | + // Given |
| 45 | + let duration = CGFloat(Int.random(in: 1...5)) / 10.0 |
| 46 | + let delay = CGFloat(Int.random(in: 1...5)) / 10.0 |
| 47 | + let options = try XCTUnwrap(getOptions().randomElement()) |
| 48 | + let damping = CGFloat(Int.random(in: 1...10)) / 10.0 |
| 49 | + let velocity = CGFloat(Int.random(in: 1...6)) / 10.0 |
| 50 | + let sut = Animation( |
| 51 | + duration: duration, |
| 52 | + delay: delay, |
| 53 | + curve: .spring(damping: damping, velocity: velocity, options: options) |
| 54 | + ) |
| 55 | + var isAnimationBlockCalled = false |
| 56 | + var isCompletionBlockCalled = false |
| 57 | + |
| 58 | + // When |
| 59 | + SpyView.animate( |
| 60 | + with: sut |
| 61 | + ) { |
| 62 | + isAnimationBlockCalled = true |
| 63 | + } completion: { _ in |
| 64 | + isCompletionBlockCalled = true |
| 65 | + } |
| 66 | + |
| 67 | + // Then |
| 68 | + XCTAssertEqual(SpyView.lastAnimation, sut) |
| 69 | + XCTAssertTrue(isAnimationBlockCalled) |
| 70 | + XCTAssertTrue(isCompletionBlockCalled) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +extension UIViewAnimationTests { |
| 75 | + func getOptions() -> [UIView.AnimationOptions] { |
| 76 | + [ |
| 77 | + [], |
| 78 | + .curveEaseIn, |
| 79 | + .curveEaseInOut, |
| 80 | + .curveEaseOut, |
| 81 | + .beginFromCurrentState |
| 82 | + ] |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +final class SpyView: UIView { |
| 87 | + static var lastAnimation: Animation? |
| 88 | + |
| 89 | + override class func animate( |
| 90 | + withDuration duration: TimeInterval, |
| 91 | + delay: TimeInterval, |
| 92 | + options: UIView.AnimationOptions = [], |
| 93 | + animations: @escaping () -> Void, |
| 94 | + completion: ((Bool) -> Void)? = nil |
| 95 | + ) { |
| 96 | + lastAnimation = Animation( |
| 97 | + duration: duration, |
| 98 | + delay: delay, |
| 99 | + curve: .regular(options: options) |
| 100 | + ) |
| 101 | + animations() |
| 102 | + completion?(true) |
| 103 | + } |
| 104 | + |
| 105 | + override class func animate( |
| 106 | + withDuration duration: TimeInterval, |
| 107 | + delay: TimeInterval, |
| 108 | + usingSpringWithDamping |
| 109 | + dampingRatio: CGFloat, |
| 110 | + initialSpringVelocity velocity: CGFloat, |
| 111 | + options: UIView.AnimationOptions = [], |
| 112 | + animations: @escaping () -> Void, |
| 113 | + completion: ((Bool) -> Void)? = nil |
| 114 | + ) { |
| 115 | + lastAnimation = Animation( |
| 116 | + duration: duration, |
| 117 | + delay: delay, |
| 118 | + curve: .spring(damping: dampingRatio, velocity: velocity, options: options) |
| 119 | + ) |
| 120 | + animations() |
| 121 | + completion?(true) |
| 122 | + } |
| 123 | + |
| 124 | + class func reset() { |
| 125 | + lastAnimation = nil |
| 126 | + } |
| 127 | +} |
0 commit comments