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