Is it possible to write ui tests/preview with Glance Compose?

93 Views Asked by At

I have started to implement widgets by using Glance library, I am quite new to Glance and would like to use Preview annotation and write some ui tests is it possible? I could not find much source. Thanks in advance!

1

There are 1 best solutions below

0
Summers Pittman On

Yes it is possible, but the feature is still in preview. First, you will need to be using Android Studio Jellyfish. Then, you will need to add the SNAPSHOT versions of Glance to your project. Finally, you can annotate your Glance Composables with @androidx.glance.preview.Preview(Surfaces.APP_WIDGET)

To use the snapshot versions first you need to use a snapshot build. You can add the build's maven repository to your settings.gradle file. You can find newer build numbers at https://androidx.dev/snapshots/builds, but the following works for me.

maven ( "https://androidx.dev/snapshots/builds/11347455/artifacts/repository")

Then you will need to add/update your Glance dependencies.

    implementation("androidx.glance:glance-preview:1.0.0-SNAPSHOT")
    implementation("androidx.glance:glance-appwidget-preview:1.0.0-SNAPSHOT")
    implementation( "androidx.glance:glance-appwidget:1.1.0-SNAPSHOT")
    implementation( "androidx.glance:glance-material3:1.1.0-SNAPSHOT" )

And now you should be able to annotate your Glance Composable

    @OptIn(ExperimentalGlancePreviewApi::class)
    @androidx.glance.preview.Preview(Surfaces.APP_WIDGET)
    @Composable
    fun MyGlanceContentWidget() {
        GlanceTheme(colors = GlanceColorScheme.colors) {
                PreviewContent()
        }
    }

If done correctly, you should be able to open your preview pane and select "Glance app widgets" from the drop down to see your widget.

Android Studio Preview with Glance widget.