-
Notifications
You must be signed in to change notification settings - Fork 70
/
LazySetNavigatorTest.kt
140 lines (118 loc) · 5.53 KB
/
LazySetNavigatorTest.kt
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
package com.wealthfront.magellan.navigation
import android.app.Activity
import android.os.Looper
import com.google.common.truth.Truth.assertThat
import com.wealthfront.magellan.ScreenContainer
import com.wealthfront.magellan.internal.test.DummyStep
import com.wealthfront.magellan.lifecycle.LifecycleState
import com.wealthfront.magellan.lifecycle.transitionToState
import com.wealthfront.magellan.transitions.CrossfadeTransition
import io.mockk.MockKAnnotations.init
import io.mockk.clearMocks
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.verify
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric.buildActivity
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
@RunWith(RobolectricTestRunner::class)
class LazySetNavigatorTest {
private val activityController = buildActivity(Activity::class.java)
private lateinit var navigator: LazySetNavigator
private lateinit var step1: DummyStep
private lateinit var step2: DummyStep
@MockK private lateinit var navigableListener: NavigationListener
@Before
fun setUp() {
init(this)
initMocks()
navigator = LazySetNavigator { ScreenContainer(activityController.get()) }
step1 = DummyStep()
step2 = DummyStep()
NavigationPropagator.addNavigableListener(navigableListener)
}
@After
fun tearDown() {
NavigationPropagator.removeNavigableListener(navigableListener)
}
private fun initMocks() {
every { navigableListener.onNavigatedTo(any()) }.answers { }
every { navigableListener.onNavigatedFrom(any()) }.answers { }
every { navigableListener.beforeNavigation() }.answers { }
every { navigableListener.afterNavigation() }.answers { }
}
@Test
fun lifecycleTeardown() {
navigator.addNavigables(setOf(step1, step2))
navigator.transitionToState(LifecycleState.Created(activityController.get()))
navigator.replace(step1, CrossfadeTransition())
navigator.transitionToState(LifecycleState.Shown(activityController.get()))
assertThat(navigator.containerView!!.childCount).isEqualTo(1)
assertThat(step1.currentState).isInstanceOf(LifecycleState.Shown::class.java)
navigator.transitionToState(LifecycleState.Created(activityController.get()))
assertThat(navigator.containerView).isNull()
assertThat(step1.currentState).isInstanceOf(LifecycleState.Created::class.java)
navigator.transitionToState(LifecycleState.Destroyed)
assertThat(navigator.containerView).isNull()
assertThat(step1.currentState).isInstanceOf(LifecycleState.Destroyed::class.java)
}
@Test
fun replace() {
navigator.addNavigables(setOf(step1, step2))
navigator.transitionToState(LifecycleState.Resumed(activityController.get()))
navigator.replace(step1, CrossfadeTransition())
step1.view!!.viewTreeObserver.dispatchOnPreDraw()
shadowOf(Looper.getMainLooper()).idle()
assertThat(navigator.containerView!!.childCount).isEqualTo(1)
assertThat(navigator.containerView!!.getChildAt(0)).isEqualTo(step1.view)
assertThat(step1.currentState).isInstanceOf(LifecycleState.Resumed::class.java)
assertThat(step2.currentState).isInstanceOf(LifecycleState.Created::class.java)
verify { navigableListener.beforeNavigation() }
verify(exactly = 0) { navigableListener.onNavigatedFrom(any()) }
verify { navigableListener.onNavigatedTo(step1) }
verify { navigableListener.afterNavigation() }
clearMocks(navigableListener)
initMocks()
navigator.replace(step2, CrossfadeTransition())
step2.view!!.viewTreeObserver.dispatchOnPreDraw()
shadowOf(Looper.getMainLooper()).idle()
assertThat(navigator.containerView!!.childCount).isEqualTo(1)
assertThat(navigator.containerView!!.getChildAt(0)).isEqualTo(step2.view)
assertThat(step1.currentState).isInstanceOf(LifecycleState.Shown::class.java)
assertThat(step2.currentState).isInstanceOf(LifecycleState.Resumed::class.java)
verify { navigableListener.beforeNavigation() }
verify { navigableListener.onNavigatedFrom(step1) }
verify { navigableListener.onNavigatedTo(step2) }
verify { navigableListener.afterNavigation() }
}
@Test
fun replaceTwice() {
navigator.addNavigables(setOf(step1, step2))
navigator.transitionToState(LifecycleState.Resumed(activityController.get()))
navigator.replace(step1, CrossfadeTransition())
step1.view!!.viewTreeObserver.dispatchOnPreDraw()
shadowOf(Looper.getMainLooper()).idle()
assertThat(navigator.containerView!!.childCount).isEqualTo(1)
assertThat(navigator.containerView!!.getChildAt(0)).isEqualTo(step1.view)
assertThat(step1.currentState).isInstanceOf(LifecycleState.Resumed::class.java)
assertThat(step2.currentState).isInstanceOf(LifecycleState.Created::class.java)
verify { navigableListener.beforeNavigation() }
verify(exactly = 0) { navigableListener.onNavigatedFrom(any()) }
verify { navigableListener.onNavigatedTo(step1) }
verify { navigableListener.afterNavigation() }
clearMocks(navigableListener)
initMocks()
navigator.replace(step2, CrossfadeTransition())
navigator.replace(step1, CrossfadeTransition())
step1.view!!.viewTreeObserver.dispatchOnPreDraw()
shadowOf(Looper.getMainLooper()).idle()
assertThat(navigator.containerView!!.childCount).isEqualTo(1)
assertThat(navigator.containerView!!.getChildAt(0)).isEqualTo(step1.view)
assertThat(step1.currentState).isInstanceOf(LifecycleState.Resumed::class.java)
assertThat(step2.currentState).isInstanceOf(LifecycleState.Shown::class.java)
}
}