/
CarouselLayoutManagerRtlTest.java
199 lines (169 loc) · 8.29 KB
/
CarouselLayoutManagerRtlTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.carousel;
import com.google.android.material.test.R;
import static com.google.android.material.carousel.CarouselHelper.createDataSetWithSize;
import static com.google.android.material.carousel.CarouselHelper.scrollHorizontallyBy;
import static com.google.android.material.carousel.CarouselHelper.scrollToPosition;
import static com.google.android.material.carousel.CarouselHelper.setAdapterItems;
import static com.google.android.material.carousel.CarouselHelper.setViewSize;
import static com.google.android.material.testing.RtlTestUtils.applyRtlPseudoLocale;
import static com.google.android.material.testing.RtlTestUtils.checkAppSupportsRtl;
import static com.google.android.material.testing.RtlTestUtils.checkPlatformSupportsRtl;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.os.Build.VERSION_CODES;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.test.core.app.ApplicationProvider;
import com.google.android.material.carousel.CarouselHelper.CarouselTestAdapter;
import com.google.android.material.carousel.CarouselHelper.WrappedCarouselLayoutManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
/** RTL tests for {@link CarouselLayoutManager}. */
@RunWith(RobolectricTestRunner.class)
@org.junit.Ignore("(b/265311943) Fix RTL support for Robolectric tests.")
public class CarouselLayoutManagerRtlTest {
private static final int DEFAULT_RECYCLER_VIEW_WIDTH = 1320;
private static final int DEFAULT_RECYCLER_VIEW_HEIGHT = 200;
private static final int DEFAULT_ITEM_WIDTH = 450;
private static final int DEFAULT_ITEM_HEIGHT = 200;
private final Context context = ApplicationProvider.getApplicationContext();
RecyclerView recyclerView;
WrappedCarouselLayoutManager layoutManager;
CarouselTestAdapter adapter;
@RequiresApi(api = VERSION_CODES.JELLY_BEAN_MR1)
@Before
public void setUp() {
checkPlatformSupportsRtl();
checkAppSupportsRtl();
applyRtlPseudoLocale();
createAndSetFixtures(DEFAULT_RECYCLER_VIEW_WIDTH, DEFAULT_ITEM_WIDTH);
layoutManager.setCarouselStrategy(
new CarouselStrategy() {
@Override
KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return getTestCenteredKeylineState();
}
});
}
@Test
public void testFirstAdapterItem_isDrawnAtRightOfContainer() throws Throwable {
setAdapterItems(recyclerView, layoutManager, adapter, createDataSetWithSize(10));
MaskableFrameLayout firstChild = (MaskableFrameLayout) recyclerView.getChildAt(0);
assertThat(recyclerView.getChildAdapterPosition(firstChild)).isEqualTo(0);
assertThat(firstChild.getRight()).isEqualTo(DEFAULT_RECYCLER_VIEW_WIDTH);
}
@Test
public void testScrollBeyondMaxHorizontalScroll_shouldLimitToMaxScrollOffset() throws Throwable {
KeylineState keylineState = getTestCenteredKeylineState();
layoutManager.setCarouselStrategy(
new CarouselStrategy() {
@Override
KeylineState onFirstChildMeasuredWithMargins(
@NonNull Carousel carousel, @NonNull View child) {
return keylineState;
}
});
setAdapterItems(recyclerView, layoutManager, adapter, createDataSetWithSize(10));
scrollToPosition(recyclerView, layoutManager, 200);
KeylineState leftState =
KeylineStateList.from(layoutManager, KeylineState.reverse(keylineState)).getLeftState();
MaskableFrameLayout child =
(MaskableFrameLayout) recyclerView.getChildAt(recyclerView.getChildCount() - 1);
float childCenterX = child.getLeft() + (child.getWidth() / 2F);
assertThat(childCenterX).isEqualTo(leftState.getFirstFocalKeyline().locOffset);
}
@Test
public void testSingleItem_shouldBeInFocalRange() throws Throwable {
setAdapterItems(recyclerView, layoutManager, adapter, CarouselHelper.createDataSetWithSize(1));
assertThat(((Maskable) recyclerView.getChildAt(0)).getMaskXPercentage()).isEqualTo(0F);
}
@Test
public void testSingleItem_shouldNotScrollLeft() throws Throwable {
setAdapterItems(recyclerView, layoutManager, adapter, CarouselHelper.createDataSetWithSize(1));
scrollHorizontallyBy(recyclerView, layoutManager, 100);
assertThat(recyclerView.getChildAt(0).getRight()).isEqualTo(DEFAULT_RECYCLER_VIEW_WIDTH);
}
@Test
public void testSingleItem_shouldNotScrollRight() throws Throwable {
setAdapterItems(recyclerView, layoutManager, adapter, CarouselHelper.createDataSetWithSize(1));
scrollHorizontallyBy(recyclerView, layoutManager, -100);
assertThat(recyclerView.getChildAt(0).getRight()).isEqualTo(DEFAULT_RECYCLER_VIEW_WIDTH);
}
@Test
public void testChangeAdapterItemCount_shouldAlignFirstItemToStart() throws Throwable {
setAdapterItems(
recyclerView, layoutManager, adapter, CarouselHelper.createDataSetWithSize(200));
scrollToPosition(recyclerView, layoutManager, 100);
setAdapterItems(recyclerView, layoutManager, adapter, createDataSetWithSize(1));
assertThat(recyclerView.getChildCount()).isEqualTo(1);
assertThat(recyclerView.getChildAt(0).getRight()).isEqualTo(DEFAULT_RECYCLER_VIEW_WIDTH);
}
@Test
public void testScrollToEndThenToStart_childrenHaveValidOrder() throws Throwable {
// TODO(b/271293808): Refactor to use parameterized tests.
setAdapterItems(recyclerView, layoutManager, adapter, CarouselHelper.createDataSetWithSize(10));
scrollToPosition(recyclerView, layoutManager, 9);
scrollToPosition(recyclerView, layoutManager, 2);
CarouselHelper.assertChildrenHaveValidOrder(layoutManager);
}
/**
* Assigns explicit sizes to fixtures being used to construct the testing environment.
*
* @param recyclerWidth The width of the recycler view being used
* @param itemWidth The width each item added to the recycler view would like to be laid out with.
* What would be defined in xml as {@code android:layout_width}.
*/
private void createAndSetFixtures(int recyclerWidth, int itemWidth) {
ApplicationProvider.getApplicationContext().setTheme(R.style.Theme_Material3_Light);
AppCompatActivity activity = Robolectric.buildActivity(AppCompatActivity.class).setup().get();
recyclerView = new RecyclerView(context);
activity.addContentView(
recyclerView, new LayoutParams(recyclerWidth, DEFAULT_RECYCLER_VIEW_HEIGHT));
setViewSize(recyclerView, recyclerWidth, DEFAULT_RECYCLER_VIEW_HEIGHT);
layoutManager = new WrappedCarouselLayoutManager();
adapter = new CarouselTestAdapter(itemWidth, DEFAULT_ITEM_HEIGHT);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
private static KeylineState getTestCenteredKeylineState() {
float smallSize = 56F;
float extraSmallSize = 10F;
float largeSize = 450F;
float mediumSize = 88F;
float extraSmallMask = 1F - (extraSmallSize / largeSize);
float smallMask = 1F - (smallSize / largeSize);
float mediumMask = 1F - (mediumSize / largeSize);
return new KeylineState.Builder(450F)
.addKeyline(5F, extraSmallMask, extraSmallSize)
.addKeylineRange(38F, smallMask, smallSize, 2)
.addKeyline(166F, mediumMask, mediumSize)
.addKeylineRange(435F, 0F, largeSize, 2, true)
.addKeyline(1154F, mediumMask, mediumSize)
.addKeylineRange(1226F, smallMask, smallSize, 2)
.addKeyline(1315F, extraSmallMask, extraSmallSize)
.build();
}
}