[go: nahoru, domu]

blob: b28a86b01fd14186ab69f31390b1426e2752ba8a [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 Silvace330782020-11-17 16:09:26 -080020import androidx.compose.ui.input.key.key
Ralston Da Silva2f58d682020-06-09 14:59:37 -070021
22/**
Ralston Da Silvab1a63ce2020-07-26 00:56:52 -070023 * Send the specified [KeyEvent] to the focused component.
Ralston Da Silva2f58d682020-06-09 14:59:37 -070024 *
25 * @return true if the event was consumed. False otherwise.
26 */
Ralston Da Silvab1a63ce2020-07-26 00:56:52 -070027fun SemanticsNodeInteraction.performKeyPress(keyEvent: KeyEvent): Boolean {
Ralston Da Silva2f58d682020-06-09 14:59:37 -070028 val semanticsNode = fetchSemanticsNode("Failed to send key Event (${keyEvent.key})")
Filip Pavlis556bf642021-01-18 16:52:32 +000029 val root = semanticsNode.root
30 requireNotNull(root) { "Failed to find owner" }
Jelle Fresen8e55c4f2020-12-16 13:15:53 +000031 @OptIn(InternalTestApi::class)
Filip Pavlis556bf642021-01-18 16:52:32 +000032 return testContext.testOwner.runOnUiThread { root.sendKeyEvent(keyEvent) }
Ralston Da Silva2f58d682020-06-09 14:59:37 -070033}