Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright 2018 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 androidx.core.view;
     18 
     19 import static org.junit.Assert.assertTrue;
     20 
     21 import android.app.Activity;
     22 import android.support.test.filters.MediumTest;
     23 import android.support.v4.BaseInstrumentationTestCase;
     24 import android.view.View;
     25 
     26 import androidx.core.test.R;
     27 
     28 import org.junit.Before;
     29 import org.junit.Test;
     30 
     31 import java.util.concurrent.CountDownLatch;
     32 import java.util.concurrent.TimeUnit;
     33 
     34 @MediumTest
     35 public class ViewPropertyAnimatorCompatTest extends BaseInstrumentationTestCase<VpaActivity> {
     36 
     37     private static final int WAIT_TIMEOUT_MS = 200;
     38 
     39     private View mView;
     40     private int mNumListenerCalls = 0;
     41 
     42     public ViewPropertyAnimatorCompatTest() {
     43         super(VpaActivity.class);
     44     }
     45 
     46     @Before
     47     public void setUp() {
     48         final Activity activity = mActivityTestRule.getActivity();
     49         mView = activity.findViewById(R.id.view);
     50     }
     51 
     52     @Test
     53     public void testWithEndAction() throws Throwable {
     54         final CountDownLatch latch1 = new CountDownLatch(1);
     55         mActivityTestRule.runOnUiThread(new Runnable() {
     56             @Override
     57             public void run() {
     58                 ViewCompat.animate(mView).alpha(0).setDuration(100).withEndAction(new Runnable() {
     59                     @Override
     60                     public void run() {
     61                         latch1.countDown();
     62                     }
     63                 });
     64             }
     65         });
     66         assertTrue(latch1.await(300, TimeUnit.MILLISECONDS));
     67 
     68         // This test ensures that the endAction listener will be called exactly once
     69         mNumListenerCalls = 0;
     70         final CountDownLatch latch2 = new CountDownLatch(1);
     71         mActivityTestRule.runOnUiThread(new Runnable() {
     72             @Override
     73             public void run() {
     74                 ViewCompat.animate(mView).alpha(0).setDuration(50).withEndAction(new Runnable() {
     75                     @Override
     76                     public void run() {
     77                         ++mNumListenerCalls;
     78                         ViewCompat.animate(mView).alpha(1);
     79                         latch2.countDown();
     80                     }
     81                 });
     82             }
     83         });
     84         assertTrue(latch2.await(200, TimeUnit.MILLISECONDS));
     85         waitAndCheckCallCount(1);
     86     }
     87 
     88     @Test
     89     public void testWithStartAction() throws Throwable {
     90         final CountDownLatch latch1 = new CountDownLatch(1);
     91         mActivityTestRule.runOnUiThread(new Runnable() {
     92             @Override
     93             public void run() {
     94                 ViewCompat.animate(mView).alpha(0).setDuration(100).withStartAction(new Runnable() {
     95                     @Override
     96                     public void run() {
     97                         latch1.countDown();
     98                     }
     99                 });
    100             }
    101         });
    102         assertTrue(latch1.await(100, TimeUnit.MILLISECONDS));
    103 
    104         // This test ensures that the startAction listener will be called exactly once
    105         mNumListenerCalls = 0;
    106         final CountDownLatch latch2 = new CountDownLatch(1);
    107         mActivityTestRule.runOnUiThread(new Runnable() {
    108             @Override
    109             public void run() {
    110                 ViewCompat.animate(mView).alpha(0).setDuration(50).withStartAction(new Runnable() {
    111                     @Override
    112                     public void run() {
    113                         ++mNumListenerCalls;
    114                         ViewCompat.animate(mView).alpha(1);
    115                         latch2.countDown();
    116                     }
    117                 });
    118             }
    119         });
    120         assertTrue(latch2.await(200, TimeUnit.MILLISECONDS));
    121         waitAndCheckCallCount(1);
    122     }
    123 
    124     void waitAndCheckCallCount(final int count) throws InterruptedException {
    125         int timeLeft = WAIT_TIMEOUT_MS;
    126         while (mNumListenerCalls != count) {
    127             Thread.sleep(20);
    128             timeLeft -= 20;
    129             assertTrue(timeLeft > 0);
    130         }
    131     }
    132 }
    133