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