@@ -19,6 +19,8 @@ package im.vector.lib.core.utils.timer
19
19
import im.vector.lib.core.utils.test.fakes.FakeClock
20
20
import io.mockk.every
21
21
import io.mockk.mockk
22
+ import io.mockk.spyk
23
+ import io.mockk.verify
22
24
import io.mockk.verifySequence
23
25
import kotlinx.coroutines.ExperimentalCoroutinesApi
24
26
import kotlinx.coroutines.test.advanceTimeBy
@@ -36,6 +38,7 @@ internal class CountUpTimerTest {
36
38
37
39
@Test
38
40
fun `when pausing and resuming the timer, the timer ticks the right values at the right moments` () = runTest {
41
+ // Given
39
42
every { fakeClock.epochMillis() } answers { currentTime }
40
43
val tickListener = mockk<CountUpTimer .TickListener >(relaxed = true )
41
44
val timer = CountUpTimer (
@@ -44,6 +47,7 @@ internal class CountUpTimerTest {
44
47
intervalInMs = AN_INTERVAL ,
45
48
).also { it.tickListener = tickListener }
46
49
50
+ // When
47
51
timer.start()
48
52
advanceTimeBy(AN_INTERVAL / 2 ) // no tick
49
53
timer.pause() // tick
@@ -52,6 +56,7 @@ internal class CountUpTimerTest {
52
56
advanceTimeBy(AN_INTERVAL * 4 ) // tick * 4
53
57
timer.stop() // tick
54
58
59
+ // Then
55
60
verifySequence {
56
61
tickListener.onTick(AN_INTERVAL / 2 )
57
62
tickListener.onTick(AN_INTERVAL )
@@ -64,6 +69,7 @@ internal class CountUpTimerTest {
64
69
65
70
@Test
66
71
fun `given an initial time, the timer ticks the right values at the right moments` () = runTest {
72
+ // Given
67
73
every { fakeClock.epochMillis() } answers { currentTime }
68
74
val tickListener = mockk<CountUpTimer .TickListener >(relaxed = true )
69
75
val timer = CountUpTimer (
@@ -72,6 +78,7 @@ internal class CountUpTimerTest {
72
78
intervalInMs = AN_INTERVAL ,
73
79
).also { it.tickListener = tickListener }
74
80
81
+ // When
75
82
timer.start(AN_INITIAL_TIME )
76
83
advanceTimeBy(AN_INTERVAL ) // tick
77
84
timer.pause() // tick
@@ -80,6 +87,7 @@ internal class CountUpTimerTest {
80
87
advanceTimeBy(AN_INTERVAL * 4 ) // tick * 4
81
88
timer.stop() // tick
82
89
90
+ // Then
83
91
val offset = AN_INITIAL_TIME % AN_INTERVAL
84
92
verifySequence {
85
93
tickListener.onTick(AN_INITIAL_TIME + AN_INTERVAL - offset)
@@ -91,4 +99,54 @@ internal class CountUpTimerTest {
91
99
tickListener.onTick(AN_INITIAL_TIME + AN_INTERVAL * 5 )
92
100
}
93
101
}
102
+
103
+ @Test
104
+ fun `when stopping the timer on tick, the stop action is called twice and the timer ticks twice` () = runTest {
105
+ // Given
106
+ every { fakeClock.epochMillis() } answers { currentTime }
107
+ val timer = spyk(
108
+ CountUpTimer (
109
+ coroutineScope = this ,
110
+ clock = fakeClock,
111
+ intervalInMs = AN_INTERVAL ,
112
+ )
113
+ )
114
+ val tickListener = mockk<CountUpTimer .TickListener > {
115
+ every { onTick(any()) } answers { timer.stop() }
116
+ }
117
+ timer.tickListener = tickListener
118
+
119
+ // When
120
+ timer.start()
121
+ advanceTimeBy(AN_INTERVAL * 10 )
122
+
123
+ // Then
124
+ verify(exactly = 2 ) { timer.stop() } // one call at the first tick, a second time because of the tick of the first stop
125
+ verify(exactly = 2 ) { tickListener.onTick(any()) } // one after reaching the first interval, a second after the stop action
126
+ }
127
+
128
+ @Test
129
+ fun `when pausing the timer on tick, the pause action is called twice and the timer ticks twice` () = runTest {
130
+ // Given
131
+ every { fakeClock.epochMillis() } answers { currentTime }
132
+ val timer = spyk(
133
+ CountUpTimer (
134
+ coroutineScope = this ,
135
+ clock = fakeClock,
136
+ intervalInMs = AN_INTERVAL ,
137
+ )
138
+ )
139
+ val tickListener = mockk<CountUpTimer .TickListener > {
140
+ every { onTick(any()) } answers { timer.pause() }
141
+ }
142
+ timer.tickListener = tickListener
143
+
144
+ // When
145
+ timer.start()
146
+ advanceTimeBy(AN_INTERVAL * 10 )
147
+
148
+ // Then
149
+ verify(exactly = 2 ) { timer.pause() } // one call at the first tick, a second time because of the tick of the first pause
150
+ verify(exactly = 2 ) { tickListener.onTick(any()) } // one after reaching the first interval, a second after the pause action
151
+ }
94
152
}
0 commit comments