blob: b38a7ee2a0ea600da435974873f8e9297607bde9 [file] [log] [blame]
Priyank Singh3c025d32019-09-27 14:20:00 -07001/*
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 Singh7361b4b2019-10-04 11:24:10 -070017package com.android.car.ui.recyclerview;
Priyank Singh3c025d32019-09-27 14:20:00 -070018
19import static com.google.common.truth.Truth.assertThat;
20
21import static org.mockito.ArgumentMatchers.any;
22import static org.mockito.Mockito.times;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25import static org.testng.Assert.assertThrows;
26
27import android.content.Context;
28import android.widget.FrameLayout;
29
30import androidx.recyclerview.widget.RecyclerView;
31
Priyank Singh3c025d32019-09-27 14:20:00 -070032import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.Mock;
36import org.mockito.MockitoAnnotations;
Ram Parameswaran39020e42020-01-23 17:50:54 -080037import org.robolectric.RobolectricTestRunner;
Priyank Singh3c025d32019-09-27 14:20:00 -070038import org.robolectric.RuntimeEnvironment;
Priyank Singh3c025d32019-09-27 14:20:00 -070039
Ram Parameswaran39020e42020-01-23 17:50:54 -080040@RunWith(RobolectricTestRunner.class)
Priyank Singh3c025d32019-09-27 14:20:00 -070041public 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 Singh7361b4b2019-10-04 11:24:10 -070070 mScrollBar.initialize(mRecyclerView, 10, CarUiRecyclerView.ScrollBarPosition.START, true);
Priyank Singh3c025d32019-09-27 14:20:00 -070071
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 Singh7361b4b2019-10-04 11:24:10 -070085 mScrollBar.initialize(mRecyclerView, 10, CarUiRecyclerView.ScrollBarPosition.START, true);
Priyank Singh3c025d32019-09-27 14:20:00 -070086
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 Singh7361b4b2019-10-04 11:24:10 -070097 mScrollBar.initialize(mRecyclerView, 10, CarUiRecyclerView.ScrollBarPosition.START, true);
Priyank Singh3c025d32019-09-27 14:20:00 -070098
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 Singh7361b4b2019-10-04 11:24:10 -0700109 mScrollBar.initialize(mRecyclerView, 10, CarUiRecyclerView.ScrollBarPosition.START, true);
Priyank Singh3c025d32019-09-27 14:20:00 -0700110 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}