Priyank Singh | 3c025d3 | 2019-09-27 14:20:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019 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.car.ui.pagedrecyclerview; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.Mockito.times; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | import static org.testng.Assert.assertThrows; |
| 26 | |
| 27 | import android.content.Context; |
| 28 | import android.widget.FrameLayout; |
| 29 | |
| 30 | import androidx.recyclerview.widget.RecyclerView; |
| 31 | |
| 32 | import com.android.car.ui.CarUiRobolectricTestRunner; |
| 33 | |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
| 37 | import org.mockito.Mock; |
| 38 | import org.mockito.MockitoAnnotations; |
| 39 | import org.robolectric.RuntimeEnvironment; |
| 40 | import org.robolectric.annotation.Config; |
| 41 | |
| 42 | @RunWith(CarUiRobolectricTestRunner.class) |
| 43 | @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) |
| 44 | public class DefaultScrollBarTest { |
| 45 | |
| 46 | private Context mContext; |
| 47 | private ScrollBar mScrollBar; |
| 48 | |
| 49 | @Mock |
| 50 | private RecyclerView mRecyclerView; |
| 51 | @Mock |
| 52 | private FrameLayout mParent; |
| 53 | @Mock |
| 54 | private FrameLayout.LayoutParams mLayoutParams; |
| 55 | @Mock |
| 56 | private RecyclerView.RecycledViewPool mRecycledViewPool; |
| 57 | |
| 58 | @Before |
| 59 | public void setUp() { |
| 60 | MockitoAnnotations.initMocks(this); |
| 61 | mContext = RuntimeEnvironment.application; |
| 62 | |
| 63 | mScrollBar = new DefaultScrollBar(); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void initialize_shouldInitializeScrollListener() { |
| 68 | when(mRecyclerView.getContext()).thenReturn(mContext); |
| 69 | when(mRecyclerView.getParent()).thenReturn(mParent); |
| 70 | when(mRecyclerView.getRecycledViewPool()).thenReturn(mRecycledViewPool); |
| 71 | when(mParent.generateLayoutParams(any())).thenReturn(mLayoutParams); |
| 72 | |
| 73 | mScrollBar.initialize(mRecyclerView, 10, PagedRecyclerView.ScrollBarPosition.START, true); |
| 74 | |
| 75 | // called once in DefaultScrollBar and once in SnapHelper while setting up the call backs |
| 76 | // when we use attachToRecyclerView(recyclerview) |
| 77 | verify(mRecyclerView, times(2)).addOnScrollListener( |
| 78 | any(RecyclerView.OnScrollListener.class)); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void initialize_shouldSetMaxRecyclerViews() { |
| 83 | when(mRecyclerView.getContext()).thenReturn(mContext); |
| 84 | when(mRecyclerView.getParent()).thenReturn(mParent); |
| 85 | when(mRecyclerView.getRecycledViewPool()).thenReturn(mRecycledViewPool); |
| 86 | when(mParent.generateLayoutParams(any())).thenReturn(mLayoutParams); |
| 87 | |
| 88 | mScrollBar.initialize(mRecyclerView, 10, PagedRecyclerView.ScrollBarPosition.START, true); |
| 89 | |
| 90 | verify(mRecycledViewPool).setMaxRecycledViews(0, 12); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void initialize_shouldNotHaveFlingListener() { |
| 95 | when(mRecyclerView.getContext()).thenReturn(mContext); |
| 96 | when(mRecyclerView.getParent()).thenReturn(mParent); |
| 97 | when(mRecyclerView.getRecycledViewPool()).thenReturn(mRecycledViewPool); |
| 98 | when(mParent.generateLayoutParams(any())).thenReturn(mLayoutParams); |
| 99 | |
| 100 | mScrollBar.initialize(mRecyclerView, 10, PagedRecyclerView.ScrollBarPosition.START, true); |
| 101 | |
| 102 | verify(mRecyclerView).setOnFlingListener(null); |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void setPadding_shouldSetStartAndEndPadding() { |
| 107 | when(mRecyclerView.getContext()).thenReturn(mContext); |
| 108 | when(mRecyclerView.getParent()).thenReturn(mParent); |
| 109 | when(mRecyclerView.getRecycledViewPool()).thenReturn(mRecycledViewPool); |
| 110 | when(mParent.generateLayoutParams(any())).thenReturn(mLayoutParams); |
| 111 | |
| 112 | mScrollBar.initialize(mRecyclerView, 10, PagedRecyclerView.ScrollBarPosition.START, true); |
| 113 | mScrollBar.setPadding(10, 20); |
| 114 | |
| 115 | DefaultScrollBar defaultScrollBar = (DefaultScrollBar) mScrollBar; |
| 116 | |
| 117 | assertThat(defaultScrollBar.mPaddingStart).isEqualTo(10); |
| 118 | assertThat(defaultScrollBar.mPaddingEnd).isEqualTo(20); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void setPadding_shouldThrowErrorWithoutInitialization() { |
| 123 | assertThrows(NullPointerException.class, () -> mScrollBar.setPadding(10, 20)); |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void requestLayout_shouldThrowErrorWithoutInitialization() { |
| 128 | assertThrows(NullPointerException.class, () -> mScrollBar.requestLayout()); |
| 129 | } |
| 130 | } |