|
| 1 | +/* |
| 2 | + * Copyright 2025 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 |
| 18 | + |
| 19 | +import androidx.compose.foundation.layout.Arrangement |
| 20 | +import androidx.compose.foundation.layout.Column |
| 21 | +import androidx.compose.foundation.layout.padding |
| 22 | +import androidx.compose.foundation.text.input.rememberTextFieldState |
| 23 | +import androidx.compose.material.OutlinedTextField |
| 24 | +import androidx.compose.material.Text |
| 25 | +import androidx.compose.runtime.Composable |
| 26 | +import androidx.compose.runtime.getValue |
| 27 | +import androidx.compose.runtime.mutableStateOf |
| 28 | +import androidx.compose.runtime.setValue |
| 29 | +import androidx.compose.runtime.snapshotFlow |
| 30 | +import androidx.compose.runtime.snapshots.Snapshot |
| 31 | +import androidx.compose.ui.awt.ComposePanel |
| 32 | +import androidx.compose.ui.semantics.SemanticsNode |
| 33 | +import androidx.compose.ui.semantics.SemanticsOwner |
| 34 | +import androidx.compose.ui.semantics.SemanticsProperties |
| 35 | +import androidx.compose.ui.semantics.getOrNull |
| 36 | +import androidx.compose.ui.text.AnnotatedString |
| 37 | +import androidx.compose.ui.unit.dp |
| 38 | +import androidx.compose.ui.window.Popup |
| 39 | +import androidx.compose.ui.window.WindowTestScope |
| 40 | +import androidx.compose.ui.window.runApplicationTest |
| 41 | +import java.awt.BorderLayout |
| 42 | +import javax.swing.JFrame |
| 43 | +import kotlin.test.Test |
| 44 | +import kotlin.test.assertContentEquals |
| 45 | +import kotlin.test.assertEquals |
| 46 | +import kotlinx.coroutines.CoroutineScope |
| 47 | +import kotlinx.coroutines.cancel |
| 48 | +import kotlinx.coroutines.launch |
| 49 | +import kotlinx.coroutines.runBlocking |
| 50 | +import kotlinx.coroutines.test.StandardTestDispatcher |
| 51 | + |
| 52 | +class SemanticsOwnersProviderTest { |
| 53 | + |
| 54 | + private fun semanticsOwnersProvidedBy(provider: SemanticsOwnersTestContext) = provider.runTest( |
| 55 | + content = { |
| 56 | + Column( |
| 57 | + verticalArrangement = Arrangement.spacedBy(10.dp), |
| 58 | + modifier = Modifier.padding(64.dp) |
| 59 | + ) { |
| 60 | + Text("Hello") |
| 61 | + OutlinedTextField(rememberTextFieldState("World")) |
| 62 | + } |
| 63 | + } |
| 64 | + ) { |
| 65 | + val strings = semanticsOwners.collectText().map { it.text } |
| 66 | + assertContentEquals(listOf("Hello", "World"), strings) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun semanticsOwnersProvidedInComposeWindow() = |
| 71 | + semanticsOwnersProvidedBy(ComposeWindowSemanticOwnersTestContext()) |
| 72 | + |
| 73 | + @Test |
| 74 | + fun semanticsOwnersProvidedInVisibleComposePanel() = |
| 75 | + semanticsOwnersProvidedBy(ComposePanelSemanticOwnersTestContext(visible = true)) |
| 76 | + |
| 77 | + @Test |
| 78 | + fun semanticsOwnersProvidedInInvisibleComposePanel() = |
| 79 | + semanticsOwnersProvidedBy(ComposePanelSemanticOwnersTestContext(visible = false)) |
| 80 | + |
| 81 | + @Test |
| 82 | + fun semanticsOwnersProvidedInImageComposeScene() = |
| 83 | + semanticsOwnersProvidedBy(ImageComposeSceneSemanticOwnersTestContext()) |
| 84 | + |
| 85 | + private fun semanticsOwnersIsSnapshotStateBy(provider: SemanticsOwnersTestContext) { |
| 86 | + var latestSemanticsOwners: Collection<SemanticsOwner> = emptyList() |
| 87 | + val dispatcher = StandardTestDispatcher() |
| 88 | + val coroutineScope = CoroutineScope(dispatcher) |
| 89 | + coroutineScope.launch { |
| 90 | + snapshotFlow { provider.semanticsOwners }.collect { |
| 91 | + latestSemanticsOwners = it |
| 92 | + } |
| 93 | + } |
| 94 | + var showPopup by mutableStateOf(false) |
| 95 | + provider.runTest( |
| 96 | + content = { |
| 97 | + Text("Hello") |
| 98 | + if (showPopup) { |
| 99 | + println("Showing popup") |
| 100 | + Popup { |
| 101 | + Text("World") |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + ) { |
| 106 | + try { |
| 107 | + dispatcher.scheduler.advanceUntilIdle() |
| 108 | + assertEquals(1, latestSemanticsOwners.size) |
| 109 | + showPopup = true |
| 110 | + awaitIdle() |
| 111 | + dispatcher.scheduler.advanceUntilIdle() |
| 112 | + assertEquals(2, latestSemanticsOwners.size) |
| 113 | + } finally { |
| 114 | + coroutineScope.cancel() |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +// @Test |
| 120 | +// fun semanticsOwnersIsSnapshotStateInComposeWindow() = |
| 121 | +// semanticsOwnersIsSnapshotStateBy(ComposeWindowSemanticOwnersTestContext()) |
| 122 | + |
| 123 | +// @Test |
| 124 | +// fun semanticsOwnersIsSnapshotStateInComposePanel() = |
| 125 | +// semanticsOwnersIsSnapshotStateBy(ComposePanelSemanticOwnersTestContext()) |
| 126 | + |
| 127 | +// @Test |
| 128 | +// fun semanticsOwnersIsSnapshotStateInImageComposeScene() = |
| 129 | +// semanticsOwnersIsSnapshotStateBy(ImageComposeSceneSemanticOwnersTestContext()) |
| 130 | +} |
| 131 | + |
| 132 | +private interface SemanticsOwnersTestContext { |
| 133 | + val semanticsOwners: Collection<SemanticsOwner> |
| 134 | + |
| 135 | + fun runTest( |
| 136 | + content: @Composable () -> Unit, |
| 137 | + test: suspend SemanticsOwnersTestContext.() -> Unit |
| 138 | + ) |
| 139 | + |
| 140 | + suspend fun awaitIdle() |
| 141 | +} |
| 142 | + |
| 143 | +private class ImageComposeSceneSemanticOwnersTestContext : SemanticsOwnersTestContext { |
| 144 | + private val scene: ImageComposeScene = ImageComposeScene(800, 600) |
| 145 | + private var time = 0L |
| 146 | + |
| 147 | + override val semanticsOwners: Collection<SemanticsOwner> |
| 148 | + get() = scene.semanticsOwners |
| 149 | + |
| 150 | + override fun runTest( |
| 151 | + content: @Composable () -> Unit, |
| 152 | + test: suspend SemanticsOwnersTestContext.() -> Unit |
| 153 | + ) { |
| 154 | + scene.setContent(content) |
| 155 | + scene.render(time) |
| 156 | + runBlocking { |
| 157 | + test() |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + override suspend fun awaitIdle() { |
| 162 | + Snapshot.sendApplyNotifications() |
| 163 | + while (scene.hasInvalidations()) { |
| 164 | + time += 16L |
| 165 | + scene.render(time) |
| 166 | + Snapshot.sendApplyNotifications() |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +private class ComposeWindowSemanticOwnersTestContext : SemanticsOwnersTestContext { |
| 172 | + private lateinit var testScope: WindowTestScope |
| 173 | + |
| 174 | + override val semanticsOwners: Collection<SemanticsOwner> |
| 175 | + get() = testScope.window.semanticsOwners |
| 176 | + |
| 177 | + override fun runTest( |
| 178 | + content: @Composable (() -> Unit), |
| 179 | + test: suspend SemanticsOwnersTestContext.() -> Unit |
| 180 | + ) = runApplicationTest { |
| 181 | + testScope = this |
| 182 | + launchTestWindowApplication { |
| 183 | + content() |
| 184 | + } |
| 185 | + awaitIdle() |
| 186 | + test() |
| 187 | + } |
| 188 | + |
| 189 | + override suspend fun awaitIdle() = testScope.awaitIdle() |
| 190 | +} |
| 191 | + |
| 192 | +private class ComposePanelSemanticOwnersTestContext( |
| 193 | + val visible: Boolean = true |
| 194 | +) : SemanticsOwnersTestContext { |
| 195 | + private lateinit var testScope: WindowTestScope |
| 196 | + private lateinit var composePanel: ComposePanel |
| 197 | + |
| 198 | + override val semanticsOwners: Collection<SemanticsOwner> |
| 199 | + get() = composePanel.semanticsOwners |
| 200 | + |
| 201 | + override fun runTest( |
| 202 | + content: @Composable (() -> Unit), |
| 203 | + test: suspend SemanticsOwnersTestContext.() -> Unit |
| 204 | + ) = runApplicationTest { |
| 205 | + testScope = this |
| 206 | + val window = JFrame() |
| 207 | + try { |
| 208 | + composePanel = ComposePanel() |
| 209 | + composePanel.setContent { |
| 210 | + content() |
| 211 | + } |
| 212 | + composePanel.isVisible = visible |
| 213 | + |
| 214 | + window.contentPane.add(composePanel, BorderLayout.CENTER) |
| 215 | + window.pack() |
| 216 | + window.isVisible = true |
| 217 | + |
| 218 | + awaitIdle() |
| 219 | + test() |
| 220 | + } finally { |
| 221 | + window.dispose() |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + override suspend fun awaitIdle() = testScope.awaitIdle() |
| 226 | +} |
| 227 | + |
| 228 | +private fun Collection<SemanticsOwner>.collectText(): List<AnnotatedString> { |
| 229 | + val result = mutableListOf<AnnotatedString>() |
| 230 | + forEach { |
| 231 | + it.rootSemanticsNode.collectTextRecursive(result) |
| 232 | + } |
| 233 | + return result |
| 234 | +} |
| 235 | + |
| 236 | +private fun SemanticsNode.collectTextRecursive(result: MutableList<AnnotatedString>) { |
| 237 | + result.addAll(config.getOrNull(SemanticsProperties.Text) ?: emptyList()) |
| 238 | + config.getOrNull(SemanticsProperties.EditableText)?.let { |
| 239 | + result.add(it) |
| 240 | + } |
| 241 | + for (child in children) { |
| 242 | + child.collectTextRecursive(result) |
| 243 | + } |
| 244 | +} |
0 commit comments