https://kotlinlang.org logo
#compose-wear
Title
# compose-wear
j

Jonathan

12/08/2023, 4:24 PM
Does the digital crown map to scroll gestures? I’m attempting to add a baseline profile to my Wear app, to improve the performance I’m seeing while rotating the crown, but I don’t see a rotary action/gesture on
UiObject2
. I assume the digital crown
==
scroll gesture but I want to make sure.
s

stevebower

12/11/2023, 2:23 PM
The digital crown does not provide scrolling by default in apps - you can use Modifier.rotaryWithSnap or Modifier.rotaryWithScroll from Horologist to handle it. I believe the digital crown fires RotaryScrollEvent and that needs to be handled to trigger scrolling. @Michail Kulaga
j

Jonathan

12/11/2023, 2:32 PM
I have scrolling setup in the app, just wanted to know if using the
UiObject.scroll(…)
would be sufficient to exercise the code for a Baseline & Macrobenchmark profile. Scrolling performance is terrible on a live device.
s

stevebower

12/11/2023, 3:37 PM
We have a macrobenchmark test for this in androidx - looks like this:
Copy code
val list = device.findObject(By.desc(CONTENT_DESCRIPTION))
            // Setting a gesture margin is important otherwise gesture nav is triggered.
            list.setGestureMargin(device.displayWidth / 5)
            repeat(5) {
                list.drag(Point(list.visibleCenter.x, list.visibleCenter.y / 3))
                device.waitForIdle()
            }
j

Jonathan

12/11/2023, 3:48 PM
I was missing the gesture margin in my code, and I wonder if that was causing the issues with click events when using
UiObject.fling(…)
🤔 Either way, I still need to figure out while rotary scrolling is so janky with my app.
Thanks for the suggestion!
👍 1
m

Michail Kulaga

12/11/2023, 4:10 PM
If you want to test that your list is scrolled with rotary you can do that in Compose Unit tests like that
Copy code
@get:Rule
    val rule = createComposeRule()

    @Test
    fun testing_rotary() {

        rule.setContent {
            // Setting content with scrollable list
        }

        @OptIn(ExperimentalTestApi::class)
        rule.onNodeWithTag(TEST_TAG).performRotaryScrollInput {
            // Scroll by 2 items backward
            rotateToScrollVertically(-50.0f)
            advanceEventTime(250)
            rotateToScrollVertically(-50.0f)
        }

        rule.runOnIdle {
           // testing that our scroll happened
        }
    }
Rotary is different from scroll because it sends a list of different events when it’s scrolled. Usually there’re dozens of events with small values during a single rotation . But performance-wise there is no difference between rotary or finger scroll, so scrolling with Ui automator for performance tests should be fine