-
Notifications
You must be signed in to change notification settings - Fork 637
/
SystemCallbacksTest.kt
66 lines (52 loc) · 2.1 KB
/
SystemCallbacksTest.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
package coil.util
import android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND
import android.content.ComponentCallbacks2.TRIM_MEMORY_COMPLETE
import android.content.Context
import android.graphics.Bitmap
import androidx.core.graphics.createBitmap
import androidx.test.core.app.ApplicationProvider
import coil.ImageLoader
import coil.RealImageLoader
import coil.memory.MemoryCache
import coil.memory.MemoryCache.Key
import coil.memory.MemoryCache.Value
import org.junit.Before
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class SystemCallbacksTest {
private lateinit var context: Context
@Before
fun before() {
context = ApplicationProvider.getApplicationContext()
}
@Test
fun imageLoaderIsFreedWithoutShutdown() {
val systemCallbacks = SystemCallbacks(ImageLoader(context) as RealImageLoader, context, true)
val bitmaps = mutableListOf<Bitmap>()
while (systemCallbacks.imageLoader.get() != null) {
// Request that garbage collection occur.
Runtime.getRuntime().gc()
// Keep allocating bitmaps until either the image loader is freed or we run out of memory.
bitmaps += createBitmap(500, 500)
}
bitmaps.clear()
// Ensure that the next system callback is called.
systemCallbacks.onTrimMemory(TRIM_MEMORY_BACKGROUND)
assertTrue(systemCallbacks.isShutdown)
}
@Test
fun trimMemoryCallsArePassedThrough() {
val memoryCache = MemoryCache.Builder(context).build()
val imageLoader = ImageLoader.Builder(context)
.memoryCache(memoryCache)
.diskCache(null)
.build()
val systemCallbacks = SystemCallbacks(imageLoader as RealImageLoader, context, true)
memoryCache[Key("1")] = Value(createBitmap(1000, 1000, Bitmap.Config.ARGB_8888))
memoryCache[Key("2")] = Value(createBitmap(1000, 1000, Bitmap.Config.ARGB_8888))
assertEquals(8_000_000, memoryCache.size)
systemCallbacks.onTrimMemory(TRIM_MEMORY_COMPLETE)
assertEquals(0, memoryCache.size)
}
}