Skip to content

Commit 41aeef1

Browse files
committed
feat: Add MyComposeTest snippet
Region-Tag: android_snippets_compose_testing_common_patterns
1 parent 83b245a commit 41aeef1

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

compose/snippets/src/androidTest/java/com/android/example/compose/testing/CommonPatternsSample.kt renamed to compose/snippets/src/androidTest/java/com/android/example/compose/testing/MyComposeTest.kt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,41 @@
1717
package com.android.example.compose.testing
1818

1919
import androidx.activity.ComponentActivity
20+
import androidx.compose.foundation.clickable
2021
import androidx.compose.material3.Text
2122
import androidx.compose.runtime.Composable
23+
import androidx.compose.runtime.getValue
24+
import androidx.compose.runtime.mutableStateOf
25+
import androidx.compose.runtime.remember
26+
import androidx.compose.runtime.setValue
27+
import androidx.compose.ui.Modifier
28+
import androidx.compose.ui.test.assertIsDisplayed
2229
import androidx.compose.ui.test.junit4.createAndroidComposeRule
2330
import androidx.compose.ui.test.onNodeWithText
2431
import androidx.compose.ui.test.performClick
2532
import org.junit.Rule
2633
import org.junit.Test
27-
import com.example.compose.snippets.R
2834

29-
class CommonPatternsSample {
35+
// [START android_snippets_compose_testing_common_patterns]
36+
class MyComposeTest {
3037

3138
@get:Rule
3239
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
3340

3441
@Test
3542
fun myTest() {
36-
// [START android_snippets_compose_testing_common_patterns]
3743
// Start the app
3844
composeTestRule.setContent {
3945
MyAppTheme {
40-
MainScreen(uiState = exampleUiState, /*...*/)
46+
MainScreen(uiState = exampleUiState)
4147
}
4248
}
43-
val continueLabel = composeTestRule.activity.getString(R.string.next)
44-
composeTestRule.onNodeWithText(continueLabel).performClick()
45-
// [END android_snippets_compose_testing_common_patterns]
49+
composeTestRule.onNodeWithText("Continue").performClick()
50+
51+
composeTestRule.onNodeWithText("Welcome").assertIsDisplayed()
4652
}
4753
}
54+
// [END android_snippets_compose_testing_common_patterns]
4855

4956
@Composable
5057
fun MyAppTheme(content: @Composable () -> Unit) {
@@ -53,10 +60,15 @@ fun MyAppTheme(content: @Composable () -> Unit) {
5360

5461
@Composable
5562
fun MainScreen(uiState: ExampleUiState) {
56-
Text(text = "Hello ${uiState.name}")
57-
Text(text = "Next")
63+
var showWelcome by remember { mutableStateOf(false) }
64+
if (showWelcome) {
65+
Text(text = "Welcome")
66+
} else {
67+
Text(text = "Hello ${uiState.name}")
68+
Text(text = "Continue", modifier = Modifier.clickable { showWelcome = true })
69+
}
5870
}
5971

6072
data class ExampleUiState(val name: String)
6173

62-
val exampleUiState = ExampleUiState("world")
74+
val exampleUiState = ExampleUiState("world")

compose/snippets/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
-->
1717
<resources>
1818
<string name="app_name">snippets</string>
19-
<string name="next" translatable="false">Next</string>
19+
<string name="next" translatable="false">Continue</string>
20+
<string name="welcome" translatable="false">Welcome</string>
2021
<string name="dog_content_description">Golden Retriever in fall leaves</string>
2122
<string name="shopping_cart_content_desc">Shopping cart</string>
2223
<string name="bus_content_description">Bus</string>

0 commit comments

Comments
 (0)