Android Testing

Handle Android animations properly

Alex Zhukovich 1 min read
Handle Android animations properly
Table of Contents

Before doing some Android testing with Espresso, we need to prepare our devices and turn off the following animations:

  • Window animation scale
  • Transition animation scale
  • Animator duration scale

We can do it manually. To do this, we need to open the developer options and set Window animation scale, Transition animation scale and Animator duration scale to "Animation scale off".

Such way can create additional problems for us. In particular, when we have device farm this will lead to a lot of maintenance. Fortunately, we can set animation scale using ADB(Android Debug Bridge):

adb shell settings put global window_animation_scale 0
adb shell settings put global transition_animation_scale 0
adb shell settings put global animator_duration_scale 0

However, it doesn't mean that everything will work fine out of the box, we still need to adapt our code.

Necessary permissions are needed in the AndroidManifest.xml:

<uses-permission android:name="android.permission.SET_ANIMATION_SCALE"/>

Now we can just grant SET_ANIMATION_SCALE before running tests:

val packageName = InstrumentationRegistry.getTargetContext().packageName
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
        .executeShellCommand("pm grant $packageName android.permission.SET_ANIMATION_SCALE")

It allows us to disable animation before running tests and enable them after they are finished. Unfortunately, in such case, minimal API level would be 21.

There is another way, checking animation scale in the code, and enable/disable animations appropriately:

val animatorScale = Settings.Global.getFloat(
    contentResolver,
    Settings.Global.ANIMATOR_DURATION_SCALE,
    0.0f
)

button.setOnClickListener {
    if (animatorScale != 0.0f) {
        textView.animate()
            .alpha(1.0f)
            .setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    textView.visibility = View.VISIBLE
                }
            })
            .duration = 4000
    } else {
        textView.visibility = View.VISIBLE
    }
}

It means that we should do more work because because we should handle all animations in our code, but it gives us more flexibility.

As you can see, we have many options for preparing devices for Espresso testing. As a benefit, you can get less flaky tests.


Mobile development with Alex

A blog about Android development & testing, Best Practices, Tips and Tricks

Share
More from Mobile development with Alex
Not all UI tests are the same
Android Testing

Not all UI tests are the same

The article explores different types of UI testing, such as end-to-end testing, UI testing with fake data, pixel perfection testing, and accessibility testing. The article also includes information on how to get started with UI testing in any project.
Alex Zhukovich 8 min read
How to test Android App Shortcuts
Android Testing

How to test Android App Shortcuts

The "App Shortcuts" feature allows users to access a specific part of the application from the device's home screen. Users can see all available shortcuts by long pressing on the icon of the applications. In this article, we will learn how to test app shortcuts in Android apps.
Alex Zhukovich 5 min read
How to group Android tests
Android Testing

How to group Android tests

Nowadays, mobile apps have many screens so that the projects can have many UI tests. When a project is large, we can have hundreds or thousands of UI tests, and running them locally can be time-consuming. In this article, we will explore different ways of grouping test cases.
Alex Zhukovich 17 min read

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Mobile development with Alex.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.