ScrollerCompatTestBase.java revision 754cb29c50f09a83251dd4bb633ba445b2411adb
1/*
2 * Copyright (C) 2014 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 */
16package android.support.v4.widget;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21
22import android.support.test.InstrumentationRegistry;
23import android.support.test.filters.MediumTest;
24import android.support.test.runner.AndroidJUnit4;
25import android.util.Log;
26import android.view.animation.Interpolator;
27
28import org.junit.Test;
29import org.junit.runner.RunWith;
30
31import java.lang.reflect.InvocationTargetException;
32
33@RunWith(AndroidJUnit4.class)
34@MediumTest
35public abstract class ScrollerCompatTestBase {
36
37    private static final boolean DEBUG = false;
38
39    private final String TAG;
40
41    private final boolean mIsIcsOrNewer;
42
43    private ScrollerCompat mScroller;
44
45    public ScrollerCompatTestBase(boolean isIcsOrNewer) {
46        mIsIcsOrNewer = isIcsOrNewer;
47        TAG = "ScrollerCompatTest ICS or newer:" + isIcsOrNewer;
48    }
49
50    protected void createScroller(Interpolator interpolator)
51            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
52            InstantiationException {
53        mScroller = new ScrollerCompat(mIsIcsOrNewer, InstrumentationRegistry.getContext(),
54                interpolator);
55    }
56
57    @Test
58    public void testTargetReached() throws Throwable {
59        if (DEBUG) {
60            Log.d(TAG, "testing if target is reached");
61        }
62        createScroller(null);
63        mScroller.fling(0, 0, 0, 1000,
64                Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
65        int target = mScroller.getFinalY();
66        while (mScroller.computeScrollOffset()) {
67            Thread.sleep(100);
68        }
69        assertEquals("given enough time, scroller should reach target position", target,
70                mScroller.getCurrY());
71    }
72
73    @Test
74    public void testAbort() throws Throwable {
75        if (DEBUG) {
76            Log.d(TAG, "testing abort");
77        }
78        createScroller(null);
79        mScroller.fling(0, 0, 0, 10000,
80                Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
81        assertTrue("Scroller should have some offset", mScroller.computeScrollOffset());
82        mScroller.abortAnimation();
83        assertFalse("Scroller should clear offset after being aborted",
84                mScroller.computeScrollOffset());
85    }
86}
87