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 android.app.Activity; 20 import android.content.res.XmlResourceParser; 21 import android.test.ActivityInstrumentationTestCase2; 22 import android.util.AttributeSet; 23 import android.util.Xml; 24 import android.view.View; 25 import android.view.animation.AlphaAnimation; 26 import android.view.animation.Animation; 27 import android.view.animation.AnimationUtils; 28 import android.view.animation.CycleInterpolator; 29 import android.view.animation.Interpolator; 30 import android.view.animation.Transformation; 31 32 import com.android.cts.stub.R; 33 34 35 /** 36 * Test {@link CycleInterpolator}. 37 */ 38 public class CycleInterpolatorTest 39 extends ActivityInstrumentationTestCase2<AnimationTestStubActivity> { 40 41 private Activity mActivity; 42 43 /** It is defined in R.anim.cycle_alpha */ 44 private static final long CYCLE_ALPHA_DURATION = 2000; 45 private static final float ALPHA_DELTA = 0.001f; 46 47 public CycleInterpolatorTest() { 48 super("com.android.cts.stub", AnimationTestStubActivity.class); 49 } 50 51 @Override 52 protected void setUp() throws Exception { 53 super.setUp(); 54 mActivity = getActivity(); 55 } 56 57 public void testConstructors() { 58 new CycleInterpolator(1.0f); 59 60 XmlResourceParser parser = mActivity.getResources().getAnimation(R.anim.cycle_alpha); 61 AttributeSet attrs = Xml.asAttributeSet(parser); 62 new CycleInterpolator(mActivity, attrs); 63 } 64 65 public void testCycyleInterpolator() { 66 final View animWindow = mActivity.findViewById(R.id.anim_window); 67 final Animation anim = AnimationUtils.loadAnimation(mActivity, R.anim.cycle_alpha); 68 assertEquals(CYCLE_ALPHA_DURATION, anim.getDuration()); 69 assertTrue(anim instanceof AlphaAnimation); 70 71 // cycle only once. 72 Interpolator interpolator = new CycleInterpolator(1.0f); 73 anim.setInterpolator(interpolator); 74 assertFalse(anim.hasStarted()); 75 76 AnimationTestUtils.assertRunAnimation(getInstrumentation(), animWindow, anim); 77 78 Transformation transformation = new Transformation(); 79 long startTime = anim.getStartTime(); 80 anim.getTransformation(startTime, transformation); 81 final float alpha1 = transformation.getAlpha(); 82 assertEquals(0.0f, alpha1, ALPHA_DELTA); 83 84 anim.getTransformation(startTime + 500, transformation); 85 final float alpha2 = transformation.getAlpha(); 86 assertEquals(1.0f, alpha2, ALPHA_DELTA); 87 88 anim.getTransformation(startTime + 1000, transformation); 89 final float alpha3 = transformation.getAlpha(); 90 91 anim.getTransformation(startTime + 1500, transformation); 92 final float alpha4 = transformation.getAlpha(); 93 assertEquals(-1.0f, alpha4, ALPHA_DELTA); 94 95 anim.getTransformation(startTime + CYCLE_ALPHA_DURATION, transformation); 96 final float alpha5 = transformation.getAlpha(); 97 assertEquals(0.0f, alpha5, ALPHA_DELTA); 98 99 // check cycle sinusoidal delta alpha 100 final float delta1 = alpha2 - alpha1; 101 final float delta2 = alpha3 - alpha2; 102 final float delta3 = alpha4 - alpha3; 103 final float delta4 = alpha5 - alpha4; 104 assertEquals(alpha1, alpha3, ALPHA_DELTA); 105 assertEquals(alpha3, alpha5, ALPHA_DELTA); 106 assertEquals(alpha2, -alpha4, ALPHA_DELTA); 107 assertEquals(delta1, -delta2, ALPHA_DELTA); 108 assertEquals(delta2, delta3, ALPHA_DELTA); 109 assertEquals(delta3, -delta4, ALPHA_DELTA); 110 111 // cycle twice. 112 interpolator = new CycleInterpolator(2.0f); 113 anim.setInterpolator(interpolator); 114 115 AnimationTestUtils.assertRunAnimation(getInstrumentation(), animWindow, anim); 116 117 transformation = new Transformation(); 118 startTime = anim.getStartTime(); 119 anim.getTransformation(startTime, transformation); 120 final float alpha6 = transformation.getAlpha(); 121 assertEquals(0.0f, alpha1, ALPHA_DELTA); 122 123 anim.getTransformation(startTime + 250, transformation); 124 final float alpha7 = transformation.getAlpha(); 125 assertEquals(1.0f, alpha7, ALPHA_DELTA); 126 127 anim.getTransformation(startTime + 500, transformation); 128 final float alpha8 = transformation.getAlpha(); 129 130 anim.getTransformation(startTime + 750, transformation); 131 final float alpha9 = transformation.getAlpha(); 132 assertEquals(-1.0f, alpha9, ALPHA_DELTA); 133 134 anim.getTransformation(startTime + 1000, transformation); 135 final float alpha10 = transformation.getAlpha(); 136 137 anim.getTransformation(startTime + 1250, transformation); 138 final float alpha11 = transformation.getAlpha(); 139 140 anim.getTransformation(startTime + 1500, transformation); 141 final float alpha12 = transformation.getAlpha(); 142 143 anim.getTransformation(startTime + 1750, transformation); 144 final float alpha13 = transformation.getAlpha(); 145 146 anim.getTransformation(startTime + CYCLE_ALPHA_DURATION, transformation); 147 final float alpha14 = transformation.getAlpha(); 148 assertEquals(0.0f, alpha14, ALPHA_DELTA); 149 150 // check cycle sinusoidal delta alpha 151 final float delta5 = alpha7 - alpha6; 152 final float delta6 = alpha8 - alpha7; 153 final float delta7 = alpha9 - alpha8; 154 final float delta8 = alpha10 - alpha9; 155 final float delta9 = alpha11 - alpha10; 156 final float delta10 = alpha12 - alpha11; 157 final float delta11 = alpha13 - alpha12; 158 final float delta12 = alpha14 - alpha13; 159 assertEquals(alpha6, alpha8, ALPHA_DELTA); 160 assertEquals(alpha8, alpha10, ALPHA_DELTA); 161 assertEquals(alpha10, alpha12, ALPHA_DELTA); 162 assertEquals(alpha12, alpha14, ALPHA_DELTA); 163 assertEquals(alpha7, -alpha9, ALPHA_DELTA); 164 assertEquals(alpha9, -alpha11, ALPHA_DELTA); 165 assertEquals(alpha11, -alpha13, ALPHA_DELTA); 166 assertEquals(delta6, -delta5, ALPHA_DELTA); 167 assertEquals(delta7, delta6, ALPHA_DELTA); 168 assertEquals(delta8, -delta7, ALPHA_DELTA); 169 assertEquals(delta9, delta8, ALPHA_DELTA); 170 assertEquals(delta10, -delta9, ALPHA_DELTA); 171 assertEquals(delta11, delta10, ALPHA_DELTA); 172 assertEquals(delta12, -delta11, ALPHA_DELTA); 173 174 // check whether it cycle the same 175 assertEquals(alpha6, alpha1, ALPHA_DELTA); 176 assertEquals(alpha7, alpha2, ALPHA_DELTA); 177 assertEquals(alpha8, alpha3, ALPHA_DELTA); 178 assertEquals(alpha9, alpha4, ALPHA_DELTA); 179 assertEquals(alpha10, alpha5, ALPHA_DELTA); 180 assertEquals(alpha10, alpha1, ALPHA_DELTA); 181 assertEquals(alpha11, alpha2, ALPHA_DELTA); 182 assertEquals(alpha12, alpha3, ALPHA_DELTA); 183 assertEquals(alpha13, alpha4, ALPHA_DELTA); 184 assertEquals(alpha14, alpha5, ALPHA_DELTA); 185 assertEquals(delta5, delta1, ALPHA_DELTA); 186 assertEquals(delta6, delta2, ALPHA_DELTA); 187 assertEquals(delta7, delta3, ALPHA_DELTA); 188 assertEquals(delta8, delta4, ALPHA_DELTA); 189 assertEquals(delta9, delta1, ALPHA_DELTA); 190 assertEquals(delta10, delta2, ALPHA_DELTA); 191 assertEquals(delta11, delta3, ALPHA_DELTA); 192 assertEquals(delta12, delta4, ALPHA_DELTA); 193 } 194 195 public void testGetInterpolation() { 196 CycleInterpolator cycleInterpolator = new CycleInterpolator(2.0f); 197 final float out1 = cycleInterpolator.getInterpolation(0.0f); 198 final float out2 = cycleInterpolator.getInterpolation(0.125f); 199 final float out3 = cycleInterpolator.getInterpolation(0.25f); 200 final float out4 = cycleInterpolator.getInterpolation(0.375f); 201 final float out5 = cycleInterpolator.getInterpolation(0.5f); 202 final float out6 = cycleInterpolator.getInterpolation(0.625f); 203 final float out7 = cycleInterpolator.getInterpolation(0.75f); 204 final float out8 = cycleInterpolator.getInterpolation(0.875f); 205 final float out9 = cycleInterpolator.getInterpolation(1.0f); 206 final float delta1 = out2 - out1; 207 final float delta2 = out3 - out2; 208 final float delta3 = out4 - out3; 209 final float delta4 = out5 - out4; 210 final float delta5 = out6 - out5; 211 final float delta6 = out7 - out6; 212 final float delta7 = out8 - out7; 213 final float delta8 = out9 - out8; 214 assertEquals(out1, out3, ALPHA_DELTA); 215 assertEquals(out3, out5, ALPHA_DELTA); 216 assertEquals(out5, out7, ALPHA_DELTA); 217 assertEquals(out7, out9, ALPHA_DELTA); 218 assertEquals(delta1, -delta2, ALPHA_DELTA); 219 assertEquals(delta2, delta3, ALPHA_DELTA); 220 assertEquals(delta3, -delta4, ALPHA_DELTA); 221 assertEquals(delta4, delta5, ALPHA_DELTA); 222 assertEquals(delta5, -delta6, ALPHA_DELTA); 223 assertEquals(delta6, delta7, ALPHA_DELTA); 224 assertEquals(delta7, -delta8, ALPHA_DELTA); 225 226 final float timeDelta = 0.015f; 227 assertEquals(cycleInterpolator.getInterpolation(0.0f + timeDelta), 228 cycleInterpolator.getInterpolation(0.5f + timeDelta), ALPHA_DELTA); 229 assertEquals(cycleInterpolator.getInterpolation(0.25f + timeDelta), 230 cycleInterpolator.getInterpolation(0.75f + timeDelta), ALPHA_DELTA); 231 } 232 } 233