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