-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
ScreenshotActivityTestRule.kt
67 lines (57 loc) 路 2.32 KB
/
ScreenshotActivityTestRule.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
package openfoodfacts.github.scrachx.openfood.test
import android.app.Activity
import android.content.Context
import android.util.Log
import androidx.preference.PreferenceManager
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import openfoodfacts.github.scrachx.openfood.utils.LocaleManager
import openfoodfacts.github.scrachx.openfood.utils.PreferencesManager
import org.apache.commons.lang3.StringUtils
import org.junit.Assert
class ScreenshotActivityTestRule<T : Activity?>
@JvmOverloads constructor(
activityClass: Class<T>,
var name: String = activityClass.simpleName,
val context: Context,
private val localeManager: LocaleManager,
) : ActivityTestRule<T>(activityClass, false, false) {
var afterActivityLaunchedAction: ((ScreenshotActivityTestRule<T>) -> Unit)? = null
var beforeActivityStartedAction: ((ScreenshotActivityTestRule<T>) -> Unit)? = null
var firstTimeLaunched = false
var screenshotParameter: ScreenshotParameter? = null
@Suppress("DEPRECATION")
override fun beforeActivityLaunched() {
try {
runOnUiThread {
PreferencesManager(context).isFirstTimeLaunch = firstTimeLaunched
localeManager.saveLanguageToPrefs(
InstrumentationRegistry.getInstrumentation().targetContext,
screenshotParameter!!.locale,
)
}
} catch (throwable: Throwable) {
throwable.printStackTrace()
Assert.fail(throwable.message)
}
beforeActivityStartedAction?.invoke(this)
}
override fun afterActivityLaunched() {
try {
afterActivityLaunchedAction?.invoke(this)
Thread.sleep(MILLIS_TO_WAIT_TO_DISPLAY_ACTIVITY.toLong())
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
takeScreenshot()
finishActivity()
} catch (throwable: Throwable) {
Log.e(ScreenshotActivityTestRule::class.simpleName, "run on ui", throwable)
}
}
@JvmOverloads
fun takeScreenshot(suffix: String? = StringUtils.EMPTY) {
ScreenshotTaker().takeScreenshot(screenshotParameter!!, suffix!!, this)
}
companion object {
const val MILLIS_TO_WAIT_TO_DISPLAY_ACTIVITY = 5000
}
}