ScrollViewScrollHandlingDelegateTest.java revision 66815fe66392bcbb12e2fc93bbf326a5c2d8782f
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.spy;
23import static org.mockito.Mockito.verify;
24import static org.robolectric.RuntimeEnvironment.application;
25
26import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
27import com.android.setupwizardlib.view.BottomScrollView;
28import com.android.setupwizardlib.view.BottomScrollView.BottomScrollListener;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.mockito.ArgumentCaptor;
34import org.mockito.Mock;
35import org.mockito.MockitoAnnotations;
36import org.robolectric.annotation.Config;
37
38@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK })
39@RunWith(SuwLibRobolectricTestRunner.class)
40public class ScrollViewScrollHandlingDelegateTest {
41
42    @Mock
43    private RequireScrollMixin mRequireScrollMixin;
44
45    private BottomScrollView mScrollView;
46    private ScrollViewScrollHandlingDelegate mDelegate;
47    private ArgumentCaptor<BottomScrollListener> mListenerCaptor;
48
49    @Before
50    public void setUp() throws Exception {
51        MockitoAnnotations.initMocks(this);
52
53        mScrollView = spy(new BottomScrollView(application));
54        mDelegate = new ScrollViewScrollHandlingDelegate(mRequireScrollMixin, mScrollView);
55
56        mListenerCaptor = ArgumentCaptor.forClass(BottomScrollListener.class);
57        doNothing().when(mScrollView).setBottomScrollListener(mListenerCaptor.capture());
58
59        mScrollView.layout(0, 0, 50, 50);
60    }
61
62    @Test
63    public void testRequireScroll() throws Throwable {
64        mDelegate.startListening();
65
66        mListenerCaptor.getValue().onRequiresScroll();
67        verify(mRequireScrollMixin).notifyScrollabilityChange(true);
68    }
69
70    @Test
71    public void testScrolledToBottom() throws Throwable {
72        mDelegate.startListening();
73
74        mListenerCaptor.getValue().onRequiresScroll();
75        verify(mRequireScrollMixin).notifyScrollabilityChange(true);
76
77        mListenerCaptor.getValue().onScrolledToBottom();
78
79        verify(mRequireScrollMixin).notifyScrollabilityChange(false);
80    }
81
82    @Test
83    public void testPageScrollDown() throws Throwable {
84        mDelegate.pageScrollDown();
85        verify(mScrollView).smoothScrollBy(anyInt(), eq(50));
86    }
87}
88