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.assertNotNull;
     22 import static org.junit.Assert.assertTrue;
     23 
     24 import android.app.Activity;
     25 import android.app.Instrumentation;
     26 import android.content.res.XmlResourceParser;
     27 import android.graphics.Matrix;
     28 import android.support.test.InstrumentationRegistry;
     29 import android.support.test.filters.MediumTest;
     30 import android.support.test.rule.ActivityTestRule;
     31 import android.support.test.runner.AndroidJUnit4;
     32 import android.util.AttributeSet;
     33 import android.util.Xml;
     34 import android.view.View;
     35 import android.view.animation.Animation;
     36 import android.view.animation.LinearInterpolator;
     37 import android.view.animation.RotateAnimation;
     38 import android.view.animation.Transformation;
     39 import android.view.cts.R;
     40 
     41 import org.junit.Before;
     42 import org.junit.Rule;
     43 import org.junit.Test;
     44 import org.junit.runner.RunWith;
     45 
     46 @MediumTest
     47 @RunWith(AndroidJUnit4.class)
     48 public class RotateAnimationTest {
     49     private static final long DURATION = 1000;
     50     private static final float ROTATE_DELTA = 0.001f;
     51     private static final float FROM_DEGREE = 0.0f;
     52     private static final float TO_DEGREE = 90.0f;
     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 testConstructors() {
     69         // Test with null AttributeSet
     70         new RotateAnimation(mActivity, null);
     71 
     72         final XmlResourceParser parser = mActivity.getResources().getAnimation(
     73                 R.anim.anim_rotate);
     74         final AttributeSet attr = Xml.asAttributeSet(parser);
     75         assertNotNull(attr);
     76         // Test with real AttributeSet
     77         new RotateAnimation(mActivity, attr);
     78 
     79         // Test {@link RotateAnimation#RotateAnimation(float, float)}
     80         new RotateAnimation(0.6f, 0.6f);
     81         // Test negative input values
     82         new RotateAnimation(-0.6f, -0.6f);
     83 
     84         // Test {@link RotateAnimation#RotateAnimation(float, float, float, float)}
     85         new RotateAnimation(0.6f, 0.6f, 0.6f, 0.6f);
     86         // Test negative input values
     87         new RotateAnimation(-0.6f, -0.6f, -0.6f, -0.6f);
     88 
     89         // Test {@link RotateAnimation#RotateAnimation(float, float, int, float, int, float)}
     90         new RotateAnimation(0.6f, 0.6f, Animation.ABSOLUTE, 0.6f, Animation.ABSOLUTE, 0.6f);
     91         // Test negative input values
     92         new RotateAnimation(-0.6f, -0.6f, Animation.ABSOLUTE, -0.6f, Animation.ABSOLUTE, -0.6f);
     93     }
     94 
     95     @Test
     96     public void testRotateAgainstOrigin() throws Throwable {
     97         final View animWindowParent = mActivity.findViewById(R.id.anim_window_parent);
     98         final View animWindow = mActivity.findViewById(R.id.anim_window);
     99         Transformation transformation = new Transformation();
    100         // Test when mPivot x and y equal to 0.
    101         MyRotateAnimation rotateAnimation = new MyRotateAnimation(FROM_DEGREE, TO_DEGREE);
    102         rotateAnimation.setDuration(DURATION);
    103         rotateAnimation.setInterpolator(new LinearInterpolator());
    104 
    105         assertFalse(rotateAnimation.isInitialized());
    106         rotateAnimation.initialize(animWindow.getWidth(), animWindow.getHeight(),
    107                 animWindowParent.getWidth(), animWindowParent.getHeight());
    108         assertTrue(rotateAnimation.isInitialized());
    109 
    110         AnimationTestUtils.assertRunAnimation(mInstrumentation, mActivityRule, animWindow,
    111                 rotateAnimation);
    112         final long startTime = rotateAnimation.getStartTime();
    113 
    114         Matrix expectedMatrix = new Matrix();
    115         expectedMatrix.setRotate(FROM_DEGREE);
    116         rotateAnimation.getTransformation(startTime, transformation);
    117         assertMatrixEquals(expectedMatrix, transformation.getMatrix());
    118         transformation.clear();
    119         rotateAnimation.applyTransformation(0.0f, transformation);
    120         assertMatrixEquals(expectedMatrix, transformation.getMatrix());
    121 
    122         expectedMatrix.reset();
    123         expectedMatrix.setRotate((FROM_DEGREE + TO_DEGREE) / 2);
    124         rotateAnimation.getTransformation(startTime + DURATION / 2, transformation);
    125         assertMatrixEquals(expectedMatrix, transformation.getMatrix());
    126         transformation.clear();
    127         rotateAnimation.applyTransformation(0.5f, transformation);
    128         assertMatrixEquals(expectedMatrix, transformation.getMatrix());
    129 
    130         expectedMatrix.reset();
    131         expectedMatrix.setRotate(TO_DEGREE);
    132         rotateAnimation.getTransformation(startTime + DURATION, transformation);
    133         assertMatrixEquals(expectedMatrix, transformation.getMatrix());
    134         rotateAnimation.applyTransformation(1.0f, transformation);
    135         assertMatrixEquals(expectedMatrix, transformation.getMatrix());
    136     }
    137 
    138     private void assertMatrixEquals(Matrix expectedMatrix, Matrix actualMatrix) {
    139         final float[] expectedMatrixValues = new float[9];
    140         final float[] actualMatrixValues = new float[9];
    141         expectedMatrix.getValues(expectedMatrixValues);
    142         actualMatrix.getValues(actualMatrixValues);
    143         for (int i = 0; i < 9; i++) {
    144             assertEquals(expectedMatrixValues[i], actualMatrixValues[i], ROTATE_DELTA);
    145         }
    146     }
    147 
    148     @Test
    149     public void testRotateAgainstPoint() throws Throwable {
    150         final View animWindowParent = mActivity.findViewById(R.id.anim_window_parent);
    151         final View animWindow = mActivity.findViewById(R.id.anim_window);
    152         Transformation transformation = new Transformation();
    153         final float pivotX = 0.2f;
    154         final float pivotY = 0.2f;
    155         final float actualPivotX = pivotX * animWindowParent.getWidth();
    156         final float actualPivotY = pivotY * animWindow.getHeight();
    157         // Test when mPivot x and y are not origin
    158         MyRotateAnimation rotateAnimation = new MyRotateAnimation(FROM_DEGREE, TO_DEGREE,
    159                     Animation.RELATIVE_TO_PARENT, pivotX, Animation.RELATIVE_TO_SELF, pivotY);
    160         rotateAnimation.setDuration(DURATION);
    161         rotateAnimation.setInterpolator(new LinearInterpolator());
    162 
    163         assertFalse(rotateAnimation.isInitialized());
    164         rotateAnimation.initialize(animWindow.getWidth(), animWindow.getHeight(),
    165                 animWindowParent.getWidth(), animWindowParent.getHeight());
    166         assertTrue(rotateAnimation.isInitialized());
    167 
    168         AnimationTestUtils.assertRunAnimation(mInstrumentation, mActivityRule, animWindow,
    169                 rotateAnimation);
    170         final long startTime = rotateAnimation.getStartTime();
    171 
    172         Matrix expectedMatrix = new Matrix();
    173         expectedMatrix.setRotate(FROM_DEGREE, actualPivotX, actualPivotY);
    174         rotateAnimation.getTransformation(startTime, transformation);
    175         assertMatrixEquals(expectedMatrix, transformation.getMatrix());
    176         transformation.clear();
    177         rotateAnimation.applyTransformation(0.0f, transformation);
    178         assertMatrixEquals(expectedMatrix, transformation.getMatrix());
    179 
    180         expectedMatrix.reset();
    181         expectedMatrix.setRotate((FROM_DEGREE + TO_DEGREE) / 2, actualPivotX, actualPivotY);
    182         rotateAnimation.getTransformation(startTime + DURATION / 2, transformation);
    183         assertMatrixEquals(expectedMatrix, transformation.getMatrix());
    184         transformation.clear();
    185         rotateAnimation.applyTransformation(0.5f, transformation);
    186         assertMatrixEquals(expectedMatrix, transformation.getMatrix());
    187 
    188         expectedMatrix.reset();
    189         expectedMatrix.setRotate(TO_DEGREE, actualPivotX, actualPivotY);
    190         rotateAnimation.getTransformation(startTime + DURATION, transformation);
    191         assertMatrixEquals(expectedMatrix, transformation.getMatrix());
    192         transformation.clear();
    193         rotateAnimation.applyTransformation(1.0f, transformation);
    194         assertMatrixEquals(expectedMatrix, transformation.getMatrix());
    195     }
    196 
    197     private static class MyRotateAnimation extends RotateAnimation {
    198 
    199         public MyRotateAnimation(float fromDegrees, float toDegrees) {
    200             super(fromDegrees, toDegrees);
    201         }
    202 
    203         public MyRotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) {
    204             super(fromDegrees, toDegrees, pivotX, pivotY);
    205         }
    206 
    207         public MyRotateAnimation(float fromDegrees, float toDegrees, int pivotXType,
    208                 float pivotX, int pivotYType, float pivotY) {
    209             super(fromDegrees, toDegrees, pivotXType, pivotX, pivotYType, pivotY);
    210         }
    211 
    212         @Override
    213         protected void applyTransformation(float interpolatedTime, Transformation t) {
    214             super.applyTransformation(interpolatedTime, t);
    215         }
    216     }
    217 }
    218