/
DropshotsScreenshotTestRuleForView.kt
100 lines (84 loc) · 3.61 KB
/
DropshotsScreenshotTestRuleForView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package sergio.sastre.uitesting.dropshots
import android.app.Dialog
import android.content.Context
import android.view.View
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.dropbox.dropshots.Dropshots
import org.junit.rules.RuleChain
import org.junit.runner.Description
import org.junit.runners.model.Statement
import sergio.sastre.uitesting.utils.activityscenario.ActivityScenarioForViewRule
import sergio.sastre.uitesting.utils.crosslibrary.config.LibraryConfig
import sergio.sastre.uitesting.utils.crosslibrary.config.ScreenshotConfigForView
import sergio.sastre.uitesting.utils.crosslibrary.testrules.ScreenshotTestRuleForView
import sergio.sastre.uitesting.utils.utils.waitForMeasuredView as androidUiTestingUtilsMeasuredView
import sergio.sastre.uitesting.utils.utils.waitForMeasuredViewHolder as androidUiTestingUtilsMeasuredViewHolder
import sergio.sastre.uitesting.utils.utils.waitForMeasuredDialog as androidUiTestingUtilsMeasuredDialog
class DropshotsScreenshotTestRuleForView(
override val config: ScreenshotConfigForView = ScreenshotConfigForView(),
) : ScreenshotTestRuleForView(config) {
private val activityScenarioForViewRule by lazy {
ActivityScenarioForViewRule(
config = config.toViewConfig(),
backgroundColor = dropshotsConfig.backgroundColor,
)
}
private val dropshotsRule: ScreenshotTaker by lazy {
ScreenshotTaker(
Dropshots(
resultValidator = dropshotsConfig.resultValidator,
imageComparator = dropshotsConfig.imageComparator,
)
)
}
private var dropshotsConfig: DropshotsConfig = DropshotsConfig()
override fun apply(
base: Statement,
description: Description,
): Statement =
RuleChain
.outerRule(dropshotsRule)
.around(activityScenarioForViewRule)
.apply(base, description)
private val Dialog.view
get() = window!!.decorView
override val context: Context
get() = activityScenarioForViewRule.activity
override fun inflate(layoutId: Int): View =
activityScenarioForViewRule.inflateAndWaitForIdle(layoutId)
override fun waitForMeasuredView(actionToDo: () -> View): View =
androidUiTestingUtilsMeasuredView { actionToDo() }
override fun waitForMeasuredDialog(actionToDo: () -> Dialog): Dialog =
androidUiTestingUtilsMeasuredDialog { actionToDo() }
override fun waitForMeasuredViewHolder(actionToDo: () -> ViewHolder): ViewHolder =
androidUiTestingUtilsMeasuredViewHolder { actionToDo() }
override fun snapshotDialog(name: String?, dialog: Dialog) {
dropshotsRule.assertSnapshot(
dialog = dialog,
bitmapCaptureMethod = dropshotsConfig.bitmapCaptureMethod,
name = name,
filePath = dropshotsConfig.filePath,
)
}
override fun snapshotView(name: String?, view: View) {
dropshotsRule.assertSnapshot(
view = view,
bitmapCaptureMethod = dropshotsConfig.bitmapCaptureMethod,
name = name,
filePath = dropshotsConfig.filePath,
)
}
override fun snapshotViewHolder(name: String?, viewHolder: ViewHolder) {
dropshotsRule.assertSnapshot(
view = viewHolder.itemView,
bitmapCaptureMethod = dropshotsConfig.bitmapCaptureMethod,
name = name,
filePath = dropshotsConfig.filePath,
)
}
override fun configure(config: LibraryConfig): ScreenshotTestRuleForView = apply {
if (config is DropshotsConfig) {
dropshotsConfig = config
}
}
}