Skip to content

Commit

Permalink
[BEHAVIOR CHANGE] Don't automatically wait for the draw size in Async…
Browse files Browse the repository at this point in the history
…ImagePainter. (#2261)

* [BEHAVIOR CHANGE] Don't automatically wait for the draw size in AsyncImagePainter.

* Formatting.

* Fix style.

* Remove unused.

* Fix style.

* Fix tests.

* Optimize.

* Fix tests.

* Fix tests.

* Fix tests.
  • Loading branch information
colinrtwhite committed May 15, 2024
1 parent eb728d6 commit d979398
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 64 deletions.
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions coil-compose-core/api/android/coil-compose-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public final class coil3/compose/ComposableSingletons$SubcomposeAsyncImageKt {
public final fun getLambda-1$coil_compose_core_release ()Lkotlin/jvm/functions/Function3;
}

public abstract interface class coil3/compose/DrawScopeSizeResolver : coil3/size/SizeResolver {
public abstract fun connect (Lkotlinx/coroutines/flow/Flow;)V
}

public final class coil3/compose/DrawScopeSizeResolverKt {
public static final fun DrawScopeSizeResolver ()Lcoil3/compose/DrawScopeSizeResolver;
}

public abstract interface class coil3/compose/EqualityDelegate {
public abstract fun equals (Ljava/lang/Object;Ljava/lang/Object;)Z
public abstract fun hashCode (Ljava/lang/Object;)I
Expand Down
8 changes: 8 additions & 0 deletions coil-compose-core/api/jvm/coil-compose-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public final class coil3/compose/ComposableSingletons$SubcomposeAsyncImageKt {
public final fun getLambda-1$coil_compose_core ()Lkotlin/jvm/functions/Function3;
}

public abstract interface class coil3/compose/DrawScopeSizeResolver : coil3/size/SizeResolver {
public abstract fun connect (Lkotlinx/coroutines/flow/Flow;)V
}

public final class coil3/compose/DrawScopeSizeResolverKt {
public static final fun DrawScopeSizeResolver ()Lcoil3/compose/DrawScopeSizeResolver;
}

public final class coil3/compose/DrawablePainter : androidx/compose/ui/graphics/painter/Painter {
public static final field $stable I
public fun <init> (Lcoil3/DrawableImage;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,37 @@ class AsyncImagePainterTest {
Image(
painter = rememberAsyncImagePainter(
model = "https://example.com/image",
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
.size(128.dp, 166.dp)
.testTag(Image),
)
}

waitForRequestComplete()

composeTestRule.onNodeWithTag(Image)
.assertIsDisplayed()
.assertWidthIsEqualTo(128.dp)
.assertHeightIsEqualTo(166.dp)
.captureToImage()
.assertIsSimilarTo(R.drawable.sample)
}

@Test
fun basicLoad_http_drawScopeSizeResolver() {
assumeSupportsCaptureToImage()

composeTestRule.setContent {
Image(
painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(LocalContext.current)
.data("https://example.com/image")
.size(DrawScopeSizeResolver())
.build(),
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
Expand Down Expand Up @@ -131,7 +161,37 @@ class AsyncImagePainterTest {
Image(
painter = rememberAsyncImagePainter(
model = R.drawable.sample,
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
.size(128.dp, 166.dp)
.testTag(Image),
)
}

waitForRequestComplete()

composeTestRule.onNodeWithTag(Image)
.assertWidthIsEqualTo(128.dp, tolerance = 1.dp)
.assertHeightIsEqualTo(166.dp, tolerance = 1.dp)
.assertIsDisplayed()
.captureToImage()
.assertIsSimilarTo(R.drawable.sample)
}

@Test
fun basicLoad_drawableId_drawScopeSizeResolver() {
assumeSupportsCaptureToImage()

composeTestRule.setContent {
Image(
painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(LocalContext.current)
.data(R.drawable.sample)
.size(DrawScopeSizeResolver())
.build(),
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
Expand Down Expand Up @@ -160,7 +220,37 @@ class AsyncImagePainterTest {
Image(
painter = rememberAsyncImagePainter(
model = resourceUri(R.drawable.sample),
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
.size(128.dp, 166.dp)
.testTag(Image),
)
}

waitForRequestComplete()

composeTestRule.onNodeWithTag(Image)
.assertWidthIsEqualTo(128.dp)
.assertHeightIsEqualTo(166.dp)
.assertIsDisplayed()
.captureToImage()
.assertIsSimilarTo(R.drawable.sample)
}

@Test
fun basicLoad_drawableUri_drawScopeSizeResolver() {
assumeSupportsCaptureToImage()

composeTestRule.setContent {
Image(
painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(LocalContext.current)
.data(resourceUri(R.drawable.sample))
.size(DrawScopeSizeResolver())
.build(),
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
Expand Down Expand Up @@ -191,6 +281,7 @@ class AsyncImagePainterTest {
override fun onSuccess(request: ImageRequest, result: SuccessResult) {
requestCompleted = true
}

override fun onError(request: ImageRequest, result: ErrorResult) {
requestThrowable = result.throwable
}
Expand Down Expand Up @@ -228,7 +319,7 @@ class AsyncImagePainterTest {
Image(
painter = rememberAsyncImagePainter(
model = data,
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
Expand Down Expand Up @@ -269,15 +360,15 @@ class AsyncImagePainterTest {
composeTestRule.setContent {
val painter = rememberAsyncImagePainter(
model = "https://example.com/image",
imageLoader = imageLoader
imageLoader = imageLoader,
)

Image(
painter = painter,
contentDescription = null,
modifier = Modifier
.size(size)
.testTag(Image)
.testTag(Image),
)

LaunchedEffect(painter) {
Expand Down Expand Up @@ -309,7 +400,7 @@ class AsyncImagePainterTest {
Image(
painter = rememberAsyncImagePainter(
model = "https://example.com/image",
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier.testTag(Image),
Expand All @@ -329,13 +420,13 @@ class AsyncImagePainterTest {
composeTestRule.setContent {
LazyColumn(
modifier = Modifier
.size(240.dp, 200.dp)
.size(240.dp, 200.dp),
) {
item {
Image(
painter = rememberAsyncImagePainter(
model = "https://example.com/image",
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
Expand Down Expand Up @@ -365,7 +456,7 @@ class AsyncImagePainterTest {
.data("https://example.com/noimage")
.error(R.drawable.red_rectangle)
.build(),
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
Expand Down Expand Up @@ -397,7 +488,7 @@ class AsyncImagePainterTest {
.data("https://example.com/image")
.placeholder(R.drawable.red_rectangle)
.build(),
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
Expand Down Expand Up @@ -426,7 +517,7 @@ class AsyncImagePainterTest {
Image(
painter = rememberAsyncImagePainter(
model = "https://example.com/noimage",
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
Expand All @@ -450,7 +541,7 @@ class AsyncImagePainterTest {
Image(
painter = rememberAsyncImagePainter(
model = painterResource(R.drawable.sample),
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier.size(128.dp),
Expand Down Expand Up @@ -478,7 +569,7 @@ class AsyncImagePainterTest {
Image(
painter = rememberAsyncImagePainter(
model = ColorPainter(Color.Magenta),
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier.size(128.dp),
Expand All @@ -498,7 +589,7 @@ class AsyncImagePainterTest {
.placeholder(R.drawable.red_rectangle)
.crossfade(true)
.build(),
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
Expand Down Expand Up @@ -557,12 +648,12 @@ class AsyncImagePainterTest {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState())
.verticalScroll(rememberScrollState()),
) {
Image(
painter = rememberAsyncImagePainter(
model = "https://example.com/image",
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null,
modifier = Modifier
Expand Down Expand Up @@ -627,7 +718,7 @@ class AsyncImagePainterTest {
onSuccess = { successCount.getAndIncrement() },
onError = { errorCount.getAndIncrement() },
),
contentDescription = null
contentDescription = null,
)
}

Expand Down Expand Up @@ -656,7 +747,7 @@ class AsyncImagePainterTest {
onSuccess = { successCount.getAndIncrement() },
onError = { errorCount.getAndIncrement() },
),
contentDescription = null
contentDescription = null,
)
}

Expand All @@ -676,9 +767,9 @@ class AsyncImagePainterTest {
Image(
painter = rememberAsyncImagePainter(
model = "https://example.com/image",
imageLoader = imageLoader
imageLoader = imageLoader,
),
contentDescription = null
contentDescription = null,
)
}

Expand Down

0 comments on commit d979398

Please sign in to comment.