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