Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2209: Create a Helper class to provide methos for [RecyclerViewActions] scrolling actions #2451

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions testing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ dependencies {
implementation(
'androidx.annotation:annotation:1.1.0',
'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha03',
'androidx.recyclerview:recyclerview:1.0.0',
'androidx.test.espresso:espresso-core:3.2.0',
'androidx.test.espresso:espresso-contrib:3.2.0',
'androidx.test:runner:1.2.0',
'com.google.dagger:dagger:2.24',
'com.google.truth:truth:0.43',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.oppia.android.testing

import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.scrollTo
import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
import androidx.test.espresso.contrib.RecyclerViewActions.scrollToHolder
import androidx.test.espresso.matcher.ViewMatchers.withId
import org.hamcrest.BaseMatcher
import javax.inject.Inject

/**
* Provides different scrolling actions for [RecyclerView]s. This is needed because different
* testing scenarios potentially require different scrolling actions for the tests to be successful.
* Use the methods in this class for all scrolling actions for [RecyclerView]s in Roboelectric and
* Espresso test scenarios. See #2209 for more information.
*/
class RecyclerViewScrollingActions @Inject constructor(
val testCoroutineDispatchers: TestCoroutineDispatchers
) {
BenHenning marked this conversation as resolved.
Show resolved Hide resolved

/**
* Performs scrolling action to particular RecyclerView at a required position. Use this method
* for all testing scenarios when scrolling to a RecyclerView at a particular position.
*/
fun scrollToPosition(@LayoutRes recyclerViewId: Int, position: Int) {
onView(withId(recyclerViewId)).perform(
actionOnItemAtPosition<RecyclerView.ViewHolder>(
position,
scrollTo()
)
)
testCoroutineDispatchers.runCurrent()
}

/**
* Performs scrolling action to [RecyclerView] of a particular ViewType designed by a view holder
* via [baseMatcher]. Use this method for all testing scenarios when the [View] type of the
* destination [RecyclerView] is known.
*/
fun scrollToViewType(
@LayoutRes recyclerViewId: Int,
baseMatcher: BaseMatcher<RecyclerView.ViewHolder>
) {
onView(withId(recyclerViewId)).perform(
scrollToHolder(baseMatcher)
)
testCoroutineDispatchers.runCurrent()
}
}