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