1 /* 2 * Copyright (C) 2022 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.carlauncher; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.view.View; 22 23 import androidx.test.ext.junit.runners.AndroidJUnit4; 24 25 import static com.android.car.carlauncher.AppGridConstants.PageOrientation; 26 27 import com.android.car.carlauncher.pagination.PageIndexingHelper; 28 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 @RunWith(AndroidJUnit4.class) 33 public class PageIndexingHelperTest { 34 private static final int LTR = View.LAYOUT_DIRECTION_LTR; 35 private static final int RTL = View.LAYOUT_DIRECTION_RTL; 36 37 @Test test2x4x1_testAdapterGridOneToOneMapping_LTR()38 public void test2x4x1_testAdapterGridOneToOneMapping_LTR() { 39 PageIndexingHelper helper = createTestAppGridPagingUtils( 40 /* numOfCols */ 4, /* numOfRows */ 2, LTR); 41 int[] gridPositions = new int[] { 42 0, 2, 4, 6, 43 1, 3, 5, 7, 44 }; 45 int[] adapterIndexes = new int[]{ 46 0, 1, 2, 3, 47 4, 5, 6, 7, 48 }; 49 runTwoWayMappingTest(gridPositions, adapterIndexes, helper); 50 } 51 52 @Test test2x4x1_testAdapterGridOneToOneMapping_RTL()53 public void test2x4x1_testAdapterGridOneToOneMapping_RTL() { 54 PageIndexingHelper helper = createTestAppGridPagingUtils( 55 /* numOfCols */ 4, /* numOfRows */ 2, RTL); 56 int[] gridPositions = new int[] { 57 0, 2, 4, 6, 58 1, 3, 5, 7, 59 }; 60 int[] adapterIndexes = new int[]{ 61 3, 2, 1, 0, 62 7, 6, 5, 4, 63 }; 64 runTwoWayMappingTest(gridPositions, adapterIndexes, helper); 65 } 66 67 @Test test3x5x2_testAdapterGridOneToOneMapping_LTR()68 public void test3x5x2_testAdapterGridOneToOneMapping_LTR() { 69 PageIndexingHelper helper = createTestAppGridPagingUtils( 70 /* numOfCols */ 5, /* numOfRows */ 3, LTR); 71 int[] gridPositions = new int[] { 72 0, 3, 6, 9, 12, 73 1, 4, 7, 10, 13, 74 2, 5, 8, 11, 14, 75 // next page 76 15, 18, 21, 24, 27, 77 16, 19, 22, 25, 28, 78 17, 20, 23, 26, 29, 79 }; 80 int[] adapterIndexes = new int[]{ 81 0, 1, 2, 3, 4, 82 5, 6, 7, 8, 9, 83 10, 11, 12, 13, 14, 84 // next page 85 15, 16, 17, 18, 19, 86 20, 21, 22, 23, 24, 87 25, 26, 27, 28, 29, 88 }; 89 runTwoWayMappingTest(gridPositions, adapterIndexes, helper); 90 } 91 92 @Test test3x5x2_testAdapterGridOneToOneMapping_RTL()93 public void test3x5x2_testAdapterGridOneToOneMapping_RTL() { 94 PageIndexingHelper helper = createTestAppGridPagingUtils( 95 /* numOfCols */ 5, /* numOfRows */ 3, RTL); 96 int[] gridPositions = new int[] { 97 0, 3, 6, 9, 12, 98 1, 4, 7, 10, 13, 99 2, 5, 8, 11, 14, 100 // next page 101 15, 18, 21, 24, 27, 102 16, 19, 22, 25, 28, 103 17, 20, 23, 26, 29, 104 }; 105 int[] adapterIndexes = new int[]{ 106 4, 3, 2, 1, 0, 107 9, 8, 7, 6, 5, 108 14, 13, 12, 11, 10, 109 // next page 110 19, 18, 17, 16, 15, 111 24, 23, 22, 21, 20, 112 29, 28, 27, 26, 25, 113 }; 114 runTwoWayMappingTest(gridPositions, adapterIndexes, helper); 115 } 116 117 @Test test1x6x3_testAdapterGridOneToOneMapping_LTR()118 public void test1x6x3_testAdapterGridOneToOneMapping_LTR() { 119 PageIndexingHelper helper = createTestAppGridPagingUtils( 120 /* numOfCols */ 6, /* numOfRows */ 1, LTR); 121 int[] gridPositions = new int[] { 122 0, 1, 2, 3, 4, 5, 123 // next page 124 6, 7, 8, 9, 10, 11, 125 // next page 126 12, 13, 14, 15, 16, 17, 127 }; 128 int[] adapterIndexes = new int[]{ 129 0, 1, 2, 3, 4, 5, 130 // next page 131 6, 7, 8, 9, 10, 11, 132 // next page 133 12, 13, 14, 15, 16, 17, 134 }; 135 runTwoWayMappingTest(gridPositions, adapterIndexes, helper); 136 } 137 138 @Test test1x6x3_testAdapterGridOneToOneMapping_RTL()139 public void test1x6x3_testAdapterGridOneToOneMapping_RTL() { 140 PageIndexingHelper helper = createTestAppGridPagingUtils( 141 /* numOfCols */ 6, /* numOfRows */ 1, RTL); 142 int[] gridPositions = new int[] { 143 0, 1, 2, 3, 4, 5, 144 // next page 145 6, 7, 8, 9, 10, 11, 146 // next page 147 12, 13, 14, 15, 16, 17, 148 }; 149 int[] adapterIndexes = new int[]{ 150 5, 4, 3, 2, 1, 0, 151 // next page 152 11, 10, 9, 8, 7, 6, 153 // next page 154 17, 16, 15, 14, 13, 12, 155 }; 156 runTwoWayMappingTest(gridPositions, adapterIndexes, helper); 157 } 158 runTwoWayMappingTest(int[] gridPositions, int[] adapterIndexes, PageIndexingHelper testUtils)159 private void runTwoWayMappingTest(int[] gridPositions, int[] adapterIndexes, 160 PageIndexingHelper testUtils) { 161 // check forward mapping from grid position generated from 162 // gridPosition = viewHolder.getAbsoluteAdapterPosition() 163 for (int i = 0; i < gridPositions.length; i++) { 164 assertEquals(testUtils.gridPositionToAdaptorIndex(gridPositions[i]), adapterIndexes[i]); 165 } 166 // check inverse mapping from adapter index 167 for (int i = 0; i < adapterIndexes.length; i++) { 168 assertEquals(testUtils.adaptorIndexToGridPosition(adapterIndexes[i]), gridPositions[i]); 169 } 170 } 171 172 @Test test2x4x1_roundToFirstAndLastIndex()173 public void test2x4x1_roundToFirstAndLastIndex() { 174 PageIndexingHelper helperLtr = createTestAppGridPagingUtils( 175 /* numOfCols */ 4, /* numOfRows */ 2, LTR); 176 PageIndexingHelper helperRtl = createTestAppGridPagingUtils( 177 /* numOfCols */ 4, /* numOfRows */ 2, RTL); 178 int[] gridPositions = new int[] { 179 0, 2, 4, 6, 180 1, 3, 5, 7, 181 }; 182 for (int i = 0; i < gridPositions.length; i++) { 183 // round to left most index on page 184 assertEquals(helperLtr.roundToFirstIndexOnPage(gridPositions[i]), 0); 185 assertEquals(helperRtl.roundToFirstIndexOnPage(gridPositions[i]), 0); 186 // round to right most index 187 assertEquals(helperLtr.roundToLastIndexOnPage(gridPositions[i]), 7); 188 assertEquals(helperRtl.roundToLastIndexOnPage(gridPositions[i]), 7); 189 } 190 } 191 192 @Test test3x5x2_roundToFirstAndLastIndex()193 public void test3x5x2_roundToFirstAndLastIndex() { 194 PageIndexingHelper helperLtr = createTestAppGridPagingUtils( 195 /* numOfCols */ 5, /* numOfRows */ 3, LTR); 196 PageIndexingHelper helperRtl = createTestAppGridPagingUtils( 197 /* numOfCols */ 5, /* numOfRows */ 3, RTL); 198 int[] gridPositions = new int[] { 199 0, 3, 6, 9, 12, 200 1, 4, 7, 10, 13, 201 2, 5, 8, 11, 14, 202 // next page 203 15, 18, 21, 24, 27, 204 16, 19, 22, 25, 28, 205 17, 20, 23, 26, 29, 206 }; 207 for (int i = 0; i < 15; i++) { 208 // round to left most index on page 209 assertEquals(helperLtr.roundToFirstIndexOnPage(gridPositions[i]), 0); 210 assertEquals(helperRtl.roundToFirstIndexOnPage(gridPositions[i]), 0); 211 // round to right most index 212 assertEquals(helperLtr.roundToLastIndexOnPage(gridPositions[i]), 14); 213 assertEquals(helperRtl.roundToLastIndexOnPage(gridPositions[i]), 14); 214 } 215 for (int i = 15; i < gridPositions.length; i++) { 216 // round to left most index on page 217 assertEquals(helperLtr.roundToFirstIndexOnPage(gridPositions[i]), 15); 218 assertEquals(helperRtl.roundToFirstIndexOnPage(gridPositions[i]), 15); 219 // round to right most index 220 assertEquals(helperLtr.roundToLastIndexOnPage(gridPositions[i]), 29); 221 assertEquals(helperRtl.roundToLastIndexOnPage(gridPositions[i]), 29); 222 } 223 } 224 225 @Test test1x6x3_roundToFirstAndLastIndex()226 public void test1x6x3_roundToFirstAndLastIndex() { 227 PageIndexingHelper helperLtr = createTestAppGridPagingUtils( 228 /* numOfCols */ 6, /* numOfRows */ 1, LTR); 229 PageIndexingHelper helperRtl = createTestAppGridPagingUtils( 230 /* numOfCols */ 6, /* numOfRows */ 1, RTL); 231 int[] gridPositions = new int[] { 232 0, 1, 2, 3, 4, 5, 233 // next page 234 6, 7, 8, 9, 10, 11, 235 // next page 236 12, 13, 14, 15, 16, 17, 237 }; 238 for (int i = 0; i < 6; i++) { 239 // round to left most index on page 240 assertEquals(helperLtr.roundToFirstIndexOnPage(gridPositions[i]), 0); 241 assertEquals(helperRtl.roundToFirstIndexOnPage(gridPositions[i]), 0); 242 // round to right most index 243 assertEquals(helperLtr.roundToLastIndexOnPage(gridPositions[i]), 5); 244 assertEquals(helperRtl.roundToLastIndexOnPage(gridPositions[i]), 5); 245 } 246 for (int i = 6; i < 11; i++) { 247 // round to left most index on page 248 assertEquals(helperLtr.roundToFirstIndexOnPage(gridPositions[i]), 6); 249 assertEquals(helperRtl.roundToFirstIndexOnPage(gridPositions[i]), 6); 250 // round to right most index 251 assertEquals(helperLtr.roundToLastIndexOnPage(gridPositions[i]), 11); 252 assertEquals(helperRtl.roundToLastIndexOnPage(gridPositions[i]), 11); 253 } 254 for (int i = 12; i < 17; i++) { 255 // round to left most index on page 256 assertEquals(helperLtr.roundToFirstIndexOnPage(gridPositions[i]), 12); 257 assertEquals(helperRtl.roundToFirstIndexOnPage(gridPositions[i]), 12); 258 // round to right most index 259 assertEquals(helperLtr.roundToLastIndexOnPage(gridPositions[i]), 17); 260 assertEquals(helperRtl.roundToLastIndexOnPage(gridPositions[i]), 17); 261 } 262 } 263 createTestAppGridPagingUtils(int numOfCols, int numOfRows, int layoutDirection)264 private PageIndexingHelper createTestAppGridPagingUtils(int numOfCols, int numOfRows, 265 int layoutDirection) { 266 PageIndexingHelper utils = new PageIndexingHelper(numOfCols, numOfRows, 267 PageOrientation.HORIZONTAL); 268 utils.setLayoutDirection(layoutDirection); 269 return utils; 270 } 271 } 272