Skip to content

Commit

Permalink
Fix focus listener not being invoked for parent if a child RecyclerVi…
Browse files Browse the repository at this point in the history
…ew has no listener attached
  • Loading branch information
rubensousa committed Jun 18, 2024
1 parent d62dacd commit baf45d7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package com.rubensousa.dpadrecyclerview.test.tests.focus
import androidx.core.view.get
import androidx.fragment.app.testing.FragmentScenario
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.espresso.Espresso
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withTagValue
import com.google.common.truth.Truth.assertThat
import com.rubensousa.dpadrecyclerview.DpadRecyclerView
import com.rubensousa.dpadrecyclerview.test.TestNestedListFragment
Expand All @@ -29,7 +32,10 @@ import com.rubensousa.dpadrecyclerview.testfixtures.DpadFocusEvent
import com.rubensousa.dpadrecyclerview.testfixtures.recording.ScreenRecorderRule
import com.rubensousa.dpadrecyclerview.testing.KeyEvents
import com.rubensousa.dpadrecyclerview.testing.R
import com.rubensousa.dpadrecyclerview.testing.actions.DpadRecyclerViewActions
import com.rubensousa.dpadrecyclerview.testing.rules.DisableIdleTimeoutRule
import org.hamcrest.Matchers
import org.hamcrest.core.AllOf.allOf
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -127,6 +133,27 @@ class NestedFocusListenerTest {
}
}

@Test
fun testParentRecyclerViewStillReceivesFocusIfChildHasNoListener() {
// given
val focusEvents = 6
Espresso.onView(
allOf(
withId(com.rubensousa.dpadrecyclerview.test.R.id.nestedRecyclerView),
withTagValue(Matchers.`is`(0))
)
).perform(DpadRecyclerViewActions.execute("Clear listener") { recyclerView ->
recyclerView.clearOnViewFocusedListeners()
})

// when
KeyEvents.pressRight(times = focusEvents - 1)
waitForIdleScrollState()

// then
assertThat(getParentFocusEvents()).hasSize(focusEvents)
}

private fun getChildFocusEvents(): List<DpadFocusEvent> {
var events = listOf<DpadFocusEvent>()
fragmentScenario.onFragment { fragment ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ internal class PivotSelector(

fun focus(view: View) {
view.requestFocus()
// Exit early if there's no one listening for focus events
if (focusListeners.isEmpty() || isRetainingFocus) {
// Do not notify listeners if we are retaining focus
if (isRetainingFocus) {
return
}
val currentRecyclerView = recyclerView ?: return
Expand Down Expand Up @@ -213,10 +213,6 @@ internal class PivotSelector(
return max(0, min(itemCount - 1, position))
}

fun onLayoutChildren(state: RecyclerView.State) {

}

fun onLayoutCompleted() {
if (isSelectionUpdatePending) {
isSelectionUpdatePending = false
Expand Down

0 comments on commit baf45d7

Please sign in to comment.