From 00c222b461e70558bc67315e1b0ba0ac9161b912 Mon Sep 17 00:00:00 2001 From: Gergely Hegedus Date: Tue, 19 Jul 2022 16:33:38 +0300 Subject: [PATCH] issue#103 Removing item animations --- .../fnives/test/showcase/ui/home/HomeRobot.kt | 14 +++++++++++ .../recycler/RemoveItemAnimations.kt | 25 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 test-util-android/src/main/java/org/fnives/test/showcase/android/testutil/viewaction/recycler/RemoveItemAnimations.kt diff --git a/app/src/sharedTest/java/org/fnives/test/showcase/ui/home/HomeRobot.kt b/app/src/sharedTest/java/org/fnives/test/showcase/ui/home/HomeRobot.kt index 7b154a6..60b5acf 100644 --- a/app/src/sharedTest/java/org/fnives/test/showcase/ui/home/HomeRobot.kt +++ b/app/src/sharedTest/java/org/fnives/test/showcase/ui/home/HomeRobot.kt @@ -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(index)) @@ -91,6 +104,7 @@ class HomeRobot { } fun assertContainsNoItems() = apply { + removeItemAnimations() Espresso.onView(withId(R.id.recycler)) .check(matches(hasChildCount(0))) } diff --git a/test-util-android/src/main/java/org/fnives/test/showcase/android/testutil/viewaction/recycler/RemoveItemAnimations.kt b/test-util-android/src/main/java/org/fnives/test/showcase/android/testutil/viewaction/recycler/RemoveItemAnimations.kt new file mode 100644 index 0000000..7e54a7a --- /dev/null +++ b/test-util-android/src/main/java/org/fnives/test/showcase/android/testutil/viewaction/recycler/RemoveItemAnimations.kt @@ -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 = + 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() + } +}