|
18 | 18 | import com.google.android.material.test.R;
|
19 | 19 |
|
20 | 20 | import static com.google.android.material.carousel.CarouselHelper.createCarouselWithWidth;
|
| 21 | +import static com.google.android.material.carousel.CarouselHelper.createCenterAlignedCarouselWithSize; |
21 | 22 | import static com.google.android.material.carousel.CarouselHelper.createViewWithSize;
|
| 23 | +import static com.google.android.material.carousel.CarouselStrategyHelper.getSmallSizeMin; |
22 | 24 | import static com.google.common.truth.Truth.assertThat;
|
23 | 25 |
|
24 | 26 | import android.view.View;
|
@@ -48,7 +50,8 @@ public void testLargeItem_withFullCarouselWidth() {
|
48 | 50 | assertThat(keylineState.getKeylines()).hasSize(4);
|
49 | 51 | assertThat(keylineState.getKeylines().get(0).locOffset).isLessThan(0F);
|
50 | 52 | assertThat(keylineState.getKeylines().get(1).mask).isEqualTo(0F);
|
51 |
| - assertThat(keylineState.getKeylines().get(2).locOffset).isEqualTo(carousel.getContainerWidth() + xSmallSize/2F); |
| 53 | + assertThat(keylineState.getKeylines().get(2).locOffset) |
| 54 | + .isEqualTo(carousel.getContainerWidth() + xSmallSize / 2F); |
52 | 55 | assertThat(Iterables.getLast(keylineState.getKeylines()).locOffset)
|
53 | 56 | .isGreaterThan((float) carousel.getContainerWidth());
|
54 | 57 | }
|
@@ -119,4 +122,85 @@ public void testRemainingSpaceWithItemSize_fitsLargeItemWithCutoff() {
|
119 | 122 | assertThat(Iterables.getLast(keylineState.getKeylines()).locOffset)
|
120 | 123 | .isGreaterThan((float) carousel.getContainerWidth());
|
121 | 124 | }
|
| 125 | + |
| 126 | + @Test |
| 127 | + public void testCenterAligned_defaultKeylineHasTwoCutoffs() { |
| 128 | + Carousel carousel = createCenterAlignedCarouselWithSize(400); |
| 129 | + UncontainedCarouselStrategy config = new UncontainedCarouselStrategy(); |
| 130 | + int itemSize = 250; |
| 131 | + // With this item size, we have 400 - 250 = 150 remaining space which means 75 on each side |
| 132 | + // of one focal item. |
| 133 | + View view = createViewWithSize(ApplicationProvider.getApplicationContext(), itemSize, 400); |
| 134 | + |
| 135 | + KeylineState keylineState = config.onFirstChildMeasuredWithMargins(carousel, view); |
| 136 | + |
| 137 | + // The layout should be [xSmall-medium-large-medium-xSmall] |
| 138 | + assertThat(keylineState.getKeylines()).hasSize(5); |
| 139 | + assertThat(keylineState.getKeylines().get(0).locOffset).isLessThan(0F); |
| 140 | + assertThat(keylineState.getKeylines().get(1).cutoff) |
| 141 | + .isEqualTo(150F); // 75*2 since 2/3 should be cut off |
| 142 | + assertThat(keylineState.getKeylines().get(2).mask).isEqualTo(0F); |
| 143 | + assertThat(keylineState.getKeylines().get(3).cutoff) |
| 144 | + .isEqualTo(150F); // 75*2 since 2/3 should be cut off |
| 145 | + assertThat(keylineState.getKeylines().get(4).locOffset) |
| 146 | + .isGreaterThan((float) carousel.getContainerWidth()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testCenterAligned_cutoffMinSize() { |
| 151 | + Carousel carousel = createCenterAlignedCarouselWithSize(400); |
| 152 | + UncontainedCarouselStrategy config = new UncontainedCarouselStrategy(); |
| 153 | + int itemSize = 200; |
| 154 | + // 2 items fit perfectly in the width so there is no remaining space. Medium items should still |
| 155 | + // be the minimum item mask size. |
| 156 | + View view = createViewWithSize(ApplicationProvider.getApplicationContext(), itemSize, 400); |
| 157 | + |
| 158 | + KeylineState keylineState = config.onFirstChildMeasuredWithMargins(carousel, view); |
| 159 | + |
| 160 | + float minSmallSize = getSmallSizeMin(ApplicationProvider.getApplicationContext()); |
| 161 | + |
| 162 | + // The layout should be [xSmall-medium-large-large-medium-xSmall] |
| 163 | + assertThat(keylineState.getKeylines()).hasSize(6); |
| 164 | + assertThat(keylineState.getKeylines().get(0).locOffset).isLessThan(0F); |
| 165 | + assertThat(keylineState.getKeylines().get(1).cutoff) |
| 166 | + .isEqualTo(keylineState.getKeylines().get(1).maskedItemSize); |
| 167 | + assertThat(keylineState.getKeylines().get(1).locOffset).isLessThan(0F); |
| 168 | + assertThat(keylineState.getKeylines().get(1).maskedItemSize).isEqualTo(minSmallSize); |
| 169 | + assertThat(keylineState.getKeylines().get(2).mask).isEqualTo(0F); |
| 170 | + assertThat(keylineState.getKeylines().get(3).mask).isEqualTo(0F); |
| 171 | + assertThat(keylineState.getKeylines().get(4).cutoff) |
| 172 | + .isEqualTo(keylineState.getKeylines().get(1).maskedItemSize); |
| 173 | + assertThat(keylineState.getKeylines().get(4).locOffset) |
| 174 | + .isGreaterThan((float) carousel.getContainerWidth()); |
| 175 | + assertThat(keylineState.getKeylines().get(4).maskedItemSize).isEqualTo(minSmallSize); |
| 176 | + assertThat(keylineState.getKeylines().get(5).locOffset) |
| 177 | + .isGreaterThan((float) carousel.getContainerWidth()); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void testCenterAligned_cutoffMaxSize() { |
| 182 | + Carousel carousel = createCenterAlignedCarouselWithSize(400); |
| 183 | + UncontainedCarouselStrategy config = new UncontainedCarouselStrategy(); |
| 184 | + int itemSize = 140; |
| 185 | + // 2 items fit into width of 400 with 120 remaining space; 60 on each side. Only a 1/3 should be |
| 186 | + // showing which means an item width of 180 for the cut off items, but we do not want these |
| 187 | + // items to be bigger than the focal item so the max item size should be the focal item size. |
| 188 | + View view = createViewWithSize(ApplicationProvider.getApplicationContext(), itemSize, 400); |
| 189 | + |
| 190 | + KeylineState keylineState = config.onFirstChildMeasuredWithMargins(carousel, view); |
| 191 | + |
| 192 | + // The layout should be [xSmall-medium-large-large-medium-xSmall] |
| 193 | + assertThat(keylineState.getKeylines()).hasSize(6); |
| 194 | + assertThat(keylineState.getKeylines().get(0).locOffset).isLessThan(0F); |
| 195 | + assertThat(keylineState.getKeylines().get(1).maskedItemSize).isEqualTo((float) itemSize); |
| 196 | + // Item size should be max size: 180F, so 140 - 60 (remaining space) = 80 |
| 197 | + assertThat(keylineState.getKeylines().get(1).cutoff).isEqualTo(80F); |
| 198 | + assertThat(keylineState.getKeylines().get(2).mask).isEqualTo(0F); |
| 199 | + assertThat(keylineState.getKeylines().get(3).mask).isEqualTo(0F); |
| 200 | + assertThat(keylineState.getKeylines().get(4).maskedItemSize).isEqualTo((float) itemSize); |
| 201 | + // Item size should be max size: 180F, so 140 - 60 (remaining space) = 80 |
| 202 | + assertThat(keylineState.getKeylines().get(4).cutoff).isEqualTo(80F); |
| 203 | + assertThat(keylineState.getKeylines().get(5).locOffset) |
| 204 | + .isGreaterThan((float) carousel.getContainerWidth()); |
| 205 | + } |
122 | 206 | }
|
0 commit comments