-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathStackAnimatorTest.kt
More file actions
243 lines (196 loc) · 8.57 KB
/
StackAnimatorTest.kt
File metadata and controls
243 lines (196 loc) · 8.57 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.reactnativenavigation.viewcontrollers.stack
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.app.Activity
import android.widget.FrameLayout
import org.mockito.kotlin.*
import com.reactnativenavigation.BaseTest
import com.reactnativenavigation.mocks.Mocks
import com.reactnativenavigation.mocks.SimpleViewController
import com.reactnativenavigation.options.Options
import com.reactnativenavigation.options.params.Bool
import com.reactnativenavigation.utils.createEnterExitAnimation
import com.reactnativenavigation.viewcontrollers.child.ChildControllersRegistry
import com.reactnativenavigation.viewcontrollers.viewcontroller.ViewController
import com.reactnativenavigation.views.element.TransitionAnimatorCreator
import org.assertj.core.api.Java6Assertions.assertThat
import org.junit.Ignore
import org.junit.Test
class StackAnimatorTest : BaseTest() {
private lateinit var uut: StackAnimator
private lateinit var activity: Activity
private lateinit var child1: SimpleViewController
private lateinit var child2: SimpleViewController
private lateinit var commandAnimator: AnimatorSet
override fun beforeEach() {
super.beforeEach()
activity = newActivity()
val transitionAnimatorCreator = mock<TransitionAnimatorCreator> { }
uut = object : StackAnimator(activity, transitionAnimatorCreator) {
override fun createAnimatorSet(): AnimatorSet {
commandAnimator = spy(super.createAnimatorSet())
return commandAnimator
}
}
val childRegistry = mock<ChildControllersRegistry>()
child1 = SimpleViewController(activity, childRegistry, "child1", Options())
child2 = SimpleViewController(activity, childRegistry, "child2", Options())
}
@Test
fun push_onlyEnteringScreenIsAnimatedByDefault() {
val onAnimationEnd = mock<Runnable>()
uut.push(child2, child1, Options.EMPTY, emptyList(), onAnimationEnd)
val pushAnimator = uut.runningPushAnimations[child2]!!
assertThat(pushAnimator.childAnimations).hasSize(1)
val appearingAnimatorSet = pushAnimator.childAnimations.first() as AnimatorSet
(appearingAnimatorSet.childAnimations).forEach {
assertThat((it as ObjectAnimator).target).isEqualTo(child2.view)
}
}
@Test
fun push_exitAndEnter() {
createEnterExitPushAnimation(child2)
val onAnimationEnd = mock<Runnable>()
uut.push(child2, child1, child2.options, emptyList(), onAnimationEnd)
assertThat(commandAnimator.childAnimations).hasSize(2)
val enter = commandAnimator.childAnimations.first() as AnimatorSet
(enter.childAnimations).forEach {
assertThat((it as ObjectAnimator).target).isEqualTo(child2.view)
}
val exit = commandAnimator.childAnimations[1] as AnimatorSet
(exit.childAnimations).forEach {
assertThat((it as ObjectAnimator).target).isEqualTo(child1.view)
}
}
@Test
fun push_onAnimationEnd() {
val onAnimationEnd = mock<Runnable>()
uut.push(child2, child1, child2.options, emptyList(), onAnimationEnd)
verify(onAnimationEnd, never()).run()
commandAnimator.end()
verify(onAnimationEnd).run()
}
@Test
fun push_waitForRender_appearingScreenIsHiddenUntilAnimationStarts() {
child2.options.animations.push.waitForRender = Bool(true)
uut.push(child2, child1, child2.options, emptyList(), mock())
assertThat(child2.view.alpha).isZero()
child2.onViewWillAppear()
idleMainLooper()
assertThat(child2.view.alpha).isOne()
}
@Test
fun pop_onlyExitAnimationIsPlayedByDefault() {
val onAnimationEnd = mock<Runnable>()
uut.pop(child1, child2, child2.options, emptyList(), onAnimationEnd)
assertThat(commandAnimator.childAnimations).hasSize(1)
val disappearingAnimatorSet = commandAnimator.childAnimations.first() as AnimatorSet
(disappearingAnimatorSet.childAnimations).forEach {
assertThat((it as ObjectAnimator).target).isEqualTo(child2.view)
}
}
@Test
fun pop_enterExitAnimations() {
createEnterExitPopAnimation(child2)
val onAnimationEnd = mock<Runnable>()
uut.pop(child1, child2, child2.options, emptyList(), onAnimationEnd)
assertThat(commandAnimator.childAnimations).hasSize(2)
val enter = commandAnimator.childAnimations[1] as AnimatorSet
(enter.childAnimations).forEach {
assertThat((it as ObjectAnimator).target).isEqualTo(child1.view)
}
val exit = commandAnimator.childAnimations.first() as AnimatorSet
(exit.childAnimations).forEach {
assertThat((it as ObjectAnimator).target).isEqualTo(child2.view)
}
}
@Test
fun setRoot_onlyEnteringScreenIsAnimatedByDefault() {
val onAnimationEnd = mock<Runnable>()
uut.setRoot(child2, child2, child1.options, emptyList(), onAnimationEnd)
assertThat(commandAnimator.childAnimations).hasSize(1)
val appearingAnimatorSet = commandAnimator.childAnimations.first() as AnimatorSet
(appearingAnimatorSet.childAnimations).forEach {
assertThat((it as ObjectAnimator).target).isEqualTo(child2.view)
}
}
@Test
fun setRoot_enterExitAnimations() {
createEnterExitSetRootAnimation(child2)
val onAnimationEnd = mock<Runnable>()
uut.setRoot(child2, child1, child2.options, emptyList(), onAnimationEnd)
assertThat(commandAnimator.childAnimations).hasSize(2)
val enter = commandAnimator.childAnimations[1] as AnimatorSet
(enter.childAnimations).forEach {
assertThat((it as ObjectAnimator).target).isEqualTo(child1.view)
}
val exit = commandAnimator.childAnimations.first() as AnimatorSet
(exit.childAnimations).forEach {
assertThat((it as ObjectAnimator).target).isEqualTo(child2.view)
}
}
@Test
fun setRoot_onAnimationEnd() {
val onAnimationEnd = mock<Runnable>()
uut.setRoot(child2, child1, child2.options, emptyList(), onAnimationEnd)
verify(onAnimationEnd, never()).run()
commandAnimator.end()
verify(onAnimationEnd).run()
}
@Test
fun setRoot_waitForRender() {
val onAnimationEnd = mock<Runnable>()
child2.options.animations.setStackRoot.waitForRender = Bool(true)
uut.setRoot(child2, child1, child2.options, emptyList(), onAnimationEnd)
verify(commandAnimator, never()).start()
child2.onViewWillAppear()
idleMainLooper()
verify(commandAnimator).start()
}
@Test
fun setRoot_waitForRender_appearingScreenIsHiddenUntilAnimationStarts() {
val onAnimationEnd = mock<Runnable>()
child2.options.animations.setStackRoot.waitForRender = Bool(true)
uut.setRoot(child2, child1, child2.options, emptyList(), onAnimationEnd)
assertThat(child2.view.alpha).isZero()
child2.onViewWillAppear()
idleMainLooper()
assertThat(child2.view.alpha).isOne()
}
@Test
fun isChildInTransition() {
val child1: ViewController<*> = Mocks.viewController()
uut.push(child1, mockViewController(), Options.EMPTY, emptyList(), mock())
assertThat(uut.isChildInTransition(child1)).isTrue()
val child2: ViewController<*> = Mocks.viewController()
uut.setRoot(child2, mockViewController(), Options.EMPTY, emptyList(), mock())
assertThat(uut.isChildInTransition(child2)).isTrue()
val child3: ViewController<*> = Mocks.viewController()
uut.pop(mock(), child3, Options.EMPTY, emptyList(), mock())
assertThat(uut.isChildInTransition(child3)).isTrue()
}
private fun mockViewController(): ViewController<*> {
val vc = mock<ViewController<*>>()
val view = FrameLayout(activity)
whenever(vc.view).thenReturn(view)
return vc
}
private fun createEnterExitPushAnimation(vc: ViewController<*>) {
Options().apply {
animations.push.content = createEnterExitAnimation()
vc.mergeOptions(this)
}
}
private fun createEnterExitPopAnimation(vc: ViewController<*>) {
Options().apply {
animations.pop.content = createEnterExitAnimation()
vc.mergeOptions(this)
}
}
private fun createEnterExitSetRootAnimation(vc: ViewController<*>) {
Options().apply {
animations.setStackRoot.content = createEnterExitAnimation()
vc.mergeOptions(this)
}
}
}