|
|
|
@ -14,8 +14,11 @@
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
@file:OptIn(ExperimentalRoborazziApi::class)
|
|
|
|
|
|
|
|
|
|
package com.google.samples.apps.nowinandroid.core.testing.util
|
|
|
|
|
|
|
|
|
|
import android.graphics.Bitmap.CompressFormat.PNG
|
|
|
|
|
import androidx.activity.ComponentActivity
|
|
|
|
|
import androidx.activity.compose.setContent
|
|
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
@ -30,12 +33,25 @@ import androidx.compose.ui.test.DeviceConfigurationOverride
|
|
|
|
|
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
|
|
|
|
|
import androidx.compose.ui.test.onRoot
|
|
|
|
|
import androidx.test.ext.junit.rules.ActivityScenarioRule
|
|
|
|
|
import com.github.takahirom.roborazzi.ExperimentalRoborazziApi
|
|
|
|
|
import com.github.takahirom.roborazzi.RoborazziATFAccessibilityCheckOptions
|
|
|
|
|
import com.github.takahirom.roborazzi.RoborazziATFAccessibilityChecker
|
|
|
|
|
import com.github.takahirom.roborazzi.RoborazziATFAccessibilityChecker.CheckLevel
|
|
|
|
|
import com.github.takahirom.roborazzi.RoborazziOptions
|
|
|
|
|
import com.github.takahirom.roborazzi.RoborazziOptions.CompareOptions
|
|
|
|
|
import com.github.takahirom.roborazzi.RoborazziOptions.RecordOptions
|
|
|
|
|
import com.github.takahirom.roborazzi.captureRoboImage
|
|
|
|
|
import com.github.takahirom.roborazzi.checkRoboAccessibility
|
|
|
|
|
import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckPreset
|
|
|
|
|
import com.google.android.apps.common.testing.accessibility.framework.AccessibilityViewCheckResult
|
|
|
|
|
import com.google.android.apps.common.testing.accessibility.framework.integrations.espresso.AccessibilityViewCheckException
|
|
|
|
|
import com.google.android.apps.common.testing.accessibility.framework.utils.contrast.BitmapImage
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
|
|
|
|
|
import org.hamcrest.Matcher
|
|
|
|
|
import org.hamcrest.Matchers
|
|
|
|
|
import org.robolectric.RuntimeEnvironment
|
|
|
|
|
import java.io.File
|
|
|
|
|
import java.io.FileOutputStream
|
|
|
|
|
|
|
|
|
|
val DefaultRoborazziOptions =
|
|
|
|
|
RoborazziOptions(
|
|
|
|
@ -52,10 +68,17 @@ enum class DefaultTestDevices(val description: String, val spec: String) {
|
|
|
|
|
}
|
|
|
|
|
fun <A : ComponentActivity> AndroidComposeTestRule<ActivityScenarioRule<A>, A>.captureMultiDevice(
|
|
|
|
|
screenshotName: String,
|
|
|
|
|
accessibilitySuppressions: Matcher<in AccessibilityViewCheckResult> = Matchers.not(Matchers.anything()),
|
|
|
|
|
body: @Composable () -> Unit,
|
|
|
|
|
) {
|
|
|
|
|
DefaultTestDevices.entries.forEach {
|
|
|
|
|
this.captureForDevice(it.description, it.spec, screenshotName, body = body)
|
|
|
|
|
this.captureForDevice(
|
|
|
|
|
deviceName = it.description,
|
|
|
|
|
deviceSpec = it.spec,
|
|
|
|
|
screenshotName = screenshotName,
|
|
|
|
|
body = body,
|
|
|
|
|
accessibilitySuppressions = accessibilitySuppressions,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -64,6 +87,7 @@ fun <A : ComponentActivity> AndroidComposeTestRule<ActivityScenarioRule<A>, A>.c
|
|
|
|
|
deviceSpec: String,
|
|
|
|
|
screenshotName: String,
|
|
|
|
|
roborazziOptions: RoborazziOptions = DefaultRoborazziOptions,
|
|
|
|
|
accessibilitySuppressions: Matcher<in AccessibilityViewCheckResult> = Matchers.not(Matchers.anything()),
|
|
|
|
|
darkMode: Boolean = false,
|
|
|
|
|
body: @Composable () -> Unit,
|
|
|
|
|
) {
|
|
|
|
@ -83,11 +107,46 @@ fun <A : ComponentActivity> AndroidComposeTestRule<ActivityScenarioRule<A>, A>.c
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run Accessibility checks first so logging is included
|
|
|
|
|
val accessibilityException = try {
|
|
|
|
|
this.onRoot().checkRoboAccessibility(
|
|
|
|
|
roborazziATFAccessibilityCheckOptions = RoborazziATFAccessibilityCheckOptions(
|
|
|
|
|
failureLevel = CheckLevel.Error,
|
|
|
|
|
checker = RoborazziATFAccessibilityChecker(
|
|
|
|
|
preset = AccessibilityCheckPreset.LATEST,
|
|
|
|
|
suppressions = accessibilitySuppressions,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
null
|
|
|
|
|
} catch (e: AccessibilityViewCheckException) {
|
|
|
|
|
e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.onRoot()
|
|
|
|
|
.captureRoboImage(
|
|
|
|
|
"src/test/screenshots/${screenshotName}_$deviceName.png",
|
|
|
|
|
roborazziOptions = roborazziOptions,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Rethrow the Accessibility exception once screenshots have passed
|
|
|
|
|
if (accessibilityException != null) {
|
|
|
|
|
accessibilityException.results.forEachIndexed { index, check ->
|
|
|
|
|
val viewImage = check.viewImage
|
|
|
|
|
if (viewImage is BitmapImage) {
|
|
|
|
|
val file = File("build/outputs/roborazzi/${screenshotName}_${deviceName}_$index.png")
|
|
|
|
|
println("Writing check.viewImage to $file")
|
|
|
|
|
FileOutputStream(
|
|
|
|
|
file,
|
|
|
|
|
).use {
|
|
|
|
|
viewImage.bitmap.compress(PNG, 100, it)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw accessibilityException
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|