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