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