Skip to content

Commit a0b8af3

Browse files
nrotondaNick Rotonda
and
Nick Rotonda
authored
added maxPixels to ScreenshotTest (#349)
Co-authored-by: Nick Rotonda <[email protected]>
1 parent eddc7d0 commit a0b8af3

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

shot-android/src/main/java/com/karumi/shot/ScreenshotTest.kt

+30-14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import androidx.test.espresso.Espresso
2424
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
2525
import com.facebook.testing.screenshot.Screenshot
2626
import com.facebook.testing.screenshot.ViewHelpers
27+
import com.facebook.testing.screenshot.internal.RecordBuilderImpl
2728
import com.facebook.testing.screenshot.internal.TestNameDetector
2829
import com.karumi.shot.compose.ComposeScreenshotRunner
2930
import com.karumi.shot.compose.ScreenshotMetadata
@@ -50,48 +51,58 @@ interface ScreenshotTest {
5051
heightInPx: Int? = null,
5152
widthInPx: Int? = null,
5253
name: String? = null,
53-
backgroundColor: Int = android.R.color.white
54+
backgroundColor: Int = android.R.color.white,
55+
maxPixels: Long = RecordBuilderImpl.DEFAULT_MAX_PIXELS,
5456
) {
5557
val view = activity.findViewById<View>(android.R.id.content)
5658

5759
if (heightInPx == null && widthInPx == null) {
5860
disableFlakyComponentsAndWaitForIdle(view)
59-
takeActivitySnapshot(activity, name)
61+
takeActivitySnapshot(activity, name, maxPixels)
6062
} else {
6163
runOnUi {
6264
view.setBackgroundResource(backgroundColor)
6365
}
64-
compareScreenshot(view = view!!, heightInPx = heightInPx, widthInPx = widthInPx, name = name)
66+
compareScreenshot(view = view!!, heightInPx = heightInPx, widthInPx = widthInPx, name = name, maxPixels = maxPixels)
6567
}
6668
}
6769

6870
fun compareScreenshot(
6971
fragment: Fragment,
7072
heightInPx: Int? = null,
7173
widthInPx: Int? = null,
72-
name: String? = null
73-
) = compareScreenshot(view = fragment.requireView(), heightInPx = heightInPx, widthInPx = widthInPx, name = name)
74+
name: String? = null,
75+
maxPixels: Long = RecordBuilderImpl.DEFAULT_MAX_PIXELS,
76+
) = compareScreenshot(view = fragment.requireView(), heightInPx = heightInPx, widthInPx = widthInPx, name = name, maxPixels = maxPixels)
7477

7578
fun compareScreenshot(
7679
dialog: Dialog,
7780
heightInPx: Int? = null,
7881
widthInPx: Int? = null,
79-
name: String? = null
82+
name: String? = null,
83+
maxPixels: Long = RecordBuilderImpl.DEFAULT_MAX_PIXELS,
8084
) {
8185
val window = dialog.window
8286
if (window != null) {
83-
compareScreenshot(view = window.decorView, heightInPx = heightInPx, widthInPx = widthInPx, name = name)
87+
compareScreenshot(view = window.decorView, heightInPx = heightInPx, widthInPx = widthInPx, name = name, maxPixels = maxPixels)
8488
}
8589
}
8690

8791
fun compareScreenshot(
8892
holder: RecyclerView.ViewHolder,
8993
heightInPx: Int,
9094
widthInPx: Int? = null,
91-
name: String? = null
92-
) = compareScreenshot(view = holder.itemView, heightInPx = heightInPx, widthInPx = widthInPx, name = name)
95+
name: String? = null,
96+
maxPixels: Long = RecordBuilderImpl.DEFAULT_MAX_PIXELS,
97+
) = compareScreenshot(view = holder.itemView, heightInPx = heightInPx, widthInPx = widthInPx, name = name, maxPixels = maxPixels)
9398

94-
fun compareScreenshot(view: View, heightInPx: Int? = null, widthInPx: Int? = null, name: String? = null) {
99+
fun compareScreenshot(
100+
view: View,
101+
heightInPx: Int? = null,
102+
widthInPx: Int? = null,
103+
name: String? = null,
104+
maxPixels: Long = RecordBuilderImpl.DEFAULT_MAX_PIXELS,
105+
) {
95106
disableFlakyComponentsAndWaitForIdle(view)
96107

97108
val context = getInstrumentation().targetContext
@@ -107,11 +118,14 @@ interface ScreenshotTest {
107118
.setExactWidthPx(width)
108119
.layout()
109120
}
110-
takeViewSnapshot(name, view)
121+
takeViewSnapshot(name, view, maxPixels)
111122
}
112123

113124
@RequiresApi(Build.VERSION_CODES.O)
114-
fun compareScreenshot(rule: ComposeTestRule, name: String? = null) {
125+
fun compareScreenshot(
126+
rule: ComposeTestRule,
127+
name: String? = null,
128+
) {
115129
rule.waitForIdle()
116130
compareScreenshot(rule.onRoot(), name)
117131
}
@@ -179,29 +193,31 @@ interface ScreenshotTest {
179193

180194
private fun notInAppMainThread() = Looper.myLooper() != Looper.getMainLooper()
181195

182-
private fun takeViewSnapshot(name: String?, view: View) {
196+
private fun takeViewSnapshot(name: String?, view: View, maxPixels: Long) {
183197
val testName = name ?: TestNameDetector.getTestName()
184198
val snapshotName = "${TestNameDetector.getTestClass()}_$testName"
185199
try {
186200
Screenshot
187201
.snap(view)
188202
.setIncludeAccessibilityInfo(false)
189203
.setName(snapshotName)
204+
.setMaxPixels(maxPixels)
190205
.record()
191206
} catch (t: Throwable) {
192207
Log.e("Shot", "Exception captured while taking screenshot for snapshot with name $snapshotName", t)
193208
throw IllegalStateException("Exception occurred while taking screenshot for snapshot with name $snapshotName", t)
194209
}
195210
}
196211

197-
private fun takeActivitySnapshot(activity: Activity, name: String?) {
212+
private fun takeActivitySnapshot(activity: Activity, name: String?, maxPixels: Long) {
198213
val testName = name ?: TestNameDetector.getTestName()
199214
val snapshotName = "${TestNameDetector.getTestClass()}_$testName"
200215
try {
201216
Screenshot
202217
.snapActivity(activity)
203218
.setIncludeAccessibilityInfo(false)
204219
.setName(snapshotName)
220+
.setMaxPixels(maxPixels)
205221
.record()
206222
} catch (t: Throwable) {
207223
Log.e("Shot", "Exception captured while taking screenshot for snapshot with name $snapshotName", t)

0 commit comments

Comments
 (0)