-
Notifications
You must be signed in to change notification settings - Fork 637
/
ImageLoaderFactoryTest.kt
97 lines (71 loc) · 2.68 KB
/
ImageLoaderFactoryTest.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
package coil
import android.app.Application
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.test.assertFalse
import kotlin.test.assertSame
import kotlin.test.assertTrue
@RunWith(RobolectricTestRunner::class)
class ImageLoaderFactoryTest {
private lateinit var context: Context
@Before
fun before() {
context = ApplicationProvider.getApplicationContext()
}
@After
fun after() {
Coil.reset()
}
@Test
@Config(application = TestApplication::class)
fun `application factory is invoked exactly once`() {
assertFalse((context.applicationContext as TestApplication).isInitialized.get())
val imageLoader1 = context.imageLoader
assertTrue((context.applicationContext as TestApplication).isInitialized.get())
val imageLoader2 = context.imageLoader
assertSame(imageLoader1, imageLoader2)
}
@Test
fun `setImageLoader factory is invoked exactly once`() {
val imageLoader1 = ImageLoader(context)
val isInitialized = AtomicBoolean(false)
Coil.setImageLoader {
check(!isInitialized.getAndSet(true)) { "newImageLoader was invoked more than once." }
imageLoader1
}
assertFalse(isInitialized.get())
val imageLoader2 = context.imageLoader
assertSame(imageLoader1, imageLoader2)
assertTrue(isInitialized.get())
val imageLoader3 = context.imageLoader
assertSame(imageLoader1, imageLoader3)
}
@Test
@Config(application = TestApplication::class)
fun `setImageLoader preempts application factory`() {
val isInitialized = AtomicBoolean(false)
assertFalse(isInitialized.get())
assertFalse((context.applicationContext as TestApplication).isInitialized.get())
Coil.setImageLoader {
check(!isInitialized.getAndSet(true)) { "newImageLoader was invoked more than once." }
ImageLoader(context)
}
context.imageLoader
assertTrue(isInitialized.get())
assertFalse((context.applicationContext as TestApplication).isInitialized.get())
}
class TestApplication : Application(), ImageLoaderFactory {
val isInitialized = AtomicBoolean(false)
override fun newImageLoader(): ImageLoader {
check(!isInitialized.getAndSet(true)) { "newImageLoader was invoked more than once." }
return ImageLoader(this)
}
}
}