-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce scrolling to the keyline whenever AlignmentLookup is used
- Loading branch information
1 parent
bcdd697
commit 0ed2c87
Showing
6 changed files
with
207 additions
and
32 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
...idTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/alignment/AlignmentLookupTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Copyright 2022 Rúben Sousa | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.rubensousa.dpadrecyclerview.test.tests.alignment | ||
|
||
import androidx.recyclerview.widget.RecyclerView | ||
import com.google.common.truth.Truth.assertThat | ||
import com.rubensousa.dpadrecyclerview.AlignmentLookup | ||
import com.rubensousa.dpadrecyclerview.ChildAlignment | ||
import com.rubensousa.dpadrecyclerview.ParentAlignment | ||
import com.rubensousa.dpadrecyclerview.ParentAlignment.Edge | ||
import com.rubensousa.dpadrecyclerview.test.TestLayoutConfiguration | ||
import com.rubensousa.dpadrecyclerview.test.helpers.getItemViewBounds | ||
import com.rubensousa.dpadrecyclerview.test.helpers.getRecyclerViewBounds | ||
import com.rubensousa.dpadrecyclerview.test.helpers.onRecyclerView | ||
import com.rubensousa.dpadrecyclerview.test.helpers.waitForIdleScrollState | ||
import com.rubensousa.dpadrecyclerview.test.helpers.waitForLayout | ||
import com.rubensousa.dpadrecyclerview.test.tests.DpadRecyclerViewTest | ||
import com.rubensousa.dpadrecyclerview.testing.KeyEvents | ||
import com.rubensousa.dpadrecyclerview.testing.rules.DisableIdleTimeoutRule | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class AlignmentLookupTest : DpadRecyclerViewTest() { | ||
|
||
@get:Rule | ||
val idleTimeoutRule = DisableIdleTimeoutRule() | ||
|
||
override fun getDefaultLayoutConfiguration(): TestLayoutConfiguration { | ||
return TestLayoutConfiguration( | ||
spans = 1, | ||
orientation = RecyclerView.VERTICAL, | ||
parentAlignment = ParentAlignment( | ||
edge = Edge.MIN_MAX, | ||
offset = 0, | ||
fraction = 0f | ||
), | ||
childAlignment = ChildAlignment( | ||
offset = 0, | ||
fraction = 0f | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun testItemRespectsParentAlignmentLookup() { | ||
// given | ||
launchFragment() | ||
val recyclerViewBounds = getRecyclerViewBounds() | ||
|
||
// when | ||
onRecyclerView("Set alignment") { recyclerView -> | ||
recyclerView.setAlignmentLookup(object : AlignmentLookup { | ||
override fun getParentAlignment( | ||
viewHolder: RecyclerView.ViewHolder, | ||
): ParentAlignment? { | ||
if (viewHolder.layoutPosition == 0) { | ||
return ParentAlignment(offset = 0, fraction = 0.5f) | ||
} | ||
return null | ||
} | ||
}) | ||
} | ||
|
||
// then | ||
waitForLayout() | ||
val viewBounds = getItemViewBounds(position = 0) | ||
assertThat(viewBounds.top).isEqualTo(recyclerViewBounds.height() / 2) | ||
} | ||
|
||
@Test | ||
fun testItemRespectsChildAlignmentLookup() { | ||
// given | ||
launchFragment() | ||
|
||
// when | ||
onRecyclerView("Set alignment") { recyclerView -> | ||
recyclerView.setAlignmentLookup(object : AlignmentLookup { | ||
override fun getChildAlignment(viewHolder: RecyclerView.ViewHolder): ChildAlignment? { | ||
if (viewHolder.layoutPosition == 0) { | ||
return ChildAlignment(offset = 0, fraction = 0.5f) | ||
} | ||
return null | ||
} | ||
}) | ||
} | ||
|
||
// then | ||
waitForLayout() | ||
val viewBounds = getItemViewBounds(position = 0) | ||
assertThat(viewBounds.top).isEqualTo(-viewBounds.height() / 2) | ||
} | ||
|
||
@Test | ||
fun testScrollingAlignsToLookup() { | ||
// given | ||
launchFragment() | ||
val centerParentAlignment = ParentAlignment(fraction = 0.5f) | ||
val centerChildAlignment = ChildAlignment(fraction = 0.5f) | ||
val recyclerViewBounds = getRecyclerViewBounds() | ||
|
||
// when | ||
onRecyclerView("Set alignment") { recyclerView -> | ||
recyclerView.setAlignmentLookup(object : AlignmentLookup { | ||
override fun getParentAlignment( | ||
viewHolder: RecyclerView.ViewHolder, | ||
): ParentAlignment? { | ||
if (viewHolder.layoutPosition % 2 == 0) { | ||
return centerParentAlignment | ||
} | ||
return null | ||
} | ||
|
||
override fun getChildAlignment(viewHolder: RecyclerView.ViewHolder): ChildAlignment? { | ||
if (viewHolder.layoutPosition % 2 == 0) { | ||
return centerChildAlignment | ||
} | ||
return null | ||
} | ||
}) | ||
} | ||
waitForLayout() | ||
|
||
repeat(10) { index -> | ||
val viewBounds = getItemViewBounds(position = index) | ||
// then | ||
if (index % 2 == 0) { | ||
assertThat(viewBounds.centerY()).isEqualTo(recyclerViewBounds.height() / 2) | ||
} else { | ||
assertThat(viewBounds.top).isEqualTo(0) | ||
} | ||
KeyEvents.pressDown() | ||
waitForIdleScrollState() | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.