Skip to content

Commit f4e52d9

Browse files
Handle deprecated InvisibleToUser semantics property.
1 parent 254fa85 commit f4e52d9

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

compose-tests/src/androidTest/java/radiography/test/compose/ComposeUiTest.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import androidx.compose.ui.semantics.SemanticsProperties.ContentDescription
2727
import androidx.compose.ui.semantics.SemanticsProperties.Disabled
2828
import androidx.compose.ui.semantics.SemanticsProperties.Focused
2929
import androidx.compose.ui.semantics.SemanticsProperties.Heading
30+
import androidx.compose.ui.semantics.SemanticsProperties.HideFromAccessibility
3031
import androidx.compose.ui.semantics.SemanticsProperties.HorizontalScrollAxisRange
3132
import androidx.compose.ui.semantics.SemanticsProperties.ImeAction
3233
import androidx.compose.ui.semantics.SemanticsProperties.InvisibleToUser
@@ -131,14 +132,13 @@ class ComposeUiTest {
131132
Box(Modifier.semantics { set(Disabled, Unit) })
132133
Box(Modifier.semantics { set(Focused, true) })
133134
Box(Modifier.semantics { set(Focused, false) })
134-
Box(Modifier.semantics { set(InvisibleToUser, Unit) })
135+
Box(Modifier.semantics { set(HideFromAccessibility, Unit) })
135136
Box(Modifier.semantics { set(IsDialog, Unit) })
136137
Box(Modifier.semantics { set(IsPopup, Unit) })
137138
Box(Modifier.semantics { set(ProgressBarRangeInfo, ProgressBarRangeInfo(.2f, 0f..0.5f)) })
138139
Box(Modifier.semantics { set(PaneTitle, "pane title") })
139140
Box(Modifier.semantics { set(SelectableGroup, Unit) })
140141
Box(Modifier.semantics { set(Heading, Unit) })
141-
Box(Modifier.semantics { set(InvisibleToUser, Unit) })
142142
Box(Modifier.semantics {
143143
set(
144144
HorizontalScrollAxisRange,
@@ -186,6 +186,24 @@ class ComposeUiTest {
186186
assertThat(hierarchy).contains("Box { PASSWORD }")
187187
}
188188

189+
/**
190+
* Copy of [semanticsAreReported] but for deprecated properties that are reported with the same
191+
* tags as their replacements.
192+
*/
193+
@Suppress("DEPRECATION")
194+
@OptIn(ExperimentalComposeUiApi::class)
195+
@Test fun deprecatedSemanticsAreReported() {
196+
composeRule.setContentWithExplicitRoot {
197+
Box(Modifier.semantics { set(InvisibleToUser, Unit) })
198+
}
199+
200+
val hierarchy = composeRule.runOnIdle {
201+
Radiography.scan()
202+
}
203+
204+
assertThat(hierarchy).contains("Box { INVISIBLE-TO-USER }")
205+
}
206+
189207
@Test fun checkableChecked() {
190208
composeRule.setContentWithExplicitRoot {
191209
Checkbox(checked = true, onCheckedChange = {})

radiography/api/radiography.api

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ public final class radiography/internal/SemanticsKt {
200200
public static final fun findTestTags (Lradiography/ScannableView$ComposeView;)Lkotlin/sequences/Sequence;
201201
}
202202

203+
public final class radiography/internal/SemanticsPropertiesHelpers {
204+
public static final field INSTANCE Lradiography/internal/SemanticsPropertiesHelpers;
205+
public final fun getHideFromAccessibility ()Landroidx/compose/ui/semantics/SemanticsPropertyKey;
206+
}
207+
203208
public final class radiography/internal/StringsKt {
204209
public static final fun ellipsize (Ljava/lang/CharSequence;I)Ljava/lang/CharSequence;
205210
public static final fun formatPixelDimensions (II)Ljava/lang/String;

radiography/src/main/java/radiography/Radiography.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public object Radiography {
9898
} catch (e: Throwable) {
9999
insert(
100100
startPosition,
101-
"Exception when going through view hierarchy: ${e.message}\n"
101+
"Exception when going through view hierarchy:\n" + e.stackTraceToString()
102102
)
103103
}
104104
}

radiography/src/main/java/radiography/ViewStateRenderers.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import androidx.compose.ui.semantics.SemanticsProperties.Text
1414
import androidx.compose.ui.semantics.getOrNull
1515
import radiography.ScannableView.AndroidView
1616
import radiography.ScannableView.ComposeView
17+
import radiography.internal.SemanticsPropertiesHelpers
1718
import radiography.internal.ellipsize
1819
import radiography.internal.formatPixelDimensions
1920
import radiography.internal.isComposeAvailable
@@ -77,6 +78,7 @@ public object ViewStateRenderers {
7778
.flatten()
7879
.sortedBy { it.key.name }
7980
.forEach { (key, value) ->
81+
@Suppress("DEPRECATION")
8082
when (key) {
8183
SemanticsProperties.TestTag -> appendLabeledValue("test-tag", value)
8284
SemanticsProperties.ContentDescription -> appendLabeledValue(
@@ -95,7 +97,8 @@ public object ViewStateRenderers {
9597
SemanticsProperties.PaneTitle -> appendLabeledValue("pane-title", value)
9698
SemanticsProperties.SelectableGroup -> append("SELECTABLE-GROUP")
9799
SemanticsProperties.Heading -> append("HEADING")
98-
SemanticsProperties.InvisibleToUser -> append("INVISIBLE-TO-USER")
100+
SemanticsProperties.InvisibleToUser, // InvisibleToUser is deprecated.
101+
SemanticsPropertiesHelpers.HideFromAccessibility -> append("INVISIBLE-TO-USER")
99102
SemanticsProperties.HorizontalScrollAxisRange ->
100103
appendLabeledValue(
101104
"horizontal-scroll-axis-range",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package radiography.internal
2+
3+
import androidx.compose.ui.semantics.SemanticsProperties
4+
import androidx.compose.ui.semantics.SemanticsPropertyKey
5+
6+
internal object SemanticsPropertiesHelpers {
7+
private var _HideFromAccessibility: Any? = UNINITIALIZED
8+
val HideFromAccessibility: SemanticsPropertyKey<Unit>?
9+
get() {
10+
if (_HideFromAccessibility === UNINITIALIZED) {
11+
_HideFromAccessibility = try {
12+
SemanticsProperties.HideFromAccessibility
13+
} catch (_: NoSuchMethodError) {
14+
// Actual compose version is too old to have this property.
15+
null
16+
}
17+
}
18+
19+
@Suppress("UNCHECKED_CAST")
20+
return _HideFromAccessibility as SemanticsPropertyKey<Unit>?
21+
}
22+
23+
private object UNINITIALIZED
24+
}

0 commit comments

Comments
 (0)