Skip to content

Commit e8e51b3

Browse files
authored
add Glance unit testing snippet (#634)
* add Glance unit testing snippet * Apply Spotless
1 parent 27c462c commit e8e51b3

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

compose/snippets/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,6 @@ dependencies {
163163
androidTestImplementation(libs.androidx.compose.ui.test.junit4.accessibility)
164164
debugImplementation(libs.androidx.compose.ui.test.manifest)
165165

166+
androidTestImplementation(libs.androidx.glance.testing)
167+
androidTestImplementation(libs.androidx.glance.appwidget.testing)
166168
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compose.snippets.glance
18+
19+
import androidx.compose.runtime.Composable
20+
import androidx.glance.GlanceModifier
21+
import androidx.glance.appwidget.testing.unit.runGlanceAppWidgetUnitTest
22+
import androidx.glance.layout.Row
23+
import androidx.glance.semantics.semantics
24+
import androidx.glance.semantics.testTag
25+
import androidx.glance.testing.unit.assertHasText
26+
import androidx.glance.testing.unit.hasTestTag
27+
import androidx.glance.text.Text
28+
import org.junit.Test
29+
30+
// [START android_compose_glance_unit_test]
31+
private const val FAKE_HEADLINE = "EXTRA! EXTRA! READ ALL ABOUT IT!"
32+
33+
class MyGlanceComposableTest {
34+
@Test
35+
fun myNewsItemComposable_largeSize_hasHeadline() = runGlanceAppWidgetUnitTest {
36+
// Set the composable to test
37+
provideComposable {
38+
MyNewsItemComposable(FAKE_HEADLINE)
39+
}
40+
41+
// Perform assertions
42+
onNode(hasTestTag("headline"))
43+
.assertHasText(FAKE_HEADLINE)
44+
}
45+
46+
47+
@Composable
48+
fun MyNewsItemComposable(headline: String) {
49+
Row {
50+
Text(
51+
text = headline,
52+
modifier = GlanceModifier.semantics { testTag = "headline" },
53+
)
54+
}
55+
}
56+
}
57+
// [END android_compose_glance_unit_test]

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ androidx-emoji2-views = { module = "androidx.emoji2:emoji2-views", version.ref =
126126
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "androidx-fragment-ktx" }
127127
androidx-glance-appwidget = { module = "androidx.glance:glance-appwidget", version.ref = "androidx-glance-appwidget" }
128128
androidx-glance-material3 = { module = "androidx.glance:glance-material3", version.ref = "androidx-glance-appwidget" }
129+
androidx-glance-testing = { group = "androidx.glance", name = "glance-testing", version.ref = "androidx-glance-appwidget" }
130+
androidx-glance-appwidget-testing = { group = "androidx.glance", name = "glance-appwidget-testing", version.ref = "androidx-glance-appwidget" }
129131
androidx-graphics-shapes = "androidx.graphics:graphics-shapes:1.0.1"
130132
androidx-hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "androidxHiltNavigationCompose" }
131133
androidx-lifecycle-runtime = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "androidx-lifecycle-runtime-compose" }

0 commit comments

Comments
 (0)