1/*
2 * Copyright (C) 2016 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
17package com.android.documentsui;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
22
23import android.support.test.filters.SmallTest;
24import android.support.test.runner.AndroidJUnit4;
25import android.view.KeyEvent;
26import android.view.MotionEvent;
27
28import com.android.documentsui.base.Procedure;
29import com.android.documentsui.dirlist.TestFocusHandler;
30import com.android.documentsui.selection.SelectionManager;
31import com.android.documentsui.testing.SelectionManagers;
32import com.android.documentsui.testing.TestFeatures;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37
38@RunWith(AndroidJUnit4.class)
39@SmallTest
40public class SharedInputHandlerTest {
41
42    private SharedInputHandler mSharedInputHandler;
43    private SelectionManager mSelectionMgr = SelectionManagers.createTestInstance();
44    private TestFeatures mFeatures = new TestFeatures();
45    private TestFocusHandler mFocusHandler = new TestFocusHandler();
46    private boolean mDirPopHappened;
47    private boolean mCanceledSearch;
48    private Procedure mDirPopper = new Procedure() {
49        @Override
50        public boolean run() {
51            mDirPopHappened = true;
52            return true;
53        }
54    };
55
56    @Before
57    public void setUp() {
58        mDirPopHappened = false;
59        mSharedInputHandler = new SharedInputHandler(
60                mFocusHandler,
61                mSelectionMgr,
62                () -> {
63                    return false;
64                },
65                mDirPopper,
66                mFeatures);
67    }
68
69    @Test
70    public void testUnrelatedButton_DoesNothing() {
71        KeyEvent event =
72                new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_A, 0, 0);
73        assertFalse(mSharedInputHandler.onKeyDown(event.getKeyCode(), event));
74    }
75
76    @Test
77    public void testBackButton_CancelsSearch() {
78        mSelectionMgr.toggleSelection("1");
79        mSharedInputHandler = new SharedInputHandler(
80                new TestFocusHandler(),
81                SelectionManagers.createTestInstance(),
82                () -> {
83                        mCanceledSearch = true;
84                        return true;
85                },
86                mDirPopper,
87                new TestFeatures());
88        KeyEvent backEvent =
89                new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK, 0, 0);
90        assertTrue(mSharedInputHandler.onKeyDown(backEvent.getKeyCode(), backEvent));
91
92        assertTrue(mCanceledSearch);
93        assertEquals(mSelectionMgr.getSelection().size(), 1);
94        assertFalse(mDirPopHappened);
95    }
96
97    @Test
98    public void testBackButton_ClearsSelection() {
99        mSelectionMgr.toggleSelection("1");
100        assertEquals(mSelectionMgr.getSelection().size(), 1);
101        KeyEvent backEvent =
102                new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK, 0, 0);
103        assertTrue(mSharedInputHandler.onKeyDown(backEvent.getKeyCode(), backEvent));
104
105        assertFalse(mCanceledSearch);
106        assertEquals(mSelectionMgr.getSelection().size(), 0);
107        assertFalse(mDirPopHappened);
108    }
109
110    @Test
111    public void testBackButton_PopsDirectory() {
112        KeyEvent backEvent =
113                new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK, 0, 0);
114        assertTrue(mSharedInputHandler.onKeyDown(backEvent.getKeyCode(), backEvent));
115
116        assertFalse(mCanceledSearch);
117        assertEquals(mSelectionMgr.getSelection().size(), 0);
118        assertTrue(mDirPopHappened);
119    }
120
121    @Test
122    public void testEscButton_CancelsSearch() {
123        mSelectionMgr.toggleSelection("1");
124        mSharedInputHandler = new SharedInputHandler(
125                new TestFocusHandler(),
126                SelectionManagers.createTestInstance(),
127                () -> {
128                        mCanceledSearch = true;
129                        return true;
130                },
131                mDirPopper,
132                new TestFeatures());
133        KeyEvent escapeEvent =
134                new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_ESCAPE, 0, 0);
135        assertTrue(mSharedInputHandler.onKeyDown(escapeEvent.getKeyCode(), escapeEvent));
136
137        assertTrue(mCanceledSearch);
138        assertEquals(mSelectionMgr.getSelection().size(), 1);
139        assertFalse(mDirPopHappened);
140    }
141
142    @Test
143    public void testEscButton_ClearsSelection() {
144        mSelectionMgr.toggleSelection("1");
145        assertEquals(mSelectionMgr.getSelection().size(), 1);
146        KeyEvent escapeEvent =
147                new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_ESCAPE, 0, 0);
148        assertTrue(mSharedInputHandler.onKeyDown(escapeEvent.getKeyCode(), escapeEvent));
149
150        assertFalse(mCanceledSearch);
151        assertEquals(mSelectionMgr.getSelection().size(), 0);
152        assertFalse(mDirPopHappened);
153    }
154
155    @Test
156    public void testEscButton_DoesNotPopDirectory() {
157        KeyEvent escapeEvent =
158                new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_ESCAPE, 0, 0);
159        assertTrue(mSharedInputHandler.onKeyDown(escapeEvent.getKeyCode(), escapeEvent));
160
161        assertFalse(mCanceledSearch);
162        assertEquals(mSelectionMgr.getSelection().size(), 0);
163        assertFalse(mDirPopHappened);
164    }
165
166    @Test
167    public void testDeleteButton_PopsDirectory() {
168        KeyEvent delEvent =
169                new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL, 0, 0);
170        assertTrue(mSharedInputHandler.onKeyDown(delEvent.getKeyCode(), delEvent));
171
172        assertTrue(mDirPopHappened);
173    }
174
175    @Test
176    public void testTab_AdvancesFocusArea() {
177        mFeatures.systemKeyboardNavigation = false;
178        KeyEvent tabEvent =
179                new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB, 0, 0);
180        assertTrue(mSharedInputHandler.onKeyDown(tabEvent.getKeyCode(), tabEvent));
181
182        assertTrue(mFocusHandler.advanceFocusAreaCalled);
183    }
184
185    @Test
186    public void testNavKey_FocusesDirectory() {
187        mFeatures.systemKeyboardNavigation = false;
188        KeyEvent navEvent =
189                new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP, 0, 0);
190        assertTrue(mSharedInputHandler.onKeyDown(navEvent.getKeyCode(), navEvent));
191
192        assertTrue(mFocusHandler.focusDirectoryCalled);
193    }
194}
195