Skip to content

Commit

Permalink
Test to verify that images for offscreen tiles aren't loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
saket committed Nov 12, 2024
1 parent 37b9e2a commit 4dbb9c6
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.dp
import assertk.assertThat
import assertk.assertions.containsExactly
import assertk.assertions.isEqualTo
import com.dropbox.dropshots.Dropshots
import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
Expand Down Expand Up @@ -81,6 +82,7 @@ import org.junit.rules.TestName
import org.junit.rules.Timeout
import org.junit.runner.RunWith
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import kotlin.time.Duration.Companion.seconds

@RunWith(TestParameterInjector::class)
Expand Down Expand Up @@ -719,6 +721,50 @@ class SubSamplingImageTest {
}
}

@Test fun do_not_load_images_for_tiles_that_are_not_visible() {
val decodedRegionCount = AtomicInteger(0)
val recordingDecoderFactory = ImageRegionDecoder.Factory { params ->
val real = AndroidImageRegionDecoder.Factory.create(params)
object : ImageRegionDecoder by real {
override suspend fun decodeRegion(region: ImageRegionTile) =
real.decodeRegion(region).also {
if (region.sampleSize == ImageSampleSize(1)) {
decodedRegionCount.incrementAndGet()
}
}
}
}

lateinit var imageState: SubSamplingImageState
rule.setContent {
val zoomableState = rememberZoomableState(
zoomSpec = ZoomSpec(maxZoomFactor = 1f)
)
CompositionLocalProvider(LocalImageRegionDecoderFactory provides recordingDecoderFactory) {
imageState = rememberSubSamplingImageState(
zoomableState = zoomableState,
imageSource = SubSamplingImageSource.asset("pahade.jpg"),
)
SubSamplingImage(
modifier = Modifier
.fillMaxSize()
.zoomable(zoomableState)
.testTag("image"),
state = imageState,
contentDescription = null,
)
}
}

rule.waitUntil { imageState.isImageDisplayed }
rule.onNodeWithTag("image").performTouchInput { doubleClick() }

rule.waitUntil(3.seconds) { imageState.isImageDisplayedInFullQuality }
rule.runOnIdle {
assertThat(decodedRegionCount.get()).isEqualTo(4)
}
}

// Regression test for https://github.com/saket/telephoto/issues/110
@Test fun unknown_color_space() {
val bitmap = rule.activity.assets.open("grayscale.jpg").use {
Expand Down

0 comments on commit 4dbb9c6

Please sign in to comment.