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 17 package com.android.documentsui; 18 19 import static org.junit.Assert.assertTrue; 20 import static org.junit.Assert.fail; 21 22 import android.graphics.Point; 23 24 import androidx.test.filters.SmallTest; 25 import androidx.test.runner.AndroidJUnit4; 26 27 import com.android.documentsui.ViewAutoScroller; 28 import com.android.documentsui.ViewAutoScroller.ScrollHost; 29 import com.android.documentsui.ViewAutoScroller.ScrollerCallbacks; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 import java.util.function.IntConsumer; 36 37 @RunWith(AndroidJUnit4.class) 38 @SmallTest 39 public final class ViewAutoScrollerTest { 40 41 private static final int VIEW_HEIGHT = 100; 42 private static final int TOP_Y_POINT = (int) (VIEW_HEIGHT 43 * ViewAutoScroller.TOP_BOTTOM_THRESHOLD_RATIO) - 1; 44 private static final int BOTTOM_Y_POINT = VIEW_HEIGHT 45 - (int) (VIEW_HEIGHT * ViewAutoScroller.TOP_BOTTOM_THRESHOLD_RATIO) 46 + 1; 47 private static final int EDGE_HEIGHT = 5; 48 49 private ViewAutoScroller mAutoScroller; 50 private Point mPoint; 51 private boolean mActive; 52 private ScrollHost mHost; 53 private ScrollerCallbacks mCallbacks; 54 private IntConsumer mScrollAssert; 55 56 @Before setUp()57 public void setUp() { 58 mActive = false; 59 mPoint = new Point(); 60 61 mHost = new ScrollHost() { 62 @Override 63 public boolean isActive() { 64 return mActive; 65 } 66 67 @Override 68 public int getViewHeight() { 69 return VIEW_HEIGHT; 70 } 71 72 @Override 73 public Point getCurrentPosition() { 74 return mPoint; 75 } 76 }; 77 78 mCallbacks = new ScrollerCallbacks() { 79 @Override 80 public void scrollBy(int dy) { 81 mScrollAssert.accept(dy); 82 } 83 84 @Override 85 public void runAtNextFrame(Runnable r) { 86 } 87 88 @Override 89 public void removeCallback(Runnable r) { 90 } 91 }; 92 93 mAutoScroller = new ViewAutoScroller(mHost, mCallbacks); 94 } 95 96 @Test testCursorNotInScrollZone()97 public void testCursorNotInScrollZone() { 98 mPoint = new Point(0, VIEW_HEIGHT/2); 99 mScrollAssert = (int dy) -> { 100 // Should not have called this method 101 fail("Received unexpected scroll event"); 102 assertTrue(false); 103 }; 104 mAutoScroller.run(); 105 } 106 107 @Test testCursorInScrollZone_notActive()108 public void testCursorInScrollZone_notActive() { 109 mActive = false; 110 mPoint = new Point(0, TOP_Y_POINT); 111 mScrollAssert = (int dy) -> { 112 // Should not have called this method 113 fail("Received unexpected scroll event"); 114 assertTrue(false); 115 }; 116 mAutoScroller.run(); 117 } 118 119 @Test testCursorInScrollZone_top()120 public void testCursorInScrollZone_top() { 121 mActive = true; 122 mPoint = new Point(0, TOP_Y_POINT); 123 int expectedScrollDistance = mAutoScroller.computeScrollDistance(-1); 124 mScrollAssert = (int dy) -> { 125 assertTrue(dy == expectedScrollDistance); 126 }; 127 mAutoScroller.run(); 128 } 129 130 @Test testCursorInScrollZone_bottom()131 public void testCursorInScrollZone_bottom() { 132 mActive = true; 133 mPoint = new Point(0, BOTTOM_Y_POINT); 134 int expectedScrollDistance = mAutoScroller.computeScrollDistance(1); 135 mScrollAssert = (int dy) -> { 136 assertTrue(dy == expectedScrollDistance); 137 }; 138 mAutoScroller.run(); 139 } 140 } 141