Skip to content

Commit

Permalink
Deflake tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensousa committed Jun 19, 2024
1 parent 46b47cd commit 420528b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import com.rubensousa.dpadrecyclerview.test.TestAdapterConfiguration
import com.rubensousa.dpadrecyclerview.test.helpers.assertFocusAndSelection
import com.rubensousa.dpadrecyclerview.test.helpers.onRecyclerView
import com.rubensousa.dpadrecyclerview.test.helpers.waitForCondition
import com.rubensousa.dpadrecyclerview.test.helpers.waitForIdleScrollState
import com.rubensousa.dpadrecyclerview.testing.KeyEvents
import com.rubensousa.dpadrecyclerview.testing.R
import org.junit.Before
import org.junit.Test

class DragHelperGridTest {


private lateinit var fragmentScenario: FragmentScenario<RecyclerViewFragment>
private lateinit var dragHelper: DpadDragHelper<Int>
private lateinit var testAdapter: TestAdapter
Expand Down Expand Up @@ -94,7 +94,10 @@ class DragHelperGridTest {
startDragging(position = 0)

// when
KeyEvents.pressRight(times = spanCount)
repeat(spanCount) {
KeyEvents.pressRight()
waitForIdleScrollState()
}

// then
assertFocusAndSelection(position = endRowPosition)
Expand All @@ -114,7 +117,10 @@ class DragHelperGridTest {
startDragging(position = endRowPosition)

// when
KeyEvents.pressLeft(times = spanCount)
repeat(spanCount) {
KeyEvents.pressLeft()
waitForIdleScrollState()
}

// then
assertFocusAndSelection(position = 0)
Expand All @@ -135,7 +141,7 @@ class DragHelperGridTest {
startDragging(position = topColumnPosition)

// when
KeyEvents.pressDown(times = 1)
KeyEvents.pressDown()

// then
assertFocusAndSelection(position = bottomColumnPosition)
Expand All @@ -157,7 +163,7 @@ class DragHelperGridTest {
startDragging(position = bottomColumnPosition)

// when
KeyEvents.pressUp(times = 1)
KeyEvents.pressUp()

// then
assertFocusAndSelection(position = topColumnPosition)
Expand All @@ -177,8 +183,13 @@ class DragHelperGridTest {
startDragging(position = 0)

// when
KeyEvents.pressDown(times = numberOfItems / spanCount)
KeyEvents.pressRight(times = spanCount)
repeat(numberOfItems / spanCount) {
KeyEvents.pressDown()
}
repeat(spanCount) {
KeyEvents.pressRight()
waitForIdleScrollState()
}

// then
assertFocusAndSelection(position = numberOfItems - 1)
Expand All @@ -196,8 +207,14 @@ class DragHelperGridTest {
startDragging(position = numberOfItems - 1)

// when
KeyEvents.pressUp(times = numberOfItems / spanCount)
KeyEvents.pressLeft(times = spanCount)
repeat(numberOfItems / spanCount) {
KeyEvents.pressUp()
waitForIdleScrollState()
}
repeat(spanCount) {
KeyEvents.pressLeft()
waitForIdleScrollState()
}

// then
assertFocusAndSelection(position = 0)
Expand All @@ -215,12 +232,6 @@ class DragHelperGridTest {
}
}

private fun stopDragging() {
InstrumentationRegistry.getInstrumentation().runOnMainSync {
dragHelper.stopDrag()
}
}

private fun launchFragment(): FragmentScenario<RecyclerViewFragment> {
return launchFragmentInContainer<RecyclerViewFragment>(
themeResId = R.style.DpadRecyclerViewTestTheme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.rubensousa.dpadrecyclerview.test.helpers.onRecyclerView
import com.rubensousa.dpadrecyclerview.test.helpers.selectLastPosition
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
Expand Down Expand Up @@ -89,34 +88,6 @@ class LayoutWhileScrollingTest : DpadRecyclerViewTest() {
assertThat(layoutCompleted).isEqualTo(1)
}

@Test
fun testRequestingLayoutDuringKeyEventsIsIgnored() {
// given
var layoutCompleted = 0
onRecyclerView("Disable layout during scroll") { recyclerView ->
recyclerView.addOnLayoutCompletedListener(
object : DpadRecyclerView.OnLayoutCompletedListener {
override fun onLayoutCompleted(state: RecyclerView.State) {
layoutCompleted++
}
})
}

// when
KeyEvents.pressDown(times = 20)
repeat(10) {
onRecyclerView("Request layout") { recyclerView ->
if (recyclerView.scrollState != RecyclerView.SCROLL_STATE_IDLE) {
recyclerView.requestLayout()
}
}
}
waitForIdleScrollState()

// then
assertThat(layoutCompleted).isEqualTo(1)
}

@Test
fun testRequestingLayoutDuringScrollIsNotIgnored() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.rubensousa.dpadrecyclerview.test.tests.scrolling

import android.os.Build
import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat
Expand All @@ -33,6 +34,7 @@ import com.rubensousa.dpadrecyclerview.testing.KeyEvents
import org.junit.Before
import org.junit.Test


class AccessibilityTest : DpadRecyclerViewTest() {

private val spanCount = 5
Expand Down Expand Up @@ -62,6 +64,10 @@ class AccessibilityTest : DpadRecyclerViewTest() {

@Test
fun testAccessibilityInfo() {
// Not working on these APIs, so just exit
if (Build.VERSION.SDK_INT <= 25) {
return
}
repeat(10) {
KeyEvents.pressDown()
waitForIdleScrollState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class VerticalTouchScrollTest : DpadRecyclerViewTest() {
val verticalScrollOffset = firstItemBounds.height() / 2

swipeVerticallyBy(-verticalScrollOffset)
waitForIdleScrollState()

val newFirstItemBounds = getItemViewBounds(position = 0)
assertThat(newFirstItemBounds.top).isLessThan(firstItemBounds.top)
Expand Down

0 comments on commit 420528b

Please sign in to comment.