ListViewScrollHandlingDelegateTest.java revision 46216d0e61318e7af168e9e903ef8d943bb49b2e
1/* 2 * Copyright (C) 2017 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.template; 18 19import static org.mockito.Matchers.anyInt; 20import static org.mockito.Matchers.eq; 21import static org.mockito.Mockito.doNothing; 22import static org.mockito.Mockito.doReturn; 23import static org.mockito.Mockito.spy; 24import static org.mockito.Mockito.verify; 25import static org.robolectric.RuntimeEnvironment.application; 26 27import android.content.Context; 28import android.view.View; 29import android.view.ViewGroup; 30import android.widget.AbsListView.OnScrollListener; 31import android.widget.BaseAdapter; 32import android.widget.ListView; 33 34import com.android.setupwizardlib.BuildConfig; 35import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; 36 37import org.junit.Before; 38import org.junit.Test; 39import org.junit.runner.RunWith; 40import org.mockito.ArgumentCaptor; 41import org.mockito.Mock; 42import org.mockito.MockitoAnnotations; 43import org.robolectric.annotation.Config; 44 45@Config(constants = BuildConfig.class, sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) 46@RunWith(SuwLibRobolectricTestRunner.class) 47public class ListViewScrollHandlingDelegateTest { 48 49 @Mock 50 private RequireScrollMixin mRequireScrollMixin; 51 52 private ListView mListView; 53 private ListViewScrollHandlingDelegate mDelegate; 54 private ArgumentCaptor<OnScrollListener> mListenerCaptor; 55 56 @Before 57 public void setUp() throws Exception { 58 MockitoAnnotations.initMocks(this); 59 60 mListView = spy(new TestListView(application)); 61 mDelegate = new ListViewScrollHandlingDelegate(mRequireScrollMixin, mListView); 62 63 mListenerCaptor = ArgumentCaptor.forClass(OnScrollListener.class); 64 doNothing().when(mListView).setOnScrollListener(mListenerCaptor.capture()); 65 66 mListView.layout(0, 0, 50, 50); 67 } 68 69 @Test 70 public void testRequireScroll() throws Throwable { 71 mDelegate.startListening(); 72 73 verify(mRequireScrollMixin).notifyScrollabilityChange(true); 74 } 75 76 @Test 77 public void testScrolledToBottom() throws Throwable { 78 mDelegate.startListening(); 79 80 verify(mRequireScrollMixin).notifyScrollabilityChange(true); 81 82 doReturn(20).when(mListView).getLastVisiblePosition(); 83 mListenerCaptor.getValue().onScroll(mListView, 2, 20, 20); 84 85 verify(mRequireScrollMixin).notifyScrollabilityChange(false); 86 } 87 88 @Test 89 public void testPageScrollDown() throws Throwable { 90 mDelegate.pageScrollDown(); 91 verify(mListView).smoothScrollBy(eq(50), anyInt()); 92 } 93 94 private static class TestListView extends ListView { 95 96 TestListView(Context context) { 97 super(context); 98 setAdapter(new BaseAdapter() { 99 @Override 100 public int getCount() { 101 return 20; 102 } 103 104 @Override 105 public Object getItem(int position) { 106 return null; 107 } 108 109 @Override 110 public long getItemId(int position) { 111 return position; 112 } 113 114 @Override 115 public View getView(int position, View convertView, ViewGroup parent) { 116 return new View(parent.getContext()); 117 } 118 }); 119 } 120 } 121} 122