Skip to content

Commit

Permalink
Merge branch 'pedrovgs:master' into junit-test-reports
Browse files Browse the repository at this point in the history
  • Loading branch information
pettero authored Feb 9, 2024
2 parents 30dcf81 + a0b8af3 commit 5d68864
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions shot-android/src/main/java/com/karumi/shot/ScreenshotTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,48 +51,58 @@ 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<View>(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)
}
}

fun compareScreenshot(
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)
}
}

fun compareScreenshot(
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
Expand All @@ -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)
}
Expand Down Expand Up @@ -179,31 +193,35 @@ 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 {
Screenshot
.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)
throw IllegalStateException("Exception occurred while taking screenshot for snapshot with name $snapshotName", t)
}
}

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 {
Screenshot
.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)
throw IllegalStateException("Exception occurred while taking screenshot for snapshot with name $snapshotName", t)
}
}

Expand Down

0 comments on commit 5d68864

Please sign in to comment.