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