How to test animation

2,972 views
Skip to first unread message

Adam Styrc

unread,
May 27, 2014, 10:21:11 AM5/27/14
to robol...@googlegroups.com
Hello,
I'd like to test my UI properties such as Views visibility. The actions of showing/hiding views are wrapped into Animations. How to test it ?
I tried to use ShadowSystemClock.sleep() method to wait until animation ends but it doesn't seem to work as I expected.


    @Test
    public void testHideSearch() throws Exception {
        mListFragment.hideSearch();                                                     //<--- animation launched here
        sleep(1000);
        View searchEditText = mListFragment.getView().findViewById(R.id.filterEditText);
        assertFalse(searchEditText.getVisibility() == View.VISIBLE);
    }

What is the correct approach to the issue ?

Graham Holker

unread,
May 27, 2014, 12:07:44 PM5/27/14
to robol...@googlegroups.com
Hey Adam,

Try: Robolectric.getUiThreadScheduler().advanceBy(1000);

Hope this helps,

Graham


--

Graham Holker

Agile Engineer 

Pivotal Labs, Toronto

gho...@pivotallabs.com

416.574.1384



--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adam Styrc

unread,
May 28, 2014, 3:30:54 AM5/28/14
to robol...@googlegroups.com
That's exactly what ShadowSystemClock.sleep() does ;)
Not solving problem.


--
You received this message because you are subscribed to a topic in the Google Groups "Robolectric" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robolectric/HQpA1IafnZk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to robolectric...@googlegroups.com.

Graham Holker

unread,
May 28, 2014, 8:46:30 AM5/28/14
to robol...@googlegroups.com
Ah, my bad. AdvanceBy has worked for me.

Can you give more information about the animation? Where do you change the visibility of the view?



--

Graham Holker

Agile Engineer 

Pivotal Labs, Toronto

gho...@pivotallabs.com

416.574.1384



Adam Styrc

unread,
May 28, 2014, 9:40:02 AM5/28/14
to robol...@googlegroups.com

    public void hideSearch() {
        Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_in_top);
        mSearchEditTextContainer.startAnimation(animation);

        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mSearchEditTextContainer.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

And R.anim.slide_in_top is:
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="200"
        android:fromYDelta="0%"
        android:toYDelta="-100%" />
    <alpha
        android:duration="200"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

Graham Holker

unread,
May 28, 2014, 10:33:04 AM5/28/14
to robol...@googlegroups.com
You can use the ShadowAnimation to trigger the animation end callback.
shadowOf(view.getAnimation()).invokeEnd();

If you use animators (instead of Animation) the sleep or advanceBy should work. 

I wrote these tests to verify, so I'm including them for reference: 

   @Test
    public void testAnimation() {
        final boolean[] hasRun = {false};
 
        Animation animation = AnimationUtils.makeOutAnimation(Robolectric.application, true);
        animation.setDuration(200);

        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                hasRun[0] = true;

            }
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        View view = new View(Robolectric.application);
        view.startAnimation(animation);
        shadowOf(view.getAnimation()).invokeEnd();
 
        assertThat(hasRun[0]).isTrue();
    }

    @Test
    public void testAnimator() {
        final boolean[] hasRun = {false};
 
        View view = new View(Robolectric.application);
        ValueAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0, 1)
                .setDuration(200);
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
            }
            @Override
            public void onAnimationEnd(Animator animator) {
                hasRun[0] = true;
            }
            @Override
            public void onAnimationCancel(Animator animator) {
            }
            @Override
            public void onAnimationRepeat(Animator animator) {
            }
        });
        animator.start();
        Robolectric.getUiThreadScheduler().advanceBy(200);
        
        assertThat(hasRun[0]).isTrue();
    }


--

Graham Holker

Agile Engineer 

Pivotal Labs, Toronto

gho...@pivotallabs.com

416.574.1384



Adam Styrc

unread,
May 30, 2014, 6:27:30 AM5/30/14
to robol...@googlegroups.com
invokeEnd() works as desired. Thanks ! :)

Graham Holker

unread,
Jun 2, 2014, 8:46:17 AM6/2/14
to robol...@googlegroups.com
Cheers :)


--

Graham Holker

Agile Engineer 

Pivotal Labs, Toronto

gho...@pivotallabs.com

416.574.1384



stanisl...@theappbusiness.com

unread,
Apr 18, 2019, 1:37:14 PM4/18/19
to Robolectric
Robolectric no more supports shadowOf(Animation)
any ideas how to replace it?
Cheers :)
To unsubscribe from this group and stop receiving emails from it, send an email to robol...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Robolectric" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robolectric/HQpA1IafnZk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to robol...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robol...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Robolectric" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robolectric/HQpA1IafnZk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to robol...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robol...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Robolectric" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robolectric/HQpA1IafnZk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to robol...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robol...@googlegroups.com.

Jonathan Gerrish

unread,
Apr 18, 2019, 1:48:47 PM4/18/19
to robol...@googlegroups.com
What are you trying to test?

Can you show an example of some code you'd ideally like to write but can't right now?

To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.

stanisl...@theappbusiness.com

unread,
Apr 23, 2019, 4:23:13 AM4/23/19
to Robolectric
Yeah, sorry for replying so late (easter holidays).
So this thread suggested to use shadowOf(animationObject).invokeEnd() in order to invoke end to an animation which runs runtime while testing with robolectric.

I need to invoke end so that I can test that the view has full alfa after the animation ends, like this:
(animation gets triggered: alpha 0 -> 1 in 500ms
    shadowOf(loadingIndicator!!.animation).invokeEnd()
assertThat(loadingIndicator!!.alpha).isEqualTo(1f)
However, since animation object cannot be a shadow I'm unable to do that and I'm stuck on this and no other approaches seem to work for me.
shadowOf supports so many things from Choreographer to TextToSpeech objects, but no Animation (If I go to class Shadows.java and search for word "Anim" I won't find anything). 

stanisl...@theappbusiness.com

unread,
Apr 29, 2019, 5:48:55 AM4/29/19
to Robolectric
I have also tried

Robolectric.getForegroundThreadScheduler().advanceBy(1000, TimeUnit.MILLISECONDS)
Robolectric.flushBackgroundThreadScheduler()
and 
getShadowMainLooper().idle(1, TimeUnit.SECONDS)

with no luck.
The animation looks like:
ObjectAnimator.ofFloat(ins_loading_view, "alpha", 1f).apply {
duration = INS_LOADING_APPEAR_DUR
start()
}

and I just want to test, that the result of the animation is that the alpha of the view is 1f.

Help please

stanisl...@theappbusiness.com

unread,
Apr 30, 2019, 4:14:19 AM4/30/19
to Robolectric
Ok, I have also tried
shadowOf(Looper.getMainLooper()).idle(1, TimeUnit.SECONDS)

and was looking for shadowOf(animation) in add-on dependencies of Robolectric 4.
Still no luck
Reply all
Reply to author
Forward
0 new messages