1 /* 2 * Copyright (C) 2010 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 com.android.gallery3d.anim; 18 19 import android.test.suitebuilder.annotation.SmallTest; 20 import android.util.Log; 21 import android.view.animation.Interpolator; 22 23 import junit.framework.TestCase; 24 25 @SmallTest 26 public class AnimationTest extends TestCase { 27 private static final String TAG = "AnimationTest"; 28 29 public void testFloatAnimation() { 30 FloatAnimation a = new FloatAnimation(0f, 1f, 10); // value 0 to 1.0, duration 10 31 a.start(); // start animation 32 assertTrue(a.isActive()); // should be active now 33 a.calculate(0); // set start time = 0 34 assertTrue(a.get() == 0); // start value should be 0 35 a.calculate(1); // calculate value for time 1 36 assertFloatEq(a.get(), 0.1f); 37 a.calculate(5); // calculate value for time 5 38 assertTrue(a.get() == 0.5);// 39 a.calculate(9); // calculate value for time 9 40 assertFloatEq(a.get(), 0.9f); 41 a.calculate(10); // calculate value for time 10 42 assertTrue(!a.isActive()); // should be inactive now 43 assertTrue(a.get() == 1.0);// 44 a.start(); // restart 45 assertTrue(a.isActive()); // should be active now 46 a.calculate(5); // set start time = 5 47 assertTrue(a.get() == 0); // start value should be 0 48 a.calculate(5+9); // calculate for time 5+9 49 assertFloatEq(a.get(), 0.9f); 50 } 51 52 private static class MyInterpolator implements Interpolator { 53 public float getInterpolation(float input) { 54 return 4f * (input - 0.5f); // maps [0,1] to [-2,2] 55 } 56 } 57 58 public void testInterpolator() { 59 FloatAnimation a = new FloatAnimation(0f, 1f, 10); // value 0 to 1.0, duration 10 60 a.setInterpolator(new MyInterpolator()); 61 a.start(); // start animation 62 a.calculate(0); // set start time = 0 63 assertTrue(a.get() == -2); // start value should be -2 64 a.calculate(1); // calculate value for time 1 65 assertFloatEq(a.get(), -1.6f); 66 a.calculate(5); // calculate value for time 5 67 assertTrue(a.get() == 0); // 68 a.calculate(9); // calculate value for time 9 69 assertFloatEq(a.get(), 1.6f); 70 a.calculate(10); // calculate value for time 10 71 assertTrue(a.get() == 2); // 72 } 73 74 public static void assertFloatEq(float expected, float actual) { 75 if (Math.abs(actual - expected) > 1e-6) { 76 Log.v(TAG, "expected: " + expected + ", actual: " + actual); 77 fail(); 78 } 79 } 80 } 81