RecyclerViewScrollHandlingDelegateTest.java revision ba1332d1b4dd525a2ef313246482708cb2016052
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.robolectric.SuwLibRobolectricTestRunner;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.ArgumentCaptor;
36import org.mockito.Mock;
37import org.mockito.MockitoAnnotations;
38import org.robolectric.annotation.Config;
39
40@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK })
41@RunWith(SuwLibRobolectricTestRunner.class)
42public 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
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
67    public void testRequireScroll() {
68        mDelegate.startListening();
69        verify(mRequireScrollMixin).notifyScrollabilityChange(true);
70    }
71
72    @Test
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
84    public void testClickScrollButton() {
85        mDelegate.pageScrollDown();
86        verify(mRecyclerView).smoothScrollBy(anyInt(), eq(50));
87    }
88}
89