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