@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
Desktop
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.
This test passes on Android but fails on Desktop:
This happens because in Android:
but on the desktop:
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.