From eddc7d07eff8a54fc9d379698860c5717a6c5708 Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 5 Feb 2024 04:33:23 -0500 Subject: [PATCH 1/2] Silent fail (#350) * added throw to failed screenshot tests * added description with throw --------- Co-authored-by: Nick Rotonda --- shot-android/src/main/java/com/karumi/shot/ScreenshotTest.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shot-android/src/main/java/com/karumi/shot/ScreenshotTest.kt b/shot-android/src/main/java/com/karumi/shot/ScreenshotTest.kt index 7ca85241..b7ad58e1 100644 --- a/shot-android/src/main/java/com/karumi/shot/ScreenshotTest.kt +++ b/shot-android/src/main/java/com/karumi/shot/ScreenshotTest.kt @@ -190,6 +190,7 @@ interface ScreenshotTest { .record() } catch (t: Throwable) { Log.e("Shot", "Exception captured while taking screenshot for snapshot with name $snapshotName", t) + throw IllegalStateException("Exception occurred while taking screenshot for snapshot with name $snapshotName", t) } } @@ -204,6 +205,7 @@ interface ScreenshotTest { .record() } catch (t: Throwable) { Log.e("Shot", "Exception captured while taking screenshot for snapshot with name $snapshotName", t) + throw IllegalStateException("Exception occurred while taking screenshot for snapshot with name $snapshotName", t) } } From a0b8af308157a3e0cd8f2878f17ad42ad75eac3f Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 5 Feb 2024 04:35:45 -0500 Subject: [PATCH 2/2] added maxPixels to ScreenshotTest (#349) Co-authored-by: Nick Rotonda --- .../java/com/karumi/shot/ScreenshotTest.kt | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/shot-android/src/main/java/com/karumi/shot/ScreenshotTest.kt b/shot-android/src/main/java/com/karumi/shot/ScreenshotTest.kt index b7ad58e1..45060d94 100644 --- a/shot-android/src/main/java/com/karumi/shot/ScreenshotTest.kt +++ b/shot-android/src/main/java/com/karumi/shot/ScreenshotTest.kt @@ -24,6 +24,7 @@ import androidx.test.espresso.Espresso import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation import com.facebook.testing.screenshot.Screenshot import com.facebook.testing.screenshot.ViewHelpers +import com.facebook.testing.screenshot.internal.RecordBuilderImpl import com.facebook.testing.screenshot.internal.TestNameDetector import com.karumi.shot.compose.ComposeScreenshotRunner import com.karumi.shot.compose.ScreenshotMetadata @@ -50,18 +51,19 @@ interface ScreenshotTest { heightInPx: Int? = null, widthInPx: Int? = null, name: String? = null, - backgroundColor: Int = android.R.color.white + backgroundColor: Int = android.R.color.white, + maxPixels: Long = RecordBuilderImpl.DEFAULT_MAX_PIXELS, ) { val view = activity.findViewById(android.R.id.content) if (heightInPx == null && widthInPx == null) { disableFlakyComponentsAndWaitForIdle(view) - takeActivitySnapshot(activity, name) + takeActivitySnapshot(activity, name, maxPixels) } else { runOnUi { view.setBackgroundResource(backgroundColor) } - compareScreenshot(view = view!!, heightInPx = heightInPx, widthInPx = widthInPx, name = name) + compareScreenshot(view = view!!, heightInPx = heightInPx, widthInPx = widthInPx, name = name, maxPixels = maxPixels) } } @@ -69,18 +71,20 @@ interface ScreenshotTest { fragment: Fragment, heightInPx: Int? = null, widthInPx: Int? = null, - name: String? = null - ) = compareScreenshot(view = fragment.requireView(), heightInPx = heightInPx, widthInPx = widthInPx, name = name) + name: String? = null, + maxPixels: Long = RecordBuilderImpl.DEFAULT_MAX_PIXELS, + ) = compareScreenshot(view = fragment.requireView(), heightInPx = heightInPx, widthInPx = widthInPx, name = name, maxPixels = maxPixels) fun compareScreenshot( dialog: Dialog, heightInPx: Int? = null, widthInPx: Int? = null, - name: String? = null + name: String? = null, + maxPixels: Long = RecordBuilderImpl.DEFAULT_MAX_PIXELS, ) { val window = dialog.window if (window != null) { - compareScreenshot(view = window.decorView, heightInPx = heightInPx, widthInPx = widthInPx, name = name) + compareScreenshot(view = window.decorView, heightInPx = heightInPx, widthInPx = widthInPx, name = name, maxPixels = maxPixels) } } @@ -88,10 +92,17 @@ interface ScreenshotTest { holder: RecyclerView.ViewHolder, heightInPx: Int, widthInPx: Int? = null, - name: String? = null - ) = compareScreenshot(view = holder.itemView, heightInPx = heightInPx, widthInPx = widthInPx, name = name) + name: String? = null, + maxPixels: Long = RecordBuilderImpl.DEFAULT_MAX_PIXELS, + ) = compareScreenshot(view = holder.itemView, heightInPx = heightInPx, widthInPx = widthInPx, name = name, maxPixels = maxPixels) - fun compareScreenshot(view: View, heightInPx: Int? = null, widthInPx: Int? = null, name: String? = null) { + fun compareScreenshot( + view: View, + heightInPx: Int? = null, + widthInPx: Int? = null, + name: String? = null, + maxPixels: Long = RecordBuilderImpl.DEFAULT_MAX_PIXELS, + ) { disableFlakyComponentsAndWaitForIdle(view) val context = getInstrumentation().targetContext @@ -107,11 +118,14 @@ interface ScreenshotTest { .setExactWidthPx(width) .layout() } - takeViewSnapshot(name, view) + takeViewSnapshot(name, view, maxPixels) } @RequiresApi(Build.VERSION_CODES.O) - fun compareScreenshot(rule: ComposeTestRule, name: String? = null) { + fun compareScreenshot( + rule: ComposeTestRule, + name: String? = null, + ) { rule.waitForIdle() compareScreenshot(rule.onRoot(), name) } @@ -179,7 +193,7 @@ interface ScreenshotTest { private fun notInAppMainThread() = Looper.myLooper() != Looper.getMainLooper() - private fun takeViewSnapshot(name: String?, view: View) { + private fun takeViewSnapshot(name: String?, view: View, maxPixels: Long) { val testName = name ?: TestNameDetector.getTestName() val snapshotName = "${TestNameDetector.getTestClass()}_$testName" try { @@ -187,6 +201,7 @@ interface ScreenshotTest { .snap(view) .setIncludeAccessibilityInfo(false) .setName(snapshotName) + .setMaxPixels(maxPixels) .record() } catch (t: Throwable) { Log.e("Shot", "Exception captured while taking screenshot for snapshot with name $snapshotName", t) @@ -194,7 +209,7 @@ interface ScreenshotTest { } } - private fun takeActivitySnapshot(activity: Activity, name: String?) { + private fun takeActivitySnapshot(activity: Activity, name: String?, maxPixels: Long) { val testName = name ?: TestNameDetector.getTestName() val snapshotName = "${TestNameDetector.getTestClass()}_$testName" try { @@ -202,6 +217,7 @@ interface ScreenshotTest { .snapActivity(activity) .setIncludeAccessibilityInfo(false) .setName(snapshotName) + .setMaxPixels(maxPixels) .record() } catch (t: Throwable) { Log.e("Shot", "Exception captured while taking screenshot for snapshot with name $snapshotName", t)