A simple activity to render a Glance composable without binding an appwidget for screenshot testing. It provides functions that can be called to initialize and render the Glance composable in it.
repositories {
mavenCentral()
}
dependencies {
debugImplementation "com.google.android.glance.tools:appwidget-testing:<version>"
}
Define an ActivityScenarioRule
in your test class for the GlanceScreenshotTestActivity
.
@get:Rule
val activityScenarioRule =
ActivityScenarioRule(GlanceScreenshotTestActivity::class.java)
Initialize the size and state for your composable in the onActivity
runner and provide the
composable to be rendered in the GlanceScreenshotTestActivity
.
activityScenarioRule.scenario.onActivity {
it.setAppWidgetSize(size)
it.setState(preferencesOf(myKey to 2))
it.renderComposable {
MyGlanceContent()
}
}
Then, using screenshot testing tool of your choice capture and compare the screenshot of the activity. For example, following sample uses Roborazzi capture and verify the screenshot.
NOTE: The device and screenshot framework you use should support hardware acceleration and
clipToOutline
to see rounded corners. For robolectric, see this
issue. When using
an emulator, you may use Espresso's captureToBitmap
to ensure that the corner radius is captured.
Espresso.onView(ViewMatchers.isRoot())
.captureRoboImage(
filePath = "src/test/resources/golden/$goldenFileName.png",
roborazziOptions = RoborazziOptions(
compareOptions = RoborazziOptions.CompareOptions(
changeThreshold = 0F
)
)
)
For a complete example, see SampleGlanceScreenshotTest.