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 17 package android.animation.cts; 18 19 import android.animation.ArgbEvaluator; 20 import android.animation.ObjectAnimator; 21 import android.animation.PropertyValuesHolder; 22 import android.animation.ValueAnimator; 23 import android.test.ActivityInstrumentationTestCase2; 24 import android.view.animation.AccelerateInterpolator; 25 import android.view.animation.Interpolator; 26 27 public class ObjectAnimatorTest extends 28 ActivityInstrumentationTestCase2<AnimationActivity> { 29 private AnimationActivity mActivity; 30 private ObjectAnimator mObjectAnimator; 31 private long mDuration = 1000; 32 33 public ObjectAnimatorTest() { 34 super(AnimationActivity.class); 35 } 36 37 @Override 38 protected void setUp() throws Exception { 39 super.setUp(); 40 setActivityInitialTouchMode(false); 41 mActivity = getActivity(); 42 mObjectAnimator = (ObjectAnimator) mActivity.createAnimatorWithDuration(mDuration); 43 } 44 45 public void testDuration() throws Throwable { 46 final long duration = 2000; 47 ObjectAnimator objectAnimatorLocal = (ObjectAnimator)mActivity.createAnimatorWithDuration( 48 duration); 49 startAnimation(objectAnimatorLocal); 50 assertEquals(duration, objectAnimatorLocal.getDuration()); 51 } 52 public void testOfFloat() throws Throwable { 53 Object object = mActivity.view.newBall; 54 String property = "y"; 55 float startY = mActivity.mStartY; 56 float endY = mActivity.mStartY + mActivity.mDeltaY; 57 ObjectAnimator objAnimator = ObjectAnimator.ofFloat(object, property, startY, endY); 58 assertTrue(objAnimator != null); 59 objAnimator.setDuration(mDuration); 60 objAnimator.setRepeatCount(ValueAnimator.INFINITE); 61 objAnimator.setInterpolator(new AccelerateInterpolator()); 62 objAnimator.setRepeatMode(ValueAnimator.REVERSE); 63 startAnimation(objAnimator); 64 assertTrue(objAnimator != null); 65 Thread.sleep(100); 66 float x = mActivity.view.newBall.getX(); 67 float y = mActivity.view.newBall.getY(); 68 assertTrue( y >= startY); 69 assertTrue( y <= endY); 70 } 71 72 public void testOfFloatBase() throws Throwable { 73 Object object = mActivity.view.newBall; 74 String property = "y"; 75 float startY = mActivity.mStartY; 76 float endY = mActivity.mStartY + mActivity.mDeltaY; 77 ObjectAnimator animator = ObjectAnimator.ofFloat(object, property, startY, endY); 78 ObjectAnimator objAnimator = new ObjectAnimator(); 79 objAnimator.setTarget(object); 80 objAnimator.setPropertyName(property); 81 assertEquals(animator.getTarget(), objAnimator.getTarget()); 82 assertEquals(animator.getPropertyName(), objAnimator.getPropertyName()); 83 } 84 85 public void testOfInt() throws Throwable { 86 Object object = mActivity.view.newBall; 87 String property = "backgroundColor"; 88 int startColor = mActivity.view.RED; 89 int endColor = mActivity.view.BLUE; 90 91 ObjectAnimator colorAnimator = ObjectAnimator.ofInt(object, property, 92 startColor, endColor); 93 colorAnimator.setDuration(1000); 94 colorAnimator.setEvaluator(new ArgbEvaluator()); 95 colorAnimator.setRepeatCount(1); 96 colorAnimator.setRepeatMode(ValueAnimator.REVERSE); 97 colorAnimator.start(); 98 startAnimation(mObjectAnimator, colorAnimator); 99 Thread.sleep(100); 100 Integer i = (Integer) colorAnimator.getAnimatedValue(); 101 //We are going from less negative value to a more negative value 102 assertTrue(i.intValue() <= startColor); 103 assertTrue(endColor <= i.intValue()); 104 } 105 106 public void testOfObject() throws Throwable { 107 Object object = mActivity.view.newBall; 108 String property = "backgroundColor"; 109 int startColor = mActivity.view.RED; 110 int endColor = mActivity.view.BLUE; 111 Object[] values = {new Integer(startColor), new Integer(endColor)}; 112 ArgbEvaluator evaluator = new ArgbEvaluator(); 113 ObjectAnimator colorAnimator = ObjectAnimator.ofObject(object, property, 114 evaluator, values); 115 colorAnimator.setDuration(1000); 116 colorAnimator.setRepeatCount(1); 117 colorAnimator.setRepeatMode(ValueAnimator.REVERSE); 118 colorAnimator.start(); 119 startAnimation(mObjectAnimator, colorAnimator); 120 Thread.sleep(100); 121 Integer i = (Integer) colorAnimator.getAnimatedValue(); 122 //We are going from less negative value to a more negative value 123 assertTrue(i.intValue() <= startColor); 124 assertTrue(endColor <= i.intValue()); 125 } 126 127 public void testOfPropertyValuesHolder() throws Throwable { 128 Object object = mActivity.view.newBall; 129 String propertyName = "backgroundColor"; 130 int startColor = mActivity.view.RED; 131 int endColor = mActivity.view.BLUE; 132 int values[] = {startColor, endColor}; 133 ArgbEvaluator evaluator = new ArgbEvaluator(); 134 PropertyValuesHolder propertyValuesHolder = PropertyValuesHolder.ofInt(propertyName, values); 135 ObjectAnimator colorAnimator = ObjectAnimator.ofPropertyValuesHolder(object, 136 propertyValuesHolder); 137 colorAnimator.setDuration(1000); 138 colorAnimator.setRepeatCount(1); 139 colorAnimator.setRepeatMode(ValueAnimator.REVERSE); 140 colorAnimator.start(); 141 startAnimation(mObjectAnimator, colorAnimator); 142 Thread.sleep(100); 143 Integer i = (Integer) colorAnimator.getAnimatedValue(); 144 //We are going from less negative value to a more negative value 145 assertTrue(i.intValue() <= startColor); 146 assertTrue(endColor <= i.intValue()); 147 } 148 149 public void testGetPropertyName() throws Throwable { 150 Object object = mActivity.view.newBall; 151 String propertyName = "backgroundColor"; 152 int startColor = mActivity.view.RED; 153 int endColor = mActivity.view.BLUE; 154 Object[] values = {new Integer(startColor), new Integer(endColor)}; 155 ArgbEvaluator evaluator = new ArgbEvaluator(); 156 ObjectAnimator colorAnimator = ObjectAnimator.ofObject(object, propertyName, 157 evaluator, values); 158 String actualPropertyName = colorAnimator.getPropertyName(); 159 assertEquals(propertyName, actualPropertyName); 160 } 161 162 public void testSetFloatValues() throws Throwable { 163 Object object = mActivity.view.newBall; 164 String property = "y"; 165 float startY = mActivity.mStartY; 166 float endY = mActivity.mStartY + mActivity.mDeltaY; 167 float[] values = {startY, endY}; 168 ObjectAnimator objAnimator = new ObjectAnimator(); 169 objAnimator.setTarget(object); 170 objAnimator.setPropertyName(property); 171 objAnimator.setFloatValues(values); 172 objAnimator.setDuration(mDuration); 173 objAnimator.setRepeatCount(ValueAnimator.INFINITE); 174 objAnimator.setInterpolator(new AccelerateInterpolator()); 175 objAnimator.setRepeatMode(ValueAnimator.REVERSE); 176 startAnimation(objAnimator); 177 Thread.sleep(100); 178 float y = mActivity.view.newBall.getY(); 179 assertTrue( y >= startY); 180 assertTrue( y <= endY); 181 } 182 183 public void testGetTarget() throws Throwable { 184 Object object = mActivity.view.newBall; 185 String propertyName = "backgroundColor"; 186 int startColor = mActivity.view.RED; 187 int endColor = mActivity.view.BLUE; 188 Object[] values = {new Integer(startColor), new Integer(endColor)}; 189 ArgbEvaluator evaluator = new ArgbEvaluator(); 190 ObjectAnimator colorAnimator = ObjectAnimator.ofObject(object, propertyName, 191 evaluator, values); 192 Object target = colorAnimator.getTarget(); 193 assertEquals(object, target); 194 } 195 196 public void testClone() throws Throwable { 197 Object object = mActivity.view.newBall; 198 String property = "y"; 199 float startY = mActivity.mStartY; 200 float endY = mActivity.mStartY + mActivity.mDeltaY; 201 Interpolator interpolator = new AccelerateInterpolator(); 202 ObjectAnimator objAnimator = ObjectAnimator.ofFloat(object, property, startY, endY); 203 objAnimator.setDuration(mDuration); 204 objAnimator.setRepeatCount(ValueAnimator.INFINITE); 205 objAnimator.setInterpolator(interpolator); 206 objAnimator.setRepeatMode(ValueAnimator.REVERSE); 207 ObjectAnimator cloneAnimator = objAnimator.clone(); 208 209 assertEquals(mDuration, cloneAnimator.getDuration()); 210 assertEquals(ValueAnimator.INFINITE, cloneAnimator.getRepeatCount()); 211 assertEquals(ValueAnimator.REVERSE, cloneAnimator.getRepeatMode()); 212 assertEquals(object, cloneAnimator.getTarget()); 213 assertEquals(property, cloneAnimator.getPropertyName()); 214 assertEquals(interpolator, cloneAnimator.getInterpolator()); 215 } 216 217 public void testIsStarted() throws Throwable { 218 Object object = mActivity.view.newBall; 219 String property = "y"; 220 float startY = mActivity.mStartY; 221 float endY = mActivity.mStartY + mActivity.mDeltaY; 222 Interpolator interpolator = new AccelerateInterpolator(); 223 ObjectAnimator objAnimator = ObjectAnimator.ofFloat(object, property, startY, endY); 224 objAnimator.setDuration(mDuration); 225 objAnimator.setRepeatCount(ValueAnimator.INFINITE); 226 objAnimator.setInterpolator(interpolator); 227 objAnimator.setRepeatMode(ValueAnimator.REVERSE); 228 startAnimation(objAnimator); 229 Thread.sleep(100); 230 assertTrue(objAnimator.isStarted()); 231 Thread.sleep(100); 232 } 233 234 private void startAnimation(final ObjectAnimator mObjectAnimator) throws Throwable { 235 Thread mAnimationRunnable = new Thread() { 236 public void run() { 237 mActivity.startAnimation(mObjectAnimator); 238 } 239 }; 240 this.runTestOnUiThread(mAnimationRunnable); 241 } 242 private void startAnimation(final ObjectAnimator mObjectAnimator, final 243 ObjectAnimator colorAnimator) throws Throwable { 244 Thread mAnimationRunnable = new Thread() { 245 public void run() { 246 mActivity.startAnimation(mObjectAnimator, colorAnimator); 247 } 248 }; 249 this.runTestOnUiThread(mAnimationRunnable); 250 } 251 } 252