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 android.test.AndroidTestCase;
20import android.test.suitebuilder.annotation.SmallTest;
21
22import com.android.documentsui.base.Features;
23import com.android.documentsui.dirlist.TestData;
24import com.android.documentsui.testing.TestModel;
25import com.android.documentsui.selection.SelectionManager;
26import com.android.documentsui.testing.SelectionManagers;
27import com.android.documentsui.testing.TestFeatures;
28import com.android.documentsui.testing.TestRecyclerView;
29
30import java.util.ArrayList;
31import java.util.List;
32
33@SmallTest
34public class FocusManagerTest extends AndroidTestCase {
35
36    private static final String TEST_AUTHORITY = "test_authority";
37
38    private static final List<String> ITEMS = TestData.create(10);
39
40    private FocusManager mManager;
41    private TestRecyclerView mView;
42    private SelectionManager mSelectionMgr;
43    private TestFeatures mFeatures;
44
45    @Override
46    public void setUp() throws Exception {
47        mView = TestRecyclerView.create(ITEMS);
48        mSelectionMgr = SelectionManagers.createTestInstance(ITEMS);
49        mFeatures = new TestFeatures();
50        mManager = new FocusManager(mFeatures, mSelectionMgr, null, null, 0)
51                .reset(mView, new TestModel(TEST_AUTHORITY, mFeatures));
52    }
53
54    public void testFocus() {
55        mManager.focusDocument(Integer.toString(3));
56        mView.assertItemViewFocused(3);
57     }
58
59    public void testPendingFocus() {
60       mManager.focusDocument(Integer.toString(10));
61       List<String> mutableItems = TestData.create(11);
62       mView.setItems(mutableItems);
63       mManager.onLayoutCompleted();
64       // Should only be called once
65       mView.assertItemViewFocused(10);
66    }
67
68    public void testFocusDirectoryList_noItemsToFocus() {
69        mView = TestRecyclerView.create(new ArrayList<>());
70        mManager = new FocusManager(
71                mFeatures, SelectionManagers.createTestInstance(), null, null, 0)
72                .reset(mView, new TestModel(TEST_AUTHORITY, mFeatures));
73        assertFalse(mManager.focusDirectoryList());
74    }
75
76    public void testFocusDirectoryList_hasSelection() {
77        mSelectionMgr.toggleSelection("0");
78        assertFalse(mManager.focusDirectoryList());
79    }
80}
81