-
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.
Merge pull request #225 from rubensousa/linear_circular_focus
Add support for circular focus in linear layouts
- Loading branch information
Showing
15 changed files
with
374 additions
and
34 deletions.
There are no files selected for viewing
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
104 changes: 104 additions & 0 deletions
104
...st/kotlin/com/rubensousa/dpadrecyclerview/test/tests/focus/HorizontalCircularFocusTest.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,104 @@ | ||
/* | ||
* 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.focus | ||
|
||
import androidx.recyclerview.widget.RecyclerView | ||
import com.rubensousa.dpadrecyclerview.ChildAlignment | ||
import com.rubensousa.dpadrecyclerview.FocusableDirection | ||
import com.rubensousa.dpadrecyclerview.ParentAlignment | ||
import com.rubensousa.dpadrecyclerview.test.TestAdapterConfiguration | ||
import com.rubensousa.dpadrecyclerview.test.TestLayoutConfiguration | ||
import com.rubensousa.dpadrecyclerview.test.helpers.assertFocusAndSelection | ||
import com.rubensousa.dpadrecyclerview.test.helpers.onRecyclerView | ||
import com.rubensousa.dpadrecyclerview.test.helpers.waitForAdapterUpdate | ||
import com.rubensousa.dpadrecyclerview.test.helpers.waitForIdleScrollState | ||
import com.rubensousa.dpadrecyclerview.test.tests.DpadRecyclerViewTest | ||
import com.rubensousa.dpadrecyclerview.testing.KeyEvents | ||
import com.rubensousa.dpadrecyclerview.testing.rules.DisableIdleTimeoutRule | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class HorizontalCircularFocusTest : DpadRecyclerViewTest() { | ||
|
||
@get:Rule | ||
val idleTimeoutRule = DisableIdleTimeoutRule() | ||
|
||
private val numberOfItems = 3 | ||
|
||
override fun getDefaultLayoutConfiguration(): TestLayoutConfiguration { | ||
return TestLayoutConfiguration( | ||
spans = 1, | ||
orientation = RecyclerView.HORIZONTAL, | ||
parentAlignment = ParentAlignment( | ||
edge = ParentAlignment.Edge.MIN_MAX | ||
), | ||
childAlignment = ChildAlignment(offset = 0) | ||
) | ||
} | ||
|
||
override fun getDefaultAdapterConfiguration(): TestAdapterConfiguration { | ||
return super.getDefaultAdapterConfiguration().copy( | ||
numberOfItems = numberOfItems, | ||
itemLayoutId = com.rubensousa.dpadrecyclerview.testing.R.layout.dpadrecyclerview_test_item_horizontal | ||
) | ||
} | ||
|
||
@Before | ||
fun setup() { | ||
launchFragment() | ||
onRecyclerView("Set focusable direction") { recyclerView -> | ||
recyclerView.setFocusableDirection(FocusableDirection.CIRCULAR) | ||
} | ||
} | ||
|
||
@Test | ||
fun testKeyUpMovesToLastPosition() { | ||
// when | ||
KeyEvents.pressLeft() | ||
waitForIdleScrollState() | ||
|
||
// then | ||
assertFocusAndSelection(numberOfItems - 1) | ||
} | ||
|
||
@Test | ||
fun testKeyDownMovesToFirstPosition() { | ||
// when | ||
KeyEvents.pressRight(numberOfItems) | ||
waitForIdleScrollState() | ||
|
||
// then | ||
assertFocusAndSelection(0) | ||
} | ||
|
||
@Test | ||
fun testCircularFocusDoesNotWorkIfLayoutIsFilled() { | ||
// given | ||
mutateAdapter { adapter -> | ||
adapter.submitList(MutableList(10) { it }) | ||
} | ||
waitForAdapterUpdate() | ||
|
||
// when | ||
KeyEvents.pressLeft() | ||
|
||
// then | ||
assertFocusAndSelection(position = 0) | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
...droidTest/kotlin/com/rubensousa/dpadrecyclerview/test/tests/focus/ReverseGridFocusTest.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,61 @@ | ||
/* | ||
* 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.focus | ||
|
||
import androidx.recyclerview.widget.RecyclerView | ||
import com.rubensousa.dpadrecyclerview.ChildAlignment | ||
import com.rubensousa.dpadrecyclerview.ParentAlignment | ||
import com.rubensousa.dpadrecyclerview.test.TestLayoutConfiguration | ||
import com.rubensousa.dpadrecyclerview.test.helpers.assertFocusAndSelection | ||
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 ReverseGridFocusTest : DpadRecyclerViewTest() { | ||
|
||
@get:Rule | ||
val idleTimeoutRule = DisableIdleTimeoutRule() | ||
|
||
private val spanCount = 5 | ||
|
||
override fun getDefaultLayoutConfiguration(): TestLayoutConfiguration { | ||
return TestLayoutConfiguration( | ||
spans = spanCount, | ||
orientation = RecyclerView.VERTICAL, | ||
parentAlignment = ParentAlignment( | ||
edge = ParentAlignment.Edge.MIN_MAX | ||
), | ||
reverseLayout = true, | ||
childAlignment = ChildAlignment(offset = 0) | ||
) | ||
} | ||
|
||
@Test | ||
fun testFocusStartOfRow() { | ||
// given | ||
launchFragment() | ||
|
||
// when | ||
KeyEvents.pressLeft(times = spanCount) | ||
|
||
// then | ||
assertFocusAndSelection(spanCount - 1) | ||
} | ||
|
||
} |
103 changes: 103 additions & 0 deletions
103
...Test/kotlin/com/rubensousa/dpadrecyclerview/test/tests/focus/VerticalCircularFocusTest.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,103 @@ | ||
/* | ||
* 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.focus | ||
|
||
import androidx.recyclerview.widget.RecyclerView | ||
import com.rubensousa.dpadrecyclerview.ChildAlignment | ||
import com.rubensousa.dpadrecyclerview.FocusableDirection | ||
import com.rubensousa.dpadrecyclerview.ParentAlignment | ||
import com.rubensousa.dpadrecyclerview.test.TestAdapterConfiguration | ||
import com.rubensousa.dpadrecyclerview.test.TestLayoutConfiguration | ||
import com.rubensousa.dpadrecyclerview.test.helpers.assertFocusAndSelection | ||
import com.rubensousa.dpadrecyclerview.test.helpers.onRecyclerView | ||
import com.rubensousa.dpadrecyclerview.test.helpers.waitForAdapterUpdate | ||
import com.rubensousa.dpadrecyclerview.test.helpers.waitForIdleScrollState | ||
import com.rubensousa.dpadrecyclerview.test.tests.DpadRecyclerViewTest | ||
import com.rubensousa.dpadrecyclerview.testing.KeyEvents | ||
import com.rubensousa.dpadrecyclerview.testing.rules.DisableIdleTimeoutRule | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class VerticalCircularFocusTest : DpadRecyclerViewTest() { | ||
|
||
@get:Rule | ||
val idleTimeoutRule = DisableIdleTimeoutRule() | ||
|
||
private val numberOfItems = 3 | ||
|
||
override fun getDefaultLayoutConfiguration(): TestLayoutConfiguration { | ||
return TestLayoutConfiguration( | ||
spans = 1, | ||
orientation = RecyclerView.VERTICAL, | ||
parentAlignment = ParentAlignment( | ||
edge = ParentAlignment.Edge.MIN_MAX | ||
), | ||
childAlignment = ChildAlignment(offset = 0) | ||
) | ||
} | ||
|
||
override fun getDefaultAdapterConfiguration(): TestAdapterConfiguration { | ||
return super.getDefaultAdapterConfiguration().copy( | ||
numberOfItems = numberOfItems | ||
) | ||
} | ||
|
||
@Before | ||
fun setup() { | ||
launchFragment() | ||
onRecyclerView("Set focusable direction") { recyclerView -> | ||
recyclerView.setFocusableDirection(FocusableDirection.CIRCULAR) | ||
} | ||
} | ||
|
||
@Test | ||
fun testKeyUpMovesToLastPosition() { | ||
// when | ||
KeyEvents.pressUp() | ||
waitForIdleScrollState() | ||
|
||
// then | ||
assertFocusAndSelection(numberOfItems - 1) | ||
} | ||
|
||
@Test | ||
fun testKeyDownMovesToFirstPosition() { | ||
// when | ||
KeyEvents.pressDown(numberOfItems) | ||
waitForIdleScrollState() | ||
|
||
// then | ||
assertFocusAndSelection(0) | ||
} | ||
|
||
@Test | ||
fun testCircularFocusDoesNotWorkIfLayoutIsFilled() { | ||
// given | ||
mutateAdapter { adapter -> | ||
adapter.submitList(MutableList(10) { it }) | ||
} | ||
waitForAdapterUpdate() | ||
|
||
// when | ||
KeyEvents.pressUp() | ||
|
||
// then | ||
assertFocusAndSelection(position = 0) | ||
} | ||
|
||
} |
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.