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
17package com.android.setupwizardlib.test;
18
19import android.content.Context;
20import android.os.Build;
21import android.os.SystemClock;
22import android.test.InstrumentationTestCase;
23import android.test.suitebuilder.annotation.SmallTest;
24import android.util.Log;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.BaseAdapter;
28import android.widget.ListView;
29
30import com.android.setupwizardlib.util.ListViewRequireScrollHelper;
31import com.android.setupwizardlib.view.NavigationBar;
32
33public class ListViewRequireScrollHelperTest extends InstrumentationTestCase {
34
35    private TestListView mListView;
36    private NavigationBar mNavigationBar;
37
38    @Override
39    protected void setUp() throws Exception {
40        super.setUp();
41        mListView = new TestListView(getInstrumentation().getTargetContext());
42        mNavigationBar = new TestNavigationBar(getInstrumentation().getTargetContext());
43
44        mListView.layout(0, 0, 50, 50);
45    }
46
47    @SmallTest
48    public void testRequireScroll() throws Throwable {
49        ListViewRequireScrollHelper.requireScroll(mNavigationBar, mListView);
50        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
51            assertEquals("More button should be shown initially", View.VISIBLE,
52                    mNavigationBar.getMoreButton().getVisibility());
53            assertEquals("Next button should be gone initially", View.GONE,
54                    mNavigationBar.getNextButton().getVisibility());
55        }
56    }
57
58    @SmallTest
59    public void testScrolledToBottom() throws Throwable {
60        ListViewRequireScrollHelper.requireScroll(mNavigationBar, mListView);
61        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
62            SystemClock.sleep(500);
63            assertEquals("More button should be shown when scroll is required", View.VISIBLE,
64                    mNavigationBar.getMoreButton().getVisibility());
65            assertEquals("Next button should not be shown when scroll is required", View.GONE,
66                    mNavigationBar.getNextButton().getVisibility());
67
68            runTestOnUiThread(new Runnable() {
69                @Override
70                public void run() {
71                    mListView.lastVisiblePosition = 20;
72                    mListView.listener.onScroll(mListView, 2, 20, 20);
73                }
74            });
75            SystemClock.sleep(500);
76            assertEquals("More button should be hidden when scrolled to bottom", View.GONE,
77                    mNavigationBar.getMoreButton().getVisibility());
78            assertEquals("Next button should be shown when scrolled to bottom", View.VISIBLE,
79                    mNavigationBar.getNextButton().getVisibility());
80        }
81    }
82
83    @SmallTest
84    public void testClickScrollButton() throws Throwable {
85        ListViewRequireScrollHelper.requireScroll(mNavigationBar, mListView);
86        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
87            assertEquals("ScrollView page should be initially 0", 0, mListView.scrollDistance);
88            mNavigationBar.getMoreButton().performClick();
89            assertEquals("ScrollView page should be scrolled by 50px",
90                    50, mListView.scrollDistance);
91        }
92    }
93
94    private static class TestListView extends ListView {
95
96        public int lastVisiblePosition = 0;
97        public int scrollDistance = 0;
98        public OnScrollListener listener;
99
100        public TestListView(Context context) {
101            super(context);
102            setAdapter(new BaseAdapter() {
103                @Override
104                public int getCount() {
105                    return 20;
106                }
107
108                @Override
109                public Object getItem(int position) {
110                    return null;
111                }
112
113                @Override
114                public long getItemId(int position) {
115                    return position;
116                }
117
118                @Override
119                public View getView(int position, View convertView, ViewGroup parent) {
120                    return new View(parent.getContext());
121                }
122            });
123        }
124
125        @Override
126        public int getLastVisiblePosition() {
127            return lastVisiblePosition;
128        }
129
130        @Override
131        public void smoothScrollBy(int distance, int duration) {
132            super.smoothScrollBy(distance, duration);
133            scrollDistance += distance;
134        }
135
136        @Override
137        public void setOnScrollListener(OnScrollListener l) {
138            super.setOnScrollListener(l);
139            listener = l;
140        }
141    }
142
143    private static class TestNavigationBar extends NavigationBar {
144
145        public TestNavigationBar(Context context) {
146            super(context);
147        }
148
149        @Override
150        public boolean post(Runnable action) {
151            // Make the post action synchronous
152            action.run();
153            return true;
154        }
155    }
156}
157