FileDocCategorySizeDatePackage
EventsTest.javaAPI DocAndroid 5.1 API26618Thu Mar 12 22:22:12 GMT 2015android.animation

EventsTest

public abstract class EventsTest extends android.test.ActivityInstrumentationTestCase2
Tests for the various lifecycle events of Animators. This abstract class is subclassed by concrete implementations that provide the actual Animator objects being tested. All of the testing mechanisms are in this class; the subclasses are only responsible for providing the mAnimator object. This test is more complicated than a typical synchronous test because much of the functionality must happen on the UI thread. Some tests do this by using the UiThreadTest annotation to automatically run the whole test on that thread. Other tests must run on the UI thread and also wait for some later event to occur before ending. These tests use a combination of an AbstractFuture mechanism and a delayed action to release that Future later.

Fields Summary
protected static final int
ANIM_DURATION
protected static final int
ANIM_DELAY
protected static final int
ANIM_MID_DURATION
protected static final int
ANIM_MID_DELAY
protected static final int
ANIM_PAUSE_DURATION
protected static final int
ANIM_PAUSE_DELAY
protected static final int
FUTURE_RELEASE_DELAY
protected static final int
ANIM_FULL_DURATION_SLOP
private boolean
mStarted
protected boolean
mRunning
private boolean
mCanceled
protected Animator.AnimatorListener
mFutureListener
protected FutureWaiter
mFuture
private Animator.AnimatorListener
mListener
protected Animator
mAnimator
Constructors Summary
public EventsTest()

        super(BasicAnimatorActivity.class);
    
Methods Summary
protected longgetTimeout()
Timeout length, based on when the animation should reasonably be complete.

        return ANIM_DURATION + ANIM_DELAY + FUTURE_RELEASE_DELAY;
    
public voidsetUp()
Sets up the fields used by each test. Subclasses must override this method to create the protected mAnimator object used in all tests. Overrides must create that animator and then call super.setup(), where further properties are set on that animator.

throws
Exception

        super.setUp();

        // mListener is the main testing mechanism of this file. The asserts of each test
        // are embedded in the listener callbacks that it implements.
        mListener = new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                // This should only be called on an animation that has not yet been started
                assertFalse(mStarted);
                assertTrue(mRunning);
                mStarted = true;
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                // This should only be called on an animation that has been started and not
                // yet canceled or ended
                assertFalse(mCanceled);
                assertTrue(mRunning || mStarted);
                mCanceled = true;
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                // This should only be called on an animation that has been started and not
                // yet ended
                assertTrue(mRunning || mStarted);
                mRunning = false;
                mStarted = false;
                super.onAnimationEnd(animation);
            }
        };

        mAnimator.addListener(mListener);
        mAnimator.setDuration(ANIM_DURATION);

        mFuture = new FutureWaiter();

        mRunning = false;
        mCanceled = false;
        mStarted = false;
    
public voidtestCancel()
Verify that calling cancel on an unstarted animator does nothing.

        mAnimator.cancel();
    
public voidtestEnd()
Verify that calling end on an unstarted animator starts/ends an animator.

        mRunning = true; // end() implicitly starts an unstarted animator
        mAnimator.end();
    
public voidtestPauseResume()
Verify that pausing and resuming an animator ends within the appropriate timeout duration.

        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Handler handler = new Handler();
                    mAnimator.addListener(mFutureListener);
                    mRunning = true;
                    mAnimator.start();
                    handler.postDelayed(new Pauser(mAnimator, mFuture), ANIM_PAUSE_DELAY);
                    handler.postDelayed(new Resumer(mAnimator, mFuture),
                            ANIM_PAUSE_DELAY + ANIM_PAUSE_DURATION);
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout() + ANIM_PAUSE_DURATION, TimeUnit.MILLISECONDS);
    
public voidtestPauseResumeDelayed()
Verify that pausing and resuming a startDelayed animator ends within the appropriate timeout duration.

        mAnimator.setStartDelay(ANIM_DELAY);
        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Handler handler = new Handler();
                    mAnimator.addListener(mFutureListener);
                    mRunning = true;
                    mAnimator.start();
                    handler.postDelayed(new Pauser(mAnimator, mFuture), ANIM_PAUSE_DELAY);
                    handler.postDelayed(new Resumer(mAnimator, mFuture),
                            ANIM_PAUSE_DELAY + ANIM_PAUSE_DURATION);
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout() + ANIM_PAUSE_DURATION + ANIM_FULL_DURATION_SLOP,
                TimeUnit.MILLISECONDS);
    
public voidtestPauseTimeout()
Verify that pausing an animator without resuming it causes a timeout.

        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Handler handler = new Handler();
                    mAnimator.addListener(mFutureListener);
                    mRunning = true;
                    mAnimator.start();
                    handler.postDelayed(new Pauser(mAnimator, mFuture), ANIM_PAUSE_DELAY);
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        try {
            mFuture.get(getTimeout() + ANIM_PAUSE_DURATION + ANIM_FULL_DURATION_SLOP,
                    TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            // Expected behavior, swallow the exception
        }
    
public voidtestPauseTimeoutDelayed()
Verify that pausing a startDelayed animator without resuming it causes a timeout.

        mAnimator.setStartDelay(ANIM_DELAY);
        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Handler handler = new Handler();
                    mAnimator.addListener(mFutureListener);
                    mRunning = true;
                    mAnimator.start();
                    handler.postDelayed(new Pauser(mAnimator, mFuture), ANIM_PAUSE_DELAY);
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        try {
            mFuture.get(getTimeout() + ANIM_PAUSE_DURATION + ANIM_FULL_DURATION_SLOP,
                    TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            // Expected behavior, swallow the exception
        }
    
public voidtestPlayingCancel()
Verify that canceling an animator that is playing does the right thing.

        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Handler handler = new Handler();
                    mAnimator.addListener(mFutureListener);
                    mRunning = true;
                    mAnimator.start();
                    handler.postDelayed(new Canceler(mAnimator, mFuture), ANIM_MID_DURATION);
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout(), TimeUnit.MILLISECONDS);
    
public voidtestPlayingDelayedCancel()
Same as testPlayingCancel, but with a startDelayed animator

        mAnimator.setStartDelay(ANIM_DELAY);
        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Handler handler = new Handler();
                    mAnimator.addListener(mFutureListener);
                    mRunning = true;
                    mAnimator.start();
                    handler.postDelayed(new Canceler(mAnimator, mFuture), ANIM_MID_DURATION);
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout(),  TimeUnit.MILLISECONDS);
    
public voidtestPlayingDelayedCancelMidDelay()
Same as testPlayingDelayedCancel, but cancel during the startDelay period

        mAnimator.setStartDelay(ANIM_DELAY);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    // Set the listener to automatically timeout after an uncanceled animation
                    // would have finished. This tests to make sure that we're not calling
                    // the listeners with cancel/end callbacks since they won't be called
                    // with the start event.
                    mFutureListener = new FutureReleaseListener(mFuture, getTimeout());
                    Handler handler = new Handler();
                    mRunning = true;
                    mAnimator.start();
                    handler.postDelayed(new Canceler(mAnimator, mFuture), ANIM_MID_DELAY);
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout() + 100,  TimeUnit.MILLISECONDS);
    
public voidtestPlayingDelayedEnd()
Same as testPlayingEnd, but with a startDelayed animator

        mAnimator.setStartDelay(ANIM_DELAY);
        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Handler handler = new Handler();
                    mAnimator.addListener(mFutureListener);
                    mRunning = true;
                    mAnimator.start();
                    handler.postDelayed(new Ender(mAnimator, mFuture), ANIM_MID_DURATION);
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout(),  TimeUnit.MILLISECONDS);
    
public voidtestPlayingDelayedEndMidDelay()
Same as testPlayingDelayedEnd, but end during the startDelay period

        mAnimator.setStartDelay(ANIM_DELAY);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    // Set the listener to automatically timeout after an uncanceled animation
                    // would have finished. This tests to make sure that we're not calling
                    // the listeners with cancel/end callbacks since they won't be called
                    // with the start event.
                    mFutureListener = new FutureReleaseListener(mFuture, getTimeout());
                    Handler handler = new Handler();
                    mRunning = true;
                    mAnimator.start();
                    handler.postDelayed(new Ender(mAnimator, mFuture), ANIM_MID_DELAY);
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout() + 100,  TimeUnit.MILLISECONDS);
    
public voidtestPlayingEnd()
Verify that ending an animator that is playing does the right thing.

        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Handler handler = new Handler();
                    mAnimator.addListener(mFutureListener);
                    mRunning = true;
                    mAnimator.start();
                    handler.postDelayed(new Ender(mAnimator, mFuture), ANIM_MID_DURATION);
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout(), TimeUnit.MILLISECONDS);
    
public voidtestStartCancel()
Verify that calling cancel on a started animator does the right thing.

        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    mRunning = true;
                    mAnimator.start();
                    mAnimator.cancel();
                    mFuture.release();
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout(), TimeUnit.MILLISECONDS);
    
public voidtestStartDelayedCancel()
Same as testStartCancel, but with a startDelayed animator

        mFutureListener = new FutureReleaseListener(mFuture);
        mAnimator.setStartDelay(ANIM_DELAY);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    mRunning = true;
                    mAnimator.start();
                    mAnimator.cancel();
                    mFuture.release();
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout(), TimeUnit.MILLISECONDS);
    
public voidtestStartDelayedDoubleCancel()
Same as testStartDoubleCancel, but with a startDelayed animator

        mAnimator.setStartDelay(ANIM_DELAY);
        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    mRunning = true;
                    mAnimator.start();
                    mAnimator.cancel();
                    mAnimator.cancel();
                    mFuture.release();
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout(),  TimeUnit.MILLISECONDS);
     
public voidtestStartDelayedDoubleEnd()
Same as testStartDoubleEnd, but with a startDelayed animator

        mAnimator.setStartDelay(ANIM_DELAY);
        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    mRunning = true;
                    mAnimator.start();
                    mAnimator.end();
                    mRunning = true; // end() implicitly starts an unstarted animator
                    mAnimator.end();
                    mFuture.release();
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout(),  TimeUnit.MILLISECONDS);
     
public voidtestStartDelayedEnd()
Same as testStartEnd, but with a startDelayed animator

        mFutureListener = new FutureReleaseListener(mFuture);
        mAnimator.setStartDelay(ANIM_DELAY);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    mRunning = true;
                    mAnimator.start();
                    mAnimator.end();
                    mFuture.release();
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout(), TimeUnit.MILLISECONDS);
    
public voidtestStartDoubleCancel()
Verifies that canceling a started animation after it has already been canceled does nothing.

        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    mRunning = true;
                    mAnimator.start();
                    mAnimator.cancel();
                    mAnimator.cancel();
                    mFuture.release();
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout(),  TimeUnit.MILLISECONDS);
    
public voidtestStartDoubleEnd()
Verifies that ending a started animation after it has already been ended does nothing.

        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    mRunning = true;
                    mAnimator.start();
                    mAnimator.end();
                    mRunning = true; // end() implicitly starts an unstarted animator
                    mAnimator.end();
                    mFuture.release();
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout(),  TimeUnit.MILLISECONDS);
    
public voidtestStartEnd()
Verify that calling end on a started animator does the right thing.

        mFutureListener = new FutureReleaseListener(mFuture);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    mRunning = true;
                    mAnimator.start();
                    mAnimator.end();
                    mFuture.release();
                } catch (junit.framework.AssertionFailedError e) {
                    mFuture.setException(new RuntimeException(e));
                }
            }
        });
        mFuture.get(getTimeout(), TimeUnit.MILLISECONDS);