1 package com.android.launcher3.util; 2 3 import static org.junit.Assert.assertEquals; 4 import static org.junit.Assert.assertFalse; 5 import static org.junit.Assert.assertTrue; 6 7 import androidx.test.ext.junit.runners.AndroidJUnit4; 8 import androidx.test.filters.SmallTest; 9 10 import org.junit.Test; 11 import org.junit.runner.RunWith; 12 13 /** 14 * Unit tests for {@link GridOccupancy} 15 */ 16 @SmallTest 17 @RunWith(AndroidJUnit4.class) 18 public class GridOccupancyTest { 19 20 @Test testFindVacantCell()21 public void testFindVacantCell() { 22 GridOccupancy grid = initGrid(4, 23 1, 1, 1, 0, 0, 24 0, 0, 1, 1, 0, 25 0, 0, 0, 0, 0, 26 1, 1, 0, 0, 0 27 ); 28 29 int[] vacant = new int[2]; 30 assertTrue(grid.findVacantCell(vacant, 2, 2)); 31 assertEquals(vacant[0], 0); 32 assertEquals(vacant[1], 1); 33 34 assertTrue(grid.findVacantCell(vacant, 3, 2)); 35 assertEquals(vacant[0], 2); 36 assertEquals(vacant[1], 2); 37 38 assertFalse(grid.findVacantCell(vacant, 3, 3)); 39 } 40 41 @Test testIsRegionVacant()42 public void testIsRegionVacant() { 43 GridOccupancy grid = initGrid(4, 44 1, 1, 1, 0, 0, 45 0, 0, 1, 1, 0, 46 0, 0, 0, 0, 0, 47 1, 1, 0, 0, 0 48 ); 49 50 assertTrue(grid.isRegionVacant(4, 0, 1, 4)); 51 assertTrue(grid.isRegionVacant(0, 1, 2, 2)); 52 assertTrue(grid.isRegionVacant(2, 2, 3, 2)); 53 54 assertFalse(grid.isRegionVacant(3, 0, 2, 4)); 55 assertFalse(grid.isRegionVacant(0, 0, 2, 1)); 56 } 57 initGrid(int rows, int... cells)58 private GridOccupancy initGrid(int rows, int... cells) { 59 int cols = cells.length / rows; 60 int i = 0; 61 GridOccupancy grid = new GridOccupancy(cols, rows); 62 for (int y = 0; y < rows; y++) { 63 for (int x = 0; x < cols; x++) { 64 grid.cells[x][y] = cells[i] != 0; 65 i++; 66 } 67 } 68 return grid; 69 } 70 } 71