JustAnotherDev
JustAnotherDev

Reputation: 475

Transition animation scale does not disable animation inside application

I encountered a problem while doing some Espresso UI testing: I want to disable the animations inside my application so that i don't need to set timeouts/need to wait for the animation to finish.

Disabling the Window animation scale, Transition animation scale, Animator duration scale inside the developers options on my android device does not disable the animations while navigating between fragments with the navigation component of android.

The animation is being set like following:

<action
    android:id="@+id/confirmationAction"
    app:destination="@id/confirmationFragment"
    app:enterAnim="@anim/slide_in_right"
    app:exitAnim="@anim/slide_out_left"
    app:popEnterAnim="@anim/slide_in_left"
    app:popExitAnim="@anim/slide_out_right" />

How can I disable this animation while running my espresso UI tests?

UPDATE:

I followed this guide of google about navigation between fragments with the navigation component.

Then I looked at the guide of google about testing my application with Espresso and on both the first guide and the second guide that i found is that the first point is to disable the animations via developers options, therefore I thought that this was the correct way of disabling the animations

Upvotes: 10

Views: 1411

Answers (2)

JustAnotherDev
JustAnotherDev

Reputation: 475

After some more research, I've found that Espresso is automatically waiting for all animations on the UI Thread to be finished before executing the assertions.

Normally you don't have anything to do if you use normal animations..

If you have to wait for a service call to return value inside your Espresso test you can write some code that will (kotlin) extend ViewInteraction class. inside your extension class you can execute the check method and catch any errors. If there is an error, you can give a small timeout (20ms) and try execute it again with a while loop. When after 2-3 seconds you still have an exception, you can then just throw that exception

Upvotes: 0

Sergei
Sergei

Reputation: 61

I experienced the same problem and best I could come up with was to disable navigation animation programmatically in case I'm running a test:

fun getBuilderWithAdditionalNavOptions(): NavOptions.Builder {
  val optionsBuilder = NavOptions.Builder()
  if (inTest)
    optionsBuilder
      .setEnterAnim(0).setExitAnim(0)
      .setPopEnterAnim(0).setPopExitAnim(0)

  return optionsBuilder
}

Where inTest is a project-wide variable that's set to true when you're running espresso tests.

Upvotes: 0

Related Questions