Skip to content

Instantly share code, notes, and snippets.

View ngallazzi's full-sized avatar
🎓
Learning by repetition

Nicola Gallazzi ngallazzi

🎓
Learning by repetition
View GitHub Profile
@ngallazzi
ngallazzi / test_tag.kt
Created January 4, 2024 09:12
test_tag
LazyColumn(modifier = Modifier.testTag("myLazyColumn")) {
// any content
}
enum class SwitchStatus(@DrawableRes val imageRes: Int) {
ON(R.drawable.pacman_lamp_on), OFF(R.drawable.pacman_lamp_off)
}
@Test
fun testButtonOff_setsOffState() {
val context = composeTestRule.activity
val onButtonNodeIdentifier = context.getString(R.string.on_button_description)
val offButtonNodeIdentifier = context.getString(R.string.off_button_description)
val currentStatusNodeIdentifier = context.getString(R.string.current_status_description)
// Start the app
composeTestRule.setContent {
SemanticsTestingPlaygroundTheme {
SwitchLayout()
<resources>
<string name="app_name">Semantics Testing Playground</string>
<string name="pacman_lamp_image_description">Pacman lamp image</string>
<string name="on_button_description">On button</string>
<string name="off_button_description">Off button</string>
<string name="current_status_description">Status description</string>
</resources>
class SwitchLayoutKtTest {
// Create test rule, we need android context so we use createAndroidComposeRule factory method
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
@Test
fun testButtonOn_setsOnState() {
val context = composeTestRule.activity
val onButtonNodeIdentifier = context.getString(R.string.on_button_description)
val currentStatusNodeIdentifier = context.getString(R.string.current_status_description)
@ngallazzi
ngallazzi / switch_layout.kt
Created December 29, 2023 15:25
Switch layout
@Composable
fun SwitchLayout() {
val context = LocalContext.current
var status: SwitchStatus by remember {
mutableStateOf(SwitchStatus.OFF)
}
Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.padding(32.dp)) {
Image(
modifier = Modifier
.width(196.dp)
class MyLayoutTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
@Test
fun testGreetingButtonClick() {
composeTestRule.setContent {
MyLayout()
}
composeTestRule.onNode(hasParent(hasText(“Greeting Button”))
@ngallazzi
ngallazzi / compose_actions.kt
Created December 29, 2023 14:44
Common compose Ui tests actions
class MyLayoutTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
@Test
fun testGreetingButtonClick() {
composeTestRule.setContent {
MyLayout()
}
composeTestRule.onNodeWithContentDescription(
@ngallazzi
ngallazzi / compose_asserts.kt
Created December 29, 2023 14:37
compose tests asserts
class MyLayoutTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
@Test
fun testGreetingButtonExists() {
// Start the app
composeTestRule.setContent {
MyLayout()
@ngallazzi
ngallazzi / compose_finders.kt
Created December 29, 2023 14:34
compose_ui_finders
class MyLayoutTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
@Test
fun testGreetingButtonClick() {
// Start the app
composeTestRule.setContent {
MyLayout()
}