-
Notifications
You must be signed in to change notification settings - Fork 586
/
CoilTest.kt
357 lines (306 loc) · 12 KB
/
CoilTest.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.chrisbanes.accompanist.coil
import android.content.ContentResolver
import android.net.Uri
import androidx.compose.foundation.Text
import androidx.compose.foundation.layout.preferredSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.ColorPainter
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import androidx.test.filters.LargeTest
import androidx.test.filters.SdkSuppress
import androidx.ui.test.assertHeightIsAtLeast
import androidx.ui.test.assertHeightIsEqualTo
import androidx.ui.test.assertIsDisplayed
import androidx.ui.test.assertPixels
import androidx.ui.test.assertWidthIsAtLeast
import androidx.ui.test.assertWidthIsEqualTo
import androidx.ui.test.captureToBitmap
import androidx.ui.test.createComposeRule
import androidx.ui.test.onNodeWithTag
import androidx.ui.test.onNodeWithText
import androidx.ui.test.runOnIdle
import coil.request.CachePolicy
import coil.request.GetRequest
import com.google.common.truth.Truth.assertThat
import dev.chrisbanes.accompanist.coil.test.R
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.withTimeoutOrNull
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
@LargeTest
@RunWith(JUnit4::class)
class CoilTest {
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun onRequestCompleted() {
val results = ArrayList<RequestResult>()
val latch = CountDownLatch(1)
composeTestRule.setContent {
CoilImage(
request = GetRequest.Builder(ContextAmbient.current)
.data(resourceUri(R.raw.sample))
.listener { _, _ -> latch.countDown() }
.build(),
modifier = Modifier.preferredSize(128.dp, 128.dp),
onRequestCompleted = { results += it }
)
}
// Wait for the Coil request listener to release the latch
latch.await(5, TimeUnit.SECONDS)
runOnIdle {
// And assert that we got a single successful result
assertThat(results).hasSize(1)
assertThat(results[0]).isInstanceOf(SuccessResult::class.java)
}
}
@Test
fun basicLoad_raw() {
val latch = CountDownLatch(1)
composeTestRule.setContent {
CoilImage(
data = resourceUri(R.raw.sample),
modifier = Modifier.preferredSize(128.dp, 128.dp).testTag(CoilTestTags.Image),
onRequestCompleted = { latch.countDown() }
)
}
// Wait for the onRequestCompleted to release the latch
latch.await(5, TimeUnit.SECONDS)
onNodeWithTag(CoilTestTags.Image)
.assertIsDisplayed()
.assertWidthIsEqualTo(128.dp)
.assertHeightIsEqualTo(128.dp)
}
@Test
@SdkSuppress(minSdkVersion = 26) // captureToBitmap is SDK 26+
fun basicLoad_drawable() {
val latch = CountDownLatch(1)
composeTestRule.setContent {
CoilImage(
data = resourceUri(R.drawable.red_rectangle),
modifier = Modifier.preferredSize(128.dp, 128.dp).testTag(CoilTestTags.Image),
onRequestCompleted = { latch.countDown() }
)
}
// Wait for the onRequestCompleted to release the latch
latch.await(5, TimeUnit.SECONDS)
onNodeWithTag(CoilTestTags.Image)
.assertWidthIsEqualTo(128.dp)
.assertHeightIsEqualTo(128.dp)
.assertIsDisplayed()
.captureToBitmap()
.assertPixels { Color.Red }
}
@OptIn(ExperimentalCoroutinesApi::class)
@Test
@SdkSuppress(minSdkVersion = 26) // captureToBitmap is SDK 26+
fun basicLoad_switchData() {
val loadCompleteSignal = Channel<Unit>(Channel.UNLIMITED)
val drawableResId = MutableStateFlow(R.drawable.red_rectangle)
composeTestRule.setContent {
val resId = drawableResId.collectAsState()
CoilImage(
data = resourceUri(resId.value),
modifier = Modifier.preferredSize(128.dp, 128.dp).testTag(CoilTestTags.Image),
onRequestCompleted = { loadCompleteSignal.offer(Unit) }
)
}
// Await the first load
runBlocking {
loadCompleteSignal.receive()
}
// Assert that the content is completely Red
onNodeWithTag(CoilTestTags.Image)
.assertWidthIsEqualTo(128.dp)
.assertHeightIsEqualTo(128.dp)
.assertIsDisplayed()
.captureToBitmap()
.assertPixels { Color.Red }
// Now switch the data URI to the blue drawable
drawableResId.value = R.drawable.blue_rectangle
// Await the second load
runBlocking { loadCompleteSignal.receive() }
// Assert that the content is completely Blue
onNodeWithTag(CoilTestTags.Image)
.assertWidthIsEqualTo(128.dp)
.assertHeightIsEqualTo(128.dp)
.assertIsDisplayed()
.captureToBitmap()
.assertPixels { Color.Blue }
// Close the signal channel
loadCompleteSignal.close()
}
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun basicLoad_changeSize() {
val loadCompleteSignal = Channel<Unit>(Channel.UNLIMITED)
val sizeFlow = MutableStateFlow(128.dp)
composeTestRule.setContent {
val size = sizeFlow.collectAsState()
CoilImage(
data = resourceUri(R.drawable.blue_rectangle),
modifier = Modifier.preferredSize(size.value).testTag(CoilTestTags.Image),
onRequestCompleted = { loadCompleteSignal.offer(Unit) }
)
}
// Await the first load
runBlocking {
loadCompleteSignal.receive()
}
// Now change the size
sizeFlow.value = 256.dp
// Await the potential second load (which shouldn't come)
runBlocking {
val result = withTimeoutOrNull(3000) { loadCompleteSignal.receive() }
assertThat(result).isNull()
}
// Close the signal channel
loadCompleteSignal.close()
}
@Test
fun basicLoad_nosize() {
val latch = CountDownLatch(1)
composeTestRule.setContent {
CoilImage(
data = resourceUri(R.raw.sample),
modifier = Modifier.testTag(CoilTestTags.Image),
onRequestCompleted = { latch.countDown() }
)
}
// Wait for the onRequestCompleted to release the latch
latch.await(5, TimeUnit.SECONDS)
onNodeWithTag(CoilTestTags.Image)
.assertWidthIsAtLeast(1.dp)
.assertHeightIsAtLeast(1.dp)
.assertIsDisplayed()
}
@Test
@SdkSuppress(minSdkVersion = 26) // captureToBitmap is SDK 26+
fun customGetPainter() {
val latch = CountDownLatch(1)
composeTestRule.setContent {
CoilImage(
data = resourceUri(R.raw.sample),
getSuccessPainter = {
// Return a custom success painter which just draws cyan
ColorPainter(Color.Cyan)
},
modifier = Modifier.preferredSize(128.dp, 128.dp).testTag(CoilTestTags.Image),
onRequestCompleted = { latch.countDown() }
)
}
// Wait for the onRequestCompleted to release the latch
latch.await(5, TimeUnit.SECONDS)
// Assert that the whole layout is drawn cyan
onNodeWithTag(CoilTestTags.Image)
.assertIsDisplayed()
.captureToBitmap()
.assertPixels { Color.Cyan }
}
@Test
fun errorStillHasSize() {
val latch = CountDownLatch(1)
composeTestRule.setContent {
CoilImage(
data = "url_which_will_never_work",
modifier = Modifier.preferredSize(128.dp, 128.dp).testTag(CoilTestTags.Image),
onRequestCompleted = { latch.countDown() }
)
}
// Wait for the onRequestCompleted to release the latch
latch.await(5, TimeUnit.SECONDS)
// Assert that the layout is in the tree and has the correct size
onNodeWithTag(CoilTestTags.Image)
.assertIsDisplayed()
.assertWidthIsEqualTo(128.dp)
.assertHeightIsEqualTo(128.dp)
}
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun loadingSlot() {
val dispatcher = TestCoroutineDispatcher()
dispatcher.runBlockingTest {
pauseDispatcher()
val latch = CountDownLatch(1)
composeTestRule.setContent {
CoilImage(
request = GetRequest.Builder(ContextAmbient.current)
.data(resourceUri(R.raw.sample))
// Disable memory cache. If the item is in the cache, the fetch is
// synchronous and the dispatcher pause has no effect
.memoryCachePolicy(CachePolicy.DISABLED)
.dispatcher(dispatcher)
.build(),
modifier = Modifier.preferredSize(128.dp, 128.dp),
loading = { Text(text = "Loading") },
onRequestCompleted = { latch.countDown() }
)
}
// Assert that the loading component is displayed
onNodeWithText("Loading").assertIsDisplayed()
// Now resume the dispatcher to start the Coil request, and wait for the
// request to complete
dispatcher.resumeDispatcher()
latch.await(5, TimeUnit.SECONDS)
// And assert that the loading component no longer exists
onNodeWithText("Loading").assertDoesNotExist()
}
}
@Test
@SdkSuppress(minSdkVersion = 26) // captureToBitmap is SDK 26+
fun customFailurePainter() {
val latch = CountDownLatch(1)
composeTestRule.setContent {
CoilImage(
data = "url_which_will_never_work",
getFailurePainter = {
// Return a custom failure painter which just draws red
ColorPainter(Color.Red)
},
modifier = Modifier.preferredSize(128.dp, 128.dp).testTag(CoilTestTags.Image),
onRequestCompleted = { latch.countDown() }
)
}
// Wait for the onRequestCompleted to release the latch
latch.await(5, TimeUnit.SECONDS)
// Assert that the whole layout is drawn red
onNodeWithTag(CoilTestTags.Image)
.assertIsDisplayed()
.captureToBitmap()
.assertPixels { Color.Red }
}
}
@Composable
fun resourceUri(id: Int): Uri {
return "${ContentResolver.SCHEME_ANDROID_RESOURCE}://${ContextAmbient.current.packageName}/$id".toUri()
}