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