@@ -24,6 +24,7 @@ import androidx.test.espresso.Espresso
24
24
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
25
25
import com.facebook.testing.screenshot.Screenshot
26
26
import com.facebook.testing.screenshot.ViewHelpers
27
+ import com.facebook.testing.screenshot.internal.RecordBuilderImpl
27
28
import com.facebook.testing.screenshot.internal.TestNameDetector
28
29
import com.karumi.shot.compose.ComposeScreenshotRunner
29
30
import com.karumi.shot.compose.ScreenshotMetadata
@@ -50,48 +51,58 @@ interface ScreenshotTest {
50
51
heightInPx : Int? = null,
51
52
widthInPx : Int? = null,
52
53
name : String? = null,
53
- backgroundColor : Int = android.R .color.white
54
+ backgroundColor : Int = android.R .color.white,
55
+ maxPixels : Long = RecordBuilderImpl .DEFAULT_MAX_PIXELS ,
54
56
) {
55
57
val view = activity.findViewById<View >(android.R .id.content)
56
58
57
59
if (heightInPx == null && widthInPx == null ) {
58
60
disableFlakyComponentsAndWaitForIdle(view)
59
- takeActivitySnapshot(activity, name)
61
+ takeActivitySnapshot(activity, name, maxPixels )
60
62
} else {
61
63
runOnUi {
62
64
view.setBackgroundResource(backgroundColor)
63
65
}
64
- compareScreenshot(view = view!! , heightInPx = heightInPx, widthInPx = widthInPx, name = name)
66
+ compareScreenshot(view = view!! , heightInPx = heightInPx, widthInPx = widthInPx, name = name, maxPixels = maxPixels )
65
67
}
66
68
}
67
69
68
70
fun compareScreenshot (
69
71
fragment : Fragment ,
70
72
heightInPx : Int? = null,
71
73
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)
74
77
75
78
fun compareScreenshot (
76
79
dialog : Dialog ,
77
80
heightInPx : Int? = null,
78
81
widthInPx : Int? = null,
79
- name : String? = null
82
+ name : String? = null,
83
+ maxPixels : Long = RecordBuilderImpl .DEFAULT_MAX_PIXELS ,
80
84
) {
81
85
val window = dialog.window
82
86
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 )
84
88
}
85
89
}
86
90
87
91
fun compareScreenshot (
88
92
holder : RecyclerView .ViewHolder ,
89
93
heightInPx : Int ,
90
94
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)
93
98
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
+ ) {
95
106
disableFlakyComponentsAndWaitForIdle(view)
96
107
97
108
val context = getInstrumentation().targetContext
@@ -107,11 +118,14 @@ interface ScreenshotTest {
107
118
.setExactWidthPx(width)
108
119
.layout()
109
120
}
110
- takeViewSnapshot(name, view)
121
+ takeViewSnapshot(name, view, maxPixels )
111
122
}
112
123
113
124
@RequiresApi(Build .VERSION_CODES .O )
114
- fun compareScreenshot (rule : ComposeTestRule , name : String? = null) {
125
+ fun compareScreenshot (
126
+ rule : ComposeTestRule ,
127
+ name : String? = null,
128
+ ) {
115
129
rule.waitForIdle()
116
130
compareScreenshot(rule.onRoot(), name)
117
131
}
@@ -179,29 +193,31 @@ interface ScreenshotTest {
179
193
180
194
private fun notInAppMainThread () = Looper .myLooper() != Looper .getMainLooper()
181
195
182
- private fun takeViewSnapshot (name : String? , view : View ) {
196
+ private fun takeViewSnapshot (name : String? , view : View , maxPixels : Long ) {
183
197
val testName = name ? : TestNameDetector .getTestName()
184
198
val snapshotName = " ${TestNameDetector .getTestClass()} _$testName "
185
199
try {
186
200
Screenshot
187
201
.snap(view)
188
202
.setIncludeAccessibilityInfo(false )
189
203
.setName(snapshotName)
204
+ .setMaxPixels(maxPixels)
190
205
.record()
191
206
} catch (t: Throwable ) {
192
207
Log .e(" Shot" , " Exception captured while taking screenshot for snapshot with name $snapshotName " , t)
193
208
throw IllegalStateException (" Exception occurred while taking screenshot for snapshot with name $snapshotName " , t)
194
209
}
195
210
}
196
211
197
- private fun takeActivitySnapshot (activity : Activity , name : String? ) {
212
+ private fun takeActivitySnapshot (activity : Activity , name : String? , maxPixels : Long ) {
198
213
val testName = name ? : TestNameDetector .getTestName()
199
214
val snapshotName = " ${TestNameDetector .getTestClass()} _$testName "
200
215
try {
201
216
Screenshot
202
217
.snapActivity(activity)
203
218
.setIncludeAccessibilityInfo(false )
204
219
.setName(snapshotName)
220
+ .setMaxPixels(maxPixels)
205
221
.record()
206
222
} catch (t: Throwable ) {
207
223
Log .e(" Shot" , " Exception captured while taking screenshot for snapshot with name $snapshotName " , t)
0 commit comments