|
| 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 ShapeLayerBackedView: UIView { |
| 25 | + override static var layerClass: AnyClass { return CAShapeLayer.self } |
| 26 | + |
| 27 | + override init(frame: CGRect) { |
| 28 | + super.init(frame: frame) |
| 29 | + |
| 30 | + let shapeLayer = self.layer as! CAShapeLayer |
| 31 | + shapeLayer.path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 100, height: 100)).cgPath |
| 32 | + } |
| 33 | + |
| 34 | + required init?(coder aDecoder: NSCoder) { |
| 35 | + fatalError("init(coder:) has not been implemented") |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class UIKitBehavioralTests: XCTestCase { |
| 40 | + var view: UIView! |
| 41 | + |
| 42 | + var originalImplementation: IMP? |
| 43 | + override func setUp() { |
| 44 | + super.setUp() |
| 45 | + |
| 46 | + let window = UIWindow() |
| 47 | + window.makeKeyAndVisible() |
| 48 | + view = ShapeLayerBackedView() |
| 49 | + window.addSubview(view) |
| 50 | + |
| 51 | + rebuildView() |
| 52 | + } |
| 53 | + |
| 54 | + override func tearDown() { |
| 55 | + view = nil |
| 56 | + |
| 57 | + super.tearDown() |
| 58 | + } |
| 59 | + |
| 60 | + private func rebuildView() { |
| 61 | + let oldSuperview = view.superview! |
| 62 | + view.removeFromSuperview() |
| 63 | + view = ShapeLayerBackedView() // Need to animate a view's layer to get implicit animations. |
| 64 | + oldSuperview.addSubview(view) |
| 65 | + |
| 66 | + // Connect our layers to the render server. |
| 67 | + CATransaction.flush() |
| 68 | + } |
| 69 | + |
| 70 | + func testSomePropertiesImplicitlyAnimateAdditively() { |
| 71 | + let properties: [AnimatableKeyPath: Any] = [ |
| 72 | + .cornerRadius: 3, |
| 73 | + .height: 100, |
| 74 | + .position: CGPoint(x: 50, y: 20), |
| 75 | + .rotation: 42, |
| 76 | + .scale: 2.5, |
| 77 | + .width: 25, |
| 78 | + .x: 12, |
| 79 | + .y: 23, |
| 80 | + ] |
| 81 | + for (keyPath, value) in properties { |
| 82 | + rebuildView() |
| 83 | + |
| 84 | + UIView.animate(withDuration: 0.01) { |
| 85 | + self.view.layer.setValue(value, forKeyPath: keyPath.rawValue) |
| 86 | + } |
| 87 | + |
| 88 | + XCTAssertNotNil(view.layer.animationKeys(), |
| 89 | + "Expected \(keyPath.rawValue) to generate at least one animation.") |
| 90 | + if let animationKeys = view.layer.animationKeys() { |
| 91 | + for key in animationKeys { |
| 92 | + let animation = view.layer.animation(forKey: key) as! CABasicAnimation |
| 93 | + XCTAssertTrue(animation.isAdditive, |
| 94 | + "Expected \(key) to be additive as a result of animating " |
| 95 | + + "\(keyPath.rawValue), but it was not: \(animation.debugDescription).") |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + func testSomePropertiesImplicitlyAnimateButNotAdditively() { |
| 102 | + let properties: [AnimatableKeyPath: Any] = [ |
| 103 | + .backgroundColor: UIColor.blue, |
| 104 | + .opacity: 0.5, |
| 105 | + ] |
| 106 | + for (keyPath, value) in properties { |
| 107 | + rebuildView() |
| 108 | + |
| 109 | + UIView.animate(withDuration: 0.01) { |
| 110 | + self.view.layer.setValue(value, forKeyPath: keyPath.rawValue) |
| 111 | + } |
| 112 | + |
| 113 | + XCTAssertNotNil(view.layer.animationKeys(), |
| 114 | + "Expected \(keyPath.rawValue) to generate at least one animation.") |
| 115 | + if let animationKeys = view.layer.animationKeys() { |
| 116 | + for key in animationKeys { |
| 117 | + let animation = view.layer.animation(forKey: key) as! CABasicAnimation |
| 118 | + XCTAssertFalse(animation.isAdditive, |
| 119 | + "Expected \(key) not to be additive as a result of animating " |
| 120 | + + "\(keyPath.rawValue), but it was: \(animation.debugDescription).") |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + func testSomePropertiesDoNotImplicitlyAnimate() { |
| 127 | + let properties: [AnimatableKeyPath: Any] = [ |
| 128 | + .strokeStart: 0.2, |
| 129 | + .strokeEnd: 0.5, |
| 130 | + ] |
| 131 | + for (keyPath, value) in properties { |
| 132 | + rebuildView() |
| 133 | + |
| 134 | + UIView.animate(withDuration: 0.01) { |
| 135 | + self.view.layer.setValue(value, forKeyPath: keyPath.rawValue) |
| 136 | + } |
| 137 | + |
| 138 | + XCTAssertNil(view.layer.animationKeys(), |
| 139 | + "Expected \(keyPath.rawValue) not to generate any animations.") |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
0 commit comments