Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2009 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.view.cts.R;
     20 
     21 
     22 import android.app.Activity;
     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.AlphaAnimation;
     29 import android.view.animation.Animation;
     30 import android.view.animation.AnimationUtils;
     31 import android.view.animation.DecelerateInterpolator;
     32 import android.view.animation.Interpolator;
     33 import android.view.animation.Transformation;
     34 
     35 /**
     36  * Test {@link DecelerateInterpolator}.
     37  */
     38 public class DecelerateInterpolatorTest
     39         extends ActivityInstrumentationTestCase2<AnimationTestCtsActivity> {
     40 
     41     private Activity mActivity;
     42     private static final float ALPHA_DELTA = 0.001f;
     43 
     44     /** It is defined in R.anim.decelerate_alpha */
     45     private static final long DECELERATE_ALPHA_DURATION = 2000;
     46 
     47     public DecelerateInterpolatorTest() {
     48         super("android.view.cts", AnimationTestCtsActivity.class);
     49     }
     50 
     51     @Override
     52     protected void setUp() throws Exception {
     53         super.setUp();
     54         mActivity = getActivity();
     55     }
     56 
     57     public void testConstructor() {
     58         new DecelerateInterpolator();
     59 
     60         new DecelerateInterpolator(1.0f);
     61 
     62         XmlResourceParser parser = mActivity.getResources().getAnimation(R.anim.decelerate_alpha);
     63         AttributeSet attrs = Xml.asAttributeSet(parser);
     64         new DecelerateInterpolator(mActivity, attrs);
     65     }
     66 
     67     public void testDecelerateInterpolator() {
     68         final View animWindow = mActivity.findViewById(R.id.anim_window);
     69 
     70         // XML file of R.anim.decelerate_alpha
     71         // <alpha xmlns:android="http://schemas.android.com/apk/res/android"
     72         //      android:interpolator="@android:anim/decelerate_interpolator"
     73         //      android:fromAlpha="0.0"
     74         //      android:toAlpha="1.0"
     75         //      android:duration="2000" />
     76         final Animation anim = AnimationUtils.loadAnimation(mActivity, R.anim.decelerate_alpha);
     77 
     78         assertEquals(DECELERATE_ALPHA_DURATION, anim.getDuration());
     79         assertTrue(anim instanceof AlphaAnimation);
     80 
     81         // factor is 1.0f
     82         Interpolator interpolator = new DecelerateInterpolator(1.0f);
     83         anim.setInterpolator(interpolator);
     84         assertFalse(anim.hasStarted());
     85 
     86         AnimationTestUtils.assertRunAnimation(getInstrumentation(), animWindow, anim);
     87 
     88         Transformation transformation = new Transformation();
     89         long startTime = anim.getStartTime();
     90         anim.getTransformation(startTime, transformation);
     91         float alpha1 = transformation.getAlpha();
     92         assertEquals(0.0f, alpha1, ALPHA_DELTA);
     93 
     94         anim.getTransformation(startTime + 500, transformation);
     95         float alpha2 = transformation.getAlpha();
     96 
     97         anim.getTransformation(startTime + 1000, transformation);
     98         float alpha3 = transformation.getAlpha();
     99 
    100         anim.getTransformation(startTime + 1500, transformation);
    101         float alpha4 = transformation.getAlpha();
    102 
    103         anim.getTransformation(startTime + DECELERATE_ALPHA_DURATION, transformation);
    104         float alpha5 = transformation.getAlpha();
    105         assertEquals(1.0f, alpha5, ALPHA_DELTA);
    106 
    107         // check decelerating delta alpha
    108         float delta1 = alpha2 - alpha1;
    109         float delta2 = alpha3 - alpha2;
    110         float delta3 = alpha4 - alpha3;
    111         float delta4 = alpha5 - alpha4;
    112         assertTrue(delta1 > delta2);
    113         assertTrue(delta2 > delta3);
    114         assertTrue(delta3 > delta4);
    115 
    116         // factor is 1.5f, it starts even faster and ends evens slower than 1.0f
    117         interpolator = new DecelerateInterpolator(1.5f);
    118         anim.setInterpolator(interpolator);
    119 
    120         AnimationTestUtils.assertRunAnimation(getInstrumentation(), animWindow, anim);
    121 
    122         transformation = new Transformation();
    123         startTime = anim.getStartTime();
    124         anim.getTransformation(startTime, transformation);
    125         float alpha6 = transformation.getAlpha();
    126         assertEquals(0.0f, alpha1, ALPHA_DELTA);
    127 
    128         anim.getTransformation(startTime + 500, transformation);
    129         float alpha7 = transformation.getAlpha();
    130 
    131         anim.getTransformation(startTime + 1000, transformation);
    132         float alpha8 = transformation.getAlpha();
    133 
    134         anim.getTransformation(startTime + 1500, transformation);
    135         float alpha9 = transformation.getAlpha();
    136 
    137         anim.getTransformation(startTime + DECELERATE_ALPHA_DURATION, transformation);
    138         float alpha10 = transformation.getAlpha();
    139         assertEquals(1.0f, alpha5, ALPHA_DELTA);
    140 
    141         // check decelerating delta alpha
    142         float delta5 = alpha7 - alpha6;
    143         float delta6 = alpha8 - alpha7;
    144         float delta7 = alpha9 - alpha8;
    145         float delta8 = alpha10 - alpha9;
    146         assertTrue(delta5 > delta6);
    147         assertTrue(delta6 > delta7);
    148         assertTrue(delta7 > delta8);
    149 
    150         // check whether it starts even faster
    151         assertTrue(delta5 > delta1);
    152     }
    153 
    154     public void testGetInterpolation() {
    155         final float input = 0.25f;
    156         Interpolator interpolator1 = new DecelerateInterpolator(1.0f);
    157         // factor is 2.0f, it starts even faster and ends evens slower than 1.0f
    158         Interpolator interpolator2 = new DecelerateInterpolator(2.0f);
    159 
    160         float delta1 = interpolator1.getInterpolation(input);
    161         float delta2 = interpolator2.getInterpolation(input);
    162 
    163         // check whether it starts even faster
    164         assertTrue(delta2 > delta1);
    165     }
    166 }
    167