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