This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
UIKitEquivalencyTests.swift
129 lines (108 loc) · 4.04 KB
/
UIKitEquivalencyTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Copyright 2017-present The Material Motion Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import XCTest
#if IS_BAZEL_BUILD
import _MotionAnimator
#else
import MotionAnimator
#endif
class UIKitEquivalencyTests: XCTestCase {
var view: UIView!
var originalImplementation: IMP?
override func setUp() {
super.setUp()
let window = UIWindow()
window.makeKeyAndVisible()
view = ShapeLayerBackedView()
window.addSubview(view)
rebuildView()
}
override func tearDown() {
view = nil
super.tearDown()
}
private func rebuildView() {
let oldSuperview = view.superview!
view.removeFromSuperview()
view = ShapeLayerBackedView() // Need to animate a view's layer to get implicit animations.
view.layer.anchorPoint = .zero // Needed to ensure that animating the width/height don't also
oldSuperview.addSubview(view)
// Connect our layers to the render server.
CATransaction.flush()
}
// MARK: Each animatable property needs to be added to exactly one of the following three tests
func testMostPropertiesImplicitlyAnimateAdditively() {
let baselineProperties: [AnimatableKeyPath: Any] = [
.borderWidth: 5,
.cornerRadius: 3,
.position: CGPoint(x: 50, y: 20),
.rotation: 42,
.scale: 2.5,
.shadowOffset: CGSize(width: 1, height: 1),
.shadowOpacity: 0.3,
.shadowRadius: 5,
.strokeStart: 0.2,
.strokeEnd: 0.5,
.transform: CGAffineTransform(scaleX: 1.5, y: 1.5),
.x: 12,
.y: 23,
.z: 3,
// Should animate additively, but blocked by
// https://github.com/material-motion/motion-animator-objc/issues/74
//.bounds: CGRect(x: 0, y: 0, width: 123, height: 456),
//.height: 100,
//.width: 25,
]
for (keyPath, value) in baselineProperties {
rebuildView()
MotionAnimator.animate(withDuration: 0.01) {
self.view.layer.setValue(value, forKeyPath: keyPath.rawValue)
}
XCTAssertNotNil(view.layer.animationKeys(),
"Expected \(keyPath.rawValue) to generate at least one animation.")
if let animationKeys = view.layer.animationKeys() {
for key in animationKeys {
let animation = view.layer.animation(forKey: key) as! CABasicAnimation
XCTAssertTrue(animation.isAdditive,
"Expected \(key) to be additive as a result of animating "
+ "\(keyPath.rawValue), but it was not: \(animation.debugDescription).")
}
}
}
}
func testSomePropertiesImplicitlyAnimateButNotAdditively() {
let baselineProperties: [AnimatableKeyPath: Any] = [
.anchorPoint: CGPoint(x: 0.2, y: 0.4),
.backgroundColor: UIColor.blue,
.borderColor: UIColor.red,
.opacity: 0.5,
.shadowColor: UIColor.blue,
]
for (keyPath, value) in baselineProperties {
rebuildView()
MotionAnimator.animate(withDuration: 0.01) {
self.view.layer.setValue(value, forKeyPath: keyPath.rawValue)
}
XCTAssertNotNil(view.layer.animationKeys(),
"Expected \(keyPath.rawValue) to generate at least one animation.")
if let animationKeys = view.layer.animationKeys() {
for key in animationKeys {
let animation = view.layer.animation(forKey: key) as! CABasicAnimation
XCTAssertFalse(animation.isAdditive,
"Expected \(key) not to be additive as a result of animating "
+ "\(keyPath.rawValue), but it was: \(animation.debugDescription).")
}
}
}
}
}