18
18
*/
19
19
package ca.rmen.android.poetassistant.main
20
20
21
- import android.Manifest
21
+ import android.app.Application
22
22
import android.graphics.Bitmap
23
23
import android.os.SystemClock
24
24
import androidx.test.core.app.ApplicationProvider
25
25
import androidx.test.espresso.Espresso
26
26
import androidx.test.filters.LargeTest
27
27
import androidx.test.platform.app.InstrumentationRegistry
28
- import androidx.test.rule.GrantPermissionRule
29
- import androidx.test.runner.screenshot.BasicScreenCaptureProcessor
28
+ import androidx.test.runner.screenshot.ScreenCapture
30
29
import androidx.test.runner.screenshot.ScreenCaptureProcessor
31
30
import androidx.test.runner.screenshot.Screenshot
32
31
import ca.rmen.android.poetassistant.R
@@ -41,6 +40,7 @@ import org.junit.Rule
41
40
import org.junit.Test
42
41
import org.junit.runner.RunWith
43
42
import org.junit.runners.MethodSorters
43
+ import java.io.File
44
44
import java.io.IOException
45
45
46
46
@@ -56,24 +56,33 @@ class ScreenshotTest {
56
56
@TestParameter
57
57
lateinit var themePreference: ThemePreference
58
58
59
+ private lateinit var deviceScreenshotsFolder: File
60
+
59
61
@JvmField
60
62
@Rule
61
- var activityTestRule: PoetAssistantActivityTestRule <MainActivity > = PoetAssistantActivityTestRule (
63
+ val activityTestRule: PoetAssistantActivityTestRule <MainActivity > = PoetAssistantActivityTestRule (
62
64
MainActivity ::class .java, true
63
65
)
64
66
65
- @JvmField
66
- @Rule
67
- var writeScreenshotRule: GrantPermissionRule = GrantPermissionRule .grant(Manifest .permission.WRITE_EXTERNAL_STORAGE )
68
-
69
67
@Before
70
- fun setupTheme () {
68
+ fun setup () {
69
+ // Set the theme
71
70
InstrumentationRegistry .getInstrumentation().runOnMainSync {
72
71
val settingsPrefs =
73
72
SettingsPrefs (ApplicationProvider .getApplicationContext())
74
73
settingsPrefs.theme = themePreference.value
75
74
setThemeFromSettings(settingsPrefs)
76
75
}
76
+
77
+ // Create a fresh folder to store the screenshots
78
+ deviceScreenshotsFolder = File (
79
+ ApplicationProvider .getApplicationContext<Application >().getExternalFilesDir(null ),
80
+ " screenshots-${themePreference.value} "
81
+ )
82
+ if (deviceScreenshotsFolder.exists()) {
83
+ deviceScreenshotsFolder.deleteRecursively()
84
+ }
85
+ deviceScreenshotsFolder.mkdirs()
77
86
}
78
87
79
88
@Test
@@ -113,6 +122,22 @@ class ScreenshotTest {
113
122
}
114
123
}
115
124
125
+ private inner class Processor : ScreenCaptureProcessor {
126
+ override fun process (capture : ScreenCapture ): String {
127
+ val deviceFile = File (deviceScreenshotsFolder, " ${capture.name} .png" )
128
+ deviceFile.outputStream().use {
129
+ capture.bitmap.compress(
130
+ Bitmap .CompressFormat .PNG ,
131
+ 0 ,
132
+ it,
133
+ )
134
+ }
135
+ return deviceFile.absolutePath
136
+ }
137
+ }
138
+
139
+ private val processor = Processor ()
140
+
116
141
// https://stackoverflow.com/questions/38519568/how-to-take-screenshot-at-the-point-where-test-fail-in-espresso
117
142
private fun takeScreenshot (filename : String ) {
118
143
SystemClock .sleep(500 ) // :(
@@ -121,11 +146,8 @@ class ScreenshotTest {
121
146
capture.setName(filename)
122
147
capture.setFormat(Bitmap .CompressFormat .PNG )
123
148
124
- val processors = HashSet <ScreenCaptureProcessor >()
125
- processors.add(BasicScreenCaptureProcessor ())
126
-
127
149
try {
128
- capture.process(processors )
150
+ capture.process(setOf (processor) )
129
151
} catch (e: IOException ) {
130
152
e.printStackTrace()
131
153
}
0 commit comments