|
| 1 | +/* |
| 2 | + Copyright 2017-present The Material Motion Authors. All Rights Reserved. |
| 3 | + |
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | + |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + |
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import XCTest |
| 18 | +#if IS_BAZEL_BUILD |
| 19 | +import _MotionAnimator |
| 20 | +#else |
| 21 | +import MotionAnimator |
| 22 | +#endif |
| 23 | + |
| 24 | +class NonAdditiveAnimationTests: XCTestCase { |
| 25 | + var animator: MotionAnimator! |
| 26 | + var timing: MotionTiming! |
| 27 | + var view: UIView! |
| 28 | + |
| 29 | + override func setUp() { |
| 30 | + super.setUp() |
| 31 | + |
| 32 | + animator = MotionAnimator() |
| 33 | + |
| 34 | + animator.additive = false |
| 35 | + |
| 36 | + timing = MotionTiming(delay: 0, |
| 37 | + duration: 1, |
| 38 | + curve: MotionCurveMakeBezier(p1x: 0, p1y: 0, p2x: 0, p2y: 0), |
| 39 | + repetition: .init(type: .none, amount: 0, autoreverses: false)) |
| 40 | + |
| 41 | + let window = UIWindow() |
| 42 | + window.makeKeyAndVisible() |
| 43 | + view = UIView() // Need to animate a view's layer to get implicit animations. |
| 44 | + window.addSubview(view) |
| 45 | + |
| 46 | + // Connect our layers to the render server. |
| 47 | + CATransaction.flush() |
| 48 | + } |
| 49 | + |
| 50 | + override func tearDown() { |
| 51 | + animator = nil |
| 52 | + timing = nil |
| 53 | + view = nil |
| 54 | + |
| 55 | + super.tearDown() |
| 56 | + } |
| 57 | + |
| 58 | + func testNumericKeyPathsDontAnimateAdditively() { |
| 59 | + animator.animate(with: timing, to: view.layer, withValues: [1, 0], keyPath: .cornerRadius) |
| 60 | + |
| 61 | + XCTAssertNotNil(view.layer.animationKeys(), |
| 62 | + "Expected an animation to be added, but none were found.") |
| 63 | + guard let animationKeys = view.layer.animationKeys() else { |
| 64 | + return |
| 65 | + } |
| 66 | + XCTAssertEqual(animationKeys.count, 1, |
| 67 | + "Expected only one animation to be added, but the following were found: " |
| 68 | + + "\(animationKeys).") |
| 69 | + guard let key = animationKeys.first, |
| 70 | + let animation = view.layer.animation(forKey: key) as? CABasicAnimation else { |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + XCTAssertFalse(animation.isAdditive, "Animation is additive when it shouldn't be.") |
| 75 | + } |
| 76 | + |
| 77 | + func testSizeKeyPathsDontAnimateAdditively() { |
| 78 | + animator.animate(with: timing, to: view.layer, |
| 79 | + withValues: [CGSize(width: 0, height: 0), |
| 80 | + CGSize(width: 1, height: 2)], keyPath: .shadowOffset) |
| 81 | + |
| 82 | + XCTAssertNotNil(view.layer.animationKeys(), |
| 83 | + "Expected an animation to be added, but none were found.") |
| 84 | + guard let animationKeys = view.layer.animationKeys() else { |
| 85 | + return |
| 86 | + } |
| 87 | + XCTAssertEqual(animationKeys.count, 1, |
| 88 | + "Expected only one animation to be added, but the following were found: " |
| 89 | + + "\(animationKeys).") |
| 90 | + guard let key = animationKeys.first, |
| 91 | + let animation = view.layer.animation(forKey: key) as? CABasicAnimation else { |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + XCTAssertFalse(animation.isAdditive, "Animation is additive when it shouldn't be.") |
| 96 | + } |
| 97 | + |
| 98 | + func testPositionKeyPathsDontAnimateAdditively() { |
| 99 | + animator.animate(with: timing, to: view.layer, |
| 100 | + withValues: [CGPoint(x: 0, y: 0), |
| 101 | + CGPoint(x: 1, y: 2)], keyPath: .position) |
| 102 | + |
| 103 | + XCTAssertNotNil(view.layer.animationKeys(), |
| 104 | + "Expected an animation to be added, but none were found.") |
| 105 | + guard let animationKeys = view.layer.animationKeys() else { |
| 106 | + return |
| 107 | + } |
| 108 | + XCTAssertEqual(animationKeys.count, 1, |
| 109 | + "Expected only one animation to be added, but the following were found: " |
| 110 | + + "\(animationKeys).") |
| 111 | + guard let key = animationKeys.first, |
| 112 | + let animation = view.layer.animation(forKey: key) as? CABasicAnimation else { |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + XCTAssertFalse(animation.isAdditive, "Animation is additive when it shouldn't be.") |
| 117 | + } |
| 118 | + |
| 119 | + func testRectKeyPathsDontAnimateAdditively() { |
| 120 | + animator.animate(with: timing, to: view.layer, |
| 121 | + withValues: [CGRect(x: 0, y: 0, width: 0, height: 0), |
| 122 | + CGRect(x: 0, y: 0, width: 100, height: 50)], keyPath: .bounds) |
| 123 | + |
| 124 | + XCTAssertNotNil(view.layer.animationKeys(), |
| 125 | + "Expected an animation to be added, but none were found.") |
| 126 | + guard let animationKeys = view.layer.animationKeys() else { |
| 127 | + return |
| 128 | + } |
| 129 | + XCTAssertEqual(animationKeys.count, 1, |
| 130 | + "Expected only one animation to be added, but the following were found: " |
| 131 | + + "\(animationKeys).") |
| 132 | + guard let key = animationKeys.first, |
| 133 | + let animation = view.layer.animation(forKey: key) as? CABasicAnimation else { |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + XCTAssertFalse(animation.isAdditive, "Animation is additive when it shouldn't be.") |
| 138 | + } |
| 139 | +} |
0 commit comments