1578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam/* 2578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * Copyright (C) 2015 The Android Open Source Project 3578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * 4578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * Licensed under the Apache License, Version 2.0 (the "License"); 5578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * you may not use this file except in compliance with the License. 6578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * You may obtain a copy of the License at 7578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * 8578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * http://www.apache.org/licenses/LICENSE-2.0 9578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * 10578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * Unless required by applicable law or agreed to in writing, software 11578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * distributed under the License is distributed on an "AS IS" BASIS, 12578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * See the License for the specific language governing permissions and 14578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam * limitations under the License. 15578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam */ 16578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 17578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lampackage com.android.setupwizardlib.test; 18578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 19578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lamimport android.content.Context; 20578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lamimport android.test.AndroidTestCase; 21578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lamimport android.test.suitebuilder.annotation.SmallTest; 22578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lamimport android.view.View; 23578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 24578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lamimport com.android.setupwizardlib.view.BottomScrollView; 25578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 26578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lampublic class BottomScrollViewTest extends AndroidTestCase { 27578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 28578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam private TestBottomScrollListener mListener; 29578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 30578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam @Override 31578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam protected void setUp() throws Exception { 32578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam super.setUp(); 33578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam mListener = new TestBottomScrollListener(); 34578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 35578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 36578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam @SmallTest 37578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam public void testNoNeedScroll() { 38578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam createScrollView(20); 39578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam assertTrue("Scroll should not be required", mListener.scrolledToBottom); 40578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 41578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 42578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam @SmallTest 43578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam public void testNeedScroll() { 44578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam createScrollView(110); 45578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam assertFalse("Scroll should be required", mListener.scrolledToBottom); 46578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 47578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 48578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam @SmallTest 49578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam public void testScrollToBottom() { 50578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam final BottomScrollView bottomScrollView = createScrollView(110); 51578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 52578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam assertFalse("Scroll should be required", mListener.scrolledToBottom); 53578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 54578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam bottomScrollView.scrollTo(0, 10); 55578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam assertTrue("Should already be scrolled to bottom", mListener.scrolledToBottom); 56578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 57578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 58578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam @SmallTest 59578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam public void testScrollThreshold() { 60578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam final BottomScrollView bottomScrollView = createScrollView(110); 61578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam assertEquals("Scroll threshold should be 10", 10, bottomScrollView.getScrollThreshold()); 62578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 63578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 64578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam private BottomScrollView createScrollView(final int childHeight) { 65578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam final BottomScrollView bottomScrollView = new TestBottomScrollView(getContext()); 66578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam bottomScrollView.setBottomScrollListener(mListener); 67578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 68578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam final View child = new TestChildView(getContext(), childHeight); 69578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 70578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam child.measure(0, 0); // TestChildView's measured dimensions doesn't depend on the arguments 71578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam bottomScrollView.addView(child); 72578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam bottomScrollView.layout(0, 0, 100, 100); 73578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 74578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam return bottomScrollView; 75578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 76578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 77578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam private static class TestChildView extends View { 78578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 79578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam private static final int WIDTH = 10; 80578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam private int mHeight; 81578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 82578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam public TestChildView(Context context, int height) { 83578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam super(context); 84578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam mHeight = height; 85578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 86578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 87578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam @Override 88578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 89578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam setMeasuredDimension(WIDTH, mHeight); 90578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 91578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 92578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam public void setHeight(int height) { 93578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam mHeight = height; 94578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 95578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 96578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 97578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam private static class TestBottomScrollView extends BottomScrollView { 98578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 99578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam public TestBottomScrollView(Context context) { 100578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam super(context); 101578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 102578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 103578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam @Override 104578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam public boolean post(Runnable action) { 105578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam // Post all runnables synchronously so that tests can check the callbacks. 106578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam action.run(); 107578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam return true; 108578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 109578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 110578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 111578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam private static class TestBottomScrollListener implements BottomScrollView.BottomScrollListener { 112578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 113578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam boolean scrolledToBottom = true; 114578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 115578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam @Override 116578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam public void onScrolledToBottom() { 117578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam scrolledToBottom = true; 118578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 119578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam 120578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam @Override 121578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam public void onRequiresScroll() { 122578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam scrolledToBottom = false; 123578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 124578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam } 125578dd1c111c2461620777b315eab3a95bee9ecd1Maurice Lam} 126