@@ -63,6 +63,8 @@ open class BaseWindowTextFieldTest {
6363 internal fun <S : TextFieldTestScope > runTextFieldTest (
6464 textFieldKind : TextFieldKind <S >,
6565 name : String ,
66+ initialText : String = "",
67+ initialSelection : TextRange = TextRange .Zero ,
6668 body : suspend S .() -> Unit
6769 ) = runApplicationTest {
6870 var scope: S ? = null
@@ -75,7 +77,12 @@ open class BaseWindowTextFieldTest {
7577 modifier = Modifier .fillMaxSize()
7678 ) {
7779 if (scope == null ) {
78- scope = textFieldKind.createScope(this @runApplicationTest, window)
80+ scope = textFieldKind.createScope(
81+ windowTestScope = this @runApplicationTest,
82+ window = window,
83+ initialText = initialText,
84+ initialSelection = initialSelection
85+ )
7986 }
8087 Column (horizontalAlignment = Alignment .CenterHorizontally ) {
8188 Text (" $name ($scope )" )
@@ -153,10 +160,17 @@ open class BaseWindowTextFieldTest {
153160
154161 internal abstract class TextField1Scope (
155162 windowTestScope : WindowTestScope ,
156- window : ComposeWindow
163+ window : ComposeWindow ,
164+ initialText : String ,
165+ initialSelection : TextRange ,
157166 ): TextFieldTestScope(windowTestScope, window) {
158167
159- protected var textFieldValue by mutableStateOf(TextFieldValue ())
168+ protected var textFieldValue by mutableStateOf(
169+ TextFieldValue (
170+ text = initialText,
171+ selection = initialSelection,
172+ )
173+ )
160174
161175 override val text: String
162176 get() = textFieldValue.text
@@ -175,9 +189,14 @@ open class BaseWindowTextFieldTest {
175189
176190 internal abstract class TextField2Scope (
177191 windowTestScope : WindowTestScope ,
178- window : ComposeWindow
192+ window : ComposeWindow ,
193+ initialText : String ,
194+ initialSelection : TextRange ,
179195 ): TextFieldTestScope(windowTestScope, window) {
180- protected val textFieldState = TextFieldState ()
196+ protected val textFieldState = TextFieldState (
197+ initialText = initialText,
198+ initialSelection = initialSelection,
199+ )
181200 var inputTransformation: InputTransformation ? by mutableStateOf(null )
182201
183202 override val text: String
@@ -198,99 +217,115 @@ open class BaseWindowTextFieldTest {
198217 windowTestScope : WindowTestScope ,
199218 window : ComposeWindow ,
200219 textObfuscationMode : TextObfuscationMode ,
201- ): TextField2Scope(windowTestScope, window) {
220+ initialText : String = " " ,
221+ initialSelection : TextRange = TextRange .Zero ,
222+ ): TextField2Scope(windowTestScope, window, initialText, initialSelection) {
202223
203224 var textObfuscationMode by mutableStateOf(textObfuscationMode)
204225
205226 }
206227
207228 internal fun interface TextFieldKind <S : TextFieldTestScope > {
208- fun createScope (windowTestScope : WindowTestScope , window : ComposeWindow ): S
229+ fun createScope (
230+ windowTestScope : WindowTestScope ,
231+ window : ComposeWindow ,
232+ initialText : String ,
233+ initialSelection : TextRange ,
234+ ): S
209235 }
210236
211237 companion object {
212238 @JvmField
213239 @DataPoint
214- internal val TextField1 = TextFieldKind <TextField1Scope > { windowTestScope, window ->
215- object : TextField1Scope (windowTestScope, window) {
216- @Composable
217- override fun TextField () {
218- val focusRequester = remember { FocusRequester () }
219- BasicTextField (
220- value = textFieldValue,
221- onValueChange = {
222- textFieldValue = it
223- },
224- onTextLayout = { textLayoutResult = it },
225- modifier = Modifier
226- .focusRequester(focusRequester)
227- .onPlaced {
228- textBoundingBox = it.boundsInWindow()
229- }
230- )
231-
232- LaunchedEffect (focusRequester) {
233- focusRequester.requestFocus()
240+ internal val TextField1 = TextFieldKind <TextField1Scope > {
241+ windowTestScope, window, initialText, initialSelection ->
242+ object : TextField1Scope (windowTestScope, window, initialText, initialSelection) {
243+ @Composable
244+ override fun TextField () {
245+ val focusRequester = remember { FocusRequester () }
246+ BasicTextField (
247+ value = textFieldValue,
248+ onValueChange = {
249+ textFieldValue = it
250+ },
251+ onTextLayout = { textLayoutResult = it },
252+ modifier = Modifier
253+ .focusRequester(focusRequester)
254+ .onPlaced {
255+ textBoundingBox = it.boundsInWindow()
256+ }
257+ )
258+
259+ LaunchedEffect (focusRequester) {
260+ focusRequester.requestFocus()
261+ }
234262 }
235- }
236263
237- override fun toString () = " TextField1"
238- }
264+ override fun toString () = " TextField1"
265+ }
239266 }
240267
241268 @JvmField
242269 @DataPoint
243- internal val TextField2 = TextFieldKind <TextField2Scope > { windowTestScope, window ->
244- object : TextField2Scope (windowTestScope, window) {
245- @Composable
246- override fun TextField () {
247- val focusRequester = remember { FocusRequester () }
248- BasicTextField (
249- state = textFieldState,
250- inputTransformation = inputTransformation,
251- onTextLayout = { textLayoutResultGetter = it },
252- modifier = Modifier
253- .focusRequester(focusRequester)
254- .onPlaced {
255- textBoundingBox = it.boundsInWindow()
256- }
257- )
258-
259- LaunchedEffect (focusRequester) {
260- focusRequester.requestFocus()
270+ internal val TextField2 = TextFieldKind <TextField2Scope > {
271+ windowTestScope, window, initialText, initialSelection ->
272+ object : TextField2Scope (windowTestScope, window, initialText, initialSelection) {
273+ @Composable
274+ override fun TextField () {
275+ val focusRequester = remember { FocusRequester () }
276+ BasicTextField (
277+ state = textFieldState,
278+ inputTransformation = inputTransformation,
279+ onTextLayout = { textLayoutResultGetter = it },
280+ modifier = Modifier
281+ .focusRequester(focusRequester)
282+ .onPlaced {
283+ textBoundingBox = it.boundsInWindow()
284+ }
285+ )
286+
287+ LaunchedEffect (focusRequester) {
288+ focusRequester.requestFocus()
289+ }
261290 }
262- }
263291
264- override fun toString () = " TextField2"
265- }
292+ override fun toString () = " TextField2"
293+ }
266294 }
267295
268296 @JvmField
269297 @DataPoint
270- internal val SecureTextField = TextFieldKind <SecureTextFieldScope > { windowTestScope, window ->
271- object : SecureTextFieldScope (windowTestScope, window, TextObfuscationMode .Hidden ) {
272- @Composable
273- override fun TextField () {
274- val focusRequester = remember { FocusRequester () }
275-
276- BasicSecureTextField (
277- state = textFieldState,
278- textObfuscationMode = textObfuscationMode,
279- onTextLayout = { textLayoutResultGetter = it },
280- modifier = Modifier
281- .focusRequester(focusRequester)
282- .onPlaced {
283- textBoundingBox = it.boundsInWindow()
284- }
285- )
286-
287- LaunchedEffect (focusRequester) {
288- focusRequester.requestFocus()
298+ internal val SecureTextField = TextFieldKind <SecureTextFieldScope > {
299+ windowTestScope, window, initialText, initialSelection ->
300+ object : SecureTextFieldScope (
301+ windowTestScope,
302+ window,
303+ TextObfuscationMode .Hidden ,
304+ initialText,
305+ initialSelection
306+ ) {
307+ @Composable
308+ override fun TextField () {
309+ val focusRequester = remember { FocusRequester () }
310+
311+ BasicSecureTextField (
312+ state = textFieldState,
313+ textObfuscationMode = textObfuscationMode,
314+ onTextLayout = { textLayoutResultGetter = it },
315+ modifier = Modifier
316+ .focusRequester(focusRequester)
317+ .onPlaced {
318+ textBoundingBox = it.boundsInWindow()
319+ }
320+ )
321+
322+ LaunchedEffect (focusRequester) {
323+ focusRequester.requestFocus()
324+ }
289325 }
290- }
291326
292- override fun toString () = " SecureTextField"
293- }
327+ override fun toString () = " SecureTextField"
328+ }
294329 }
295330 }
296331}
0 commit comments