issue#103 Removing item animations

This commit is contained in:
Gergely Hegedus 2022-07-19 16:33:38 +03:00
parent 4f6bba6cb1
commit 00c222b461
2 changed files with 39 additions and 0 deletions

View file

@ -21,6 +21,7 @@ import androidx.test.espresso.matcher.ViewMatchers.withText
import org.fnives.test.showcase.R
import org.fnives.test.showcase.android.testutil.intent.notIntended
import org.fnives.test.showcase.android.testutil.viewaction.imageview.WithDrawable
import org.fnives.test.showcase.android.testutil.viewaction.recycler.RemoveItemAnimations
import org.fnives.test.showcase.android.testutil.viewaction.swiperefresh.PullToRefresh
import org.fnives.test.showcase.model.content.Content
import org.fnives.test.showcase.model.content.FavouriteContent
@ -29,6 +30,16 @@ import org.hamcrest.Matchers.allOf
class HomeRobot {
/**
* Needed because Espresso idling sometimes not in sync with RecyclerView's animation.
* So we simply remove the item animations, the animations should be disabled anyway for test.
*
* Reference: https://github.com/android/android-test/issues/223
*/
fun removeItemAnimations() = apply {
Espresso.onView(withId(R.id.recycler)).perform(RemoveItemAnimations())
}
fun setupIntentResults() {
Intents.intending(IntentMatchers.hasComponent(AuthActivity::class.java.canonicalName))
.respondWith(Instrumentation.ActivityResult(Activity.RESULT_OK, Intent()))
@ -50,6 +61,7 @@ class HomeRobot {
}
fun assertContainsItem(index: Int, item: FavouriteContent) = apply {
removeItemAnimations()
val isFavouriteResourceId = if (item.isFavourite) {
R.drawable.favorite_24
} else {
@ -69,6 +81,7 @@ class HomeRobot {
}
fun clickOnContentItem(index: Int, item: Content) = apply {
removeItemAnimations()
Espresso.onView(withId(R.id.recycler))
.perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(index))
@ -91,6 +104,7 @@ class HomeRobot {
}
fun assertContainsNoItems() = apply {
removeItemAnimations()
Espresso.onView(withId(R.id.recycler))
.check(matches(hasChildCount(0)))
}

View file

@ -0,0 +1,25 @@
package org.fnives.test.showcase.android.testutil.viewaction.recycler
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.matcher.ViewMatchers
import org.hamcrest.Matcher
/**
* Sets the [RecyclerView]'s [itemAnimator][RecyclerView.setItemAnimator] to null, thus disabling animations.
*/
class RemoveItemAnimations : ViewAction {
override fun getConstraints(): Matcher<View> =
ViewMatchers.isAssignableFrom(RecyclerView::class.java)
override fun getDescription(): String =
"Remove item animations"
override fun perform(uiController: UiController, view: View) {
val recycler: RecyclerView = view as RecyclerView
recycler.itemAnimator = null
uiController.loopMainThreadUntilIdle()
}
}