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.support.v7.widget.RecyclerView; 28import android.support.v7.widget.RecyclerView.OnScrollListener; 29 30import com.android.setupwizardlib.BuildConfig; 31import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; 32 33import org.junit.Before; 34import org.junit.Test; 35import org.junit.runner.RunWith; 36import org.mockito.ArgumentCaptor; 37import org.mockito.Mock; 38import org.mockito.MockitoAnnotations; 39import org.robolectric.annotation.Config; 40 41@Config(constants = BuildConfig.class, sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) 42@RunWith(SuwLibRobolectricTestRunner.class) 43public class RecyclerViewScrollHandlingDelegateTest { 44 45 @Mock 46 private RequireScrollMixin mRequireScrollMixin; 47 48 private RecyclerView mRecyclerView; 49 private RecyclerViewScrollHandlingDelegate mDelegate; 50 private ArgumentCaptor<OnScrollListener> mListenerCaptor; 51 52 @Before 53 public void setUp() throws Exception { 54 MockitoAnnotations.initMocks(this); 55 56 mRecyclerView = spy(new RecyclerView(application)); 57 doReturn(20).when(mRecyclerView).computeVerticalScrollRange(); 58 doReturn(0).when(mRecyclerView).computeVerticalScrollExtent(); 59 doReturn(0).when(mRecyclerView).computeVerticalScrollOffset(); 60 mListenerCaptor = ArgumentCaptor.forClass(OnScrollListener.class); 61 doNothing().when(mRecyclerView).addOnScrollListener(mListenerCaptor.capture()); 62 63 mDelegate = new RecyclerViewScrollHandlingDelegate(mRequireScrollMixin, mRecyclerView); 64 mRecyclerView.layout(0, 0, 50, 50); 65 } 66 67 @Test 68 public void testRequireScroll() { 69 mDelegate.startListening(); 70 verify(mRequireScrollMixin).notifyScrollabilityChange(true); 71 } 72 73 @Test 74 public void testScrolledToBottom() { 75 mDelegate.startListening(); 76 verify(mRequireScrollMixin).notifyScrollabilityChange(true); 77 78 doReturn(20).when(mRecyclerView).computeVerticalScrollOffset(); 79 mListenerCaptor.getValue().onScrolled(mRecyclerView, 0, 20); 80 81 verify(mRequireScrollMixin).notifyScrollabilityChange(false); 82 } 83 84 @Test 85 public void testClickScrollButton() { 86 mDelegate.pageScrollDown(); 87 verify(mRecyclerView).smoothScrollBy(anyInt(), eq(50)); 88 } 89} 90