Skip to content

Commit 83b245a

Browse files
committed
feat(compose): Add Compose testing common patterns snippet
1 parent 97d93a1 commit 83b245a

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.android.example.compose.testing
18+
19+
import androidx.activity.ComponentActivity
20+
import androidx.compose.material3.Text
21+
import androidx.compose.runtime.Composable
22+
import androidx.compose.ui.test.junit4.createAndroidComposeRule
23+
import androidx.compose.ui.test.onNodeWithText
24+
import androidx.compose.ui.test.performClick
25+
import org.junit.Rule
26+
import org.junit.Test
27+
import com.example.compose.snippets.R
28+
29+
class CommonPatternsSample {
30+
31+
@get:Rule
32+
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
33+
34+
@Test
35+
fun myTest() {
36+
// [START android_snippets_compose_testing_common_patterns]
37+
// Start the app
38+
composeTestRule.setContent {
39+
MyAppTheme {
40+
MainScreen(uiState = exampleUiState, /*...*/)
41+
}
42+
}
43+
val continueLabel = composeTestRule.activity.getString(R.string.next)
44+
composeTestRule.onNodeWithText(continueLabel).performClick()
45+
// [END android_snippets_compose_testing_common_patterns]
46+
}
47+
}
48+
49+
@Composable
50+
fun MyAppTheme(content: @Composable () -> Unit) {
51+
content()
52+
}
53+
54+
@Composable
55+
fun MainScreen(uiState: ExampleUiState) {
56+
Text(text = "Hello ${uiState.name}")
57+
Text(text = "Next")
58+
}
59+
60+
data class ExampleUiState(val name: String)
61+
62+
val exampleUiState = ExampleUiState("world")

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
-->
1717
<resources>
1818
<string name="app_name">snippets</string>
19+
<string name="next" translatable="false">Next</string>
1920
<string name="dog_content_description">Golden Retriever in fall leaves</string>
2021
<string name="shopping_cart_content_desc">Shopping cart</string>
2122
<string name="bus_content_description">Bus</string>

0 commit comments

Comments
 (0)