Ralston Da Silva | 2f58d68 | 2020-06-09 14:59:37 -0700 | [diff] [blame] | 1 | /* |
| 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 Pavlis | bc81dcd | 2020-10-21 19:25:11 +0100 | [diff] [blame] | 17 | package androidx.compose.ui.test |
Ralston Da Silva | 2f58d68 | 2020-06-09 14:59:37 -0700 | [diff] [blame] | 18 | |
Ralston Da Silva | b1a63ce | 2020-07-26 00:56:52 -0700 | [diff] [blame] | 19 | import androidx.compose.ui.input.key.KeyEvent |
Ralston Da Silva | ce33078 | 2020-11-17 16:09:26 -0800 | [diff] [blame] | 20 | import androidx.compose.ui.input.key.key |
Ralston Da Silva | 2f58d68 | 2020-06-09 14:59:37 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
Ralston Da Silva | b1a63ce | 2020-07-26 00:56:52 -0700 | [diff] [blame] | 23 | * Send the specified [KeyEvent] to the focused component. |
Ralston Da Silva | 2f58d68 | 2020-06-09 14:59:37 -0700 | [diff] [blame] | 24 | * |
| 25 | * @return true if the event was consumed. False otherwise. |
| 26 | */ |
Ralston Da Silva | b1a63ce | 2020-07-26 00:56:52 -0700 | [diff] [blame] | 27 | fun SemanticsNodeInteraction.performKeyPress(keyEvent: KeyEvent): Boolean { |
Ralston Da Silva | 2f58d68 | 2020-06-09 14:59:37 -0700 | [diff] [blame] | 28 | val semanticsNode = fetchSemanticsNode("Failed to send key Event (${keyEvent.key})") |
Filip Pavlis | 556bf64 | 2021-01-18 16:52:32 +0000 | [diff] [blame^] | 29 | val root = semanticsNode.root |
| 30 | requireNotNull(root) { "Failed to find owner" } |
Jelle Fresen | 8e55c4f | 2020-12-16 13:15:53 +0000 | [diff] [blame] | 31 | @OptIn(InternalTestApi::class) |
Filip Pavlis | 556bf64 | 2021-01-18 16:52:32 +0000 | [diff] [blame^] | 32 | return testContext.testOwner.runOnUiThread { root.sendKeyEvent(keyEvent) } |
Ralston Da Silva | 2f58d68 | 2020-06-09 14:59:37 -0700 | [diff] [blame] | 33 | } |