1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.animation.cts;
17 
18 import android.animation.Animator;
19 import android.animation.ObjectAnimator;
20 import android.animation.ValueAnimator;
21 import android.test.ActivityInstrumentationTestCase2;
22 import android.test.UiThreadTest;
23 import android.view.animation.AccelerateInterpolator;
24 
25 import java.util.List;
26 
27 public class AnimatorTest extends ActivityInstrumentationTestCase2<AnimationActivity> {
28     private AnimationActivity mActivity;
29     private Animator mAnimator;
30     private long mDuration = 1000;
AnimatorTest()31     public AnimatorTest() {
32         super(AnimationActivity.class);
33     }
34 
35     @Override
setUp()36     protected void setUp() throws Exception {
37         super.setUp();
38         setActivityInitialTouchMode(false);
39         mActivity = getActivity();
40         mAnimator = mActivity.createAnimatorWithDuration(mDuration);
41     }
42 
testConstructor()43     public void testConstructor() {
44         mAnimator = new ValueAnimator();
45         assertNotNull(mAnimator);
46     }
47 
testClone()48     public void testClone() {
49         Animator animatorClone = mAnimator.clone();
50         assertEquals(mAnimator.getDuration(), animatorClone.getDuration());
51     }
52 
testStartDelay()53     public void testStartDelay() {
54         long startDelay = 1000;
55         mAnimator.setStartDelay(startDelay);
56         assertEquals(startDelay, mAnimator.getStartDelay());
57     }
58 
59     @UiThreadTest
testStart()60     public void testStart() throws Exception {
61         mAnimator.start();
62         assertTrue(mAnimator.isRunning());
63         assertTrue(mAnimator.isStarted());
64     }
65 
testGetDuration()66     public void testGetDuration() throws Throwable {
67         final long duration = 2000;
68         Animator animatorLocal = mActivity.createAnimatorWithDuration(duration);
69         startAnimation(animatorLocal);
70         assertEquals(duration, animatorLocal.getDuration());
71     }
72 
testIsRunning()73     public void testIsRunning() throws Throwable {
74         assertFalse(mAnimator.isRunning());
75         startAnimation(mAnimator);
76         assertTrue(mAnimator.isRunning());
77     }
78 
testIsStarted()79     public void testIsStarted() throws Throwable {
80         assertFalse(mAnimator.isRunning());
81         assertFalse(mAnimator.isStarted());
82         long startDelay = 10000;
83         mAnimator.setStartDelay(startDelay);
84         startAnimation(mAnimator);
85         assertFalse(mAnimator.isRunning());
86         assertTrue(mAnimator.isStarted());
87     }
88 
testSetInterpolator()89     public void testSetInterpolator() throws Throwable {
90         AccelerateInterpolator interpolator = new AccelerateInterpolator();
91         ValueAnimator mValueAnimator = mActivity.createAnimatorWithInterpolator(interpolator);
92         startAnimation(mValueAnimator);
93         assertTrue(interpolator.equals(mValueAnimator.getInterpolator()));
94     }
95 
testCancel()96     public void testCancel() throws Throwable {
97         startAnimation(mAnimator);
98         Thread.sleep(100);
99         runTestOnUiThread(new Runnable() {
100             public void run() {
101                 mAnimator.cancel();
102             }
103         });
104         assertFalse(mAnimator.isRunning());
105     }
106 
testEnd()107     public void testEnd() throws Throwable {
108         Object object = mActivity.view.newBall;
109         String property = "y";
110         float startY = mActivity.mStartY;
111         float endY = mActivity.mStartY + mActivity.mDeltaY;
112         Animator animator = ObjectAnimator.ofFloat(object, property, startY, endY);
113         animator.setDuration(mDuration);
114         ((ObjectAnimator)animator).setRepeatCount(ValueAnimator.INFINITE);
115         animator.setInterpolator(new AccelerateInterpolator());
116         ((ObjectAnimator)animator).setRepeatMode(ValueAnimator.REVERSE);
117         startAnimation(animator);
118         Thread.sleep(100);
119         endAnimation(animator);
120         float y = mActivity.view.newBall.getY();
121         assertEquals(y, endY);
122     }
123 
testSetListener()124     public void testSetListener() throws Throwable {
125         List<Animator.AnimatorListener> listListeners = mAnimator.getListeners();
126         assertNull(listListeners);
127         MyListener listener = new MyListener();
128         assertFalse(listener.mStart);
129         assertFalse(listener.mEnd);
130         assertEquals(listener.mRepeat, 0);
131         mAnimator.addListener(listener);
132         mAnimator.setDuration(100l);
133         startAnimation(mAnimator);
134         Thread.sleep(200);
135 
136         assertTrue(listener.mStart);
137         assertFalse(listener.mEnd);
138         assertTrue(listener.mRepeat >= 0);
139 
140         mActivity.runOnUiThread(new Runnable() {
141             public void run() {
142                 mAnimator.cancel();
143             }
144         });
145         getInstrumentation().waitForIdleSync();
146         assertTrue(listener.mCancel);
147 
148         mActivity.runOnUiThread(new Runnable() {
149             public void run() {
150                 mAnimator.end();
151             }
152         });
153         getInstrumentation().waitForIdleSync();
154         assertTrue(listener.mEnd);
155     }
156 
testRemoveListener()157     public void testRemoveListener() throws Throwable {
158         List<Animator.AnimatorListener> listListenersOne = mAnimator.getListeners();
159         assertNull(listListenersOne);
160         MyListener listener = new MyListener();
161         mAnimator.addListener(listener);
162 
163         List<Animator.AnimatorListener> listListenersTwo = mAnimator.getListeners();
164         assertEquals(listListenersTwo.size(), 1);
165         mAnimator.removeListener(listener);
166 
167         List<Animator.AnimatorListener> listListenersThree = mAnimator.getListeners();
168         assertNull(listListenersThree);
169     }
170 
testRemoveAllListenerers()171     public void testRemoveAllListenerers() throws Throwable {
172         MyListener listener1 = new MyListener();
173         MyListener listener2 = new MyListener();
174         mAnimator.addListener(listener1);
175         mAnimator.addListener(listener2);
176 
177         List<Animator.AnimatorListener> listListenersOne = mAnimator.getListeners();
178         assertEquals(listListenersOne.size(), 2);
179         mAnimator.removeAllListeners();
180 
181         List<Animator.AnimatorListener> listListenersTwo = mAnimator.getListeners();
182         assertNull(listListenersTwo);
183     }
184 
testNullObjectAnimator()185     public void testNullObjectAnimator()  throws Throwable {
186         Object object = mActivity.view.newBall;
187         final ObjectAnimator animator = ObjectAnimator.ofFloat(object, "y", 0, 100);
188         MyListener listener = new MyListener();
189         animator.addListener(listener);
190         mActivity.view.newBall.setY(0);
191         startAnimation(animator);
192         int sleepCount = 0;
193         while (mActivity.view.newBall.getY() == 0 && sleepCount++ < 50) {
194             Thread.sleep(1);
195         }
196         assertNotSame(0, mActivity.view.newBall.getY());
197         runTestOnUiThread(new Runnable() {
198             @Override
199             public void run() {
200                 animator.setTarget(null);
201             }
202         });
203         assertTrue(listener.mCancel);
204     }
205 
206     class MyListener implements Animator.AnimatorListener{
207         boolean mStart = false;
208         boolean mEnd = false;
209         boolean mCancel = false;
210         int mRepeat = 0;
211 
onAnimationCancel(Animator animation)212         public void onAnimationCancel(Animator animation) {
213             mCancel = true;
214         }
215 
onAnimationEnd(Animator animation)216         public void onAnimationEnd(Animator animation) {
217             mEnd = true;
218         }
219 
onAnimationRepeat(Animator animation)220         public void onAnimationRepeat(Animator animation) {
221             mRepeat++;
222         }
223 
onAnimationStart(Animator animation)224         public void onAnimationStart(Animator animation) {
225             mStart = true;
226         }
227     }
startAnimation(final Animator animator)228     private void startAnimation(final Animator animator) throws Throwable {
229         this.runTestOnUiThread(new Runnable() {
230             public void run() {
231                 mActivity.startAnimation(animator);
232             }
233         });
234     }
235 
endAnimation(final Animator animator)236     private void endAnimation(final Animator animator) throws Throwable {
237         Thread animationRunnable = new Thread() {
238             public void run() {
239                 animator.end();
240             }
241         };
242         this.runTestOnUiThread(animationRunnable);
243     }
244 }
245 
246