BottomScrollViewTest.java revision 578dd1c111c2461620777b315eab3a95bee9ecd1
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
17package com.android.setupwizardlib.test;
18
19import android.content.Context;
20import android.test.AndroidTestCase;
21import android.test.suitebuilder.annotation.SmallTest;
22import android.view.View;
23
24import com.android.setupwizardlib.view.BottomScrollView;
25
26public class BottomScrollViewTest extends AndroidTestCase {
27
28    private TestBottomScrollListener mListener;
29
30    @Override
31    protected void setUp() throws Exception {
32        super.setUp();
33        mListener = new TestBottomScrollListener();
34    }
35
36    @SmallTest
37    public void testNoNeedScroll() {
38        createScrollView(20);
39        assertTrue("Scroll should not be required", mListener.scrolledToBottom);
40    }
41
42    @SmallTest
43    public void testNeedScroll() {
44        createScrollView(110);
45        assertFalse("Scroll should be required", mListener.scrolledToBottom);
46    }
47
48    @SmallTest
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
59    public void testScrollThreshold() {
60        final BottomScrollView bottomScrollView = createScrollView(110);
61        assertEquals("Scroll threshold should be 10", 10, bottomScrollView.getScrollThreshold());
62    }
63
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
82        public TestChildView(Context context, int height) {
83            super(context);
84            mHeight = height;
85        }
86
87        @Override
88        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
89            setMeasuredDimension(WIDTH, mHeight);
90        }
91
92        public void setHeight(int height) {
93            mHeight = height;
94        }
95    }
96
97    private static class TestBottomScrollView extends BottomScrollView {
98
99        public TestBottomScrollView(Context context) {
100            super(context);
101        }
102
103        @Override
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
116        public void onScrolledToBottom() {
117            scrolledToBottom = true;
118        }
119
120        @Override
121        public void onRequiresScroll() {
122            scrolledToBottom = false;
123        }
124    }
125}
126