Skip to content

Commit

Permalink
Fix Picasso tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes committed Sep 25, 2020
1 parent 1f2862c commit 5ba8890
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,8 @@ object Libs {
const val truth = "com.google.truth:truth:1.0.1"
const val mockk = "io.mockk:mockk-android:1.10.0"

const val mockWebServer = "com.squareup.okhttp3:mockwebserver:3.12.2"
object OkHttp {
const val okhttp = "com.squareup.okhttp3:okhttp:3.12.2"
const val mockWebServer = "com.squareup.okhttp3:mockwebserver:3.12.2"
}
}
2 changes: 1 addition & 1 deletion coil/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ dependencies {
androidTestImplementation Libs.truth
androidTestImplementation Libs.mockk

androidTestImplementation Libs.mockWebServer
androidTestImplementation Libs.OkHttp.mockWebServer

androidTestImplementation Libs.Coroutines.test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.withTimeoutOrNull
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
Expand Down Expand Up @@ -238,7 +239,9 @@ class CoilTest {

// Await the first load
runBlocking {
loadCompleteSignal.receive()
withTimeout(5000) {
loadCompleteSignal.receive()
}
}

// Assert that the content is completely Red
Expand All @@ -253,7 +256,11 @@ class CoilTest {
drawableResId.value = R.drawable.blue_rectangle

// Await the second load
runBlocking { loadCompleteSignal.receive() }
runBlocking {
withTimeout(5000) {
loadCompleteSignal.receive()
}
}

// Assert that the content is completely Blue
composeTestRule.onNodeWithTag(CoilTestTags.Image)
Expand All @@ -276,7 +283,7 @@ class CoilTest {
composeTestRule.setContent {
val size = sizeFlow.collectAsState()
CoilImage(
data = resourceUri(R.drawable.blue_rectangle),
data = resourceUri(R.drawable.red_rectangle),
modifier = Modifier.preferredSize(size.value).testTag(CoilTestTags.Image),
onRequestCompleted = { loadCompleteSignal.offer(Unit) }
)
Expand Down
2 changes: 1 addition & 1 deletion imageloading-core/api/imageloading-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class dev/chrisbanes/accompanist/imageloading/DataSource : java/lan
}

public final class dev/chrisbanes/accompanist/imageloading/ImageLoad {
public static final fun ImageLoad (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
public static final fun ImageLoad (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Modifier;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
}

public abstract class dev/chrisbanes/accompanist/imageloading/ImageLoadState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,27 @@ import kotlinx.coroutines.flow.flow
* @param request The request to execute.
* @param executeRequest Suspending lambda to execute an image loading request.
* @param modifier [Modifier] used to adjust the layout algorithm or draw decoration content.
* @param requestKey The object to key this request on. If the request type supports equality then
* the default value will work. Otherwise pass in the `data` value.
* @param modifier [Modifier] used to adjust the layout algorithm or draw decoration content.
* @param transformRequestForSize Optionally transform [request] for the given [IntSize].
* @param shouldRefetchOnSizeChange Lambda which will be invoked when the size changes, allowing
* optional re-fetching of the image. Return true to re-fetch the image.
* @param onRequestCompleted Listener which will be called when the loading request has finished.
* @param content Content to be displayed for the given state.
*/
@Composable
fun <T> ImageLoad(
fun <T : Any> ImageLoad(
request: T,
executeRequest: suspend (T) -> ImageLoadState,
modifier: Modifier = Modifier,
requestKey: Any = request,
transformRequestForSize: (T, IntSize) -> T? = { r, _ -> r },
shouldRefetchOnSizeChange: (currentResult: ImageLoadState, size: IntSize) -> Boolean = defaultRefetchOnSizeChangeLambda,
onRequestCompleted: (ImageLoadState) -> Unit = emptySuccessLambda,
content: @Composable (imageLoadState: ImageLoadState) -> Unit
) {
var state by stateFor<ImageLoadState>(request) { ImageLoadState.Empty }
var state by stateFor<ImageLoadState>(requestKey) { ImageLoadState.Empty }

// This may look a little weird, but allows the launchInComposition callback to always
// invoke the last provided [onRequestCompleted].
Expand All @@ -77,7 +81,7 @@ fun <T> ImageLoad(
val callback = remember { mutableStateOf(onRequestCompleted, referentialEqualityPolicy()) }
callback.value = onRequestCompleted

val requestActor = remember(request) {
val requestActor = remember(requestKey) {
ImageLoadRequestActor(executeRequest)
}

Expand Down
12 changes: 0 additions & 12 deletions picasso/api/coil.api

This file was deleted.

5 changes: 5 additions & 0 deletions picasso/api/picasso.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public final class dev/chrisbanes/accompanist/picasso/PicassoImage {
public static final fun PicassoImage (Ljava/lang/Object;Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;Landroidx/compose/ui/graphics/ColorFilter;ZLcom/squareup/picasso/Picasso;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun PicassoImage (Ljava/lang/Object;Landroidx/compose/ui/Modifier;Lcom/squareup/picasso/Picasso;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
}

4 changes: 3 additions & 1 deletion picasso/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ dependencies {

api Libs.picasso

implementation Libs.OkHttp.okhttp

implementation Libs.AndroidX.coreKtx
implementation Libs.AndroidX.Compose.runtime
implementation Libs.AndroidX.Compose.foundation
Expand All @@ -88,7 +90,7 @@ dependencies {
androidTestImplementation Libs.truth
androidTestImplementation Libs.mockk

androidTestImplementation Libs.mockWebServer
androidTestImplementation Libs.OkHttp.mockWebServer

androidTestImplementation Libs.Coroutines.test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.withTimeoutOrNull
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
Expand Down Expand Up @@ -147,7 +148,9 @@ class PicassoTest {

// Await the first load
runBlocking {
loadCompleteSignal.receive()
withTimeout(5000) {
loadCompleteSignal.receive()
}
}

// Assert that the content is completely Red
Expand All @@ -162,7 +165,11 @@ class PicassoTest {
drawableResId.value = R.drawable.blue_rectangle

// Await the second load
runBlocking { loadCompleteSignal.receive() }
runBlocking {
withTimeout(5000) {
loadCompleteSignal.receive()
}
}

// Assert that the content is completely Blue
composeTestRule.onNodeWithTag(TestTags.Image)
Expand All @@ -179,21 +186,29 @@ class PicassoTest {
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun basicLoad_changeSize() {
val loadCompleteSignal = Channel<Unit>(Channel.UNLIMITED)
val loadCompleteSignal = Channel<ImageLoadState>(Channel.UNLIMITED)
val sizeFlow = MutableStateFlow(128.dp)

composeTestRule.setContent {
val size = sizeFlow.collectAsState()
PicassoImage(
data = R.drawable.blue_rectangle,
data = R.drawable.red_rectangle,
modifier = Modifier.preferredSize(size.value).testTag(TestTags.Image),
onRequestCompleted = { loadCompleteSignal.offer(Unit) }
onRequestCompleted = { loadCompleteSignal.offer(it) }
)
}

// Await the first load
runBlocking {
loadCompleteSignal.receive()

val result = loadCompleteSignal.receive()

if (result is ImageLoadState.Error) {
throw result.throwable
}

assertThat(result)
.isInstanceOf(ImageLoadState.Success::class.java)
}

// Now change the size
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 0 additions & 20 deletions picasso/src/androidTest/res/drawable-nodpi/blue_rectangle.xml

This file was deleted.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 0 additions & 20 deletions picasso/src/androidTest/res/drawable-nodpi/red_rectangle.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fun PicassoImage(
) {
ImageLoad(
request = data.toRequestCreator(picasso),
requestKey = data, // Picasso RequestCreator doesn't support equality so we use the data
executeRequest = { r ->
@OptIn(ExperimentalCoroutinesApi::class)
suspendCancellableCoroutine { cont ->
Expand Down

0 comments on commit 5ba8890

Please sign in to comment.