[go: nahoru, domu]

blob: 0af80100f5f0874435e4b6f885070399af4edad3 [file] [log] [blame]
Ralston Da Silva2f58d682020-06-09 14:59:37 -07001/*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Filip Pavlisbc81dcd2020-10-21 19:25:11 +010017package androidx.compose.ui.test
Ralston Da Silva2f58d682020-06-09 14:59:37 -070018
Ralston Da Silvab1a63ce2020-07-26 00:56:52 -070019import androidx.compose.ui.input.key.KeyEvent
Ralston Da Silva2f58d682020-06-09 14:59:37 -070020
21/**
Ralston Da Silvab1a63ce2020-07-26 00:56:52 -070022 * Send the specified [KeyEvent] to the focused component.
Ralston Da Silva2f58d682020-06-09 14:59:37 -070023 *
24 * @return true if the event was consumed. False otherwise.
25 */
Ralston Da Silvab1a63ce2020-07-26 00:56:52 -070026fun SemanticsNodeInteraction.performKeyPress(keyEvent: KeyEvent): Boolean {
Ralston Da Silva2f58d682020-06-09 14:59:37 -070027 val semanticsNode = fetchSemanticsNode("Failed to send key Event (${keyEvent.key})")
Andrey Kulikov5fb82da2020-12-03 15:24:33 +000028 val owner = semanticsNode.owner
Ralston Da Silva2f58d682020-06-09 14:59:37 -070029 requireNotNull(owner) { "Failed to find owner" }
Jelle Fresen8e55c4f2020-12-16 13:15:53 +000030 @OptIn(InternalTestApi::class)
Filip Pavlis2bf78912020-10-26 18:01:47 +000031 return testContext.testOwner.runOnUiThread { owner.sendKeyEvent(keyEvent) }
Ralston Da Silva2f58d682020-06-09 14:59:37 -070032}