• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.setupwizardlib.test;
18 
19 import android.content.Context;
20 import android.test.AndroidTestCase;
21 import android.test.suitebuilder.annotation.SmallTest;
22 import android.view.View;
23 
24 import com.android.setupwizardlib.view.BottomScrollView;
25 
26 public class BottomScrollViewTest extends AndroidTestCase {
27 
28     private TestBottomScrollListener mListener;
29 
30     @Override
setUp()31     protected void setUp() throws Exception {
32         super.setUp();
33         mListener = new TestBottomScrollListener();
34     }
35 
36     @SmallTest
testNoNeedScroll()37     public void testNoNeedScroll() {
38         createScrollView(20);
39         assertTrue("Scroll should not be required", mListener.scrolledToBottom);
40     }
41 
42     @SmallTest
testNeedScroll()43     public void testNeedScroll() {
44         createScrollView(110);
45         assertFalse("Scroll should be required", mListener.scrolledToBottom);
46     }
47 
48     @SmallTest
testScrollToBottom()49     public void testScrollToBottom() {
50         final BottomScrollView bottomScrollView = createScrollView(110);
51 
52         assertFalse("Scroll should be required", mListener.scrolledToBottom);
53 
54         bottomScrollView.scrollTo(0, 10);
55         assertTrue("Should already be scrolled to bottom", mListener.scrolledToBottom);
56     }
57 
58     @SmallTest
testScrollThreshold()59     public void testScrollThreshold() {
60         final BottomScrollView bottomScrollView = createScrollView(110);
61         assertEquals("Scroll threshold should be 10", 10, bottomScrollView.getScrollThreshold());
62     }
63 
createScrollView(final int childHeight)64     private BottomScrollView createScrollView(final int childHeight) {
65         final BottomScrollView bottomScrollView = new TestBottomScrollView(getContext());
66         bottomScrollView.setBottomScrollListener(mListener);
67 
68         final View child = new TestChildView(getContext(), childHeight);
69 
70         child.measure(0, 0); // TestChildView's measured dimensions doesn't depend on the arguments
71         bottomScrollView.addView(child);
72         bottomScrollView.layout(0, 0, 100, 100);
73 
74         return bottomScrollView;
75     }
76 
77     private static class TestChildView extends View {
78 
79         private static final int WIDTH = 10;
80         private int mHeight;
81 
TestChildView(Context context, int height)82         public TestChildView(Context context, int height) {
83             super(context);
84             mHeight = height;
85         }
86 
87         @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)88         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
89             setMeasuredDimension(WIDTH, mHeight);
90         }
91 
setHeight(int height)92         public void setHeight(int height) {
93             mHeight = height;
94         }
95     }
96 
97     private static class TestBottomScrollView extends BottomScrollView {
98 
TestBottomScrollView(Context context)99         public TestBottomScrollView(Context context) {
100             super(context);
101         }
102 
103         @Override
post(Runnable action)104         public boolean post(Runnable action) {
105             // Post all runnables synchronously so that tests can check the callbacks.
106             action.run();
107             return true;
108         }
109     }
110 
111     private static class TestBottomScrollListener implements BottomScrollView.BottomScrollListener {
112 
113         boolean scrolledToBottom = true;
114 
115         @Override
onScrolledToBottom()116         public void onScrolledToBottom() {
117             scrolledToBottom = true;
118         }
119 
120         @Override
onRequiresScroll()121         public void onRequiresScroll() {
122             scrolledToBottom = false;
123         }
124     }
125 }
126