GlanceAppWidgetUnitTest


sealed interface GlanceAppWidgetUnitTest : GlanceNodeAssertionsProvider


Provides methods to enable you to test your logic of building Glance composable content in the runGlanceAppWidgetUnitTest scope.

Summary

Public functions

Unit

Wait until all recompositions are calculated.

Unit
provideComposable(composable: @Composable () -> Unit)

Sets the Glance composable function to be tested.

Unit

Sets the size of the appWidget to be assumed for the test.

Unit
setContext(context: Context)

Sets the context to be used for the test.

Unit
<T : Any?> setState(state: T)

Sets the state to be used for the test if your composable under test accesses it via currentState<*>() or LocalState.current.

Inherited functions

From androidx.glance.testing.GlanceNodeAssertionsProvider
GlanceNodeAssertionCollection<MappedNodeGlanceMappedNode>

Finds all Glance nodes that matches the given condition.

GlanceNodeAssertion<MappedNodeGlanceMappedNode>

Finds a Glance node that matches the given condition.

Public functions

awaitIdle

Added in 1.1.0-beta02
fun awaitIdle(): Unit

Wait until all recompositions are calculated. For example if you have LaunchedEffect with delays in your composable.

provideComposable

Added in 1.1.0-beta02
fun provideComposable(composable: @Composable () -> Unit): Unit

Sets the Glance composable function to be tested. Each unit test should test a composable in isolation and assume specific state as input. Prefer keeping composables side-effects free. Perform any state changes needed for the test before calling provideComposable or runGlanceAppWidgetUnitTest.

Parameters
composable: @Composable () -> Unit

the composable function under test

setAppWidgetSize

fun setAppWidgetSize(size: DpSize): Unit

Sets the size of the appWidget to be assumed for the test. This corresponds to the LocalSize.current composition local. If you are accessing the local size, you must call this method to set the intended size for the test.

Note: This should be called before calling provideComposable. Default is 349.dp, 455.dp that of a 5x4 widget in Pixel 4 portrait mode. See GlanceAppWidgetUnitTestDefaults.size

  1. If your appWidget uses sizeMode == Single, you can set this to the minWidth and minHeight set in your appwidget info xml.

  2. If your appWidget uses sizeMode == Exact, you can identify the sizes to test looking at the documentation on Determine a size for your widget. and identifying landscape and portrait sizes that your widget may appear on.

  3. If your appWidget uses sizeMode == Responsive, you can set this to one of the sizes from the list that you provide when specifying the sizeMode.

setContext

Added in 1.1.0-beta02
fun setContext(context: Context): Unit

Sets the context to be used for the test.

It is optional to call this method. However, you must set this if your composable needs access to LocalContext. You may need to use a Android unit test framework such as Robolectric to get the context.

Note: This should be called before calling provideComposable, updates to the state after providing content has no effect

setState

Added in 1.1.0-beta02
fun <T : Any?> setState(state: T): Unit

Sets the state to be used for the test if your composable under test accesses it via currentState<*>() or LocalState.current.

Default state is null. Note: This should be called before calling provideComposable, updates to the state after providing content has no effect. This matches the appWidget behavior where you need to call update on the widget for state changes to take effect.

Parameters
<T : Any?>

type of state used in your GlanceStateDefinition e.g. Preferences if your state definition is GlanceStateDefinition<Preferences>

state: T

the state to be used for testing the composable.