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