Skip to content

Commit 29341a1

Browse files
add TextField tests
1 parent 170bd73 commit 29341a1

1 file changed

Lines changed: 114 additions & 21 deletions

File tree

  • Tutorial2-2UI-Testing/src/androidTest/java/com/smarttoolfactory/tutorial2_2ui_testing

Tutorial2-2UI-Testing/src/androidTest/java/com/smarttoolfactory/tutorial2_2ui_testing/Test2TextField.kt

Lines changed: 114 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.smarttoolfactory.tutorial2_2ui_testing
22

33
import androidx.compose.material3.Text
44
import androidx.compose.material3.TextField
5+
import androidx.compose.runtime.Composable
56
import androidx.compose.runtime.getValue
67
import androidx.compose.runtime.mutableStateOf
78
import androidx.compose.runtime.remember
@@ -14,7 +15,9 @@ import androidx.compose.ui.test.assertTextEquals
1415
import androidx.compose.ui.test.hasText
1516
import androidx.compose.ui.test.junit4.createComposeRule
1617
import androidx.compose.ui.test.onNodeWithContentDescription
18+
import androidx.compose.ui.test.performTextClearance
1719
import androidx.compose.ui.test.performTextInput
20+
import androidx.compose.ui.test.performTextReplacement
1821
import org.junit.Rule
1922
import org.junit.Test
2023

@@ -24,28 +27,10 @@ class Test2TextField {
2427
val composeTestRule = createComposeRule()
2528

2629
@Test
27-
fun enter_text_TextFieldTest() {
30+
fun performTextInput_TextField_Test() {
2831

2932
composeTestRule.setContent {
30-
var text by remember {
31-
mutableStateOf("")
32-
}
33-
34-
TextField(
35-
modifier = Modifier.semantics {
36-
contentDescription = "input"
37-
},
38-
label = {
39-
Text("Label Text")
40-
},
41-
placeholder = {
42-
Text("Placeholder Text")
43-
},
44-
value = text,
45-
onValueChange = {
46-
text = it
47-
}
48-
)
33+
TextFieldSample()
4934
}
5035

5136
// set TextField text
@@ -70,7 +55,115 @@ class Test2TextField {
7055
.assertTextEquals("Label Text", "100")
7156

7257
// 🔥 This is for displaying TextField, do not use sleep in actual tests
73-
Thread.sleep(2000)
58+
Thread.sleep(1000)
7459
}
7560

61+
@Test
62+
fun performTextInput_twice_TextField_Test() {
63+
64+
composeTestRule.setContent {
65+
TextFieldSample()
66+
}
67+
68+
// set TextField text
69+
composeTestRule
70+
.onNodeWithContentDescription("input")
71+
// Sends the given text to this node in similar way to IME.
72+
.performTextInput("100")
73+
74+
75+
// add another char to TextField text
76+
composeTestRule
77+
.onNodeWithContentDescription("input")
78+
// Sends the given text to this node in similar way to IME.
79+
.performTextInput("1")
80+
81+
// assert that text is set
82+
composeTestRule.onNodeWithContentDescription("input")
83+
.assert(hasText("1001", ignoreCase = true))
84+
85+
// 🔥 This is for displaying TextField, do not use sleep in actual tests
86+
Thread.sleep(1000)
87+
}
88+
89+
@Test
90+
fun performTextReplacement_TextField_Test() {
91+
92+
composeTestRule.setContent {
93+
TextFieldSample()
94+
}
95+
96+
// set TextField text
97+
composeTestRule
98+
.onNodeWithContentDescription("input")
99+
// Sends the given text to this node in similar way to IME.
100+
.performTextInput("100")
101+
102+
103+
// replace TextField text
104+
composeTestRule
105+
.onNodeWithContentDescription("input")
106+
// Replaces existing text with the given text in this node in similar way to IME.
107+
//This does not reflect text selection. All the text gets cleared out and new inserted.
108+
.performTextReplacement("200")
109+
110+
// assert that text is set
111+
composeTestRule.onNodeWithContentDescription("input")
112+
.assert(hasText("200", ignoreCase = true))
113+
114+
// 🔥 This is for displaying TextField, do not use sleep in actual tests
115+
Thread.sleep(1000)
116+
}
117+
118+
@Test
119+
fun performTextClearance_TextField_Test() {
120+
121+
composeTestRule.setContent {
122+
TextFieldSample()
123+
}
124+
125+
// set TextField text
126+
composeTestRule
127+
.onNodeWithContentDescription("input")
128+
// Sends the given text to this node in similar way to IME.
129+
.performTextInput("100")
130+
131+
132+
// replace TextField text
133+
composeTestRule
134+
.onNodeWithContentDescription("input")
135+
// Clears the text in this node in similar way to IME.
136+
.performTextClearance()
137+
138+
// assert that text is set
139+
composeTestRule.onNodeWithContentDescription("input")
140+
.assert(hasText("", ignoreCase = true))
141+
142+
// 🔥 This is for displaying TextField, do not use sleep in actual tests
143+
Thread.sleep(1000)
144+
}
145+
146+
}
147+
148+
@Composable
149+
private fun TextFieldSample() {
150+
var text by remember {
151+
mutableStateOf("")
152+
}
153+
154+
TextField(
155+
modifier = Modifier.semantics {
156+
contentDescription = "input"
157+
},
158+
label = {
159+
Text("Label Text")
160+
},
161+
placeholder = {
162+
Text("Placeholder Text")
163+
},
164+
value = text,
165+
onValueChange = {
166+
text = it
167+
}
168+
)
76169
}

0 commit comments

Comments
 (0)