JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
16.24k stars 1.18k forks source link

Can't generate key-typed event in tests #3710

Closed m-sasha closed 1 month ago

m-sasha commented 1 year ago

This test passes on Android but fails on Desktop:

    @OptIn(ExperimentalTestApi::class)
    @Test
    fun testKeyTyped() {
        rule.setContent {
            var textFieldValue by remember { mutableStateOf(TextFieldValue("")) }

            TextField(
                value = textFieldValue,
                onValueChange = {
                    textFieldValue = it
                },
                modifier = Modifier
                    .testTag("textfield")
            )
        }
        rule.onNodeWithTag("textfield").apply {
            performClick()
            assertIsFocused()

            performKeyInput {
                keyDown(Key.A)
                keyUp(Key.A)
            }

            assertTextEquals("a")
        }
    }

This happens because in Android:

internal actual val KeyEvent.isTypedEvent: Boolean
    get() = nativeKeyEvent.action == android.view.KeyEvent.ACTION_DOWN &&
        !Character.isISOControl(nativeKeyEvent.unicodeChar)

but on the desktop:

actual val KeyEvent.isTypedEvent: Boolean
    get() = awtEventOrNull?.id == java.awt.event.KeyEvent.KEY_TYPED &&
        awtEventOrNull?.keyChar?.isPrintable() == true

Affected platforms

A possible solution would be to add and use an intermediate (say) NativeKeyEvent which we can create from either an AWT key event, or "synthetically" in a test.

okushnikov commented 2 months ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.