• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.launcher3.util;
2 
3 import android.test.suitebuilder.annotation.SmallTest;
4 
5 import junit.framework.TestCase;
6 
7 /**
8  * Unit tests for {@link GridOccupancy}
9  */
10 @SmallTest
11 public class GridOccupancyTest extends TestCase {
12 
testFindVacantCell()13     public void testFindVacantCell() {
14         GridOccupancy grid = initGrid(4,
15                 1, 1, 1, 0, 0,
16                 0, 0, 1, 1, 0,
17                 0, 0, 0, 0, 0,
18                 1, 1, 0, 0, 0
19                 );
20 
21         int[] vacant = new int[2];
22         assertTrue(grid.findVacantCell(vacant, 2, 2));
23         assertEquals(vacant[0], 0);
24         assertEquals(vacant[1], 1);
25 
26         assertTrue(grid.findVacantCell(vacant, 3, 2));
27         assertEquals(vacant[0], 2);
28         assertEquals(vacant[1], 2);
29 
30         assertFalse(grid.findVacantCell(vacant, 3, 3));
31     }
32 
testIsRegionVacant()33     public void testIsRegionVacant() {
34         GridOccupancy grid = initGrid(4,
35                 1, 1, 1, 0, 0,
36                 0, 0, 1, 1, 0,
37                 0, 0, 0, 0, 0,
38                 1, 1, 0, 0, 0
39         );
40 
41         assertTrue(grid.isRegionVacant(4, 0, 1, 4));
42         assertTrue(grid.isRegionVacant(0, 1, 2, 2));
43         assertTrue(grid.isRegionVacant(2, 2, 3, 2));
44 
45         assertFalse(grid.isRegionVacant(3, 0, 2, 4));
46         assertFalse(grid.isRegionVacant(0, 0, 2, 1));
47     }
48 
initGrid(int rows, int... cells)49     private GridOccupancy initGrid(int rows, int... cells) {
50         int cols = cells.length / rows;
51         int i = 0;
52         GridOccupancy grid = new GridOccupancy(cols, rows);
53         for (int y = 0; y < rows; y++) {
54             for (int x = 0; x < cols; x++) {
55                 grid.cells[x][y] = cells[i] != 0;
56                 i++;
57             }
58         }
59         return grid;
60     }
61 }
62