-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lazily allocate drawSizeFlow in AsyncImagePainter. (#2810)
* Lazily allocate drawSizeFlow in AsyncImagePainter. * Fix function names. * Don't run instrumentation. * Fix tests. * Try fix. * Give up on Compose multiplatform test. * Fix tests. * Try fix. * Add idling resource.
- Loading branch information
1 parent
27b5482
commit 009abba
Showing
6 changed files
with
127 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...ompose-core/src/androidInstrumentedTest/kotlin/coil3/compose/DrawScopeSizeResolverTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package coil3.compose | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.test.ExperimentalTestApi | ||
import androidx.compose.ui.test.runComposeUiTest | ||
import androidx.compose.ui.unit.dp | ||
import coil3.ColorImage | ||
import coil3.ImageLoader | ||
import coil3.request.ImageRequest | ||
import coil3.request.SuccessResult | ||
import coil3.size.Dimension | ||
import coil3.size.Size as CoilSize | ||
import kotlin.coroutines.EmptyCoroutineContext | ||
import kotlin.math.roundToInt | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotEquals | ||
|
||
@OptIn(ExperimentalTestApi::class) | ||
class DrawScopeSizeResolverTest { | ||
private val idlingResource = ImageLoaderIdlingResource() | ||
|
||
@Test | ||
fun imageIsLoadedWithDrawScopeSize() = runComposeUiTest { | ||
registerIdlingResource(idlingResource) | ||
|
||
val expectedSizeDp = 100.dp | ||
var expectedSizePx = Size.Unspecified | ||
var actualSizePx = Size.Unspecified | ||
|
||
setContent { | ||
expectedSizePx = with(LocalDensity.current) { | ||
expectedSizeDp.toPx().roundToInt().toFloat().let { Size(it, it) } | ||
} | ||
|
||
val context = LocalPlatformContext.current | ||
val imageLoader = remember(context) { | ||
ImageLoader.Builder(context) | ||
.components { | ||
add { chain -> | ||
actualSizePx = chain.size.toComposeSize() | ||
SuccessResult( | ||
image = ColorImage(), | ||
request = chain.request, | ||
) | ||
} | ||
} | ||
.coroutineContext(EmptyCoroutineContext) | ||
.eventListener(idlingResource) | ||
.build() | ||
} | ||
val request = remember(context) { | ||
ImageRequest.Builder(context) | ||
.data(Unit) | ||
.size(DrawScopeSizeResolver()) | ||
.build() | ||
} | ||
val painter = rememberAsyncImagePainter(request, imageLoader) | ||
|
||
Image( | ||
painter = painter, | ||
contentDescription = null, | ||
modifier = Modifier.size(expectedSizeDp), | ||
) | ||
} | ||
|
||
waitForIdle() | ||
|
||
assertNotEquals(Size.Unspecified, actualSizePx) | ||
assertEquals(expectedSizePx, actualSizePx) | ||
} | ||
|
||
private fun CoilSize.toComposeSize() = Size( | ||
width = (width as Dimension.Pixels).px.toFloat(), | ||
height = (height as Dimension.Pixels).px.toFloat(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters