1 /*
2  * Copyright (C) 2012 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.animation.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.animation.ArgbEvaluator;
23 import android.animation.FloatArrayEvaluator;
24 import android.animation.FloatEvaluator;
25 import android.animation.IntArrayEvaluator;
26 import android.animation.IntEvaluator;
27 import android.animation.PointFEvaluator;
28 import android.animation.RectEvaluator;
29 import android.graphics.Color;
30 import android.graphics.PointF;
31 import android.graphics.Rect;
32 import android.support.test.filters.SmallTest;
33 import android.support.test.runner.AndroidJUnit4;
34 
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 
38 /**
39  * Tests for the various Evaluator classes in android.animation
40  */
41 @SmallTest
42 @RunWith(AndroidJUnit4.class)
43 public class EvaluatorTest {
44     private static final float EPSILON = 0.001f;
45 
46     @Test
testFloatEvaluator()47     public void testFloatEvaluator() {
48         float start = 0.0f;
49         float end = 1.0f;
50         float fraction = 0.5f;
51         FloatEvaluator floatEvaluator = new FloatEvaluator();
52 
53         float result = floatEvaluator.evaluate(0, start, end);
54         assertEquals(start, result, EPSILON);
55 
56         result = floatEvaluator.evaluate(fraction, start, end);
57         assertEquals(.5f, result, EPSILON);
58 
59         result = floatEvaluator.evaluate(1, start, end);
60         assertEquals(end, result, EPSILON);
61     }
62 
63     @Test
testFloatArrayEvaluator()64     public void testFloatArrayEvaluator() {
65         FloatArrayEvaluator evaluator = new FloatArrayEvaluator();
66         floatArrayEvaluatorTestImpl(evaluator, null);
67 
68         float[] reusableArray = new float[2];
69         FloatArrayEvaluator evaluator2 = new FloatArrayEvaluator(reusableArray);
70         floatArrayEvaluatorTestImpl(evaluator2, reusableArray);
71     }
72 
floatArrayEvaluatorTestImpl(FloatArrayEvaluator evaluator, float[] reusedArray)73     private void floatArrayEvaluatorTestImpl(FloatArrayEvaluator evaluator, float[] reusedArray) {
74         float[] start = {0f, 0f};
75         float[] end = {.8f, 1.0f};
76         float fraction = 0.5f;
77 
78         float[] result = evaluator.evaluate(0, start, end);
79         assertEquals(start[0], result[0], EPSILON);
80         assertEquals(start[1], result[1], EPSILON);
81 
82         result = evaluator.evaluate(fraction, start, end);
83         assertEquals(.4f, result[0], EPSILON);
84         assertEquals(.5f, result[1], EPSILON);
85 
86         result = evaluator.evaluate(1, start, end);
87         assertEquals(end[0], result[0], EPSILON);
88         assertEquals(end[1], result[1], EPSILON);
89 
90         if (reusedArray != null) {
91             assertEquals(reusedArray, result);
92         }
93     }
94 
95     @Test
testArgbEvaluator()96     public void testArgbEvaluator() throws Throwable {
97         final int START =  0xffFF8080;
98         final int END = 0xff8080FF;
99         int aSTART = Color.alpha(START);
100         int rSTART = Color.red(START);
101         int gSTART = Color.green(START);
102         int bSTART = Color.blue(START);
103         int aEND = Color.alpha(END);
104         int rEND = Color.red(END);
105         int gEND = Color.green(END);
106         int bEND = Color.blue(END);
107 
108         final ArgbEvaluator evaluator = new ArgbEvaluator();
109 
110         int result = (Integer) evaluator.evaluate(0, START, END);
111         int aResult = Color.alpha(result);
112         int rResult = Color.red(result);
113         int gResult = Color.green(result);
114         int bResult = Color.blue(result);
115         assertEquals(aSTART, aResult);
116         assertEquals(rSTART, rResult);
117         assertEquals(gSTART, gResult);
118         assertEquals(bSTART, bResult);
119 
120         result = (Integer) evaluator.evaluate(.5f, START, END);
121         aResult = Color.alpha(result);
122         rResult = Color.red(result);
123         gResult = Color.green(result);
124         bResult = Color.blue(result);
125         assertEquals(0xff, aResult);
126         assertEquals(0x80, gResult);
127         if (rSTART < rEND) {
128             assertTrue(rResult > rSTART && rResult < rEND);
129         } else {
130             assertTrue(rResult < rSTART && rResult > rEND);
131         }
132         if (bSTART < bEND) {
133             assertTrue(bResult > bSTART && bResult < bEND);
134         } else {
135             assertTrue(bResult < bSTART && bResult > bEND);
136         }
137 
138         result = (Integer) evaluator.evaluate(1, START, END);
139         aResult = Color.alpha(result);
140         rResult = Color.red(result);
141         gResult = Color.green(result);
142         bResult = Color.blue(result);
143         assertEquals(aEND, aResult);
144         assertEquals(rEND, rResult);
145         assertEquals(gEND, gResult);
146         assertEquals(bEND, bResult);
147     }
148 
149     @Test
testIntEvaluator()150     public void testIntEvaluator() throws Throwable {
151         final int start = 0;
152         final int end = 100;
153         final float fraction = 0.5f;
154         final IntEvaluator intEvaluator = new IntEvaluator();
155 
156         int result = intEvaluator.evaluate(0, start, end);
157         assertEquals(start, result);
158 
159         result = intEvaluator.evaluate(fraction, start, end);
160         assertEquals(50, result);
161 
162         result = intEvaluator.evaluate(1, start, end);
163         assertEquals(end, result);
164     }
165 
166     @Test
testIntArrayEvaluator()167     public void testIntArrayEvaluator() {
168         IntArrayEvaluator evaluator = new IntArrayEvaluator();
169         intArrayEvaluatorTestImpl(evaluator, null);
170 
171         int[] reusableArray = new int[2];
172         IntArrayEvaluator evaluator2 = new IntArrayEvaluator(reusableArray);
173         intArrayEvaluatorTestImpl(evaluator2, reusableArray);
174     }
175 
intArrayEvaluatorTestImpl(IntArrayEvaluator evaluator, int[] reusedArray)176     private void intArrayEvaluatorTestImpl(IntArrayEvaluator evaluator, int[] reusedArray) {
177         int[] start = {0, 0};
178         int[] end = {80, 100};
179         float fraction = 0.5f;
180 
181         int[] result = evaluator.evaluate(0, start, end);
182         assertEquals(start[0], result[0]);
183         assertEquals(start[1], result[1]);
184 
185         result = evaluator.evaluate(fraction, start, end);
186         assertEquals(40, result[0]);
187         assertEquals(50, result[1]);
188 
189         result = evaluator.evaluate(1, start, end);
190         assertEquals(end[0], result[0]);
191         assertEquals(end[1], result[1]);
192 
193         if (reusedArray != null) {
194             assertEquals(reusedArray, result);
195         }
196     }
197 
198     @Test
testRectEvaluator()199     public void testRectEvaluator() throws Throwable {
200         final RectEvaluator evaluator = new RectEvaluator();
201         rectEvaluatorTestImpl(evaluator, null);
202 
203         Rect reusableRect = new Rect();
204         final RectEvaluator evaluator2 = new RectEvaluator(reusableRect);
205         rectEvaluatorTestImpl(evaluator2, reusableRect);
206     }
207 
rectEvaluatorTestImpl(RectEvaluator evaluator, Rect reusedRect)208     private void rectEvaluatorTestImpl(RectEvaluator evaluator, Rect reusedRect) {
209         final Rect start = new Rect(0, 0, 0, 0);
210         final Rect end = new Rect(100, 200, 300, 400);
211         final float fraction = 0.5f;
212 
213         Rect result = evaluator.evaluate(0, start, end);
214         assertEquals(start.left, result.left, EPSILON);
215         assertEquals(start.top, result.top, EPSILON);
216         assertEquals(start.right, result.right, EPSILON);
217         assertEquals(start.bottom, result.bottom, 001f);
218 
219         result = evaluator.evaluate(fraction, start, end);
220         assertEquals(50, result.left, EPSILON);
221         assertEquals(100, result.top, EPSILON);
222         assertEquals(150, result.right, EPSILON);
223         assertEquals(200, result.bottom, EPSILON);
224 
225         result = evaluator.evaluate(1, start, end);
226         assertEquals(end.left, result.left, EPSILON);
227         assertEquals(end.top, result.top, EPSILON);
228         assertEquals(end.right, result.right, EPSILON);
229         assertEquals(end.bottom, result.bottom, EPSILON);
230 
231         if (reusedRect != null) {
232             assertEquals(reusedRect, result);
233         }
234     }
235 
236     @Test
testPointFEvaluator()237     public void testPointFEvaluator() throws Throwable {
238         final PointFEvaluator evaluator = new PointFEvaluator();
239         pointFEvaluatorTestImpl(evaluator, null);
240 
241         PointF reusablePoint = new PointF();
242         final PointFEvaluator evaluator2 = new PointFEvaluator(reusablePoint);
243         pointFEvaluatorTestImpl(evaluator2, reusablePoint);
244     }
245 
pointFEvaluatorTestImpl(PointFEvaluator evaluator, PointF reusedPoint)246     private void pointFEvaluatorTestImpl(PointFEvaluator evaluator, PointF reusedPoint) {
247         final PointF start = new PointF(0, 0);
248         final PointF end = new PointF(100, 200);
249         final float fraction = 0.5f;
250 
251         PointF result = evaluator.evaluate(0, start, end);
252         assertEquals(start.x, result.x, EPSILON);
253         assertEquals(start.y, result.y, EPSILON);
254 
255         result = evaluator.evaluate(fraction, start, end);
256         assertEquals(50, result.x, EPSILON);
257         assertEquals(100, result.y, EPSILON);
258 
259         result = evaluator.evaluate(1, start, end);
260         assertEquals(end.x, result.x, EPSILON);
261         assertEquals(end.y, result.y, EPSILON);
262 
263         if (reusedPoint != null) {
264             assertEquals(reusedPoint, result);
265         }
266     }
267 }
268 
269