-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathHandlerDispatcherTest.kt
135 lines (122 loc) · 3.99 KB
/
HandlerDispatcherTest.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
/*
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.android
import android.os.*
import kotlinx.coroutines.*
import org.junit.Test
import org.junit.runner.*
import org.robolectric.*
import org.robolectric.annotation.*
import org.robolectric.shadows.*
import java.util.concurrent.*
import kotlin.test.*
@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE, sdk = [28])
@LooperMode(LooperMode.Mode.LEGACY)
class HandlerDispatcherTest : TestBase() {
@Test
fun testImmediateDispatcherYield() = runBlocking(Dispatchers.Main) {
expect(1)
// launch in the immediate dispatcher
launch(Dispatchers.Main.immediate) {
expect(2)
yield()
expect(4)
}
expect(3) // after yield
yield() // yield back
finish(5)
}
@Test
fun testMainDispatcherToString() {
assertEquals("Dispatchers.Main", Dispatchers.Main.toString())
assertEquals("Dispatchers.Main.immediate", Dispatchers.Main.immediate.toString())
}
@Test
fun testDefaultDelayIsNotDelegatedToMain() = runTest {
val mainLooper = Shadows.shadowOf(Looper.getMainLooper())
mainLooper.pause()
assertFalse { mainLooper.scheduler.areAnyRunnable() }
val job = launch(Dispatchers.Default, start = CoroutineStart.UNDISPATCHED) {
expect(1)
delay(Long.MAX_VALUE)
expectUnreached()
}
expect(2)
assertEquals(0, mainLooper.scheduler.size())
job.cancelAndJoin()
finish(3)
}
@Test
fun testWithTimeoutIsDelegatedToMain() = runTest {
val mainLooper = Shadows.shadowOf(Looper.getMainLooper())
mainLooper.pause()
assertFalse { mainLooper.scheduler.areAnyRunnable() }
val job = launch(Dispatchers.Main, start = CoroutineStart.UNDISPATCHED) {
withTimeout(1) {
expect(1)
hang { expect(3) }
}
expectUnreached()
}
expect(2)
assertEquals(1, mainLooper.scheduler.size())
// Schedule cancellation
mainLooper.runToEndOfTasks()
job.join()
finish(4)
}
@Test
fun testDelayDelegatedToMain() = runTest {
val mainLooper = Shadows.shadowOf(Looper.getMainLooper())
mainLooper.pause()
val job = launch(Dispatchers.Main, start = CoroutineStart.UNDISPATCHED) {
expect(1)
delay(1)
expect(3)
}
expect(2)
assertEquals(1, mainLooper.scheduler.size())
// Schedule cancellation
mainLooper.runToEndOfTasks()
job.join()
finish(4)
}
@Test
fun testAwaitFrame() = runTest {
doTestAwaitFrame()
reset()
// Now the second test: we cannot test it separately because we're caching choreographer in HandlerDispatcher
doTestAwaitWithDetectedChoreographer()
}
private fun CoroutineScope.doTestAwaitFrame() {
ShadowChoreographer.setPostFrameCallbackDelay(100)
val mainLooper = Shadows.shadowOf(Looper.getMainLooper())
mainLooper.pause()
launch(Dispatchers.Main, start = CoroutineStart.UNDISPATCHED) {
expect(1)
awaitFrame()
expect(3)
}
expect(2)
// Run choreographer detection
mainLooper.runOneTask()
finish(4)
}
private fun CoroutineScope.doTestAwaitWithDetectedChoreographer() {
ShadowChoreographer.setPostFrameCallbackDelay(100)
val mainLooper = Shadows.shadowOf(Looper.getMainLooper())
launch(Dispatchers.Main, start = CoroutineStart.UNDISPATCHED) {
expect(1)
awaitFrame()
expect(4)
}
// Run choreographer detection
expect(2)
mainLooper.scheduler.advanceBy(50, TimeUnit.MILLISECONDS)
expect(3)
mainLooper.scheduler.advanceBy(51, TimeUnit.MILLISECONDS)
finish(5)
}
}