RequireScrollHelperTest.java revision 7bc6f176937ed369b180fa89f6c311d2801f206c
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.util.RequireScrollHelper; 25import com.android.setupwizardlib.view.BottomScrollView; 26import com.android.setupwizardlib.view.NavigationBar; 27 28public class RequireScrollHelperTest extends AndroidTestCase { 29 30 private TestBottomScrollView mScrollView; 31 private NavigationBar mNavigationBar; 32 33 @Override 34 protected void setUp() throws Exception { 35 super.setUp(); 36 mScrollView = new TestBottomScrollView(getContext()); 37 mNavigationBar = new TestNavigationBar(getContext()); 38 } 39 40 @SmallTest 41 public void testRequireScroll() { 42 RequireScrollHelper.requireScroll(mNavigationBar, mScrollView); 43 assertEquals("More button should be gone initially", View.GONE, 44 mNavigationBar.getMoreButton().getVisibility()); 45 assertEquals("Next button should be shown", View.VISIBLE, 46 mNavigationBar.getNextButton().getVisibility()); 47 48 mScrollView.listener.onRequiresScroll(); 49 assertEquals("More button should be shown when scroll is required", View.VISIBLE, 50 mNavigationBar.getMoreButton().getVisibility()); 51 assertEquals("Next button should not be shown when scroll is required", View.GONE, 52 mNavigationBar.getNextButton().getVisibility()); 53 } 54 55 @SmallTest 56 public void testScrolledToBottom() { 57 RequireScrollHelper.requireScroll(mNavigationBar, mScrollView); 58 mScrollView.listener.onRequiresScroll(); 59 assertEquals("More button should be shown when scroll is required", View.VISIBLE, 60 mNavigationBar.getMoreButton().getVisibility()); 61 assertEquals("Next button should not be shown when scroll is required", View.GONE, 62 mNavigationBar.getNextButton().getVisibility()); 63 64 mScrollView.listener.onScrolledToBottom(); 65 assertEquals("More button should be hidden when scrolled to bottom", View.GONE, 66 mNavigationBar.getMoreButton().getVisibility()); 67 assertEquals("Next button should be shown when scrolled to bottom", View.VISIBLE, 68 mNavigationBar.getNextButton().getVisibility()); 69 } 70 71 @SmallTest 72 public void testClickScrollButton() { 73 RequireScrollHelper.requireScroll(mNavigationBar, mScrollView); 74 assertEquals("ScrollView page should be initially 0", 0, mScrollView.page); 75 mScrollView.listener.onRequiresScroll(); 76 mNavigationBar.getMoreButton().performClick(); 77 assertEquals("ScrollView page should be scrolled by 1", 1, mScrollView.page); 78 } 79 80 private static class TestBottomScrollView extends BottomScrollView { 81 82 public BottomScrollListener listener; 83 public int page = 0; 84 85 public TestBottomScrollView(Context context) { 86 super(context); 87 } 88 89 @Override 90 public void setBottomScrollListener(BottomScrollListener listener) { 91 this.listener = listener; 92 } 93 94 @Override 95 public boolean pageScroll(int direction) { 96 if (direction == FOCUS_DOWN) { 97 page++; 98 } else if (direction == FOCUS_UP) { 99 page--; 100 } 101 return super.pageScroll(direction); 102 } 103 } 104 105 private static class TestNavigationBar extends NavigationBar { 106 107 public TestNavigationBar(Context context) { 108 super(context); 109 } 110 111 @Override 112 public boolean post(Runnable action) { 113 action.run(); 114 return true; 115 } 116 } 117} 118