From dad6c5c326f0f23dee307b0e00a154bd58e410c8 Mon Sep 17 00:00:00 2001 From: Ruben Sousa Date: Thu, 26 Sep 2024 00:43:03 +0200 Subject: [PATCH] Add new test for smooth scrolling changes of lookup --- .../tests/alignment/AlignmentLookupTest.kt | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/alignment/AlignmentLookupTest.kt b/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/alignment/AlignmentLookupTest.kt index 9bf33d7f..e99910d0 100644 --- a/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/alignment/AlignmentLookupTest.kt +++ b/dpadrecyclerview/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/alignment/AlignmentLookupTest.kt @@ -186,4 +186,49 @@ class AlignmentLookupTest : DpadRecyclerViewTest() { assertThat(viewBounds.bottom).isEqualTo(recyclerViewBounds.bottom) } + @Test + fun testAlignmentLookupSmoothScrolling() = report { + Given("Launch Fragment with top alignment") { + launchFragment() + } + + var scrolled = false + When("Set AlignmentLookup for 50% of screen height") { + onRecyclerView("Set alignment") { recyclerView -> + recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { + override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { + super.onScrollStateChanged(recyclerView, newState) + if (newState != RecyclerView.SCROLL_STATE_IDLE) { + scrolled = true + } + } + }) + recyclerView.setAlignmentLookup( + object : AlignmentLookup { + override fun getParentAlignment( + viewHolder: RecyclerView.ViewHolder, + ): ParentAlignment { + return ParentAlignment(fraction = 0.5f) + } + + override fun getChildAlignment( + viewHolder: RecyclerView.ViewHolder, + ): ChildAlignment { + return ChildAlignment(fraction = 0.5f) + } + }, + smooth = true + ) + } + } + + Then("RecyclerView scrolled to new position") { + waitForIdleScrollState() + val recyclerViewBounds = getRecyclerViewBounds() + val viewBounds = getItemViewBounds(position = 0) + assertThat(scrolled).isTrue() + assertThat(viewBounds.centerY()).isEqualTo(recyclerViewBounds.height() / 2) + } + } + }