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