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