|
| 1 | +/* |
| 2 | + * Copyright 2026 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 | + |
| 17 | +package androidx.compose.ui.interaction |
| 18 | + |
| 19 | +import androidx.compose.foundation.background |
| 20 | +import androidx.compose.foundation.border |
| 21 | +import androidx.compose.foundation.layout.Column |
| 22 | +import androidx.compose.foundation.layout.fillMaxSize |
| 23 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 24 | +import androidx.compose.foundation.layout.height |
| 25 | +import androidx.compose.foundation.layout.padding |
| 26 | +import androidx.compose.foundation.text.BasicTextField |
| 27 | +import androidx.compose.runtime.Composable |
| 28 | +import androidx.compose.ui.ExperimentalComposeUiApi |
| 29 | +import androidx.compose.ui.Modifier |
| 30 | +import androidx.compose.ui.focus.onFocusChanged |
| 31 | +import androidx.compose.ui.geometry.Rect |
| 32 | +import androidx.compose.ui.graphics.Color |
| 33 | +import androidx.compose.ui.layout.boundsInWindow |
| 34 | +import androidx.compose.ui.layout.onGloballyPositioned |
| 35 | +import androidx.compose.ui.test.findFocusedUITextInput |
| 36 | +import androidx.compose.ui.test.runUIKitInstrumentedTest |
| 37 | +import androidx.compose.ui.test.utils.center |
| 38 | +import androidx.compose.ui.unit.DpRect |
| 39 | +import androidx.compose.ui.unit.dp |
| 40 | +import androidx.compose.ui.unit.toDpRect |
| 41 | +import androidx.compose.ui.viewinterop.UIKitInteropProperties |
| 42 | +import androidx.compose.ui.viewinterop.UIKitView |
| 43 | +import androidx.compose.ui.window.ComposeUIView |
| 44 | +import kotlin.test.Test |
| 45 | +import kotlin.test.assertEquals |
| 46 | +import kotlin.test.assertFalse |
| 47 | +import kotlin.test.assertTrue |
| 48 | +import kotlinx.cinterop.ExperimentalForeignApi |
| 49 | +import platform.CoreGraphics.CGRectMake |
| 50 | +import platform.UIKit.UITextInputProtocol |
| 51 | +import platform.UIKit.UIView |
| 52 | + |
| 53 | +@OptIn(ExperimentalComposeUiApi::class, ExperimentalForeignApi::class) |
| 54 | +class NestedComposeTextFieldFocusTest { |
| 55 | + @Test |
| 56 | + fun focusMovesToTextFieldInNestedComposeUIView() = runUIKitInstrumentedTest { |
| 57 | + var outerFocused = false |
| 58 | + var nestedFocused = false |
| 59 | + var outerBounds: DpRect? = null |
| 60 | + var nestedBounds: DpRect? = null |
| 61 | + |
| 62 | + setContent { |
| 63 | + Column( |
| 64 | + modifier = Modifier |
| 65 | + .fillMaxSize() |
| 66 | + .background(Color.White) |
| 67 | + .padding(24.dp) |
| 68 | + ) { |
| 69 | + UIKitView( |
| 70 | + factory = { |
| 71 | + UIView().apply { |
| 72 | + val nestedComposeView = ComposeUIView( |
| 73 | + configure = { enforceStrictPlistSanityCheck = false } |
| 74 | + ) { |
| 75 | + FocusReportingTextField( |
| 76 | + value = NestedFieldText, |
| 77 | + onFocusChanged = { nestedFocused = it }, |
| 78 | + onBoundsChanged = { nestedBounds = it.toDpRect(density) } |
| 79 | + ) |
| 80 | + } |
| 81 | + nestedComposeView.setFrame(CGRectMake(16.0, 16.0, 300.0, 80.0)) |
| 82 | + addSubview(nestedComposeView) |
| 83 | + } |
| 84 | + }, |
| 85 | + modifier = Modifier |
| 86 | + .fillMaxWidth() |
| 87 | + .height(120.dp), |
| 88 | + properties = UIKitInteropProperties(placedAsOverlay = true) |
| 89 | + ) |
| 90 | + |
| 91 | + FocusReportingTextField( |
| 92 | + value = OuterFieldText, |
| 93 | + onFocusChanged = { outerFocused = it }, |
| 94 | + onBoundsChanged = { outerBounds = it.toDpRect(density) } |
| 95 | + ) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + waitUntil("Both text fields should be laid out") { |
| 100 | + outerBounds != null && nestedBounds != null |
| 101 | + } |
| 102 | + |
| 103 | + tap(outerBounds!!.center()) |
| 104 | + waitUntil("Outer text field should be focused after tap") { |
| 105 | + outerFocused && !nestedFocused |
| 106 | + } |
| 107 | + assertEquals(OuterFieldText, findFocusedUITextInput()?.text) |
| 108 | + |
| 109 | + tap(nestedBounds!!.center()) |
| 110 | + waitUntil("Nested text field should take focus and release the outer text field") { |
| 111 | + nestedFocused && !outerFocused |
| 112 | + } |
| 113 | + assertEquals(NestedFieldText, findFocusedUITextInput()?.text) |
| 114 | + |
| 115 | + assertTrue(nestedFocused) |
| 116 | + assertFalse(outerFocused) |
| 117 | + } |
| 118 | + |
| 119 | + @Composable |
| 120 | + private fun FocusReportingTextField( |
| 121 | + value: String, |
| 122 | + onFocusChanged: (Boolean) -> Unit, |
| 123 | + onBoundsChanged: (Rect) -> Unit |
| 124 | + ) { |
| 125 | + BasicTextField( |
| 126 | + value = value, |
| 127 | + onValueChange = {}, |
| 128 | + modifier = Modifier |
| 129 | + .fillMaxWidth() |
| 130 | + .height(64.dp) |
| 131 | + .padding(8.dp) |
| 132 | + .border(1.dp, Color.Black) |
| 133 | + .padding(8.dp) |
| 134 | + .onGloballyPositioned { onBoundsChanged(it.boundsInWindow()) } |
| 135 | + .onFocusChanged { onFocusChanged(it.isFocused) } |
| 136 | + ) |
| 137 | + } |
| 138 | + |
| 139 | + private val UITextInputProtocol.text: String? |
| 140 | + get() { |
| 141 | + val range = textRangeFromPosition(beginningOfDocument, endOfDocument) ?: return null |
| 142 | + return textInRange(range) |
| 143 | + } |
| 144 | + |
| 145 | + private companion object { |
| 146 | + const val OuterFieldText = "outer field" |
| 147 | + const val NestedFieldText = "nested field" |
| 148 | + } |
| 149 | +} |
0 commit comments