Dan Peluso
05/25/2022, 3:17 PMandroidx.compose.ui.test
library that I'm looking for advice on. Does performing a click action on a node using onNodeWithTag
actually pass that click to the children of that node? In my example, the node with tag is a composable that applies the fastClickable
modifier on a column (so the entire view is clickable) but the test does not actually click the component with the tag. In the past I've been able to get the click to work, but only by directly accessing the tag on a Button
composable. More code for context in the threadDan Peluso
05/25/2022, 3:19 PM@Composable
fun NodeWithTag(
modifier: Modifier = Modifier,
onToggle: () -> Unit,
(... other args)
) {
Column(
modifier = Modifier
.background(backGroundColor, itemShape)
.border(1.dp, borderColor, itemShape)
.fastClickable(MutableInteractionSource()) {
onToggle()
}
.padding(1.5.paddingDp, 1.paddingDp, 1.5.paddingDp, 1.5.paddingDp)
.then(modifier),
) {
// column content
}
}
Dan Peluso
05/25/2022, 3:20 PMDan Peluso
05/25/2022, 3:33 PMDan Peluso
05/25/2022, 3:33 PM@Composable
fun NodeWithTag(
modifier: Modifier = Modifier,
onToggle: () -> Unit,
nodeName: String
(... other args)
) {
Column(
modifier = Modifier
.testTag("nodeWithTag_$nodeName")
.background(backGroundColor, itemShape)
.border(1.dp, borderColor, itemShape)
.fastClickable(MutableInteractionSource()) {
onToggle()
}
.padding(1.5.paddingDp, 1.paddingDp, 1.5.paddingDp, 1.5.paddingDp)
.then(modifier),
) {
// column content
}
}
Dan Peluso
05/25/2022, 3:57 PM|-Node #24 at (l=16.0, t=602.0, r=304.0, b=686.0)px, Tag: 'nodeWithTag_node1'
| MergeDescendants = 'true'
| |-Node #27 at (l=28.0, t=611.0, r=34.0, b=646.0)px
| | Text = '[node1]'
| | Actions = [GetTextLayoutResult]
| |-Node #28 at (l=42.0, t=611.0, r=44.0, b=646.0)px
| | Text = '[3%]'
| | Actions = [GetTextLayoutResult]
| |-Node #29 at (l=44.0, t=610.0, r=292.0, b=646.0)px
| Text = '[0:17]'
| Actions = [GetTextLayoutResult]
Zach Klippenstein (he/him) [MOD]
05/25/2022, 4:08 PMfastClickable
?Dan Peluso
05/25/2022, 4:10 PM/**
* Currently compose clickable has an internal delay of 100ms in order to determine whether a press is follow by
* scrollable behavior, and then emits the press state follow by the tap behavior. This will result in a slight UI
* delay that is visible when there is an onPressed interaction. This fastClickable modifier will simply observe
* the PressInteractions' states and emit appropriately without any locking.
*
* @see androidx.compose.foundation.handlePressInteraction
*/
@SuppressLint("UnnecessaryComposedModifier")
fun Modifier.fastClickable(
interactionSource: MutableInteractionSource,
enabled: Boolean = true,
role: Role? = null,
onClick: () -> Unit
) = composed {
val onClickState = rememberUpdatedState(onClick)
val semanticModifier = Modifier.semantics(mergeDescendants = true) {
if (role != null) {
this.role = role
}
}
val pointerInputModifier = pointerInput(interactionSource, enabled) {
if (enabled) {
detectTapGestures(
onPress = { offset ->
val press = PressInteraction.Press(offset)
interactionSource.emit(press)
val released = tryAwaitRelease()
if (released) {
interactionSource.emit(PressInteraction.Release(press))
onClickState.value.invoke()
} else {
interactionSource.emit(PressInteraction.Cancel(press))
}
},
)
}
}
this
.then(semanticModifier)
.then(pointerInputModifier)
}
Zach Klippenstein (he/him) [MOD]
05/25/2022, 4:13 PMperformClick
, it looks like it doesn’t use the semantics action, it actually sends a click gesture, so i would think that should work. That said, you should still be setting the click action so accessibility services can interact with it.Zach Klippenstein (he/him) [MOD]
05/25/2022, 4:15 PMDan Peluso
05/25/2022, 4:16 PMfastClickable
for .clickable { onToggle() }
and unfortunately the click still did not go through. Good to know about the accessibility services though, will forward that to our team 👍Dan Peluso
05/25/2022, 4:20 PMonToggle
never gets calledZach Klippenstein (he/him) [MOD]
05/25/2022, 4:29 PMDan Peluso
05/25/2022, 4:40 PMZach Klippenstein (he/him) [MOD]
05/25/2022, 4:56 PMDan Peluso
05/25/2022, 7:21 PMZach Klippenstein (he/him) [MOD]
05/25/2022, 7:51 PM