ViewPropertyAnimatorCompatTest.java revision 3ac77bf186f87ecad4bf0063b2f6c4384efbd56a
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 android.app.Activity;
20import android.support.test.InstrumentationRegistry;
21import android.support.v4.BaseInstrumentationTestCase;
22import android.support.v4.test.R;
23import android.test.suitebuilder.annotation.MediumTest;
24import android.view.View;
25
26import java.util.concurrent.CountDownLatch;
27import java.util.concurrent.TimeUnit;
28
29import org.junit.Before;
30import org.junit.Test;
31import static org.junit.Assert.assertEquals;
32import static org.junit.Assert.assertTrue;
33
34@MediumTest
35public 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        InstrumentationRegistry.getInstrumentation().runOnMainSync(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        InstrumentationRegistry.getInstrumentation().runOnMainSync(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        InstrumentationRegistry.getInstrumentation().runOnMainSync(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        InstrumentationRegistry.getInstrumentation().runOnMainSync(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