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 
     22 import android.content.Context;
     23 import android.content.res.XmlResourceParser;
     24 import android.support.test.InstrumentationRegistry;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.runner.AndroidJUnit4;
     27 import android.util.AttributeSet;
     28 import android.util.Xml;
     29 import android.view.animation.AlphaAnimation;
     30 import android.view.animation.Transformation;
     31 import android.view.cts.R;
     32 
     33 import org.junit.Before;
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 
     37 /**
     38  * Test {@link AlphaAnimation}.
     39  */
     40 @SmallTest
     41 @RunWith(AndroidJUnit4.class)
     42 public class AlphaAnimationTest {
     43     private Context mContext;
     44 
     45     @Before
     46     public void setup() {
     47         mContext = InstrumentationRegistry.getTargetContext();
     48     }
     49 
     50     @Test
     51     public void testConstructor() {
     52         XmlResourceParser parser = mContext.getResources().getAnimation(R.anim.alpha);
     53         AttributeSet attrs = Xml.asAttributeSet(parser);
     54         new AlphaAnimation(mContext, attrs);
     55 
     56         new AlphaAnimation(0.0f, 1.0f);
     57     }
     58 
     59     @Test
     60     public void testWillChangeBounds() {
     61         AlphaAnimation animation = new AlphaAnimation(mContext, null);
     62         assertFalse(animation.willChangeBounds());
     63     }
     64 
     65     @Test
     66     public void testWillChangeTransformationMatrix() {
     67         AlphaAnimation animation = new AlphaAnimation(0.0f, 0.5f);
     68         assertFalse(animation.willChangeTransformationMatrix());
     69     }
     70 
     71     @Test
     72     public void testApplyTransformation() {
     73         MyAlphaAnimation animation = new MyAlphaAnimation(0.0f, 1.0f);
     74         Transformation transformation = new Transformation();
     75         assertEquals(1.0f, transformation.getAlpha(), 0.0001f);
     76 
     77         animation.applyTransformation(0.0f, transformation);
     78         assertEquals(0.0f, transformation.getAlpha(), 0.0001f);
     79 
     80         animation.applyTransformation(0.5f, transformation);
     81         assertEquals(0.5f, transformation.getAlpha(), 0.0001f);
     82 
     83         animation.applyTransformation(1.0f, transformation);
     84         assertEquals(1.0f, transformation.getAlpha(), 0.0001f);
     85 
     86         animation = new MyAlphaAnimation(0.2f, 0.9f);
     87         transformation = new Transformation();
     88         assertEquals(1.0f, transformation.getAlpha(), 0.0001f);
     89 
     90         animation.applyTransformation(0.0f, transformation);
     91         assertEquals(0.2f, transformation.getAlpha(), 0.0001f);
     92 
     93         animation.applyTransformation(0.5f, transformation);
     94         assertEquals(0.55f, transformation.getAlpha(), 0.0001f);
     95 
     96         animation.applyTransformation(1.0f, transformation);
     97         assertEquals(0.9f, transformation.getAlpha(), 0.0001f);
     98     }
     99 
    100     private final class MyAlphaAnimation extends AlphaAnimation {
    101         public MyAlphaAnimation(float fromAlpha, float toAlpha) {
    102             super(fromAlpha, toAlpha);
    103         }
    104 
    105         @Override
    106         protected void applyTransformation(float interpolatedTime, Transformation t) {
    107             super.applyTransformation(interpolatedTime, t);
    108         }
    109     }
    110 }
    111