diff --git a/paparazzi-gradle-plugin/src/test/java/app/cash/paparazzi/gradle/PaparazziPluginTest.kt b/paparazzi-gradle-plugin/src/test/java/app/cash/paparazzi/gradle/PaparazziPluginTest.kt index 5bef3864b0..7fc2f37be6 100644 --- a/paparazzi-gradle-plugin/src/test/java/app/cash/paparazzi/gradle/PaparazziPluginTest.kt +++ b/paparazzi-gradle-plugin/src/test/java/app/cash/paparazzi/gradle/PaparazziPluginTest.kt @@ -10,6 +10,7 @@ import okio.buffer import okio.source import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.TaskOutcome import org.gradle.testkit.runner.TaskOutcome.FROM_CACHE import org.gradle.testkit.runner.TaskOutcome.SUCCESS import org.junit.After @@ -626,6 +627,29 @@ class PaparazziPluginTest { assertThat(result.task(":testDebugUnitTest")).isNotNull() } + @Test + fun verifyAccessibilityRenderExtension() { + val fixtureRoot = File("src/test/projects/verify-accessibility-render-extension") + + gradleRunner + .withArguments("testDebug", "--stacktrace") + .forwardOutput() + .runFixture(fixtureRoot) { build() } + + val snapshotsDir = File(fixtureRoot, "build/reports/paparazzi/debug/images") + val snapshots = snapshotsDir.listFiles() + assertThat(snapshots!!).hasLength(2) + + var snapshotImage = snapshots[0] + var goldenImage = File(fixtureRoot, "src/test/resources/accessibility-shrink.png") + assertThat(snapshotImage).isSimilarTo(goldenImage).withDefaultThreshold() + snapshotImage = snapshots[1] + goldenImage = File(fixtureRoot, "src/test/resources/accessibility-new-view.png") + assertThat(snapshotImage).isSimilarTo(goldenImage).withDefaultThreshold() + goldenImage = File(fixtureRoot, "src/test/resources/accessibility.png") + assertThat(snapshotImage).isSimilarTo(goldenImage).withDefaultThreshold() + } + @Test fun verifyAllVariants() { val fixtureRoot = File("src/test/projects/verify-mode-success") diff --git a/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/build.gradle b/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/build.gradle new file mode 100644 index 0000000000..5847ed5eaf --- /dev/null +++ b/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/build.gradle @@ -0,0 +1,32 @@ +plugins { + id 'com.android.library' + id 'kotlin-android' + id 'app.cash.paparazzi' +} + +android { + namespace 'app.cash.paparazzi.plugin.test' + compileSdk libs.versions.compileSdk.get() as int + defaultConfig { + minSdk libs.versions.minSdk.get() as int + } + compileOptions { + sourceCompatibility = libs.versions.javaTarget.get() + targetCompatibility = libs.versions.javaTarget.get() + } + kotlinOptions { + jvmTarget = libs.versions.javaTarget.get() + } + buildFeatures { + compose true + } + composeOptions { + kotlinCompilerExtensionVersion libs.versions.composeCompiler.get() + } +} + +dependencies { + implementation libs.androidx.appcompat +} + +apply from: '../guava-fix.gradle' diff --git a/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/java/app/cash/paparazzi/plugin/test/AccessibilityRenderExtensionShrinkTest.kt b/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/java/app/cash/paparazzi/plugin/test/AccessibilityRenderExtensionShrinkTest.kt new file mode 100644 index 0000000000..4d9987f216 --- /dev/null +++ b/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/java/app/cash/paparazzi/plugin/test/AccessibilityRenderExtensionShrinkTest.kt @@ -0,0 +1,29 @@ +package app.cash.paparazzi.plugin.test + +import android.widget.TextView +import app.cash.paparazzi.DeviceConfig +import app.cash.paparazzi.Paparazzi +import app.cash.paparazzi.accessibility.AccessibilityRenderExtension +import com.android.ide.common.rendering.api.SessionParams +import org.junit.Rule +import org.junit.Test + +class AccessibilityRenderExtensionShrinkTest { + + @get:Rule + val paparazzi = Paparazzi( + deviceConfig = DeviceConfig.NEXUS_5, + renderExtensions = setOf(AccessibilityRenderExtension()), + renderingMode = SessionParams.RenderingMode.SHRINK + ) + + @Test + fun `verify baseline`() { + val view = TextView(paparazzi.context).apply { + id = 1 + text = "Text View Sample" + setPadding(50, 50, 50, 50) + } + paparazzi.snapshot(view, name = "accessibility-shrink") + } +} diff --git a/paparazzi/src/test/java/app/cash/paparazzi/accessibility/AccessibilityRenderExtensionTest.kt b/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/java/app/cash/paparazzi/plugin/test/AccessibilityRenderExtensionTest.kt similarity index 72% rename from paparazzi/src/test/java/app/cash/paparazzi/accessibility/AccessibilityRenderExtensionTest.kt rename to paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/java/app/cash/paparazzi/plugin/test/AccessibilityRenderExtensionTest.kt index 8e6e56109c..900efd627e 100644 --- a/paparazzi/src/test/java/app/cash/paparazzi/accessibility/AccessibilityRenderExtensionTest.kt +++ b/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/java/app/cash/paparazzi/plugin/test/AccessibilityRenderExtensionTest.kt @@ -1,4 +1,4 @@ -package app.cash.paparazzi.accessibility +package app.cash.paparazzi.plugin.test import android.content.Context import android.graphics.Color @@ -13,22 +13,15 @@ import android.widget.LinearLayout import android.widget.TextView import app.cash.paparazzi.DeviceConfig import app.cash.paparazzi.Paparazzi -import app.cash.paparazzi.Snapshot -import app.cash.paparazzi.SnapshotHandler -import app.cash.paparazzi.internal.ImageUtils +import app.cash.paparazzi.accessibility.AccessibilityRenderExtension import org.junit.Rule import org.junit.Test -import java.awt.image.BufferedImage -import java.io.File -import javax.imageio.ImageIO class AccessibilityRenderExtensionTest { - private val snapshotHandler = TestSnapshotVerifier() @get:Rule val paparazzi = Paparazzi( deviceConfig = DeviceConfig.NEXUS_5, - snapshotHandler = snapshotHandler, renderExtensions = setOf(AccessibilityRenderExtension()) ) @@ -81,7 +74,7 @@ class AccessibilityRenderExtensionTest { View(context).apply { id = 3 layoutParams = LinearLayout.LayoutParams(100, 100).apply { - setMarginsRelative(20, 20, 20, 20) + setMargins(20, 20, 20, 20) } contentDescription = "Margin Sample" } @@ -91,7 +84,7 @@ class AccessibilityRenderExtensionTest { View(context).apply { id = 4 layoutParams = LinearLayout.LayoutParams(100, 100).apply { - setMarginsRelative(20, 20, 20, 20) + setMargins(20, 20, 20, 20) } foreground = GradientDrawable(TL_BR, intArrayOf(Color.YELLOW, Color.BLUE)).apply { shape = OVAL @@ -113,28 +106,4 @@ class AccessibilityRenderExtensionTest { } ) } - - private class TestSnapshotVerifier : SnapshotHandler { - override fun newFrameHandler( - snapshot: Snapshot, - frameCount: Int, - fps: Int - ): SnapshotHandler.FrameHandler { - return object : SnapshotHandler.FrameHandler { - override fun handle(image: BufferedImage) { - val expected = File("src/test/resources/${snapshot.name}.png") - ImageUtils.assertImageSimilar( - relativePath = expected.path, - image = image, - goldenImage = ImageIO.read(expected), - maxPercentDifferent = 0.1 - ) - } - - override fun close() = Unit - } - } - - override fun close() = Unit - } } diff --git a/paparazzi/src/test/resources/accessibility-new-view.png b/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/resources/accessibility-new-view.png similarity index 100% rename from paparazzi/src/test/resources/accessibility-new-view.png rename to paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/resources/accessibility-new-view.png diff --git a/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/resources/accessibility-shrink.png b/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/resources/accessibility-shrink.png new file mode 100644 index 0000000000..acba682fb1 Binary files /dev/null and b/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/resources/accessibility-shrink.png differ diff --git a/paparazzi/src/test/resources/accessibility.png b/paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/resources/accessibility.png similarity index 100% rename from paparazzi/src/test/resources/accessibility.png rename to paparazzi-gradle-plugin/src/test/projects/verify-accessibility-render-extension/src/test/resources/accessibility.png diff --git a/paparazzi/src/main/java/app/cash/paparazzi/accessibility/AccessibilityRenderExtension.kt b/paparazzi/src/main/java/app/cash/paparazzi/accessibility/AccessibilityRenderExtension.kt index eb17ce3465..49fc5af4e3 100644 --- a/paparazzi/src/main/java/app/cash/paparazzi/accessibility/AccessibilityRenderExtension.kt +++ b/paparazzi/src/main/java/app/cash/paparazzi/accessibility/AccessibilityRenderExtension.kt @@ -40,16 +40,17 @@ public class AccessibilityRenderExtension : RenderExtension { orientation = LinearLayout.HORIZONTAL weightSum = 2f layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT) + isMeasureWithLargestChildEnabled = true // Allows for snapshot with SHRINK render mode val overlay = AccessibilityOverlayView(context).apply { addView(contentView, FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)) } val contentLayoutParams = contentView.layoutParams ?: generateLayoutParams(null) - addView(overlay, LinearLayout.LayoutParams(contentLayoutParams.width, contentLayoutParams.height, 1f)) + addView(overlay, LinearLayout.LayoutParams(0, contentLayoutParams.height, 1f)) val overlayDetailsView = AccessibilityOverlayDetailsView(context) - addView(overlayDetailsView, LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT, 1f)) + addView(overlayDetailsView, LinearLayout.LayoutParams(0, MATCH_PARENT, 1f)) OneShotPreDrawListener.add(this) { val elements = buildList {