ScrollerCompatTestBase.java revision 3ac77bf186f87ecad4bf0063b2f6c4384efbd56a
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 android.content.Context;
19import android.support.test.InstrumentationRegistry;
20import android.support.test.runner.AndroidJUnit4;
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.MediumTest;
23import android.util.Log;
24import android.view.animation.AnimationUtils;
25import android.view.animation.Interpolator;
26import android.view.animation.LinearInterpolator;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29
30import java.lang.reflect.Constructor;
31import java.lang.reflect.Field;
32import java.lang.reflect.InvocationTargetException;
33
34import static org.junit.Assert.assertEquals;
35import static org.junit.Assert.assertFalse;
36import static org.junit.Assert.assertTrue;
37
38@RunWith(AndroidJUnit4.class)
39@MediumTest
40public abstract class ScrollerCompatTestBase {
41
42    private static final boolean DEBUG = false;
43
44    private final String TAG;
45
46    private final int mApiLevel;
47
48    private ScrollerCompat mScroller;
49
50    public ScrollerCompatTestBase(int apiLevel) {
51        mApiLevel = apiLevel;
52        TAG = "ScrollerCompatTest api:" + apiLevel;
53    }
54
55    protected void createScroller(Interpolator interpolator)
56            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
57            InstantiationException {
58        Constructor<ScrollerCompat> constructor = ScrollerCompat.class
59                .getDeclaredConstructor(int.class, Context.class, Interpolator.class);
60        constructor.setAccessible(true);
61        mScroller = constructor.newInstance(mApiLevel, InstrumentationRegistry.getContext(),
62                interpolator);
63    }
64
65    @Test
66    public void testTargetReached() throws Throwable {
67        if (DEBUG) {
68            Log.d(TAG, "testing if target is reached");
69        }
70        createScroller(null);
71        mScroller.fling(0, 0, 0, 1000,
72                Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
73        int target = mScroller.getFinalY();
74        while (mScroller.computeScrollOffset()) {
75            Thread.sleep(100);
76        }
77        assertEquals("given enough time, scroller should reach target position", target,
78                mScroller.getCurrY());
79    }
80
81    @Test
82    public void testAbort() throws Throwable {
83        if (DEBUG) {
84            Log.d(TAG, "testing abort");
85        }
86        createScroller(null);
87        mScroller.fling(0, 0, 0, 10000,
88                Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
89        assertTrue("Scroller should have some offset", mScroller.computeScrollOffset());
90        mScroller.abortAnimation();
91        assertFalse("Scroller should clear offset after being aborted",
92                mScroller.computeScrollOffset());
93    }
94}
95