Skip to content

Commit

Permalink
Create GlideComposeRule to simplify adding new compose tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Nov 7, 2022
1 parent 37bc006 commit 84f20eb
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,36 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
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.compose.test.GlideComposeRule
import com.bumptech.glide.integration.compose.test.bitmapSize
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.engine.executor.GlideIdlingResourceInit
import com.bumptech.glide.testutil.TearDownGlide
import java.util.concurrent.atomic.AtomicReference
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class GlideComposeTest {
private val context: Context = ApplicationProvider.getApplicationContext()
@get:Rule(order = 1) val composeRule = createComposeRule()
@get:Rule(order = 2) val tearDownGlide = TearDownGlide()

@Before
fun setUp() {
GlideIdlingResourceInit.initGlide(composeRule)
}
@get:Rule val glideComposeRule = GlideComposeRule()

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

composeRule.waitForIdle()
glideComposeRule.waitForIdle()

val expectedSize = resourceId.bitmapSize()
composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawableSize(expectedSize))
}
Expand All @@ -60,18 +51,18 @@ class GlideComposeTest {
fun glideImage_withSizeLargerThanImage_noTransformSet_doesNotUpscaleImage() {
val description = "test"
val resourceId = android.R.drawable.star_big_on
composeRule.setContent {
glideComposeRule.setContent {
GlideImage(
model = resourceId,
contentDescription = description,
modifier = Modifier.size(300.dp, 300.dp)
)
}

composeRule.waitForIdle()
glideComposeRule.waitForIdle()

val expectedSize = resourceId.bitmapSize()
composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawableSize(expectedSize))
}
Expand All @@ -83,7 +74,7 @@ class GlideComposeTest {
val firstDrawable: Drawable = context.getDrawable(android.R.drawable.star_big_off)!!
val secondDrawable: Drawable = context.getDrawable(android.R.drawable.star_big_on)!!

composeRule.setContent {
glideComposeRule.setContent {
val model = remember { mutableStateOf(firstDrawable) }

fun swapModel() {
Expand All @@ -100,11 +91,11 @@ class GlideComposeTest {
}
}

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

composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawable(secondDrawable))
}
Expand All @@ -114,7 +105,7 @@ class GlideComposeTest {
val viewDimension = 300
val description = "test"
val sizeRef = AtomicReference<Size>()
composeRule.setContent {
glideComposeRule.setContent {
GlideImage(
model = android.R.drawable.star_big_on,
requestBuilderTransform = { it.fitCenter() },
Expand All @@ -128,10 +119,10 @@ class GlideComposeTest {
}
}

composeRule.waitForIdle()
glideComposeRule.waitForIdle()

val pixels = sizeRef.get()
composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawableSize(pixels))
}
Expand All @@ -142,17 +133,17 @@ class GlideComposeTest {
val thumbnailDrawable = context.getDrawable(android.R.drawable.star_big_off)
val fullsizeDrawable = context.getDrawable(android.R.drawable.star_big_on)

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

composeRule.waitForIdle()
glideComposeRule.waitForIdle()

composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawable(fullsizeDrawable))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.bumptech.glide.integration.compose

class GlideImageCustomDrawableTransformationTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ package com.bumptech.glide.integration.compose

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.Log
import androidx.compose.ui.test.assert
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.test.core.app.ApplicationProvider
import com.bumptech.glide.integration.compose.test.GlideComposeRule
import com.bumptech.glide.integration.compose.test.expectDisplayedDrawable
import com.bumptech.glide.integration.compose.test.expectDisplayedResource
import com.bumptech.glide.integration.compose.test.expectNoDrawable
import com.bumptech.glide.load.engine.executor.GlideIdlingResourceInit
import com.bumptech.glide.testutil.TearDownGlide
import org.junit.Before
import org.junit.Rule
import org.junit.Test

Expand All @@ -24,25 +20,19 @@ import org.junit.Test
*/
class GlideImageErrorTest {
private val context: Context = ApplicationProvider.getApplicationContext()
@get:Rule(order = 1) val composeRule = createComposeRule()
@get:Rule(order = 2) val tearDownGlide = TearDownGlide()

@Before
fun before() {
GlideIdlingResourceInit.initGlide(composeRule)
}
@get:Rule val glideComposeRule = GlideComposeRule()

@Test
fun requestBuilderTransform_withErrorResourceId_displaysError() {
val description = "test"
val errorResourceId = android.R.drawable.star_big_off
composeRule.setContent {
glideComposeRule.setContent {
GlideImage(model = null, contentDescription = description) {
it.error(errorResourceId)
}
}

composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedResource(errorResourceId))
}
Expand All @@ -51,11 +41,11 @@ class GlideImageErrorTest {
fun requestBuilderTransform_withErrorDrawable_displaysError() {
val description = "test"
val errorDrawable = context.getDrawable(android.R.drawable.star_big_off)
composeRule.setContent {
glideComposeRule.setContent {
GlideImage(model = null, contentDescription = description) { it.error(errorDrawable) }
}

composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawable(errorDrawable))
}
Expand All @@ -64,15 +54,15 @@ class GlideImageErrorTest {
fun failureParameter_withErrorResourceId_displaysError() {
val description = "test"
val failureResourceId = android.R.drawable.star_big_off
composeRule.setContent {
glideComposeRule.setContent {
GlideImage(
model = null,
contentDescription = description,
failure = placeholder(failureResourceId),
)
}

composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedResource(failureResourceId))
}
Expand All @@ -81,38 +71,38 @@ class GlideImageErrorTest {
fun failureParameter_withDrawable_displaysDrawable() {
val description = "test"
val failureDrawable = context.getDrawable(android.R.drawable.star_big_off)
composeRule.setContent {
glideComposeRule.setContent {
GlideImage(
model = null,
contentDescription = description,
failure = placeholder(failureDrawable),
)
}

composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawable(failureDrawable))
}

@Test
fun failureParameter_withNullDrawable_displaysNothing() {
val description = "test"
composeRule.setContent {
glideComposeRule.setContent {
GlideImage(
model = null,
contentDescription = description,
failure = placeholder(null as Drawable?)
)
}

composeRule.onNodeWithContentDescription(description).assert(expectNoDrawable())
glideComposeRule.onNodeWithContentDescription(description).assert(expectNoDrawable())
}

@Test
fun failureParameter_withComposable_displaysComposable() {
val failureResourceId = android.R.drawable.star_big_off
val description = "test"
composeRule.setContent {
glideComposeRule.setContent {
GlideImage(
model = null,
contentDescription = "none",
Expand All @@ -130,7 +120,7 @@ class GlideImageErrorTest {
)
}

composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedResource(failureResourceId))
}
Expand All @@ -139,7 +129,7 @@ class GlideImageErrorTest {
fun failure_setViaFailureParameterWithResourceId_andRequestBuilderTransform_prefersFailureParameter() {
val description = "test"
val failureResourceId = android.R.drawable.star_big_off
composeRule.setContent {
glideComposeRule.setContent {
GlideImage(
model = null,
contentDescription = description,
Expand All @@ -149,7 +139,7 @@ class GlideImageErrorTest {
}
}

composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedResource(failureResourceId))
}
Expand All @@ -158,7 +148,7 @@ class GlideImageErrorTest {
fun failure_setViaFailureParameterWithDrawable_andRequestBuilderTransform_prefersFailureParameter() {
val description = "test"
val failureDrawable = context.getDrawable(android.R.drawable.star_big_off)
composeRule.setContent {
glideComposeRule.setContent {
GlideImage(
model = null,
contentDescription = description,
Expand All @@ -168,15 +158,15 @@ class GlideImageErrorTest {
}
}

composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedDrawable(failureDrawable))
}

@Test
fun failure_setViaFailureParameterWithNullDrawable_andRequestBuilderTransformWithNonNullDrawable_showsNoPlaceholder() {
val description = "test"
composeRule.setContent {
glideComposeRule.setContent {
GlideImage(
model = null,
contentDescription = description,
Expand All @@ -186,14 +176,14 @@ class GlideImageErrorTest {
}
}

composeRule.onNodeWithContentDescription(description).assert(expectNoDrawable())
glideComposeRule.onNodeWithContentDescription(description).assert(expectNoDrawable())
}

@Test
fun failure_setViaFailureParameterWithComposable_andRequestBuilderTransform_showsComposable() {
val description = "test"
val failureResourceId = android.R.drawable.star_big_off
composeRule.setContent {
glideComposeRule.setContent {
GlideImage(
model = null,
contentDescription = "other",
Expand All @@ -210,7 +200,7 @@ class GlideImageErrorTest {
}
}

composeRule
glideComposeRule
.onNodeWithContentDescription(description)
.assert(expectDisplayedResource(failureResourceId))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import org.junit.Rule
import org.junit.Test

/**
* Avoids [com.bumptech.glide.load.engine.executor.GlideIdlingResourceInit] because we want to make
* assertions about loads that have not yet completed.
* Avoids [com.bumptech.glide.load.engine.executor.GlideIdlingResourceInit] and
* [com.bumptech.glide.integration.compose.test.GlideComposeRule] because we want to make assertions
* about loads that have not yet completed.
*/
class GlideImagePlaceholderTest {
private val context: Context = ApplicationProvider.getApplicationContext()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.bumptech.glide.integration.compose.test

import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.junit4.createComposeRule
import com.bumptech.glide.load.engine.executor.GlideIdlingResourceInit
import com.bumptech.glide.testutil.TearDownGlide
import org.junit.rules.RuleChain
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

class GlideComposeRule(
private val composeRule: ComposeContentTestRule = createComposeRule(),
) : TestRule, ComposeContentTestRule by composeRule {
private val rules = RuleChain.outerRule(TearDownGlide()).around(composeRule)

override fun apply(base: Statement?, description: Description?): Statement {
return rules.apply(
object : Statement() {
override fun evaluate() {
GlideIdlingResourceInit.initGlide(this@GlideComposeRule)
base?.evaluate()
}
},
description,
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.bumptech.glide.integration.compose.test

0 comments on commit 84f20eb

Please sign in to comment.