ScrollerCompatTestBase.java revision 4ad0efc06631e6e35d8ced424c36438bf5827569
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.test.AndroidTestCase;
20import android.view.animation.Interpolator;
21
22import java.lang.reflect.Constructor;
23
24abstract public class ScrollerCompatTestBase extends AndroidTestCase {
25
26    private final int mApiLevel;
27
28    private ScrollerCompat mScroller;
29
30    public ScrollerCompatTestBase(int apiLevel) {
31        mApiLevel = apiLevel;
32    }
33
34    @Override
35    protected void setUp() throws Exception {
36        super.setUp();
37        Constructor<ScrollerCompat> constructor = ScrollerCompat.class
38                .getDeclaredConstructor(int.class, Context.class, Interpolator.class);
39        constructor.setAccessible(true);
40        mScroller = constructor.newInstance(mApiLevel, getContext(), null);
41    }
42
43    public void testTargetReached() throws InterruptedException {
44        mScroller.fling(0, 0, 0, 1000,
45                Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
46        int target = mScroller.getFinalY();
47        while (mScroller.computeScrollOffset()) {
48            Thread.sleep(100);
49        }
50        assertEquals("given enough time, scroller should reach target position", target,
51                mScroller.getCurrY());
52    }
53
54    public void testAbort() throws InterruptedException {
55        mScroller.fling(0, 0, 0, 10000,
56                Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
57        assertTrue("Scroller should have some offset", mScroller.computeScrollOffset());
58        mScroller.abortAnimation();
59        assertFalse("Scroller should clear offset after being aborted",
60                mScroller.computeScrollOffset());
61    }
62}
63