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