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