• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.android.launcher3.util;
18 
19 import android.support.test.filters.SmallTest;
20 import android.support.test.runner.AndroidJUnit4;
21 import android.view.KeyEvent;
22 
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 
29 /**
30  * Tests the {@link FocusLogic} class that handles key event based focus handling.
31  */
32 @SmallTest
33 @RunWith(AndroidJUnit4.class)
34 public final class FocusLogicTest {
35 
36     @Test
testShouldConsume()37     public void testShouldConsume() {
38          assertTrue(FocusLogic.shouldConsume(KeyEvent.KEYCODE_DPAD_LEFT));
39          assertTrue(FocusLogic.shouldConsume(KeyEvent.KEYCODE_DPAD_RIGHT));
40          assertTrue(FocusLogic.shouldConsume(KeyEvent.KEYCODE_DPAD_UP));
41          assertTrue(FocusLogic.shouldConsume(KeyEvent.KEYCODE_DPAD_DOWN));
42          assertTrue(FocusLogic.shouldConsume(KeyEvent.KEYCODE_MOVE_HOME));
43          assertTrue(FocusLogic.shouldConsume(KeyEvent.KEYCODE_MOVE_END));
44          assertTrue(FocusLogic.shouldConsume(KeyEvent.KEYCODE_PAGE_UP));
45          assertTrue(FocusLogic.shouldConsume(KeyEvent.KEYCODE_PAGE_DOWN));
46     }
47 
48     @Test
testCreateSparseMatrix()49     public void testCreateSparseMatrix() {
50          // Either, 1) create a helper method to generate/instantiate all possible cell layout that
51          // may get created in real world to test this method. OR 2) Move all the matrix
52          // management routine to celllayout and write tests for them.
53     }
54 
55     @Test
testMoveFromBottomRightToBottomLeft()56     public void testMoveFromBottomRightToBottomLeft() {
57         int[][] map = transpose(new int[][] {
58                 {-1, 0, -1, -1, -1, -1},
59                 {-1, -1, -1, -1, -1, -1},
60                 {-1, -1, -1, -1, -1, -1},
61                 {-1, -1, -1, -1, -1, -1},
62                 {100, 1, -1, -1, -1, -1},
63         });
64         int i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_RIGHT, map, 100, 1, 2, false);
65         assertEquals(1, i);
66     }
67 
68     @Test
testMoveFromBottomRightToTopLeft()69     public void testMoveFromBottomRightToTopLeft() {
70         int[][] map = transpose(new int[][] {
71                 {-1, 0, -1, -1, -1, -1},
72                 {-1, -1, -1, -1, -1, -1},
73                 {-1, -1, -1, -1, -1, -1},
74                 {-1, -1, -1, -1, -1, -1},
75                 {100, -1, -1, -1, -1, -1},
76         });
77         int i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_RIGHT, map, 100, 1, 2, false);
78         assertEquals(FocusLogic.NEXT_PAGE_FIRST_ITEM, i);
79     }
80 
81     @Test
testMoveIntoHotseatWithEqualHotseatAndWorkspaceColumns()82     public void testMoveIntoHotseatWithEqualHotseatAndWorkspaceColumns() {
83         // Test going from an icon right above the All Apps button to the All Apps button.
84         int[][] map = transpose(new int[][] {
85                 {-1, -1, -1, -1, -1},
86                 {-1, -1, -1, -1, -1},
87                 {-1, -1, -1, -1, -1},
88                 {-1, -1,  0, -1, -1},
89                 { 2,  3,  1,  4,  5},
90         });
91         int i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, map, 0, 1, 1, true);
92         assertEquals(1, i);
93         // Test going from an icon above and to the right of the All Apps
94         // button to an icon to the right of the All Apps button.
95         map = transpose(new int[][] {
96                 {-1, -1, -1, -1, -1},
97                 {-1, -1, -1, -1, -1},
98                 {-1, -1, -1, -1, -1},
99                 {-1, -1, -1,  0, -1},
100                 { 2,  3,  1,  4,  5},
101         });
102         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, map, 0, 1, 1, true);
103         assertEquals(4, i);
104     }
105 
106     @Test
testMoveIntoHotseatWithExtraColumnForAllApps()107     public void testMoveIntoHotseatWithExtraColumnForAllApps() {
108         // Test going from an icon above and to the left
109         // of the All Apps button to the All Apps button.
110         int[][] map = transpose(new int[][] {
111                 {-1, -1, -1,-11, -1, -1, -1},
112                 {-1, -1, -1,-11, -1, -1, -1},
113                 {-1, -1, -1,-11, -1, -1, -1},
114                 {-1, -1, -1,-11, -1, -1, -1},
115                 {-1, -1,  0,-11, -1, -1, -1},
116                 {-1, -1, -1,  1,  1, -1, -1},
117         });
118         int i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, map, 0, 1, 1, true);
119         assertEquals(1, i);
120         // Test going from an icon above and to the right
121         // of the All Apps button to the All Apps button.
122         map = transpose(new int[][] {
123                 {-1, -1, -1,-11, -1, -1, -1},
124                 {-1, -1, -1,-11, -1, -1, -1},
125                 {-1, -1, -1,-11, -1, -1, -1},
126                 {-1, -1, -1,-11, -1, -1, -1},
127                 {-1, -1, -1,-11,  0, -1, -1},
128                 {-1, -1, -1,  1, -1, -1, -1},
129         });
130         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, map, 0, 1, 1, true);
131         assertEquals(1, i);
132         // Test going from the All Apps button to an icon
133         // above and to the right of the All Apps button.
134         map = transpose(new int[][] {
135                 {-1, -1, -1,-11, -1, -1, -1},
136                 {-1, -1, -1,-11, -1, -1, -1},
137                 {-1, -1, -1,-11, -1, -1, -1},
138                 {-1, -1, -1,-11, -1, -1, -1},
139                 {-1, -1, -1,-11,  0, -1, -1},
140                 {-1, -1, -1,  1, -1, -1, -1},
141         });
142         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_UP, map, 1, 1, 1, true);
143         assertEquals(0, i);
144         // Test going from an icon above and to the left of the
145         // All Apps button in landscape to the All Apps button.
146         map = transpose(new int[][] {
147                 { -1, -1, -1, -1, -1},
148                 { -1, -1, -1,  0, -1},
149                 {-11,-11,-11,-11,  1},
150                 { -1, -1, -1, -1, -1},
151                 { -1, -1, -1, -1, -1},
152         });
153         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_RIGHT, map, 0, 1, 1, true);
154         assertEquals(1, i);
155         // Test going from the All Apps button in landscape to
156         // an icon above and to the left of the All Apps button.
157         map = transpose(new int[][] {
158                 { -1, -1, -1, -1, -1},
159                 { -1, -1, -1,  0, -1},
160                 {-11,-11,-11,-11,  1},
161                 { -1, -1, -1, -1, -1},
162                 { -1, -1, -1, -1, -1},
163         });
164         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_LEFT, map, 1, 1, 1, true);
165         assertEquals(0, i);
166         // Test that going to the hotseat always goes to the same row as the original icon.
167         map = transpose(new int[][]{
168                 { 0,  1,  2,-11,  3,  4,  5},
169                 {-1, -1, -1,-11, -1, -1, -1},
170                 {-1, -1, -1,-11, -1, -1, -1},
171                 {-1, -1, -1,-11, -1, -1, -1},
172                 {-1, -1, -1,-11, -1, -1, -1},
173                 { 7,  8,  9,  6, 10, 11, 12},
174         });
175         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, map, 0, 1, 1, true);
176         assertEquals(7, i);
177         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, map, 1, 1, 1, true);
178         assertEquals(8, i);
179         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, map, 2, 1, 1, true);
180         assertEquals(9, i);
181         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, map, 3, 1, 1, true);
182         assertEquals(10, i);
183         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, map, 4, 1, 1, true);
184         assertEquals(11, i);
185         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, map, 5, 1, 1, true);
186         assertEquals(12, i);
187     }
188 
189     @Test
testCrossingAllAppsColumn()190     public void testCrossingAllAppsColumn() {
191         // Test crossing from left to right in portrait.
192         int[][] map = transpose(new int[][] {
193                 {-1, -1,-11, -1, -1},
194                 {-1,  0,-11, -1, -1},
195                 {-1, -1,-11,  1, -1},
196                 {-1, -1,-11, -1, -1},
197                 {-1, -1,  2, -1, -1},
198         });
199         int i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, map, 0, 1, 1, true);
200         assertEquals(1, i);
201         // Test crossing from right to left in portrait.
202         map = transpose(new int[][] {
203                 {-1, -1,-11, -1, -1},
204                 {-1, -1,-11,  0, -1},
205                 {-1,  1,-11, -1, -1},
206                 {-1, -1,-11, -1, -1},
207                 {-1, -1,  2, -1, -1},
208         });
209         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN, map, 0, 1, 1, true);
210         assertEquals(1, i);
211         // Test crossing from left to right in landscape.
212         map = transpose(new int[][] {
213                 { -1, -1, -1, -1, -1},
214                 { -1, -1, -1,  0, -1},
215                 {-11,-11,-11,-11,  2},
216                 { -1,  1, -1, -1, -1},
217                 { -1, -1, -1, -1, -1},
218         });
219         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_LEFT, map, 0, 1, 1, true);
220         assertEquals(1, i);
221         // Test crossing from right to left in landscape.
222         map = transpose(new int[][] {
223                 { -1, -1, -1, -1, -1},
224                 { -1,  0, -1, -1, -1},
225                 {-11,-11,-11,-11,  2},
226                 { -1, -1,  1, -1, -1},
227                 { -1, -1, -1, -1, -1},
228         });
229         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_RIGHT, map, 0, 1, 1, true);
230         assertEquals(1, i);
231         // Test NOT crossing it, if the All Apps button is the only suitable candidate.
232         map = transpose(new int[][]{
233                 {-1, 0, -1, -1, -1},
234                 {-1, 1, -1, -1, -1},
235                 {-11, -11, -11, -11, 4},
236                 {-1, 2, -1, -1, -1},
237                 {-1, 3, -1, -1, -1},
238         });
239         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_RIGHT, map, 1, 1, 1, true);
240         assertEquals(4, i);
241         i = FocusLogic.handleKeyEvent(KeyEvent.KEYCODE_DPAD_RIGHT, map, 2, 1, 1, true);
242         assertEquals(4, i);
243     }
244 
245     /** Transposes the matrix so that we can write it in human-readable format in the tests. */
transpose(int[][] m)246     private int[][] transpose(int[][] m) {
247         int[][] t = new int[m[0].length][m.length];
248         for (int i = 0; i < m.length; i++) {
249             for (int j = 0; j < m[0].length; j++) {
250                 t[j][i] = m[i][j];
251             }
252         }
253         return t;
254     }
255 }
256