Skip to content

Commit

Permalink
Fix Compose Role.Tab to correctly translate to Java's AccessibleRole.…
Browse files Browse the repository at this point in the history
…PAGE_TAB
  • Loading branch information
m-sasha committed Sep 15, 2023
1 parent ded4e35 commit 9af05f8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -383,28 +383,25 @@ internal class ComposeAccessible(

override fun getAccessibleRole(): AccessibleRole {
controller?.notifyIsInUse()
when (semanticsNode.config.getOrNull(SemanticsProperties.Role)) {
Role.Button -> return AccessibleRole.PUSH_BUTTON
Role.Checkbox -> return AccessibleRole.CHECK_BOX
Role.RadioButton -> return AccessibleRole.RADIO_BUTTON
val fromSemanticRole = when (semanticsNode.config.getOrNull(SemanticsProperties.Role)) {
Role.Button -> AccessibleRole.PUSH_BUTTON
Role.Checkbox -> AccessibleRole.CHECK_BOX
Role.RadioButton -> AccessibleRole.RADIO_BUTTON
Role.Tab -> AccessibleRole.PAGE_TAB
else -> null
// ?
// Role.Switch ->
// Role.Image ->
}
if (isPassword) {
return AccessibleRole.PASSWORD_TEXT
}
if (scrollBy != null) {
return AccessibleRole.SCROLL_PANE
}
if (setText != null) {
return AccessibleRole.TEXT
}
if (text != null) {
return AccessibleRole.LABEL

return when {
fromSemanticRole != null -> fromSemanticRole
isPassword -> AccessibleRole.PASSWORD_TEXT
scrollBy != null -> AccessibleRole.SCROLL_PANE
setText != null -> AccessibleRole.TEXT
text != null -> AccessibleRole.LABEL
else -> AccessibleRole.PANEL
}
return AccessibleRole.PANEL
}

override fun getAccessibleStateSet(): AccessibleStateSet {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,84 @@
/*
* Copyright 2021 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.platform

import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import javax.accessibility.AccessibleText
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class AccessibilityTest {

@get:Rule
val rule = createComposeRule()

@Test
fun accessibleText() {
rule.setContent {
Text("Hello world. Hi world.", modifier = Modifier.testTag("text"))
}

val node = rule.onNodeWithTag("text").fetchSemanticsNode()
val accessibleNode = ComposeAccessible(node)
val accessibleText = accessibleNode.accessibleContext.accessibleText!!
assertEquals(22, accessibleText.charCount)

assertEquals("H", accessibleText.getAtIndex(AccessibleText.CHARACTER, 0))
assertEquals("Hello", accessibleText.getAtIndex(AccessibleText.WORD, 0))
assertEquals("Hello world. ", accessibleText.getAtIndex(AccessibleText.SENTENCE, 0))

assertEquals("e", accessibleText.getAfterIndex(AccessibleText.CHARACTER, 0))
assertEquals("world", accessibleText.getAfterIndex(AccessibleText.WORD, 0))
assertEquals("Hi world.", accessibleText.getAfterIndex(AccessibleText.SENTENCE, 0))

assertEquals("d", accessibleText.getBeforeIndex(AccessibleText.CHARACTER, 21))
assertEquals("world", accessibleText.getBeforeIndex(AccessibleText.WORD, 21))
assertEquals("Hi world", accessibleText.getBeforeIndex(AccessibleText.SENTENCE, 21))
}
/*
* Copyright 2021 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.platform

import androidx.compose.material.Tab
import androidx.compose.material.TabRow
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.assertThat
import androidx.compose.ui.isEqualTo
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import javax.accessibility.AccessibleRole
import javax.accessibility.AccessibleText
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class AccessibilityTest {

@get:Rule
val rule = createComposeRule()

@Test
fun accessibleText() {
rule.setContent {
Text("Hello world. Hi world.", modifier = Modifier.testTag("text"))
}

val node = rule.onNodeWithTag("text").fetchSemanticsNode()
val accessibleNode = ComposeAccessible(node)
val accessibleText = accessibleNode.accessibleContext.accessibleText!!
assertEquals(22, accessibleText.charCount)

assertEquals("H", accessibleText.getAtIndex(AccessibleText.CHARACTER, 0))
assertEquals("Hello", accessibleText.getAtIndex(AccessibleText.WORD, 0))
assertEquals("Hello world. ", accessibleText.getAtIndex(AccessibleText.SENTENCE, 0))

assertEquals("e", accessibleText.getAfterIndex(AccessibleText.CHARACTER, 0))
assertEquals("world", accessibleText.getAfterIndex(AccessibleText.WORD, 0))
assertEquals("Hi world.", accessibleText.getAfterIndex(AccessibleText.SENTENCE, 0))

assertEquals("d", accessibleText.getBeforeIndex(AccessibleText.CHARACTER, 21))
assertEquals("world", accessibleText.getBeforeIndex(AccessibleText.WORD, 21))
assertEquals("Hi world", accessibleText.getBeforeIndex(AccessibleText.SENTENCE, 21))
}

@Test
fun accessibleTab() {
rule.setContent {
TabRow(selectedTabIndex = 0) {
Tab(
selected = true,
onClick = { },
modifier = Modifier.testTag("tab"),
text = { Text("Tab") }
)
}
}

val node = rule.onNodeWithTag("tab").fetchSemanticsNode()
val accessibleNode = ComposeAccessible(node)
assertThat(accessibleNode.accessibleContext.accessibleRole)
.isEqualTo(AccessibleRole.PAGE_TAB)
}

}

0 comments on commit 9af05f8

Please sign in to comment.