diff options
author | Chet Haase <chet@google.com> | 2013-08-08 14:00:00 -0700 |
---|---|---|
committer | Chet Haase <chet@google.com> | 2013-08-12 11:51:32 -0700 |
commit | 8aa1ffb0ed292891030992c65df4e5dc8bd37524 (patch) | |
tree | 2483af50718dacfb46f0a1fc3caa4f2cefb59875 /core/tests | |
parent | 9a68f82f3730432399618bf2e4f73208f84dc87f (diff) | |
download | frameworks_base-8aa1ffb0ed292891030992c65df4e5dc8bd37524.zip frameworks_base-8aa1ffb0ed292891030992c65df4e5dc8bd37524.tar.gz frameworks_base-8aa1ffb0ed292891030992c65df4e5dc8bd37524.tar.bz2 |
pause/resume for Animators
It is now possible to pause Animator-based animations. Pausing an
animator causes it to hold the current time/value indefinitely, or
until end/cancel/resume is called. When resume() is called, it continues
from where it left off.
There is a new listener interface on Animator, AnimatorPauseListener,
which can be used to listen to pause/resume events.
Change-Id: I77d1535e792fb7bf349f549a0ac0a0d85958cb47
Diffstat (limited to 'core/tests')
-rw-r--r-- | core/tests/coretests/src/android/animation/AnimatorSetEventsTest.java | 4 | ||||
-rw-r--r-- | core/tests/coretests/src/android/animation/EventsTest.java | 160 |
2 files changed, 159 insertions, 5 deletions
diff --git a/core/tests/coretests/src/android/animation/AnimatorSetEventsTest.java b/core/tests/coretests/src/android/animation/AnimatorSetEventsTest.java index d415e4e..7eb32ee 100644 --- a/core/tests/coretests/src/android/animation/AnimatorSetEventsTest.java +++ b/core/tests/coretests/src/android/animation/AnimatorSetEventsTest.java @@ -37,14 +37,12 @@ public class AnimatorSetEventsTest extends EventsTest { button = (Button) getActivity().findViewById(R.id.animatingButton); mAnimator = new AnimatorSet(); ((AnimatorSet)mAnimator).playSequentially(xAnim, yAnim); - super.setUp(); } @Override protected long getTimeout() { - return (xAnim.getDuration() + yAnim.getDuration()) + - (xAnim.getStartDelay() + yAnim.getStartDelay()) + + return (2 * mAnimator.getDuration()) + (2 * mAnimator.getStartDelay()) + ANIM_DELAY + FUTURE_RELEASE_DELAY; } diff --git a/core/tests/coretests/src/android/animation/EventsTest.java b/core/tests/coretests/src/android/animation/EventsTest.java index 8df711b..28cfe3d 100644 --- a/core/tests/coretests/src/android/animation/EventsTest.java +++ b/core/tests/coretests/src/android/animation/EventsTest.java @@ -22,6 +22,7 @@ import android.test.suitebuilder.annotation.MediumTest; import android.test.suitebuilder.annotation.SmallTest; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** * Tests for the various lifecycle events of Animators. This abstract class is subclassed by @@ -42,12 +43,15 @@ public abstract class EventsTest protected static final int ANIM_DELAY = 100; protected static final int ANIM_MID_DURATION = ANIM_DURATION / 2; protected static final int ANIM_MID_DELAY = ANIM_DELAY / 2; + protected static final int ANIM_PAUSE_DURATION = ANIM_DELAY; + protected static final int ANIM_PAUSE_DELAY = ANIM_DELAY / 2; protected static final int FUTURE_RELEASE_DELAY = 50; + protected static final int ANIM_FULL_DURATION_SLOP = 100; private boolean mStarted; // tracks whether we've received the onAnimationStart() callback protected boolean mRunning; // tracks whether we've started the animator - private boolean mCanceled; // trackes whether we've canceled the animator - protected Animator.AnimatorListener mFutureListener; // mechanism for delaying the end of the test + private boolean mCanceled; // tracks whether we've canceled the animator + protected Animator.AnimatorListener mFutureListener; // mechanism for delaying end of the test protected FutureWaiter mFuture; // Mechanism for waiting for the UI test to complete private Animator.AnimatorListener mListener; // Listener that handles/tests the events @@ -104,6 +108,48 @@ public abstract class EventsTest }; /** + * Pauses the given animator. Used to delay pausing until some later time (after the + * animator has started playing). + */ + static class Pauser implements Runnable { + Animator mAnim; + FutureWaiter mFuture; + public Pauser(Animator anim, FutureWaiter future) { + mAnim = anim; + mFuture = future; + } + @Override + public void run() { + try { + mAnim.pause(); + } catch (junit.framework.AssertionFailedError e) { + mFuture.setException(new RuntimeException(e)); + } + } + }; + + /** + * Resumes the given animator. Used to delay resuming until some later time (after the + * animator has paused for some duration). + */ + static class Resumer implements Runnable { + Animator mAnim; + FutureWaiter mFuture; + public Resumer(Animator anim, FutureWaiter future) { + mAnim = anim; + mFuture = future; + } + @Override + public void run() { + try { + mAnim.resume(); + } catch (junit.framework.AssertionFailedError e) { + mFuture.setException(new RuntimeException(e)); + } + } + }; + + /** * Releases the given Future object when the listener's end() event is called. Specifically, * it releases it after some further delay, to give the test time to do other things right * after an animation ends. @@ -555,4 +601,114 @@ public abstract class EventsTest mFuture.get(getTimeout(), TimeUnit.MILLISECONDS); } + /** + * Verify that pausing and resuming an animator ends within + * the appropriate timeout duration. + */ + @MediumTest + public void testPauseResume() throws Exception { + 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); + } + + /** + * Verify that pausing and resuming a startDelayed animator ends within + * the appropriate timeout duration. + */ + @MediumTest + public void testPauseResumeDelayed() throws Exception { + 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); + } + + /** + * Verify that pausing an animator without resuming it causes a timeout. + */ + @MediumTest + public void testPauseTimeout() throws Exception { + 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 + } + } + + /** + * Verify that pausing a startDelayed animator without resuming it causes a timeout. + */ + @MediumTest + public void testPauseTimeoutDelayed() throws Exception { + 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 + } + } } |