forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce dedicated test for virtual keyboard events
- Loading branch information
Showing
12 changed files
with
229 additions
and
19 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
compose/ui/ui/src/webTest/kotlin/androidx/compose/ui/events/InputEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.ui.events | ||
|
||
import org.w3c.dom.events.Event | ||
|
||
internal external interface InputEventInit { | ||
val data: String | ||
val inputType: String | ||
} | ||
|
||
internal fun InputEventInit(inputType: String, data: String): InputEventInit = js("({data, inputType})") | ||
|
||
internal external class InputEvent(type: String, options: InputEventInit) : Event |
21 changes: 21 additions & 0 deletions
21
compose/ui/ui/src/webTest/kotlin/androidx/compose/ui/events/MouseEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.ui.events | ||
|
||
import org.w3c.dom.events.Event | ||
|
||
internal external class MouseEvent(type: String) : Event |
23 changes: 23 additions & 0 deletions
23
compose/ui/ui/src/webTest/kotlin/androidx/compose/ui/events/TouchEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.ui.events | ||
|
||
import org.w3c.dom.events.Event | ||
|
||
internal external interface TouchEventInit | ||
|
||
internal external class TouchEvent(type: String, initParams: TouchEventInit) : Event |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
compose/ui/ui/src/webTest/kotlin/androidx/compose/ui/input/TextInputTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.ui.input | ||
|
||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material.TextField | ||
import androidx.compose.runtime.SideEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.OnCanvasTests | ||
import androidx.compose.ui.events.InputEvent | ||
import androidx.compose.ui.events.InputEventInit | ||
import androidx.compose.ui.events.createMouseEvent | ||
import androidx.compose.ui.events.createTouchEvent | ||
import androidx.compose.ui.events.keyDownEvent | ||
import androidx.compose.ui.focus.FocusRequester | ||
import androidx.compose.ui.focus.focusRequester | ||
import androidx.compose.ui.window.CanvasBasedWindow | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
import kotlinx.browser.document | ||
import kotlinx.coroutines.channels.BufferOverflow | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.test.runTest | ||
import org.w3c.dom.HTMLCanvasElement | ||
|
||
class TextInputTests : OnCanvasTests { | ||
|
||
@BeforeTest | ||
fun setup() { | ||
// Because AfterTest is fixed only in kotlin 2.0 | ||
// https://youtrack.jetbrains.com/issue/KT-61888 | ||
commonAfterTest() | ||
createCanvasAndAttach() | ||
} | ||
|
||
@Test | ||
fun keyboardEventPassedToTextField() = runTest { | ||
|
||
val textInputChannel = Channel<String>( | ||
1, onBufferOverflow = BufferOverflow.DROP_OLDEST | ||
) | ||
|
||
val canvas = document.getElementById(canvasId) as HTMLCanvasElement | ||
|
||
CanvasBasedWindow(canvasElementId = canvasId) { | ||
val (firstFocusRequester, secondFocusRequester) = remember { FocusRequester.createRefs() } | ||
|
||
TextField( | ||
value = "", | ||
onValueChange = { value -> | ||
textInputChannel.trySend(value) | ||
if (value == "step1") { | ||
canvas.dispatchEvent(createTouchEvent("touchstart")) | ||
secondFocusRequester.requestFocus() | ||
} | ||
}, | ||
modifier = Modifier.fillMaxSize().focusRequester(firstFocusRequester) | ||
) | ||
|
||
TextField( | ||
value = "", | ||
onValueChange = { value -> | ||
textInputChannel.trySend(value) | ||
if (value == "step2step3step4") { | ||
canvas.dispatchEvent(createMouseEvent("mousedown")) | ||
firstFocusRequester.requestFocus() | ||
} | ||
}, | ||
modifier = Modifier.fillMaxSize().focusRequester(secondFocusRequester) | ||
) | ||
|
||
SideEffect { | ||
secondFocusRequester.requestFocus() | ||
firstFocusRequester.requestFocus() | ||
} | ||
} | ||
|
||
assertNull(document.querySelector("textarea")) | ||
|
||
canvas.dispatchEvent(keyDownEvent("s")) | ||
canvas.dispatchEvent(keyDownEvent("t")) | ||
canvas.dispatchEvent(keyDownEvent("e")) | ||
canvas.dispatchEvent(keyDownEvent("p")) | ||
canvas.dispatchEvent(keyDownEvent("1")) | ||
|
||
assertEquals("step1", textInputChannel.receive()) | ||
assertNotNull(document.querySelector("textarea")) | ||
|
||
canvas.dispatchEvent(keyDownEvent("s")) | ||
canvas.dispatchEvent(keyDownEvent("t")) | ||
canvas.dispatchEvent(keyDownEvent("e")) | ||
canvas.dispatchEvent(keyDownEvent("p")) | ||
canvas.dispatchEvent(keyDownEvent("2")) | ||
|
||
assertEquals("step2", textInputChannel.receive()) | ||
|
||
val backingField = document.querySelector("textarea")!! | ||
|
||
backingField.dispatchEvent(keyDownEvent("s")) | ||
backingField.dispatchEvent(keyDownEvent("t")) | ||
backingField.dispatchEvent(keyDownEvent("e")) | ||
backingField.dispatchEvent(keyDownEvent("p")) | ||
backingField.dispatchEvent(keyDownEvent("3")) | ||
|
||
assertEquals("step2step3", textInputChannel.receive()) | ||
|
||
backingField.dispatchEvent(InputEvent("input", InputEventInit("insertText", "step4"))) | ||
|
||
assertEquals("step2step3step4", textInputChannel.receive()) | ||
|
||
canvas.dispatchEvent(keyDownEvent("s")) | ||
canvas.dispatchEvent(keyDownEvent("t")) | ||
canvas.dispatchEvent(keyDownEvent("e")) | ||
canvas.dispatchEvent(keyDownEvent("p")) | ||
canvas.dispatchEvent(keyDownEvent("5")) | ||
|
||
assertEquals("step1step5", textInputChannel.receive()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters