/
GlideComposeTest.kt
195 lines (162 loc) · 6.29 KB
/
GlideComposeTest.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
@file:OptIn(ExperimentalGlideComposeApi::class, InternalGlideApi::class)
package com.bumptech.glide.integration.compose
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.semantics.SemanticsPropertyKey
import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.assert
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.unit.dp
import androidx.test.core.app.ApplicationProvider
import com.bumptech.glide.Glide
import com.bumptech.glide.integration.ktx.InternalGlideApi
import com.bumptech.glide.integration.ktx.Size
import com.bumptech.glide.load.engine.executor.GlideIdlingResourceInit
import java.util.concurrent.atomic.AtomicReference
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
class GlideComposeTest {
private val context: Context = ApplicationProvider.getApplicationContext()
@get:Rule val composeRule = createComposeRule()
@Before
fun setUp() {
GlideIdlingResourceInit.initGlide(composeRule)
}
@After
fun tearDown() {
Glide.tearDown()
}
@Test
fun glideImage_noModifierSize_resourceDrawable_displaysDrawable() {
val description = "test"
val resourceId = android.R.drawable.star_big_on
composeRule.setContent { GlideImage(model = resourceId, contentDescription = description) }
composeRule.waitForIdle()
val expectedSize = resourceId.bitmapSize()
composeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawableSize(expectedSize))
}
@Test
fun glideImage_withSizeLargerThanImage_noTransformSet_doesNotUpscaleImage() {
val description = "test"
val resourceId = android.R.drawable.star_big_on
composeRule.setContent {
GlideImage(
model = resourceId,
contentDescription = description,
modifier = Modifier.size(300.dp, 300.dp)
)
}
composeRule.waitForIdle()
val expectedSize = resourceId.bitmapSize()
composeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawableSize(expectedSize))
}
@Test
fun glideImage_withChangingModel_refreshes() {
val description = "test"
val firstDrawable: Drawable = context.getDrawable(android.R.drawable.star_big_off)!!
val secondDrawable: Drawable = context.getDrawable(android.R.drawable.star_big_on)!!
composeRule.setContent {
val model = remember { mutableStateOf(firstDrawable) }
fun swapModel() {
model.value = secondDrawable
}
Column {
TextButton(onClick = ::swapModel) {Text(text="Swap")}
GlideImage(
model = model.value,
modifier = Modifier.size(100.dp),
contentDescription = description
)
}
}
composeRule.waitForIdle()
composeRule.onNodeWithText("Swap").performClick()
composeRule.waitForIdle()
val fullsizeBitmap = (secondDrawable as BitmapDrawable).bitmap
composeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawable(fullsizeBitmap) { (it as BitmapDrawable).bitmap })
}
@Test
fun glideImage_withSizeLargerThanImage_upscaleTransformSet_upscalesImage() {
val viewDimension = 300
val description = "test"
val sizeRef = AtomicReference<Size>()
composeRule.setContent {
GlideImage(
model = android.R.drawable.star_big_on,
requestBuilderTransform = { it.fitCenter() },
contentDescription = description,
modifier = Modifier.size(viewDimension.dp, viewDimension.dp)
)
with(LocalDensity.current) {
val pixels = viewDimension.dp.roundToPx()
sizeRef.set(Size(pixels, pixels))
}
}
composeRule.waitForIdle()
val pixels = sizeRef.get()
composeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawableSize(pixels))
}
@Test
fun glideImage_withThumbnail_prefersFullSizeImage() {
val description = "test"
val thumbnailDrawable = context.getDrawable(android.R.drawable.star_big_off)
val fullsizeDrawable = context.getDrawable(android.R.drawable.star_big_on)
val fullsizeBitmap = (fullsizeDrawable as BitmapDrawable).bitmap
composeRule.setContent {
GlideImage(
model = fullsizeDrawable,
requestBuilderTransform = { it.thumbnail(Glide.with(context).load(thumbnailDrawable)) },
contentDescription = description,
)
}
composeRule.waitForIdle()
composeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawable(fullsizeBitmap) { (it as BitmapDrawable).bitmap })
}
private fun Int.bitmapSize() = context.resources.getDrawable(this, context.theme).size()
}
private fun Drawable.size() = (this as BitmapDrawable).bitmap.let { Size(it.width, it.height) }
private fun expectDisplayedDrawableSize(widthPixels: Int, heightPixels: Int): SemanticsMatcher =
expectDisplayedDrawable(Size(widthPixels, heightPixels)) { it?.size() }
private fun expectDisplayedDrawableSize(expectedSize: Size): SemanticsMatcher =
expectDisplayedDrawable(expectedSize) { it?.size() }
private fun <ValueT> expectDisplayedDrawable(
expectedValue: ValueT,
transform: (Drawable?) -> ValueT
): SemanticsMatcher = expectStateValue(DisplayedDrawableKey, expectedValue) { transform(it) }
private fun <ValueT, TransformedValueT> expectStateValue(
key: SemanticsPropertyKey<MutableState<ValueT?>>,
expectedValue: TransformedValueT,
transform: (ValueT?) -> TransformedValueT?
): SemanticsMatcher =
SemanticsMatcher("${key.name} = '$expectedValue'") {
val value = transform(it.config.getOrElseNullable(key) { null }?.value)
if (value != expectedValue) {
throw AssertionError("Expected: $expectedValue, but was: $value")
}
true
}