@@ -19,8 +19,10 @@ import io.mockk.verify
19
19
import kotlinx.coroutines.ExperimentalCoroutinesApi
20
20
import kotlinx.coroutines.flow.MutableSharedFlow
21
21
import kotlinx.coroutines.flow.first
22
+ import kotlinx.coroutines.test.StandardTestDispatcher
22
23
import kotlinx.coroutines.test.TestResult
23
24
import kotlinx.coroutines.test.TestScope
25
+ import kotlinx.coroutines.test.runCurrent
24
26
import kotlinx.coroutines.test.runTest
25
27
import org.junit.Assert.assertEquals
26
28
import org.junit.Assert.assertFalse
@@ -33,6 +35,8 @@ import org.robolectric.RobolectricTestRunner
33
35
@OptIn(ExperimentalCoroutinesApi ::class )
34
36
@RunWith(RobolectricTestRunner ::class )
35
37
public class PagerModelTest {
38
+ private val testDispatcher = StandardTestDispatcher ()
39
+ private val testScope = TestScope (testDispatcher)
36
40
37
41
private val scrollsFlow = MutableSharedFlow <PagerScrollEvent >()
38
42
@@ -41,7 +45,7 @@ public class PagerModelTest {
41
45
private val mockEnv: ModelEnvironment = mockk(relaxed = true ) {
42
46
every { reporter } returns mockReporter
43
47
every { actionsRunner } returns mockActionsRunner
44
- every { modelScope } returns TestScope ()
48
+ every { modelScope } returns testScope
45
49
}
46
50
private val mockView: PagerView = mockk(relaxed = true )
47
51
private val mockViewListener: PagerModel .Listener = mockk(relaxed = true )
@@ -65,6 +69,8 @@ public class PagerModelTest {
65
69
66
70
mockkStatic(PagerView ::pagerScrolls)
67
71
every { mockView.pagerScrolls() } returns scrollsFlow
72
+
73
+ testScope.runCurrent()
68
74
}
69
75
70
76
@Test
@@ -81,7 +87,10 @@ public class PagerModelTest {
81
87
// Verify that the correct number of page items is available via the model
82
88
assertEquals(3 , pagerModel.items.size)
83
89
90
+ // Verify that the model notified the view to scroll to the first page
84
91
verify { mockViewListener.scrollTo(0 ) }
92
+ // Verify that actions were run for the initial page display
93
+ verify(exactly = 1 ) { mockActionsRunner.run (any(), any()) }
85
94
}
86
95
87
96
@Test
@@ -105,10 +114,9 @@ public class PagerModelTest {
105
114
assertTrue(updatedState.hasNext)
106
115
assertTrue(updatedState.hasPrevious)
107
116
108
- verify {
109
- mockReporter.report(any(), any())
110
- mockActionsRunner.run (any(), any())
111
- }
117
+ // Verify that we reported an event and ran actions
118
+ verify { mockReporter.report(any(), any()) }
119
+ verify(exactly = 1 ) { mockActionsRunner.run (any(), any()) }
112
120
113
121
ensureAllEventsConsumed()
114
122
}
@@ -135,7 +143,7 @@ public class PagerModelTest {
135
143
assertTrue(updatedState.hasNext)
136
144
assertTrue(updatedState.hasPrevious)
137
145
138
- // Verify that we didn't report an event, but ran any actions for the given page .
146
+ // Verify that we didn't report an event, but did run actions.
139
147
verify(exactly = 0 ) { mockReporter.report(any(), any()) }
140
148
verify(exactly = 1 ) { mockActionsRunner.run (any(), any()) }
141
149
@@ -148,14 +156,20 @@ public class PagerModelTest {
148
156
pagerModel.onViewAttached(mockView)
149
157
150
158
verify { mockViewListener.scrollTo(0 ) }
159
+ // Verify actions were run for the initial page display
160
+ verify(exactly = 1 ) { mockActionsRunner.run (any(), any()) }
151
161
152
162
pagerState.update { it.copyWithPageIndex(1 ) }
163
+ // Run the pending state update task, so the model can process it.
164
+ testScope.runCurrent()
153
165
154
166
val state = pagerState.changes.first()
155
167
// Sanity check
156
168
assertEquals(1 , state.pageIndex)
157
169
158
170
verify { mockViewListener.scrollTo(1 ) }
171
+ // Verify actions were also run on display of the next page
172
+ verify(exactly = 2 ) { mockActionsRunner.run (any(), any()) }
159
173
}
160
174
161
175
@Test
0 commit comments