1 /* 2 * Copyright (C) 2015 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 android.os.RemoteException; 20 import android.view.KeyEvent; 21 22 import androidx.test.filters.LargeTest; 23 import androidx.test.filters.Suppress; 24 25 import com.android.documentsui.files.FilesActivity; 26 27 @LargeTest 28 public class KeyboardNavigationUiTest extends ActivityTest<FilesActivity> { 29 KeyboardNavigationUiTest()30 public KeyboardNavigationUiTest() { 31 super(FilesActivity.class); 32 } 33 34 @Override setUp()35 public void setUp() throws Exception { 36 super.setUp(); 37 initTestFiles(); 38 } 39 40 @Override initTestFiles()41 public void initTestFiles() throws RemoteException { 42 mDocsHelper.createDocument(rootDir0, "image/png", "file1.png"); 43 } 44 45 // Tests that pressing tab switches focus between the roots and directory listings. 46 @Suppress testKeyboard_tab()47 public void testKeyboard_tab() throws Exception { 48 bots.keyboard.pressKey(KeyEvent.KEYCODE_TAB); 49 bots.roots.assertHasFocus(); 50 bots.keyboard.pressKey(KeyEvent.KEYCODE_TAB); 51 bots.directory.assertHasFocus(); 52 } 53 54 // Tests that arrow keys do not switch focus away from the dir list. 55 @Suppress testKeyboard_arrowsDirList()56 public void testKeyboard_arrowsDirList() throws Exception { 57 for (int i = 0; i < 10; i++) { 58 bots.keyboard.pressKey(KeyEvent.KEYCODE_DPAD_LEFT); 59 bots.directory.assertHasFocus(); 60 } 61 for (int i = 0; i < 10; i++) { 62 bots.keyboard.pressKey(KeyEvent.KEYCODE_DPAD_RIGHT); 63 bots.directory.assertHasFocus(); 64 } 65 } 66 67 @Suppress testKeyboard_tabFocuses()68 public void testKeyboard_tabFocuses() throws Exception { 69 bots.roots.closeDrawer(); 70 if (bots.main.inFixedLayout()) { 71 // Tablet devices need to press one more tab since it focuses on root list first 72 bots.keyboard.pressKey(KeyEvent.KEYCODE_TAB); 73 } 74 bots.keyboard.pressKey(KeyEvent.KEYCODE_TAB); 75 bots.directory.assertFirstDocumentHasFocus(); 76 77 // This should not cause any exceptions 78 bots.keyboard.pressKey(KeyEvent.KEYCODE_F); 79 } 80 81 // Tests that arrow keys do not switch focus away from the roots list. testKeyboard_arrowsRootsList()82 public void testKeyboard_arrowsRootsList() throws Exception { 83 84 // Open the drawer so we can ensure root list available even for phones 85 bots.roots.openDrawer(); 86 87 bots.keyboard.pressKey(KeyEvent.KEYCODE_TAB); 88 for (int i = 0; i < 10; i++) { 89 bots.keyboard.pressKey(KeyEvent.KEYCODE_DPAD_RIGHT); 90 bots.roots.assertHasFocus(); 91 } 92 for (int i = 0; i < 10; i++) { 93 bots.keyboard.pressKey(KeyEvent.KEYCODE_DPAD_LEFT); 94 bots.roots.assertHasFocus(); 95 } 96 } 97 } 98