aboutsummaryrefslogtreecommitdiff
path: root/integration/compose/src/androidTest/java/com/bumptech/glide/integration/compose/GlideImageTest.kt
blob: 7873de4e23bb18479f76ae43dab42701814953da (plain)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package com.bumptech.glide.integration.compose

import android.content.Context
import android.graphics.drawable.Drawable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.assert
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollToIndex
import androidx.compose.ui.unit.dp
import androidx.test.core.app.ApplicationProvider
import com.bumptech.glide.Glide
import com.bumptech.glide.integration.compose.test.GlideComposeRule
import com.bumptech.glide.integration.compose.test.assertDisplays
import com.bumptech.glide.integration.compose.test.bitmapSize
import com.bumptech.glide.integration.compose.test.dpToPixels
import com.bumptech.glide.integration.compose.test.expectDisplayedDrawable
import com.bumptech.glide.integration.compose.test.expectDisplayedDrawableSize
import com.bumptech.glide.integration.ktx.InternalGlideApi
import com.bumptech.glide.integration.ktx.Size
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.google.common.truth.Truth.assertThat
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicReference
import org.junit.Rule
import org.junit.Test

@OptIn(ExperimentalGlideComposeApi::class, InternalGlideApi::class)
class GlideImageTest {
  private val context: Context = ApplicationProvider.getApplicationContext()

  @get:Rule
  val glideComposeRule = GlideComposeRule()

  @Test
  fun glideImage_noModifierSize_resourceDrawable_displaysDrawable() {
    val description = "test"
    val resourceId = android.R.drawable.star_big_on
    glideComposeRule.setContent { GlideImage(model = resourceId, contentDescription = description) }

    glideComposeRule.waitForIdle()

    val expectedSize = resourceId.bitmapSize()
    glideComposeRule
      .onNodeWithContentDescription(description)
      .assert(expectDisplayedDrawableSize(expectedSize))
  }

  @Test
  fun glideImage_withSizeLargerThanImage_noTransformSet_doesNotUpscaleImage() {
    val description = "test"
    val resourceId = android.R.drawable.star_big_on
    glideComposeRule.setContent {
      GlideImage(
        model = resourceId,
        contentDescription = description,
        modifier = Modifier.size(300.dp, 300.dp)
      )
    }

    glideComposeRule.waitForIdle()

    val expectedSize = resourceId.bitmapSize()
    glideComposeRule
      .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)!!

    glideComposeRule.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
        )
      }
    }

    // Precondition - ensure we loaded the first drawable.
    glideComposeRule
      .onNodeWithContentDescription(description)
      .assert(expectDisplayedDrawable(firstDrawable))

    glideComposeRule.waitForIdle()
    glideComposeRule.onNodeWithText("Swap").performClick()
    glideComposeRule.waitForIdle()

    glideComposeRule
      .onNodeWithContentDescription(description)
      .assert(expectDisplayedDrawable(secondDrawable))
  }

  @Test
  fun glideImage_withSizeLargerThanImage_upscaleTransformSet_upscalesImage() {
    val viewDimension = 300
    val description = "test"
    val sizeRef = AtomicReference<Size>()
    glideComposeRule.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))
      }
    }

    glideComposeRule.waitForIdle()

    val pixels = sizeRef.get()
    glideComposeRule
      .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)

    glideComposeRule.setContent {
      GlideImage(
        model = fullsizeDrawable,
        requestBuilderTransform = { it.thumbnail(Glide.with(context).load(thumbnailDrawable)) },
        contentDescription = description,
      )
    }

    glideComposeRule.waitForIdle()
    glideComposeRule
      .onNodeWithContentDescription(description)
      .assert(expectDisplayedDrawable(fullsizeDrawable))
  }

  @Test
  fun glideImage_withZeroSize_doesNotStartLoad() {
    val description = "test"
    glideComposeRule.setContent {
      Box(modifier = Modifier.size(0.dp)) {
        GlideImage(
          model = android.R.drawable.star_big_on,
          contentDescription = description,
        )
      }
    }
    glideComposeRule.waitForIdle()
    glideComposeRule
      .onNodeWithContentDescription(description)
      .assertDisplays(null)
  }

  @Test
  fun glideImage_withNegativeSize_doesNotStartLoad() {
    val description = "test"
    glideComposeRule.setContent {
      Box(modifier = Modifier.size((-10).dp)) {
        GlideImage(
          model = android.R.drawable.star_big_on,
          contentDescription = description,
        )
      }
    }
    glideComposeRule.waitForIdle()
    glideComposeRule
      .onNodeWithContentDescription(description)
      .assertDisplays(null)
  }

  @Test
  fun glideImage_withZeroWidth_validHeight_doesNotStartLoad() {
    val description = "test"
    glideComposeRule.setContent {
      Box(modifier = Modifier.size(0.dp, 10.dp)) {
        GlideImage(
          model = android.R.drawable.star_big_on,
          contentDescription = description,
        )
      }
    }
    glideComposeRule.waitForIdle()
    glideComposeRule
      .onNodeWithContentDescription(description)
      .assertDisplays(null)
  }

  @Test
  fun glideImage_withValidWidth_zeroHeight_doesNotStartLoad() {
    val description = "test"
    glideComposeRule.setContent {
      Box(modifier = Modifier.size(10.dp, 0.dp)) {
        GlideImage(
          model = android.R.drawable.star_big_on,
          contentDescription = description,
        )
      }
    }
    glideComposeRule.waitForIdle()
    glideComposeRule
      .onNodeWithContentDescription(description)
      .assertDisplays(null)
  }

  @Test
  fun glideImage_withZeroSize_thenValidSize_startsLoadWithValidSize() {
    val description = "test"
    val resourceId = android.R.drawable.star_big_on
    val validSizeDp = 10
    glideComposeRule.setContent {
      val currentSize = remember { mutableStateOf(0.dp) }
      fun swapSize() {
        currentSize.value = validSizeDp.dp
      }

      TextButton(onClick = ::swapSize) { Text(text = "Swap") }
      Box(modifier = Modifier.size(currentSize.value)) {
        GlideImage(
          model = resourceId,
          contentDescription = description,
        )
      }
    }
    glideComposeRule.waitForIdle()
    glideComposeRule.onNodeWithText("Swap").performClick()
    glideComposeRule.waitForIdle()

    glideComposeRule
      .onNodeWithContentDescription(description)
      .assert(expectDisplayedDrawableSize(Size(validSizeDp.dpToPixels(), validSizeDp.dpToPixels())))
  }

  @Test
  fun glideImage_withZeroSize_thenMultipleValidSizes_startsLoadWithFirstValidSize() {
    val description = "test"
    val resourceId = android.R.drawable.star_big_on
    val validSizeDps = listOf(10, 20, 30, 40)
    glideComposeRule.setContent {
      val currentSize = remember { mutableStateOf(0.dp) }
      val currentSizeIndex = remember { mutableStateOf(0) }
      fun swapSize() {
        currentSize.value = validSizeDps[currentSizeIndex.value].dp
        currentSizeIndex.value++
      }

      TextButton(onClick = ::swapSize) { Text(text = "Swap") }
      Box(modifier = Modifier.size(currentSize.value)) {
        GlideImage(
          model = resourceId,
          contentDescription = description,
        )
      }
    }
    repeat(validSizeDps.size) {
      glideComposeRule.waitForIdle()
      glideComposeRule.onNodeWithText("Swap").performClick()
    }
    glideComposeRule.waitForIdle()

    val expectedSize = validSizeDps[0]
    glideComposeRule
      .onNodeWithContentDescription(description)
      .assert(
        expectDisplayedDrawableSize(Size(expectedSize.dpToPixels(), expectedSize.dpToPixels()))
      )
  }

  @Test
  fun glideImage_withSuccessfulResource_callsOnResourceReadyOnce() {
    val onResourceReadyCounter = AtomicInteger()
    val requestListener = object : RequestListener<Drawable> {
      override fun onLoadFailed(
        e: GlideException?,
        model: Any?,
        target: Target<Drawable>,
        isFirstResource: Boolean,
      ): Boolean {
        throw UnsupportedOperationException()
      }

      override fun onResourceReady(
        resource: Drawable,
        model: Any,
        target: Target<Drawable>,
        dataSource: DataSource,
        isFirstResource: Boolean,
      ): Boolean {
        onResourceReadyCounter.incrementAndGet()
        return false
      }
    }

    glideComposeRule.setContent {
      GlideImage(model = android.R.drawable.star_big_on, contentDescription = "") {
        it.listener(requestListener)
      }
    }

    glideComposeRule.waitForIdle()

    assertThat(onResourceReadyCounter.get()).isEqualTo(1)
  }

  @Test
  fun glideImage_whenDetachedAndReattached_rendersImage() {
    val description = "test"
    val testTag = "testTag"
    glideComposeRule.setContent {
      LazyRow(
        horizontalArrangement = Arrangement.spacedBy(10.dp),
        modifier = Modifier.testTag(testTag)
      ) {
        items(3) {
          GlideImage(
            model = android.R.drawable.star_big_on,
            contentDescription = description + it,
            modifier = Modifier.fillParentMaxSize()
          )
        }
      }
    }

    // Scroll back and forth to trigger re-use of the GlideImages with the same
    // parameters.
    for (i in 0..2) {
      glideComposeRule.onNode(hasTestTag(testTag)).performScrollToIndex(i)
      glideComposeRule.waitForIdle()
    }
    glideComposeRule.onNode(hasTestTag(testTag)).performScrollToIndex(0)
    glideComposeRule.waitForIdle()

    val drawable = context.getDrawable(android.R.drawable.star_big_on)
    // Make sure that all images are rendered
    for (i in 0..2) {
      glideComposeRule.onNode(hasTestTag(testTag)).performScrollToIndex(i)
      glideComposeRule.waitForIdle()
      glideComposeRule
        .onNodeWithContentDescription(description + i)
        .assert(expectDisplayedDrawable(drawable))
    }
  }
}