[go: nahoru, domu]

17bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam/*
27bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * Copyright (C) 2016 The Android Open Source Project
37bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam *
47bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * Licensed under the Apache License, Version 2.0 (the "License");
57bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * you may not use this file except in compliance with the License.
67bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * You may obtain a copy of the License at
77bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam *
87bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam *      http://www.apache.org/licenses/LICENSE-2.0
97bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam *
107bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * Unless required by applicable law or agreed to in writing, software
117bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * distributed under the License is distributed on an "AS IS" BASIS,
127bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * See the License for the specific language governing permissions and
147bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * limitations under the License.
157bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam */
167bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
177bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lampackage com.android.setupwizardlib.test;
187bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
197bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport android.content.Context;
207bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport android.os.Build;
217bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport android.support.v7.widget.RecyclerView;
227bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport android.test.AndroidTestCase;
237bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport android.test.suitebuilder.annotation.SmallTest;
247bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport android.view.View;
257bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
267bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport com.android.setupwizardlib.util.RecyclerViewRequireScrollHelper;
277bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport com.android.setupwizardlib.view.NavigationBar;
287bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
297bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lampublic class RecyclerViewRequireScrollHelperTest extends AndroidTestCase {
307bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
317bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    private TestRecyclerView mRecyclerView;
327bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    private NavigationBar mNavigationBar;
337bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
347bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    @Override
357bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    protected void setUp() throws Exception {
367bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        super.setUp();
377bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        mRecyclerView = new TestRecyclerView(getContext());
387bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        mNavigationBar = new TestNavigationBar(getContext());
397bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
407bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        mRecyclerView.layout(0, 0, 50, 50);
417bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
427bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
437bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    @SmallTest
447bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    public void testRequireScroll() {
457bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        RecyclerViewRequireScrollHelper.requireScroll(mNavigationBar, mRecyclerView);
467bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
477bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            assertEquals("More button should be shown initially", View.VISIBLE,
487bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                    mNavigationBar.getMoreButton().getVisibility());
497bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            assertEquals("Next button should be gone initially", View.GONE,
507bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                    mNavigationBar.getNextButton().getVisibility());
517bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
527bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
537bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
547bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    @SmallTest
557bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    public void testScrolledToBottom() {
567bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        RecyclerViewRequireScrollHelper.requireScroll(mNavigationBar, mRecyclerView);
577bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
587bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            assertEquals("More button should be shown when scroll is required", View.VISIBLE,
597bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                    mNavigationBar.getMoreButton().getVisibility());
607bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            assertEquals("Next button should not be shown when scroll is required", View.GONE,
617bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                    mNavigationBar.getNextButton().getVisibility());
627bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
637bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            mRecyclerView.scrollOffset = 20;
647bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            mRecyclerView.listener.onScrolled(mRecyclerView, 0, 20);
657bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            assertEquals("More button should be hidden when scrolled to bottom", View.GONE,
667bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                    mNavigationBar.getMoreButton().getVisibility());
677bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            assertEquals("Next button should be shown when scrolled to bottom", View.VISIBLE,
687bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                    mNavigationBar.getNextButton().getVisibility());
697bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
707bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
717bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
727bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    @SmallTest
737bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    public void testClickScrollButton() {
747bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        RecyclerViewRequireScrollHelper.requireScroll(mNavigationBar, mRecyclerView);
757bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
767bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            assertEquals("ScrollView page should be initially 0", 0, mRecyclerView.scrollDistance);
777bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            mNavigationBar.getMoreButton().performClick();
787bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            assertEquals("ScrollView page should be scrolled by 50px",
797bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                    50, mRecyclerView.scrollDistance);
807bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
817bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
827bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
837bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    private static class TestRecyclerView extends RecyclerView {
847bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
857bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public int scrollOffset = 0;
867bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public int scrollRange = 20;
877bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public int scrollExtent = 0;
887bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
897bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public int scrollDistance = 0;
907bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
917bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public OnScrollListener listener;
927bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
937bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public TestRecyclerView(Context context) {
947bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            super(context);
957bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
967bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
977bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        @Override
987bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public void addOnScrollListener(OnScrollListener listener) {
997bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            super.addOnScrollListener(listener);
1007bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            this.listener = listener;
1017bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
1027bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
1037bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        @Override
1047bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public int computeVerticalScrollOffset() {
1057bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            return scrollOffset;
1067bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
1077bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
1087bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        @Override
1097bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public int computeVerticalScrollRange() {
1107bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            return scrollRange;
1117bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
1127bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
1137bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        @Override
1147bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public int computeVerticalScrollExtent() {
1157bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            return scrollExtent;
1167bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
1177bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
1187bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        @Override
1197bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public void smoothScrollBy(int dx, int dy) {
1207bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            super.smoothScrollBy(dx, dy);
1217bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            scrollDistance += dy;
1227bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
1237bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
1247bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
1257bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    private static class TestNavigationBar extends NavigationBar {
1267bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
1277bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public TestNavigationBar(Context context) {
1287bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            super(context);
1297bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
1307bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
1317bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        @Override
1327bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        public boolean post(Runnable action) {
1337bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            action.run();
1347bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            return true;
1357bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
1367bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
1377bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam}
138