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 android.test.AndroidTestCase;
20 import android.test.suitebuilder.annotation.SmallTest;
21 
22 import androidx.recyclerview.selection.SelectionTracker;
23 import androidx.recyclerview.widget.RecyclerView;
24 
25 import com.android.documentsui.dirlist.TestData;
26 import com.android.documentsui.testing.TestFeatures;
27 import com.android.documentsui.testing.TestGridLayoutManager;
28 import com.android.documentsui.testing.TestModel;
29 import com.android.documentsui.testing.TestRecyclerView;
30 import com.android.documentsui.testing.Views;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 @SmallTest
36 public class FocusManagerTest extends AndroidTestCase {
37 
38     private static final String TEST_AUTHORITY = "test_authority";
39 
40     private static final List<String> ITEMS = TestData.create(10);
41 
42     private FocusManager mManager;
43     private TestRecyclerView mView;
44     private TestGridLayoutManager mTestGridLayoutManager;
45     private SelectionTracker<String> mSelectionMgr;
46     private TestFeatures mFeatures;
47 
48     @Override
setUp()49     public void setUp() throws Exception {
50         mView = TestRecyclerView.create(ITEMS);
51         mTestGridLayoutManager = TestGridLayoutManager.create();
52         mView.setLayoutManager(mTestGridLayoutManager);
53 
54         mSelectionMgr = SelectionHelpers.createTestInstance(ITEMS);
55         mFeatures = new TestFeatures();
56         mManager = new FocusManager(mFeatures, mSelectionMgr, null, null, 0)
57                 .reset(mView, new TestModel(TEST_AUTHORITY, mFeatures));
58     }
59 
testFocus()60     public void testFocus() {
61         mManager.focusDocument(Integer.toString(3));
62         mView.assertItemViewFocused(3);
63      }
64 
testPendingFocus()65     public void testPendingFocus() {
66        mManager.focusDocument(Integer.toString(10));
67        List<String> mutableItems = TestData.create(11);
68        mView.setItems(mutableItems);
69        mManager.onLayoutCompleted();
70        // Should only be called once
71        mView.assertItemViewFocused(10);
72     }
73 
testFocusDirectoryList_noItemsToFocus()74     public void testFocusDirectoryList_noItemsToFocus() {
75         mView = TestRecyclerView.create(new ArrayList<>());
76         mManager = new FocusManager(
77                 mFeatures, SelectionHelpers.createTestInstance(), null, null, 0)
78                 .reset(mView, new TestModel(TEST_AUTHORITY, mFeatures));
79         assertFalse(mManager.focusDirectoryList());
80     }
81 
testFocusDirectoryList_noVisibleItems()82     public void testFocusDirectoryList_noVisibleItems() {
83         mTestGridLayoutManager.setFirstVisibleItemPosition(RecyclerView.NO_POSITION);
84         assertFalse(mManager.focusDirectoryList());
85     }
86 
testFocusDirectoryList_hasSelection()87     public void testFocusDirectoryList_hasSelection() {
88         mSelectionMgr.select("0");
89         assertFalse(mManager.focusDirectoryList());
90     }
91 
testFocusDirectoryList_invalidContentScope()92     public void testFocusDirectoryList_invalidContentScope() {
93         mManager = new FocusManager(
94                 mFeatures, SelectionHelpers.createTestInstance(), null, null, 0);
95         // pass if no exception is thrown.
96         mManager.focusDirectoryList();
97     }
98 
testOnFocusChange_invalidContentScope()99     public void testOnFocusChange_invalidContentScope() {
100         mManager = new FocusManager(
101                 mFeatures, SelectionHelpers.createTestInstance(), null, null, 0);
102         // pass if no exception is thrown.
103         mManager.onFocusChange(Views.createTestView(), true);
104     }
105 
testClearFocus_invalidContentScope()106     public void testClearFocus_invalidContentScope() {
107         mManager = new FocusManager(
108                 mFeatures, SelectionHelpers.createTestInstance(), null, null, 0);
109         // pass if no exception is thrown.
110         mManager.clearFocus();
111     }
112 
testFocusDocument_invalidContentScope()113     public void testFocusDocument_invalidContentScope() {
114         mManager = new FocusManager(
115                 mFeatures, SelectionHelpers.createTestInstance(), null, null, 0);
116         // pass if no exception is thrown.
117         mManager.focusDocument(Integer.toString(0));
118     }
119 }
120