Hi.. I'm trying to do some automation testing. I t...
# compose-android
v
Hi.. I'm trying to do some automation testing. I try to check the KeyboardType mentioned in the UI using compose-testing. I found the below answer in StackOverflow. But PlatformTextInputService is deprecated. and this solution uses mock. Is there any other solution to verify KeyboardType in compose automation testing?
z
See https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/PlatformTextInputInterceptor, you can install an interceptor to grab the EditorInfo and check the android input type on android. Or whatever other platforms use.
v
Copy code
@OptIn(ExperimentalComposeUiApi::class)
@Test
fun keyboardOptionsTest() {
    rule.setContent {
        InterceptPlatformTextInput(
            interceptor = { request, nextHandler ->
                val modifiedRequest =
                    object : PlatformTextInputMethodRequest {
                        override fun createInputConnection(outAttributes: EditorInfo): InputConnection {
                            Log.d(">>>", "create input connection is called with inputType: ${outAttributes.inputType}")
                            Log.d(">>>", "create input connection is called with imeOptions: ${outAttributes.imeOptions}")
                            val inputConnection = request.createInputConnection(outAttributes)
                            return inputConnection
                        }
                    }

                // Send our wrapping request to the next handler, which could be the system or another
                // interceptor up the tree.
                nextHandler.startInputMethod(modifiedRequest)
            }
        ) {
            var value by remember { mutableStateOf("") }
            BasicTextField(
                value = value,
                onValueChange = {
                    value = it
                },
                modifier = Modifier.testTag(TEXT_INPUT_TEST_TAG),
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
            )
        }
    }


    rule.onNodeWithTag(TEXT_INPUT_TEST_TAG).performTextClearance()
    rule.onNodeWithTag(TEXT_INPUT_TEST_TAG).performTextInput("98")
    rule.onNodeWithTag(TEXT_INPUT_TEST_TAG).assert(hasText("98"))
}
I tried with this test with keyboardOptions KeyboardType.Number and Keyboard.Text but all the time i'm getting inputType and imeOptions as 0 only in the createInputConnection method.
z
You’re reading EditorInfo before passing it to createInputConnection, so yea all the values will be their initial values. createInputConnection configures the EditorInfo so it won’t have any meaningful values until after that call.
v
That Works. Thanks!!