1 /*
2  * Copyright (C) 2019 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.uirendering.cts.testclasses;
18 
19 import static junit.framework.Assert.assertEquals;
20 
21 import android.animation.Animator;
22 import android.animation.AnimatorListenerAdapter;
23 import android.graphics.Color;
24 import android.uirendering.cts.R;
25 import android.uirendering.cts.bitmapverifiers.ColorVerifier;
26 import android.uirendering.cts.testclasses.view.AlphaTestView;
27 import android.uirendering.cts.testinfrastructure.ActivityTestBase;
28 import android.uirendering.cts.testinfrastructure.ViewInitializer;
29 import android.view.View;
30 import android.view.ViewPropertyAnimator;
31 
32 import androidx.test.filters.SmallTest;
33 import androidx.test.runner.AndroidJUnit4;
34 
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 
38 import java.util.concurrent.CountDownLatch;
39 
40 @SmallTest
41 @RunWith(AndroidJUnit4.class)
42 public class ViewPropertyAnimatorTests extends ActivityTestBase {
43 
44     @Test
testViewCustomAlpha()45     public void testViewCustomAlpha() {
46         createViewPropertyAnimatorTest(new ViewPropertyAnimatorTestDelegate<AlphaTestView>() {
47             @Override
48             public void configureView(AlphaTestView target) {
49                 target.setStartColor(Color.RED);
50                 target.setEndColor(Color.BLUE);
51             }
52 
53             @Override
54             public void configureAnimator(ViewPropertyAnimator animator) {
55                 animator.alpha(0.0f);
56             }
57 
58             @Override
59             public void verifyViewState(AlphaTestView target) {
60                 assertEquals(Color.BLUE, target.getBlendedColor());
61             }
62 
63         }).runWithVerifier(new ColorVerifier(Color.BLUE));
64     }
65 
66     @Test
testViewNonCustomAlpha()67     public void testViewNonCustomAlpha() {
68         final CountDownLatch latch = new CountDownLatch(1);
69         createTest().addLayout(R.layout.viewpropertyanimator_test_layout,
70                 (ViewInitializer) view -> {
71                     View testContent = view.findViewById(R.id.viewalpha_test_container);
72                     testContent.animate().alpha(0.0f).setListener(new AnimatorListenerAdapter() {
73                         @Override
74                         public void onAnimationEnd(Animator animation) {
75                             latch.countDown();
76                         }
77                     }).setDuration(16).start();
78                 }, true, latch).runWithVerifier(new ColorVerifier(Color.WHITE));
79     }
80 
81     @Test
testViewCustomAlphaBy()82     public void testViewCustomAlphaBy() {
83         createViewPropertyAnimatorTest(new ViewPropertyAnimatorTestDelegate<AlphaTestView>() {
84             @Override
85             public void configureView(AlphaTestView target) {
86                 target.setStartColor(Color.RED);
87                 target.setEndColor(Color.BLUE);
88                 target.setAlpha(0.5f);
89             }
90 
91             @Override
92             public void configureAnimator(ViewPropertyAnimator animator) {
93                 animator.alphaBy(-0.5f);
94             }
95 
96             @Override
97             public void verifyViewState(AlphaTestView target) {
98                 assertEquals(Color.BLUE, target.getBlendedColor());
99             }
100 
101         }).runWithVerifier(new ColorVerifier(Color.BLUE));
102     }
103 
104     @Test
testViewTranslateX()105     public void testViewTranslateX() {
106         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
107 
108             @Override
109             public void configureAnimator(ViewPropertyAnimator animator) {
110                 animator.translationX(100.0f);
111             }
112 
113             @Override
114             public void verifyViewState(View target) {
115                 assertEquals(100.0f, target.getTranslationX());
116             }
117         });
118     }
119 
120     @Test
testViewTranslateXBy()121     public void testViewTranslateXBy() {
122         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
123 
124             @Override
125             public void configureView(View target) {
126                 target.setTranslationX(20.0f);
127             }
128 
129             @Override
130             public void configureAnimator(ViewPropertyAnimator animator) {
131                 animator.translationXBy(100.0f);
132             }
133 
134             @Override
135             public void verifyViewState(View target) {
136                 assertEquals(120.0f, target.getTranslationX());
137             }
138         });
139     }
140 
141     @Test
testViewTranslateY()142     public void testViewTranslateY() {
143         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
144 
145             @Override
146             public void configureAnimator(ViewPropertyAnimator animator) {
147                 animator.translationY(60.0f);
148 
149             }
150 
151             @Override
152             public void verifyViewState(View target) {
153                 assertEquals(60.0f, target.getTranslationY());
154             }
155         });
156     }
157 
158     @Test
testViewTranslateYBy()159     public void testViewTranslateYBy() {
160         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
161 
162             @Override
163             public void configureView(View target) {
164                 target.setTranslationY(30.0f);
165             }
166 
167             @Override
168             public void configureAnimator(ViewPropertyAnimator animator) {
169                 animator.translationYBy(60.0f);
170             }
171 
172             @Override
173             public void verifyViewState(View target) {
174                 assertEquals(90.0f, target.getTranslationY());
175             }
176         });
177     }
178 
179     @Test
testViewTranslateZ()180     public void testViewTranslateZ() {
181         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
182             @Override
183             public void configureAnimator(ViewPropertyAnimator animator) {
184                 animator.translationZ(30.0f);
185             }
186 
187             @Override
188             public void verifyViewState(View target) {
189                 assertEquals(30.0f, target.getTranslationZ());
190             }
191         });
192     }
193 
194     @Test
testViewTranslateZBy()195     public void testViewTranslateZBy() {
196         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
197 
198             @Override
199             public void configureView(View target) {
200                 target.setTranslationZ(40.0f);
201             }
202 
203             @Override
204             public void configureAnimator(ViewPropertyAnimator animator) {
205                 animator.translationZBy(30.0f);
206             }
207 
208             @Override
209             public void verifyViewState(View target) {
210                 assertEquals(70.0f, target.getTranslationZ());
211             }
212         });
213     }
214 
215     @Test
testViewRotation()216     public void testViewRotation() {
217         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
218             @Override
219             public void configureAnimator(ViewPropertyAnimator animator) {
220                 animator.rotation(20.0f);
221             }
222 
223             @Override
224             public void verifyViewState(View target) {
225                 assertEquals(20.0f, target.getRotation());
226             }
227         });
228     }
229 
230     @Test
testViewRotationBy()231     public void testViewRotationBy() {
232         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
233 
234             @Override
235             public void configureView(View target) {
236                 target.setRotation(30.0f);
237             }
238 
239             @Override
240             public void configureAnimator(ViewPropertyAnimator animator) {
241                 animator.rotationBy(20.0f);
242             }
243 
244             @Override
245             public void verifyViewState(View target) {
246                 assertEquals(50.0f, target.getRotation());
247             }
248         });
249     }
250 
251     @Test
testViewRotationX()252     public void testViewRotationX() {
253         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
254             @Override
255             public void configureAnimator(ViewPropertyAnimator animator) {
256                 animator.rotationX(80.0f);
257             }
258 
259             @Override
260             public void verifyViewState(View target) {
261                 assertEquals(80.0f, target.getRotationX());
262             }
263         });
264     }
265 
266     @Test
testViewRotationXBy()267     public void testViewRotationXBy() {
268         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
269 
270             @Override
271             public void configureView(View target) {
272                 target.setRotationX(30.0f);
273             }
274 
275             @Override
276             public void configureAnimator(ViewPropertyAnimator animator) {
277                 animator.rotationXBy(80.0f);
278             }
279 
280             @Override
281             public void verifyViewState(View target) {
282                 assertEquals(110.0f, target.getRotationX());
283             }
284         });
285     }
286 
287     @Test
testViewRotationY()288     public void testViewRotationY() {
289         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
290             @Override
291             public void configureAnimator(ViewPropertyAnimator animator) {
292                 animator.rotationY(25.0f);
293             }
294 
295             @Override
296             public void verifyViewState(View target) {
297                 assertEquals(25.0f, target.getRotationY());
298             }
299         });
300     }
301 
302     @Test
testViewRotationYBy()303     public void testViewRotationYBy() {
304         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
305 
306             @Override
307             public void configureView(View target) {
308                 target.setRotationY(10.0f);
309             }
310 
311             @Override
312             public void configureAnimator(ViewPropertyAnimator animator) {
313                 animator.rotationYBy(25.0f);
314             }
315 
316             @Override
317             public void verifyViewState(View target) {
318                 assertEquals(35.0f, target.getRotationY());
319             }
320         });
321     }
322 
323     @Test
testViewScaleX()324     public void testViewScaleX() {
325         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
326             @Override
327             public void configureAnimator(ViewPropertyAnimator animator) {
328                 animator.scaleX(2.5f);
329             }
330 
331             @Override
332             public void verifyViewState(View target) {
333                 assertEquals(2.5f, target.getScaleX());
334             }
335         });
336     }
337 
338     @Test
testViewScaleXBy()339     public void testViewScaleXBy() {
340         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
341 
342             @Override
343             public void configureView(View target) {
344                 target.setScaleX(1.2f);
345             }
346 
347             @Override
348             public void configureAnimator(ViewPropertyAnimator animator) {
349                 animator.scaleXBy(2.5f);
350             }
351 
352             @Override
353             public void verifyViewState(View target) {
354                 assertEquals(3.7f, target.getScaleX());
355             }
356         });
357     }
358 
359     @Test
testViewScaleY()360     public void testViewScaleY() {
361         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
362             @Override
363             public void configureAnimator(ViewPropertyAnimator animator) {
364                 animator.scaleY(3.2f);
365             }
366 
367             @Override
368             public void verifyViewState(View target) {
369                 assertEquals(3.2f, target.getScaleY());
370             }
371         });
372     }
373 
374     @Test
testViewScaleYBy()375     public void testViewScaleYBy() {
376         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
377 
378             @Override
379             public void configureView(View target) {
380                 target.setScaleY(1.2f);
381             }
382 
383             @Override
384             public void configureAnimator(ViewPropertyAnimator animator) {
385                 animator.scaleYBy(3.2f);
386             }
387 
388             @Override
389             public void verifyViewState(View target) {
390                 assertEquals(4.4f, target.getScaleY());
391             }
392         });
393     }
394 
395     @Test
testViewX()396     public void testViewX() {
397         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
398             @Override
399             public void configureAnimator(ViewPropertyAnimator animator) {
400                 animator.x(27.0f);
401             }
402 
403             @Override
404             public void verifyViewState(View target) {
405                 assertEquals(27.0f, target.getX());
406             }
407         });
408     }
409 
410     @Test
testViewXBy()411     public void testViewXBy() {
412         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
413 
414             @Override
415             public void configureView(View target) {
416                 target.setX(140.0f);
417             }
418 
419             @Override
420             public void configureAnimator(ViewPropertyAnimator animator) {
421                 animator.xBy(27.0f);
422             }
423 
424             @Override
425             public void verifyViewState(View target) {
426                 assertEquals(167.0f, target.getX());
427             }
428         });
429     }
430 
431     @Test
testViewY()432     public void testViewY() {
433         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
434             @Override
435             public void configureAnimator(ViewPropertyAnimator animator) {
436                 animator.y(77.0f);
437             }
438 
439             @Override
440             public void verifyViewState(View target) {
441                 assertEquals(77.0f, target.getY());
442             }
443         });
444     }
445 
446     @Test
testViewYBy()447     public void testViewYBy() {
448         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
449 
450             @Override
451             public void configureView(View target) {
452                 target.setY(80.0f);
453             }
454 
455             @Override
456             public void configureAnimator(ViewPropertyAnimator animator) {
457                 animator.yBy(77.0f);
458             }
459 
460             @Override
461             public void verifyViewState(View target) {
462                 assertEquals(157.0f, target.getY());
463             }
464         });
465     }
466 
467     @Test
testViewZ()468     public void testViewZ() {
469         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
470             @Override
471             public void configureAnimator(ViewPropertyAnimator animator) {
472                 animator.z(17.0f);
473             }
474 
475             @Override
476             public void verifyViewState(View target) {
477                 assertEquals(17.0f, target.getZ());
478             }
479         });
480     }
481 
482     @Test
testViewZBy()483     public void testViewZBy() {
484         runViewPropertyAnimatorTestWithoutVerification(new ViewPropertyAnimatorTestDelegate() {
485 
486             @Override
487             public void configureView(View target) {
488                 target.setZ(38.0f);
489             }
490 
491             @Override
492             public void configureAnimator(ViewPropertyAnimator animator) {
493                 animator.zBy(17.0f);
494             }
495 
496             @Override
497             public void verifyViewState(View target) {
498                 assertEquals(55.0f, target.getZ());
499             }
500         });
501     }
502 
runViewPropertyAnimatorTestWithoutVerification( ViewPropertyAnimatorTestDelegate delegate)503     private void runViewPropertyAnimatorTestWithoutVerification(
504             ViewPropertyAnimatorTestDelegate delegate) {
505         createViewPropertyAnimatorTest(delegate).runWithoutVerification();
506     }
507 
createViewPropertyAnimatorTest( final ViewPropertyAnimatorTestDelegate delegate)508     private TestCaseBuilder createViewPropertyAnimatorTest(
509             final ViewPropertyAnimatorTestDelegate delegate) {
510         final CountDownLatch latch = new CountDownLatch(1);
511         return createTest().addLayout(R.layout.viewpropertyanimator_test_layout,
512                 (ViewInitializer) view -> {
513                     AlphaTestView alphaView = view.findViewById(R.id.alpha_test_view);
514                     delegate.configureView(alphaView);
515                     alphaView.setStartColor(Color.RED);
516                     alphaView.setEndColor(Color.BLUE);
517                     ViewPropertyAnimator animator = alphaView.animate();
518                     delegate.configureAnimator(animator);
519                     animator.setListener(
520                             new AnimatorListenerAdapter() {
521 
522                                 @Override
523                                 public void onAnimationEnd(Animator animator) {
524                                     delegate.verifyViewState(alphaView);
525                                     latch.countDown();
526                                 }
527 
528                             }).setDuration(16).start();
529                 }, true, latch);
530     }
531 
532     private interface ViewPropertyAnimatorTestDelegate<T extends View> {
533 
configureView(T target)534         default void configureView(T target) {
535             // NO-OP
536         }
537 
configureAnimator(ViewPropertyAnimator animator)538         void configureAnimator(ViewPropertyAnimator animator);
539 
verifyViewState(T target)540         void verifyViewState(T target);
541     }
542 }
543