Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid duplicating Modifiers in GlideModifer #5276

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
Expand All @@ -16,6 +18,8 @@ import androidx.compose.material.TextButton
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.testTag
Expand All @@ -42,6 +46,7 @@ import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.test.compareToGolden
import com.bumptech.glide.test.pxToDp
import com.google.common.truth.Truth.assertThat
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicReference
Expand All @@ -53,11 +58,12 @@ import org.junit.rules.TestName
class GlideImageTest {
private val context: Context = ApplicationProvider.getApplicationContext()

@get:Rule
val glideComposeRule = GlideComposeRule()
@get:Rule
@get:Rule(order = 1)
val testName = TestName()

@get:Rule(order = 2)
val glideComposeRule = GlideComposeRule()

@Test
fun glideImage_noModifierSize_resourceDrawable_displaysDrawable() {
val description = "test"
Expand Down Expand Up @@ -387,7 +393,7 @@ class GlideImageTest {
@Test
fun glideImage_withZeroSize_doesNotCrash() {
glideComposeRule.setContent {
GlideImage(
GlideImage(
model = android.R.drawable.star_big_on,
contentDescription = null,
modifier = Modifier.width(IntrinsicSize.Min),
Expand Down Expand Up @@ -419,4 +425,47 @@ class GlideImageTest {
.captureToImage()
.compareToGolden(testName.methodName)
}

@Test
fun glideImage_withDrawBehind_drawsImageOnTopOfBackground() {
glideComposeRule.setContent {
GlideImage(
android.R.drawable.star_big_on,
"test",
Modifier
.size(100.pxToDp())
.drawBehind { drawRect(Color.Red) }) {
it.override(100)
}
}

glideComposeRule
.onNodeWithContentDescription("test")
.captureToImage()
.compareToGolden(testName.methodName)
}

// See #5272
@Test
fun glideImage_withPadding_appliesPaddingOnce() {
glideComposeRule.setContent {
GlideImage(
model = android.R.drawable.star_big_on,
contentDescription = "test",
modifier = Modifier
.size(400.pxToDp())
.aspectRatio(1f)
.drawBehind {
drawRect(Color.Blue)
}
.padding(80.pxToDp()),
)
}
glideComposeRule.waitForIdle()

glideComposeRule
.onNodeWithContentDescription("test")
.captureToImage()
.compareToGolden(testName.methodName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,43 @@ package com.bumptech.glide.integration.compose
import android.content.Context
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Email
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.geometry.isUnspecified
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.test.captureToImage
import androidx.compose.ui.test.onNodeWithContentDescription
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.load.DataSource
import com.bumptech.glide.test.compareToGolden
import com.bumptech.glide.test.pxToDp
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestName

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

@get:Rule
@get:Rule(order = 1)
val testName = TestName()
@get:Rule(order = 2)
val glideComposeRule = GlideComposeRule()

@Test
Expand Down Expand Up @@ -162,5 +183,49 @@ class GlideSubcompositionTest {
glideComposeRule.waitForIdle()
assertThat(dataSource).isEqualTo(DataSource.MEMORY_CACHE)
}

// See #5272
@Test
fun glideSubcomposition_withPadding_appliesPaddingOnce() {
glideComposeRule.setContent {
val lastSize = remember { mutableStateOf(Size.Unspecified) }

GlideSubcomposition(
model = null,
modifier = Modifier
.semantics {
contentDescription = "test"
}
.width(400.pxToDp())
.aspectRatio(1f)
.drawBehind {
if (lastSize.value.isUnspecified) {
lastSize.value = size
drawRect(Color.Blue)
} else if (lastSize.value != this.size) {
drawRect(Color.Red)
} else {
drawRect(Color.Blue)
}
}
.padding(80.pxToDp()),
) {
when (state) {
RequestState.Failure -> Image(
imageVector = Icons.Default.Email,
contentDescription = "placeholder",
modifier = Modifier.width(400.pxToDp())
)
RequestState.Loading -> Spacer(modifier = Modifier.size(100.pxToDp()))
is RequestState.Success -> Image(painter = painter, contentDescription = null)
}
}
}
glideComposeRule.waitForIdle()
glideComposeRule.onNodeWithContentDescription("test")
.captureToImage()
.compareToGolden(testName.methodName)
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Environment
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.toPixelMap
import androidx.compose.ui.platform.LocalDensity
import androidx.test.core.app.ApplicationProvider
import java.io.BufferedOutputStream
import java.io.File
Expand All @@ -18,6 +20,9 @@ import java.lang.IllegalStateException
const val GENERATED_FILES_DIR = "compose_goldens"
const val EXTENSION = "png"

@Composable
fun Int.pxToDp() = with(LocalDensity.current) { toDp() }

fun ImageBitmap.compareToGolden(testName: String) {
val bitmap = toBitmap()
val existingGolden = readExistingGolden(testName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,14 @@ internal fun Modifier.glideNode(
requestListener,
draw,
transitionFactory,
) then
clipToBounds() then
)
.clipToBounds()
.semantics {
if (contentDescription != null) {
semantics {
[email protected] = contentDescription
role = Role.Image
}
} else {
Modifier
[email protected] = contentDescription
}
role = Role.Image
}
}

@ExperimentalGlideComposeApi
Expand Down