Skip to content

Commit

Permalink
Add configuration for image size
Browse files Browse the repository at this point in the history
  • Loading branch information
sdoward authored and gabrielittner committed Jan 24, 2024
1 parent 31c8c3d commit 24e38fc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
8 changes: 8 additions & 0 deletions paparazzi/src/main/java/app/cash/paparazzi/ImageSize.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package app.cash.paparazzi

import app.cash.paparazzi.internal.ImageUtils

sealed class ImageSize {
object FullBleed : ImageSize()
data class Limit(val height: Int = ImageUtils.THUMBNAIL_SIZE) : ImageSize()
}
14 changes: 10 additions & 4 deletions paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ class Paparazzi @JvmOverloads constructor(
private val renderExtensions: Set<RenderExtension> = setOf(),
private val supportsRtl: Boolean = false,
private val showSystemUi: Boolean = false,
private val validateAccessibility: Boolean = false
private val validateAccessibility: Boolean = false,
private val imageSize: ImageSize = ImageSize.Limit()
) : TestRule {
private val logger = PaparazziLogger()
private lateinit var renderSession: RenderSessionImpl
Expand Down Expand Up @@ -412,9 +413,14 @@ class Paparazzi @JvmOverloads constructor(
}

private fun scaleImage(image: BufferedImage): BufferedImage {
val scale = ImageUtils.getThumbnailScale(image)
// Only scale images down so we don't waste storage space enlarging smaller layouts.
return if (scale < 1f) ImageUtils.scale(image, scale, scale) else image
return when (imageSize) {
ImageSize.FullBleed -> image
is ImageSize.Limit -> {
val scale = ImageUtils.getThumbnailScale(image)
// Only scale images down so we don't waste storage space enlarging smaller layouts.
return if (scale < 1f) ImageUtils.scale(image, scale, scale) else image
}
}
}

private fun validateLayoutAccessibility(view: View, image: BufferedImage? = null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal object ImageUtils {
*/
private val FAIL_ON_MISSING_THUMBNAIL = true

private const val THUMBNAIL_SIZE = 1000
const val THUMBNAIL_SIZE = 1000

/** Directory where to write the thumbnails and deltas. */
private val failureDir: File
Expand Down Expand Up @@ -368,9 +368,9 @@ internal object ImageUtils {
}
}

fun getThumbnailScale(image: BufferedImage): Double {
fun getThumbnailScale(image: BufferedImage, limit: Int = THUMBNAIL_SIZE): Double {
val maxDimension = max(image.width, image.height)
return THUMBNAIL_SIZE / maxDimension.toDouble()
return limit / maxDimension.toDouble()
}

private fun setRenderingHints(g2: Graphics2D) {
Expand Down
1 change: 1 addition & 0 deletions paparazzi/src/main/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

.screen img, .screen video {
width: 300px;
max-height:1000px;
}

div.overlay__hovered {
Expand Down
34 changes: 34 additions & 0 deletions sample/src/test/java/app/cash/paparazzi/sample/ScrollingTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package app.cash.paparazzi.sample

import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
import app.cash.paparazzi.ImageSize
import app.cash.paparazzi.Paparazzi
import com.android.ide.common.rendering.api.SessionParams
import org.junit.Rule
import org.junit.Test

class ScrollingTest {

@get:Rule
val paparazzi = Paparazzi(
renderingMode = SessionParams.RenderingMode.V_SCROLL,
imageSize = ImageSize.FullBleed
)

@Test
fun verticalScrolling() {
val scrollView = ScrollView(paparazzi.context)
val linearLayout = LinearLayout(paparazzi.context)
linearLayout.orientation = LinearLayout.VERTICAL
repeat(1000) {
val textView = TextView(paparazzi.context)
textView.setText("Hello world $it")
linearLayout.addView(textView)
}
scrollView.addView(linearLayout)

paparazzi.snapshot(scrollView)
}
}

0 comments on commit 24e38fc

Please sign in to comment.