Home | History | Annotate | Download | only in cts
      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 static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertTrue;
     22 
     23 import android.app.Activity;
     24 import android.app.Instrumentation;
     25 import android.content.res.XmlResourceParser;
     26 import android.util.AttributeSet;
     27 import android.util.Xml;
     28 import android.view.View;
     29 import android.view.animation.AccelerateDecelerateInterpolator;
     30 import android.view.animation.AlphaAnimation;
     31 import android.view.animation.Animation;
     32 import android.view.animation.AnimationUtils;
     33 import android.view.animation.Interpolator;
     34 import android.view.animation.Transformation;
     35 import android.view.cts.R;
     36 
     37 import androidx.test.InstrumentationRegistry;
     38 import androidx.test.filters.MediumTest;
     39 import androidx.test.rule.ActivityTestRule;
     40 import androidx.test.runner.AndroidJUnit4;
     41 
     42 import org.junit.Before;
     43 import org.junit.Rule;
     44 import org.junit.Test;
     45 import org.junit.runner.RunWith;
     46 
     47 @MediumTest
     48 @RunWith(AndroidJUnit4.class)
     49 public class AccelerateDecelerateInterpolatorTest {
     50     private static final float ALPHA_DELTA = 0.001f;
     51     /** It is defined in R.anim.accelarate_decelerate_alpha */
     52     private static final long ALPHA_DURATION = 2000;
     53 
     54     private Instrumentation mInstrumentation;
     55     private Activity mActivity;
     56 
     57     @Rule
     58     public ActivityTestRule<AnimationTestCtsActivity> mActivityRule =
     59             new ActivityTestRule<>(AnimationTestCtsActivity.class);
     60 
     61     @Before
     62     public void setup() {
     63         mInstrumentation = InstrumentationRegistry.getInstrumentation();
     64         mActivity = mActivityRule.getActivity();
     65     }
     66 
     67     @Test
     68     public void testConstructor() {
     69         new AccelerateDecelerateInterpolator();
     70 
     71         XmlResourceParser parser = mActivity.getResources().getAnimation(
     72                 R.anim.accelerate_decelerate_alpha);
     73         AttributeSet attrs = Xml.asAttributeSet(parser);
     74         new AccelerateDecelerateInterpolator(mActivity, attrs);
     75     }
     76 
     77     @Test
     78     public void testAccelerateDecelerateInterpolator() throws Throwable {
     79         final View animWindow = mActivity.findViewById(R.id.anim_window);
     80 
     81         // XML file of R.anim.accelerate_decelerate_alpha
     82         // <alpha xmlns:android="http://schemas.android.com/apk/res/android"
     83         //      android:interpolator="@android:anim/accelerate_decelerate_interpolator"
     84         //      android:fromAlpha="0.0"
     85         //      android:toAlpha="1.0"
     86         //      android:duration="2000" />
     87         final Animation anim = AnimationUtils.loadAnimation(mActivity,
     88                 R.anim.accelerate_decelerate_alpha);
     89         assertEquals(ALPHA_DURATION, anim.getDuration());
     90         assertTrue(anim instanceof AlphaAnimation);
     91         assertFalse(anim.hasStarted());
     92 
     93         AnimationTestUtils.assertRunAnimation(mInstrumentation, mActivityRule, animWindow, anim);
     94 
     95         Transformation transformation = new Transformation();
     96         long startTime = anim.getStartTime();
     97         anim.getTransformation(startTime, transformation);
     98         float alpha1 = transformation.getAlpha();
     99         assertEquals(0.0f, alpha1, ALPHA_DELTA);
    100 
    101         anim.getTransformation(startTime + 500, transformation);
    102         float alpha2 = transformation.getAlpha();
    103 
    104         anim.getTransformation(startTime + 1000, transformation);
    105         float alpha3 = transformation.getAlpha();
    106 
    107         anim.getTransformation(startTime + 1500, transformation);
    108         float alpha4 = transformation.getAlpha();
    109 
    110         anim.getTransformation(startTime + ALPHA_DURATION, transformation);
    111         float alpha5 = transformation.getAlpha();
    112         assertEquals(1.0f, alpha5, ALPHA_DELTA);
    113 
    114         // check accelerating and decelerating delta alpha
    115         // starts and ends slowly, accelerates through middle
    116         float delta1 = alpha2 - alpha1;
    117         float delta2 = alpha3 - alpha2;
    118         float delta3 = alpha4 - alpha3;
    119         float delta4 = alpha5 - alpha4;
    120         assertTrue(delta1 < delta2);
    121         assertTrue(delta3 > delta4);
    122     }
    123 
    124     @Test
    125     public void testGetInterpolation() {
    126         Interpolator interpolator = new AccelerateDecelerateInterpolator();
    127 
    128         float alpha1 = interpolator.getInterpolation(0f);
    129         float alpha2 = interpolator.getInterpolation(0.25f);
    130         float alpha3 = interpolator.getInterpolation(0.5f);
    131         float alpha4 = interpolator.getInterpolation(0.75f);
    132         float alpha5 = interpolator.getInterpolation(1f);
    133         // check accelerating and decelerating delta alpha
    134         // starts and ends slowly, accelerates through middle
    135         float delta1 = alpha2 - alpha1;
    136         float delta2 = alpha3 - alpha2;
    137         float delta3 = alpha4 - alpha3;
    138         float delta4 = alpha5 - alpha4;
    139         assertTrue(delta1 < delta2);
    140         assertTrue(delta3 > delta4);
    141     }
    142 }
    143