1cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase/* 2cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Copyright (C) 2011 The Android Open Source Project 3cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * 4cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Licensed under the Apache License, Version 2.0 (the "License"); 5cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * you may not use this file except in compliance with the License. 6cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * You may obtain a copy of the License at 7cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * 8cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * http://www.apache.org/licenses/LICENSE-2.0 9cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * 10cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Unless required by applicable law or agreed to in writing, software 11cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * distributed under the License is distributed on an "AS IS" BASIS, 12cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * See the License for the specific language governing permissions and 14cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * limitations under the License. 15cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 16cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haasepackage android.animation; 17cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 18cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haaseimport android.os.Handler; 19cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haaseimport android.test.ActivityInstrumentationTestCase2; 20cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haaseimport android.test.UiThreadTest; 21cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haaseimport android.test.suitebuilder.annotation.MediumTest; 22cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haaseimport android.test.suitebuilder.annotation.SmallTest; 23cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haaseimport android.view.ViewPropertyAnimator; 24cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haaseimport android.widget.Button; 25cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haaseimport com.android.frameworks.coretests.R; 26cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 27cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haaseimport java.util.concurrent.TimeUnit; 28cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 29cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase/** 30cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Tests for the various lifecycle events of Animators. This abstract class is subclassed by 31cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * concrete implementations that provide the actual Animator objects being tested. All of the 32cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * testing mechanisms are in this class; the subclasses are only responsible for providing 33cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * the mAnimator object. 34cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * 35cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * This test is more complicated than a typical synchronous test because much of the functionality 36cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * must happen on the UI thread. Some tests do this by using the UiThreadTest annotation to 37cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * automatically run the whole test on that thread. Other tests must run on the UI thread and also 38cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * wait for some later event to occur before ending. These tests use a combination of an 39cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * AbstractFuture mechanism and a delayed action to release that Future later. 40cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 41cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haasepublic abstract class ViewPropertyAnimatorTest 42cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase extends ActivityInstrumentationTestCase2<BasicAnimatorActivity> { 43cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 44cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase protected static final int ANIM_DURATION = 400; 45cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase protected static final int ANIM_DELAY = 100; 46cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase protected static final int ANIM_MID_DURATION = ANIM_DURATION / 2; 47cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase protected static final int ANIM_MID_DELAY = ANIM_DELAY / 2; 48cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase protected static final int FUTURE_RELEASE_DELAY = 50; 49cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 50cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase private boolean mStarted; // tracks whether we've received the onAnimationStart() callback 51cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase protected boolean mRunning; // tracks whether we've started the animator 52cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase private boolean mCanceled; // trackes whether we've canceled the animator 53cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase protected Animator.AnimatorListener mFutureListener; // mechanism for delaying the end of the test 54cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase protected FutureWaiter mFuture; // Mechanism for waiting for the UI test to complete 55cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase private Animator.AnimatorListener mListener; // Listener that handles/tests the events 56cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 57cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase protected ViewPropertyAnimator mAnimator; // The animator used in the tests. Must be set in subclass 58cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase // setup() method prior to calling the superclass setup() 59cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 60cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 61cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Cancels the given animator. Used to delay cancellation until some later time (after the 62cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * animator has started playing). 63cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 64cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase protected static class Canceler implements Runnable { 65cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase ViewPropertyAnimator mAnim; 66cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase FutureWaiter mFuture; 67cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public Canceler(ViewPropertyAnimator anim, FutureWaiter future) { 68cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnim = anim; 69cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture = future; 70cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 71cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 72cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void run() { 73cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase try { 74cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnim.cancel(); 75cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } catch (junit.framework.AssertionFailedError e) { 76cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.setException(new RuntimeException(e)); 77cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 78cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 79cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase }; 80cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 81cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 82cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Timeout length, based on when the animation should reasonably be complete. 83cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 84cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase protected long getTimeout() { 85cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase return ANIM_DURATION + ANIM_DELAY + FUTURE_RELEASE_DELAY; 86cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 87cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 88cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 89cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Releases the given Future object when the listener's end() event is called. Specifically, 90cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * it releases it after some further delay, to give the test time to do other things right 91cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * after an animation ends. 92cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 93cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase protected static class FutureReleaseListener extends AnimatorListenerAdapter { 94cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase FutureWaiter mFuture; 95cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 96cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public FutureReleaseListener(FutureWaiter future) { 97cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture = future; 98cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 99cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 100cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 101cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Variant constructor that auto-releases the FutureWaiter after the specified timeout. 102cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * @param future 103cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * @param timeout 104cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 105cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public FutureReleaseListener(FutureWaiter future, long timeout) { 106cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture = future; 107cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase Handler handler = new Handler(); 108cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase handler.postDelayed(new Runnable() { 109cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 110cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void run() { 111cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.release(); 112cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 113cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase }, timeout); 114cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 115cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 116cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 117cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void onAnimationEnd(Animator animation) { 118cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase Handler handler = new Handler(); 119cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase handler.postDelayed(new Runnable() { 120cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 121cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void run() { 122cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.release(); 123cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 124cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase }, FUTURE_RELEASE_DELAY); 125cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 126cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase }; 127cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 128cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public ViewPropertyAnimatorTest() { 129cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase super(BasicAnimatorActivity.class); 130cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 131cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 132cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 133cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Sets up the fields used by each test. Subclasses must override this method to create 134cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * the protected mAnimator object used in all tests. Overrides must create that animator 135cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * and then call super.setup(), where further properties are set on that animator. 136cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * @throws Exception 137cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 138cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 139cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void setUp() throws Exception { 140cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase final BasicAnimatorActivity activity = getActivity(); 141cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase Button button = (Button) activity.findViewById(R.id.animatingButton); 142cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 143cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator = button.animate().x(100).y(100); 144cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 145cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase super.setUp(); 146cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 147cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase // mListener is the main testing mechanism of this file. The asserts of each test 148cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase // are embedded in the listener callbacks that it implements. 149cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mListener = new AnimatorListenerAdapter() { 150cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 151cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void onAnimationStart(Animator animation) { 152cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase // This should only be called on an animation that has not yet been started 153cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase assertFalse(mStarted); 154cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase assertTrue(mRunning); 155cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mStarted = true; 156cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 157cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 158cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 159cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void onAnimationCancel(Animator animation) { 160cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase // This should only be called on an animation that has been started and not 161cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase // yet canceled or ended 162cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase assertFalse(mCanceled); 163cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase assertTrue(mRunning); 164cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase assertTrue(mStarted); 165cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mCanceled = true; 166cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 167cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 168cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 169cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void onAnimationEnd(Animator animation) { 170cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase // This should only be called on an animation that has been started and not 171cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase // yet ended 172cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase assertTrue(mRunning); 173cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase assertTrue(mStarted); 174cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mRunning = false; 175cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mStarted = false; 176cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase super.onAnimationEnd(animation); 177cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 178cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase }; 179cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 180cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.setListener(mListener); 181cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.setDuration(ANIM_DURATION); 182cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 183cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture = new FutureWaiter(); 184cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 185cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mRunning = false; 186cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mCanceled = false; 187cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mStarted = false; 188cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 189cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 190cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 191cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Verify that calling cancel on an unstarted animator does nothing. 192cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 193cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @UiThreadTest 194cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @SmallTest 195cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void testCancel() throws Exception { 196cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.cancel(); 197cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 198cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 199cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 200cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Verify that calling cancel on a started animator does the right thing. 201cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 202cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @UiThreadTest 203cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @SmallTest 204cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void testStartCancel() throws Exception { 205cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFutureListener = new FutureReleaseListener(mFuture); 206cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase getActivity().runOnUiThread(new Runnable() { 207cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 208cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void run() { 209cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase try { 210cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mRunning = true; 211cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.start(); 212cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.cancel(); 213cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.release(); 214cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } catch (junit.framework.AssertionFailedError e) { 215cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.setException(new RuntimeException(e)); 216cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 217cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 218cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase }); 219cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.get(getTimeout(), TimeUnit.MILLISECONDS); 220cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 221cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 222cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 223cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Same as testStartCancel, but with a startDelayed animator 224cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 225cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @SmallTest 226cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void testStartDelayedCancel() throws Exception { 227cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFutureListener = new FutureReleaseListener(mFuture); 228cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.setStartDelay(ANIM_DELAY); 229cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase getActivity().runOnUiThread(new Runnable() { 230cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 231cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void run() { 232cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase try { 233cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mRunning = true; 234cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.start(); 235cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.cancel(); 236cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.release(); 237cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } catch (junit.framework.AssertionFailedError e) { 238cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.setException(new RuntimeException(e)); 239cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 240cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 241cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase }); 242cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.get(getTimeout(), TimeUnit.MILLISECONDS); 243cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 244cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 245cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 246cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Verify that canceling an animator that is playing does the right thing. 247cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 248cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @MediumTest 249cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void testPlayingCancel() throws Exception { 250cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFutureListener = new FutureReleaseListener(mFuture); 251cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase getActivity().runOnUiThread(new Runnable() { 252cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 253cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void run() { 254cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase try { 255cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase Handler handler = new Handler(); 256cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.setListener(mFutureListener); 257cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mRunning = true; 258cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.start(); 259cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase handler.postDelayed(new Canceler(mAnimator, mFuture), ANIM_MID_DURATION); 260cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } catch (junit.framework.AssertionFailedError e) { 261cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.setException(new RuntimeException(e)); 262cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 263cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 264cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase }); 265cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.get(getTimeout(), TimeUnit.MILLISECONDS); 266cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 267cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 268cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 269cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Same as testPlayingCancel, but with a startDelayed animator 270cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 271cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @MediumTest 272cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void testPlayingDelayedCancel() throws Exception { 273cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.setStartDelay(ANIM_DELAY); 274cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFutureListener = new FutureReleaseListener(mFuture); 275cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase getActivity().runOnUiThread(new Runnable() { 276cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 277cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void run() { 278cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase try { 279cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase Handler handler = new Handler(); 280cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.setListener(mFutureListener); 281cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mRunning = true; 282cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.start(); 283cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase handler.postDelayed(new Canceler(mAnimator, mFuture), ANIM_MID_DURATION); 284cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } catch (junit.framework.AssertionFailedError e) { 285cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.setException(new RuntimeException(e)); 286cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 287cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 288cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase }); 289cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.get(getTimeout(), TimeUnit.MILLISECONDS); 290cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 291cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 292cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 293cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Same as testPlayingDelayedCancel, but cancel during the startDelay period 294cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 295cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @MediumTest 296cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void testPlayingDelayedCancelMidDelay() throws Exception { 297cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.setStartDelay(ANIM_DELAY); 298cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase getActivity().runOnUiThread(new Runnable() { 299cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 300cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void run() { 301cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase try { 302cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase // Set the listener to automatically timeout after an uncanceled animation 303cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase // would have finished. This tests to make sure that we're not calling 304cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase // the listeners with cancel/end callbacks since they won't be called 305cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase // with the start event. 306cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFutureListener = new FutureReleaseListener(mFuture, getTimeout()); 307cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase Handler handler = new Handler(); 308cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mRunning = true; 309cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.start(); 310cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase handler.postDelayed(new Canceler(mAnimator, mFuture), ANIM_MID_DELAY); 311cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } catch (junit.framework.AssertionFailedError e) { 312cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.setException(new RuntimeException(e)); 313cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 314cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 315cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase }); 316cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.get(getTimeout() + 100, TimeUnit.MILLISECONDS); 317cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 318cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 319cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 320cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Verifies that canceling a started animation after it has already been canceled 321cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * does nothing. 322cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 323cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @MediumTest 324cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void testStartDoubleCancel() throws Exception { 325cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFutureListener = new FutureReleaseListener(mFuture); 326cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase getActivity().runOnUiThread(new Runnable() { 327cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 328cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void run() { 329cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase try { 330cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mRunning = true; 331cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.start(); 332cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.cancel(); 333cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.cancel(); 334cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.release(); 335cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } catch (junit.framework.AssertionFailedError e) { 336cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.setException(new RuntimeException(e)); 337cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 338cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 339cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase }); 340cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.get(getTimeout(), TimeUnit.MILLISECONDS); 341cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 342cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 343cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase /** 344cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase * Same as testStartDoubleCancel, but with a startDelayed animator 345cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase */ 346cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @MediumTest 347cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void testStartDelayedDoubleCancel() throws Exception { 348cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.setStartDelay(ANIM_DELAY); 349cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFutureListener = new FutureReleaseListener(mFuture); 350cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase getActivity().runOnUiThread(new Runnable() { 351cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase @Override 352cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase public void run() { 353cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase try { 354cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mRunning = true; 355cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.start(); 356cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.cancel(); 357cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mAnimator.cancel(); 358cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.release(); 359cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } catch (junit.framework.AssertionFailedError e) { 360cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.setException(new RuntimeException(e)); 361cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 362cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 363cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase }); 364cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase mFuture.get(getTimeout(), TimeUnit.MILLISECONDS); 365cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase } 366cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase 367cbbd93ae701b9af2e5054d59286bdbf6275c2838Chet Haase} 368