Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unit test Android code that has View Animators?

I have an instance where a couple buttons are shown and hidden depending on which page in a ViewPager is being shown. The are shown and hidden with Animators. Is there a way to check for/delay unit testing until this has been completed?

I'm using Robolectric since that's probably relevant. I tried calling Robolectric.runUiThreadTasksIncludingDelayedTasks(); but this didn't seem to fix anything.

The animation code is as follows:

public static void regularFadeView(final boolean show, final View view) {
    view.animate()
            .setInterpolator(mDecelerateInterpolator)
            .alpha(show ? 1 : 0)
            .setListener(new SimpleAnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                    if (show) view.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    if (!show) view.setVisibility(View.INVISIBLE);
                }
            })
            .start();
}
like image 689
loeschg Avatar asked May 05 '14 20:05

loeschg


1 Answers

I think you could solve this problem rearranging the approach. This is, by extracting the SimpleAnimatorListener to a protected variable, and then unit test based on that. Something like:

@VisibleForTesting
SimpleAnimatorListener getAnimationListener(boolean show, View view) {
   return new SimpleAnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            if (show) view.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            if (!show) view.setVisibility(View.INVISIBLE);
        }
    }

public static void regularFadeView(boolean show, View view) {
     view.animate()
        .setInterpolator(mDecelerateInterpolator)
        .alpha(show ? 1 : 0)
        .setListener(getAnimationListener(show, view))
        .start();
}

And then on your test:

private void shouldShowViewWhenShowIsTrue() {
     View mockedView = Mockito.mock(View.class);
     SimpleAnimatorListener animationListener = getAnimationListener(true, mockedView);
     animationListener.onAnimationStart(null);
     Mockito.verify(mockedView).setVisibility(View.VISIBLE);
}

Even better could be to have instead of a method like getAnimationListener(), would be to create a FadeAnimationListener that would extend SimpleAnimatorListener, and put the animation logic there.

Hope this helps!

like image 90
Pedro Lopes Avatar answered Oct 03 '22 22:10

Pedro Lopes