• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 
17 package android.view.animation.cts;
18 
19 import java.util.List;
20 
21 import android.app.Activity;
22 import android.content.Context;
23 import android.content.res.XmlResourceParser;
24 import android.test.ActivityInstrumentationTestCase2;
25 import android.util.AttributeSet;
26 import android.util.Xml;
27 import android.view.View;
28 import android.view.animation.AccelerateInterpolator;
29 import android.view.animation.AlphaAnimation;
30 import android.view.animation.Animation;
31 import android.view.animation.AnimationSet;
32 import android.view.animation.ScaleAnimation;
33 import android.view.animation.Transformation;
34 import android.view.animation.TranslateAnimation;
35 
36 import android.view.cts.R;
37 
38 
39 public class AnimationSetTest
40         extends ActivityInstrumentationTestCase2<AnimationTestCtsActivity> {
41 
42     private static final float DELTA = 0.001f;
43     private static final long SHORT_CHILD_DURATION = 400;
44     private static final long MEDIUM_CHILD_DURATION = 800;
45     private static final long LONG_CHILD_DURATION = 1200;
46     /**
47      * initial size for initialize(int width, int height, int parentWidth, int parentHeight)
48      */
49     private static final int INITIAL_SIZE = 100;
50     private static final long ANIMATIONSET_DURATION = 1000;
51     private Activity mActivity;
52 
AnimationSetTest()53     public AnimationSetTest() {
54         super("android.view.cts", AnimationTestCtsActivity.class);
55     }
56 
57     @Override
setUp()58     protected void setUp() throws Exception {
59         super.setUp();
60         mActivity = getActivity();
61     }
62 
testConstructor()63     public void testConstructor() {
64         new AnimationSet(true);
65 
66         final XmlResourceParser parser = mActivity.getResources().getAnimation(
67                 R.anim.anim_set);
68         final AttributeSet attr = Xml.asAttributeSet(parser);
69         assertNotNull(attr);
70         // Test with real AttributeSet
71         new AnimationSet(mActivity, attr);
72     }
73 
testInitialize()74     public void testInitialize() {
75         final AnimationSet animationSet = createAnimationSet();
76         animationSet.setDuration(ANIMATIONSET_DURATION);
77         // Before initialize, the durations are original.
78         List<Animation> children = animationSet.getAnimations();
79         assertEquals(SHORT_CHILD_DURATION, children.get(0).getDuration());
80         assertEquals(MEDIUM_CHILD_DURATION, children.get(1).getDuration());
81         assertEquals(LONG_CHILD_DURATION, children.get(2).getDuration());
82 
83         // After initialize, AnimationSet override the child values.
84         assertFalse(animationSet.isInitialized());
85         animationSet.initialize(INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE);
86         assertTrue(animationSet.isInitialized());
87         children = animationSet.getAnimations();
88         assertEquals(ANIMATIONSET_DURATION, children.get(0).getDuration());
89         assertEquals(ANIMATIONSET_DURATION, children.get(1).getDuration());
90         assertEquals(ANIMATIONSET_DURATION, children.get(2).getDuration());
91     }
92 
createAnimationSet()93     private AnimationSet createAnimationSet() {
94         AnimationSet animationSet = new AnimationSet(true);
95 
96         Animation animation1 = new AlphaAnimation(0.0f, 1.0f);
97         animation1.setDuration(SHORT_CHILD_DURATION);
98         animationSet.addAnimation(animation1);
99 
100         Animation animation2 = new ScaleAnimation(1.0f, 2.0f, 1.0f, 3.0f);
101         animation2.setDuration(MEDIUM_CHILD_DURATION);
102         animationSet.addAnimation(animation2);
103 
104         Animation animation3 = new TranslateAnimation(0.0f, 50.0f, 0.0f, 5.0f);
105         animation3.setDuration(LONG_CHILD_DURATION);
106         animationSet.addAnimation(animation3);
107 
108         return animationSet;
109     }
110 
testSetFillAfter()111     public void testSetFillAfter() {
112         final AnimationSet animationSet = createAnimationSet();
113         assertFalse(animationSet.getFillAfter());
114 
115         List<Animation> children = animationSet.getAnimations();
116         children.get(0).setFillAfter(true);
117         children.get(1).setFillAfter(false);
118 
119         animationSet.setFillAfter(true);
120         animationSet.initialize(INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE);
121         assertTrue(animationSet.getFillAfter());
122         children = animationSet.getAnimations();
123         for (int i = 0; i < children.size(); i++) {
124             assertTrue(children.get(i).getFillAfter());
125         }
126     }
127 
testSetFillBefore()128     public void testSetFillBefore() {
129         final AnimationSet animationSet = createAnimationSet();
130         assertTrue(animationSet.getFillBefore());
131 
132         List<Animation> children = animationSet.getAnimations();
133         children.get(0).setFillBefore(true);
134         children.get(1).setFillBefore(false);
135 
136         animationSet.setFillBefore(false);
137         animationSet.initialize(INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE);
138         assertFalse(animationSet.getFillBefore());
139         children = animationSet.getAnimations();
140         for (int i = 0; i < children.size(); i++) {
141             assertFalse(children.get(i).getFillBefore());
142         }
143     }
144 
testAccessDuration()145     public void testAccessDuration() {
146         final AnimationSet animationSet = createAnimationSet();
147         assertEquals(LONG_CHILD_DURATION, animationSet.getDuration());
148 
149         assertTrue(animationSet.getDuration() > ANIMATIONSET_DURATION);
150         animationSet.setDuration(ANIMATIONSET_DURATION);
151         animationSet.initialize(INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE);
152         assertEquals(ANIMATIONSET_DURATION, animationSet.getDuration());
153         final List<Animation> children = animationSet.getAnimations();
154         for (int i = 0; i < children.size(); i++) {
155             assertEquals(ANIMATIONSET_DURATION, children.get(i).getDuration());
156         }
157     }
158 
testRestrictDuration()159     public void testRestrictDuration() {
160         final AnimationSet animationSet = new AnimationSet(false);
161         Animation child = null;
162         final long[] originChildDuration = { 1000, 1000, 500 };
163         final long[] originChildStartOffset = { 2000, 1000, 0 };
164         final int[] originChildRepeatCount = { 0, 0, 4 };
165         final long[] originChildDurationHint = new long[3];
166         for (int i = 0; i < 3; i++) {
167             child = new AlphaAnimation(0.0f, 1.0f);
168             child.setDuration(originChildDuration[i]);
169             child.setStartOffset(originChildStartOffset[i]);
170             child.setRepeatCount(originChildRepeatCount[i]);
171             originChildDurationHint[i] = child.computeDurationHint();
172             animationSet.addAnimation(child);
173         }
174         final long restrictDuration = 1500;
175         animationSet.restrictDuration(restrictDuration);
176         final List<Animation> children = animationSet.getAnimations();
177 
178         assertTrue(originChildStartOffset[0] > restrictDuration);
179         assertEquals(0, children.get(0).getDuration());
180         assertEquals(restrictDuration, children.get(0).getStartOffset());
181 
182         assertTrue(originChildStartOffset[1] < restrictDuration);
183         assertTrue(originChildDurationHint[1] > restrictDuration);
184         assertTrue(children.get(1).computeDurationHint() <= restrictDuration);
185 
186         assertTrue(originChildDurationHint[2] > restrictDuration);
187         assertTrue(children.get(2).computeDurationHint() <= restrictDuration);
188         assertTrue(originChildRepeatCount[2] > children.get(2).getRepeatCount());
189     }
190 
testComputeDurationHint()191     public void testComputeDurationHint() {
192         final AnimationSet animationSet = createAnimationSet();
193         final List<Animation> children = animationSet.getAnimations();
194         long expectedDuration = 0;
195         for (int i = 0; i < children.size(); i++) {
196             expectedDuration = Math.max(expectedDuration, children.get(i).computeDurationHint());
197         }
198         assertEquals(expectedDuration, animationSet.computeDurationHint());
199     }
200 
testScaleCurrentDuration()201     public void testScaleCurrentDuration() {
202         final AnimationSet animationSet = createAnimationSet();
203         List<Animation> children = animationSet.getAnimations();
204         final long[] originDurations = new long[children.size()];
205         for (int i = 0; i < children.size(); i++) {
206             originDurations[i] = children.get(i).getDuration();
207         }
208 
209         final float scaleFactor = 2.0f;
210         animationSet.scaleCurrentDuration(scaleFactor);
211         children = animationSet.getAnimations();
212         for (int i = 0; i < children.size(); i++) {
213             assertEquals((long) (originDurations[i] * scaleFactor), children.get(i).getDuration());
214         }
215     }
216 
testAccessRepeatMode()217     public void testAccessRepeatMode() {
218         final AnimationSet animationSet = createAnimationSet();
219         animationSet.setRepeatMode(Animation.RESTART);
220         animationSet.initialize(INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE);
221         assertEquals(Animation.RESTART, animationSet.getRepeatMode());
222         List<Animation> children = animationSet.getAnimations();
223         for (int i = 0; i < children.size(); i++) {
224             assertEquals(Animation.RESTART, children.get(i).getRepeatMode());
225         }
226 
227         animationSet.setRepeatMode(Animation.REVERSE);
228         animationSet.initialize(INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE);
229         assertEquals(Animation.REVERSE, animationSet.getRepeatMode());
230         children = animationSet.getAnimations();
231         for (int i = 0; i < children.size(); i++) {
232             assertEquals(Animation.REVERSE, children.get(i).getRepeatMode());
233         }
234     }
235 
testAccessStartOffset()236     public void testAccessStartOffset() {
237         final AnimationSet animationSet = createAnimationSet();
238         assertEquals(0, animationSet.getStartOffset());
239         List<Animation> children = animationSet.getAnimations();
240         final long[] originStartOffset = new long[children.size()];
241         for (int i = 0; i < children.size(); i++) {
242             originStartOffset[i] = children.get(i).getStartOffset();
243         }
244 
245         animationSet.setStartOffset(100);
246         animationSet.initialize(INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE);
247         assertEquals(100, animationSet.getStartOffset());
248         children = animationSet.getAnimations();
249         for (int i = 0; i < children.size(); i++) {
250             assertEquals(originStartOffset[i] + animationSet.getStartOffset(),
251                     children.get(i).getStartOffset());
252         }
253 
254         assertTrue(animationSet.isInitialized());
255         animationSet.reset();
256         assertFalse(animationSet.isInitialized());
257         children = animationSet.getAnimations();
258         for (int i = 0; i < children.size(); i++) {
259             assertEquals(originStartOffset[i], children.get(i).getStartOffset());
260         }
261     }
262 
testAccessStartTime()263     public void testAccessStartTime() {
264         final AnimationSet animationSet = createAnimationSet();
265         final long[] originChildStartTime = {1000, 2000, 3000};
266         List<Animation> children = animationSet.getAnimations();
267         for (int i = 0; i < children.size(); i++) {
268             children.get(i).setStartTime(originChildStartTime[i]);
269         }
270 
271         // Get earliest start time of children animations
272         assertEquals(1000, animationSet.getStartTime());
273 
274         final long startTime = 200;
275         animationSet.setStartTime(startTime);
276         assertEquals(startTime, animationSet.getStartTime());
277 
278         children = animationSet.getAnimations();
279         for (int i = 0; i < children.size(); i++) {
280             assertEquals(startTime, children.get(i).getStartTime());
281         }
282     }
283 
testGetTransformation()284     public void testGetTransformation() {
285         final View animWindowParent = mActivity.findViewById(R.id.anim_window_parent);
286         final View animWindow = mActivity.findViewById(R.id.anim_window);
287         final AnimationSet animationSet = createAnimationSet();
288         animationSet.setDuration(2000);
289         animationSet.initialize(animWindow.getWidth(), animWindow.getHeight(),
290                 animWindowParent.getWidth(), animWindowParent.getHeight());
291 
292         AnimationTestUtils.assertRunAnimation(getInstrumentation(), animWindow, animationSet);
293         final long startTime = animationSet.getStartTime();
294 
295         assertGetTransformation(animationSet, startTime, true);
296         assertGetTransformation(animationSet, startTime + 100, true);
297         assertGetTransformation(animationSet, startTime + animationSet.getDuration(), false);
298     }
299 
assertGetTransformation(final AnimationSet animationSet, final long currentTime, final boolean result)300     private void assertGetTransformation(final AnimationSet animationSet,
301             final long currentTime, final boolean result) {
302         final Transformation transformation = new Transformation();
303         final Transformation expectedTransformation = new Transformation();
304         final Transformation tempTransformation = new Transformation();
305 
306         assertEquals(result, animationSet.getTransformation(currentTime, transformation));
307         final List<Animation> children = animationSet.getAnimations();
308         for (int i = children.size() - 1; i >= 0; i--) {
309             tempTransformation.clear();
310             children.get(i).getTransformation(currentTime, tempTransformation);
311             expectedTransformation.compose(tempTransformation);
312         }
313         assertTransformationEquals(expectedTransformation, transformation);
314     }
315 
assertTransformationEquals(final Transformation expected, final Transformation actual)316     private void assertTransformationEquals(final Transformation expected,
317             final Transformation actual) {
318         assertEquals(expected.getAlpha(), actual.getAlpha(), DELTA);
319         final float[] expectedValues = new float[9];
320         final float[] actualValues = new float[9];
321         expected.getMatrix().getValues(expectedValues);
322         actual.getMatrix().getValues(actualValues);
323         for (int i = 0; i < expectedValues.length; i++) {
324             assertEquals(expectedValues[i], actualValues[i], DELTA);
325         }
326     }
327 
testAccessAnimations()328     public void testAccessAnimations() {
329         final AnimationSet animationSet = new AnimationSet(true);
330         final Animation animation1 = new AlphaAnimation(0.0f, 1.0f);
331         animationSet.addAnimation(animation1);
332         final Animation animation2 = new AlphaAnimation(0.5f, 1.0f);
333         animationSet.addAnimation(animation2);
334         final Animation animation3 = new AlphaAnimation(1.0f, 0.5f);
335         animationSet.addAnimation(animation3);
336 
337         final List<Animation> children = animationSet.getAnimations();
338         assertEquals(3, children.size());
339         assertSame(animation1, children.get(0));
340         assertSame(animation2, children.get(1));
341         assertSame(animation3, children.get(2));
342     }
343 
testWillChangeTransformationMatrix()344     public void testWillChangeTransformationMatrix() {
345         final AnimationSet animationSet = new AnimationSet(true);
346         assertFalse(animationSet.willChangeTransformationMatrix());
347 
348         // Add first animation, this is an alpha animation and will not change
349         // the transformation matrix.
350         animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
351         assertFalse(animationSet.willChangeTransformationMatrix());
352         assertFalse(animationSet.willChangeBounds());
353 
354         // Add second animation, this is an scale animation and will change
355         // the transformation matrix.
356         animationSet.addAnimation(new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f));
357         assertTrue(animationSet.willChangeTransformationMatrix());
358         assertTrue(animationSet.willChangeBounds());
359     }
360 
testClone()361     public void testClone() throws CloneNotSupportedException {
362         final MyAnimationSet animationSet = new MyAnimationSet(false);
363         final Animation alpha = new AlphaAnimation(0.0f, 1.0f);
364         alpha.setInterpolator(new AccelerateInterpolator());
365         alpha.initialize(INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE);
366         final Animation scale = new ScaleAnimation(1.0f, 2.0f, 1.0f, 3.0f);
367         scale.initialize(INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE);
368         animationSet.addAnimation(alpha);
369         animationSet.addAnimation(scale);
370         final long startTime = 0;
371         animationSet.setStartTime(startTime);
372         animationSet.setDuration(ANIMATIONSET_DURATION);
373         animationSet.initialize(INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE);
374 
375         final AnimationSet clone = animationSet.clone();
376         clone.initialize(INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE, INITIAL_SIZE);
377         final List<Animation> children = animationSet.getAnimations();
378         final List<Animation> cloneChildren = clone.getAnimations();
379         assertEquals(children.size(), cloneChildren.size());
380         final Transformation expectedTransformation = new Transformation();
381         final Transformation transformation = new Transformation();
382         for (int i = 0; i < children.size(); i++) {
383             children.get(i).getTransformation(startTime, expectedTransformation);
384             cloneChildren.get(i).getTransformation(startTime, transformation);
385             assertTransformationEquals(expectedTransformation, transformation);
386 
387             children.get(i).getTransformation(startTime + ANIMATIONSET_DURATION / 2,
388                     expectedTransformation);
389             cloneChildren.get(i).getTransformation(startTime  + ANIMATIONSET_DURATION /2,
390                     transformation);
391             assertTransformationEquals(expectedTransformation, transformation);
392 
393             children.get(i).getTransformation(startTime + ANIMATIONSET_DURATION,
394                     expectedTransformation);
395             cloneChildren.get(i).getTransformation(startTime  + ANIMATIONSET_DURATION,
396                     transformation);
397             assertTransformationEquals(expectedTransformation, transformation);
398         }
399     }
400 
401     private static class MyAnimationSet extends AnimationSet {
402 
MyAnimationSet(boolean shareInterpolator)403         public MyAnimationSet(boolean shareInterpolator) {
404             super(shareInterpolator);
405         }
406 
407         @Override
clone()408         protected AnimationSet clone() throws CloneNotSupportedException {
409             return super.clone();
410         }
411     }
412 }
413