Android + Compose test question How do i perform ...
# compose
a
Android + Compose test question How do i perform a long press (touch – not click) in tests? I'm doing the following but it also gives focus to the composable, which I believe is wrong. When I run my app on a device, clicking on a button doesn't give focus to it:
Copy code
onNodeWithTag("long_press_target").performTouchInput {
    down(center)
}
onNodeWithTag("long_press_target").assertIsNotFocused() // <- fails because the down() gave it focus, while it shouldn't
s
If the component is focusable this is correct
It should reflect in the interactionsource
a
If the component is focusable this is correct
by 'this' you mean touching it giving it focus? if that is true, how come the behavior is different when running the app on a device?
s
Yes that's what I mean
That is not different than how it works in a device. A focusable component grabs focus on mouse down
Not sure where you're seeing a different behaviour
But that's how clickable works
a
will share more info when i get a chance. might be wrong
👍 1
hold on. im not talking about 'mouse down'. im talking about touch
s
Ahhh ok sorry, I didn't know it works differently
(if it does)
true 1
a
That is not different than how it works in a device. A focusable component grabs focus on mouse down
This is not true on Android, Compose for Desktop works differently here
s
I see. Thanks for clarifying :)
a
If this is a test for compose-unstyled - it looks like your tests are running on Compose for Desktop, so seeing different behavior than running on Android directly would make sense to me. If you have an Android instrumentation test, you should see different behavior.
a
it is indeed for unstyled yes If JVM treats touches as clicks then this makes sense from technical POV I'll open an issue about this on CMP for further discussion. I don't think that any sort of touch should give focus to the item no matter the platform so the behavior is predictable
a
Random musing since I've been looking at this area: How Android models focus and how web models focus is very different at a fundamental level - and then of course there's all the other platforms too. Writing a cohesive set of APIs when the underlying system works different is hard
As an example, both Android and web sort of have a concept of "in touch mode" - the end result for the user is that keyboard focus visuals go away once you click back somewhere. On Android, that "going into touch mode" changes the set of items that are focused, so focus usually goes away as a result of a thing no longer being eligible to be focusable. On web, that "going into touch mode" is a visual change, where the item remains focused, but you can no longer see the focus visual for it.
a
Update: I ended up running the test on Android and it passes. Items are not given focus on touch as expected