# GlanceAppWidgetUnitTest

Artifact: [androidx.glance:glance-appwidget-testing](https://developer.android.com/jetpack/androidx/releases/glance) [View Source](https://cs.android.com/search?q=file:androidx/glance/appwidget/testing/unit/GlanceAppWidgetUnitTest.kt+class:androidx.glance.appwidget.testing.unit.GlanceAppWidgetUnitTest) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/glance#1.1.0)


```
sealed interface GlanceAppWidgetUnitTest : GlanceNodeAssertionsProvider<MappedNode, GlanceMappedNode>
```

<br />

*** ** * ** ***

Provides methods to enable you to test your logic of building Glance composable content in the `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/package-summary#runGlanceAppWidgetUnitTest(kotlin.time.Duration,kotlin.Function1)` scope.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/package-summary#runGlanceAppWidgetUnitTest(kotlin.time.Duration,kotlin.Function1)` |   |

## Summary

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/GlanceAppWidgetUnitTest#awaitIdle()()` Wait until all recompositions are calculated. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/GlanceAppWidgetUnitTest#provideComposable(kotlin.Function0)(composable: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Sets the Glance composable function to be tested. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/GlanceAppWidgetUnitTest#setAppWidgetSize(androidx.compose.ui.unit.DpSize)(size: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/DpSize)` Sets the size of the appWidget to be assumed for the test. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/GlanceAppWidgetUnitTest#setContext(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Sets the context to be used for the test. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/GlanceAppWidgetUnitTest#setState(kotlin.Any)(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](https://developer.android.com/reference/kotlin/androidx/glance/testing/GlanceNodeAssertionsProvider) `https://developer.android.com/reference/kotlin/androidx/glance/testing/GlanceNodeAssertionCollection<https://developer.android.com/reference/kotlin/androidx/glance/testing/unit/MappedNode, https://developer.android.com/reference/kotlin/androidx/glance/testing/unit/GlanceMappedNode>` `https://developer.android.com/reference/kotlin/androidx/glance/testing/GlanceNodeAssertionsProvider#onAllNodes(androidx.glance.testing.GlanceNodeMatcher)(matcher: https://developer.android.com/reference/kotlin/androidx/glance/testing/GlanceNodeMatcher<https://developer.android.com/reference/kotlin/androidx/glance/testing/unit/MappedNode>)`

Finds all Glance nodes that matches the given condition.
`https://developer.android.com/reference/kotlin/androidx/glance/testing/GlanceNodeAssertion<https://developer.android.com/reference/kotlin/androidx/glance/testing/unit/MappedNode, https://developer.android.com/reference/kotlin/androidx/glance/testing/unit/GlanceMappedNode>` `https://developer.android.com/reference/kotlin/androidx/glance/testing/GlanceNodeAssertionsProvider#onNode(androidx.glance.testing.GlanceNodeMatcher)(matcher: https://developer.android.com/reference/kotlin/androidx/glance/testing/GlanceNodeMatcher<https://developer.android.com/reference/kotlin/androidx/glance/testing/unit/MappedNode>)`

Finds a Glance node that matches the given condition.

## Public functions

### awaitIdle

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/glance#1.1.0)

```
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](https://developer.android.com/jetpack/androidx/releases/glance#1.1.0)

```
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 `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/GlanceAppWidgetUnitTest#provideComposable(kotlin.Function0)` or `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/package-summary#runGlanceAppWidgetUnitTest(kotlin.time.Duration,kotlin.Function1)`.

| Parameters |
|---|---|
| `composable: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | the composable function under test |

### setAppWidgetSize

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/glance#1.2.0)

```
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 `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/GlanceAppWidgetUnitTest#provideComposable(kotlin.Function0)`. Default is `349.dp, 455.dp` that of a 5x4 widget in Pixel 4 portrait mode. See `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/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](https://developer.android.com/develop/ui/views/appwidgets/layouts#anatomy_determining_size). 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](https://developer.android.com/jetpack/androidx/releases/glance#1.1.0)

```
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](https://github.com/robolectric/robolectric) to get the context.

Note: This should be called before calling `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/GlanceAppWidgetUnitTest#provideComposable(kotlin.Function0)`, updates to the state after providing content has no effect

### setState

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/glance#1.1.0)

```
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 `https://developer.android.com/reference/kotlin/androidx/glance/appwidget/testing/unit/GlanceAppWidgetUnitTest#provideComposable(kotlin.Function0)`, 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 : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?>` | type of state used in your `https://developer.android.com/reference/kotlin/androidx/glance/state/GlanceStateDefinition` e.g. `Preferences` if your state definition is `GlanceStateDefinition<Preferences>` |
| `state: T` | the state to be used for testing the composable. |