Last active
December 8, 2022 01:04
-
-
Save victory316/6e3f4782d7a38b8a4293bb9f282c1287 to your computer and use it in GitHub Desktop.
Matcher using when Jetpack Compose UI test with Role
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
import androidx.compose.ui.semantics.Role | |
import androidx.compose.ui.semantics.SemanticsProperties | |
import androidx.compose.ui.semantics.getOrNull | |
import androidx.compose.ui.test.SemanticsMatcher | |
// needs import below | |
// debugImplementation 'androidx.compose.ui:ui-test-manifest:1.0.0-beta05' | |
// from Yasin Kaçmaz : https://proandroiddev.com/test-jetpack-compose-layouts-easily-with-role-semanticproperty-dcf19f64130f | |
fun withRole(role: Role) = SemanticsMatcher("${SemanticsProperties.Role.name} contains '$role'") { | |
val roleProperty = it.config.getOrNull(SemanticsProperties.Role) ?: false | |
roleProperty == role | |
} | |
// Code snippet describes the types of Role in SemanticProperties | |
/** | |
* The type of user interface element. Accessibility services might use this to describe the | |
* element or do customizations. Most roles can be automatically resolved by the semantics | |
* properties of this element. But some elements with subtle differences need an exact role. If an | |
* exact role is not listed, [SemanticsPropertyReceiver.role] should not be set and the framework | |
* will automatically resolve it. | |
*/ | |
@Immutable | |
@kotlin.jvm.JvmInline | |
value class Role private constructor(@Suppress("unused") private val value: Int) { | |
companion object { | |
/** | |
* This element is a button control. Associated semantics properties for accessibility: | |
* [SemanticsProperties.Disabled], [SemanticsActions.OnClick] | |
*/ | |
val Button = Role(0) | |
/** | |
* This element is a Checkbox which is a component that represents two states (checked / | |
* unchecked). Associated semantics properties for accessibility: | |
* [SemanticsProperties.Disabled], [SemanticsProperties.StateDescription], | |
* [SemanticsActions.OnClick] | |
*/ | |
val Checkbox = Role(1) | |
/** | |
* This element is a Switch which is a two state toggleable component that provides on/off | |
* like options. Associated semantics properties for accessibility: | |
* [SemanticsProperties.Disabled], [SemanticsProperties.StateDescription], | |
* [SemanticsActions.OnClick] | |
*/ | |
val Switch = Role(2) | |
/** | |
* This element is a RadioButton which is a component to represent two states, selected and not | |
* selected. Associated semantics properties for accessibility: [SemanticsProperties.Disabled], | |
* [SemanticsProperties.StateDescription], [SemanticsActions.OnClick] | |
*/ | |
val RadioButton = Role(3) | |
/** | |
* This element is a Tab which represents a single page of content using a text label and/or | |
* icon. A Tab also has two states: selected and not selected. Associated semantics properties | |
* for accessibility: [SemanticsProperties.Disabled], [SemanticsProperties.StateDescription], | |
* [SemanticsActions.OnClick] | |
*/ | |
val Tab = Role(4) | |
/** | |
* This element is an image. Associated semantics properties for accessibility: | |
* [SemanticsProperties.ContentDescription] | |
*/ | |
val Image = Role(5) | |
} | |
override fun toString() = when (this) { | |
Button -> "Button" | |
Checkbox -> "Checkbox" | |
Switch -> "Switch" | |
RadioButton -> "RadioButton" | |
Tab -> "Tab" | |
Image -> "Image" | |
else -> "Unknown" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment