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