Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c1e7c48d authored by Jordan Demeulenaere's avatar Jordan Demeulenaere
Browse files

Avoid NPE when nested drag starts before enabling nestedDraggable()

This CL fixes an NPE that was happening when a nested drag was started
right before enabling a nestedDraggable(), leading to a null
lastFirstDown.

Bug: 378470603
Test: atest NestedDraggableTest
Flag: EXEMPT NestedDraggable is not used yet
Change-Id: Ib7a4122b2815b1d1e1a664f04bcc5d6bd76bf2d8
parent be9d1529
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -549,9 +549,10 @@ private class NestedDraggableNode(
            nestedScrollController == null &&
                // TODO(b/388231324): Remove this.
                !lastEventWasScrollWheel &&
                draggable.shouldConsumeNestedScroll(sign)
                draggable.shouldConsumeNestedScroll(sign) &&
                lastFirstDown != null
        ) {
            val startedPosition = checkNotNull(lastFirstDown) { "lastFirstDown is not set" }
            val startedPosition = checkNotNull(lastFirstDown)

            // TODO(b/382665591): Ensure that there is at least one pointer down.
            val pointersDownCount = pointersDown.size.coerceAtLeast(1)
+23 −0
Original line number Diff line number Diff line
@@ -773,6 +773,29 @@ class NestedDraggableTest(override val orientation: Orientation) : OrientationAw
        rule.onNodeWithTag(buttonTag).assertTextEquals("Count: 3")
    }

    @Test
    fun nestedDragNotStartedWhenEnabledAfterDragStarted() {
        val draggable = TestDraggable()
        var enabled by mutableStateOf(false)
        val touchSlop =
            rule.setContentWithTouchSlop {
                Box(
                    Modifier.fillMaxSize()
                        .nestedDraggable(draggable, orientation, enabled = enabled)
                        .scrollable(rememberScrollableState { 0f }, orientation)
                )
            }

        rule.onRoot().performTouchInput { down(center) }

        enabled = true
        rule.waitForIdle()

        rule.onRoot().performTouchInput { moveBy((touchSlop + 1f).toOffset()) }

        assertThat(draggable.onDragStartedCalled).isFalse()
    }

    private fun ComposeContentTestRule.setContentWithTouchSlop(
        content: @Composable () -> Unit
    ): Float {