Home | History | Annotate | Download | only in documentsui
      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.support.v7.widget.RecyclerView;
     20 import android.test.AndroidTestCase;
     21 import android.test.suitebuilder.annotation.SmallTest;
     22 
     23 import com.android.documentsui.base.Features;
     24 import com.android.documentsui.dirlist.TestData;
     25 import com.android.documentsui.selection.SelectionHelper;
     26 import com.android.documentsui.testing.TestModel;
     27 import com.android.documentsui.testing.SelectionHelpers;
     28 import com.android.documentsui.testing.TestFeatures;
     29 import com.android.documentsui.testing.TestGridLayoutManager;
     30 import com.android.documentsui.testing.TestRecyclerView;
     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 SelectionHelper mSelectionMgr;
     46     private TestFeatures mFeatures;
     47 
     48     @Override
     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 
     60     public void testFocus() {
     61         mManager.focusDocument(Integer.toString(3));
     62         mView.assertItemViewFocused(3);
     63      }
     64 
     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 
     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 
     82     public void testFocusDirectoryList_noVisibleItems() {
     83         mTestGridLayoutManager.setFirstVisibleItemPosition(RecyclerView.NO_POSITION);
     84         assertFalse(mManager.focusDirectoryList());
     85     }
     86 
     87     public void testFocusDirectoryList_hasSelection() {
     88         mSelectionMgr.select("0");
     89         assertFalse(mManager.focusDirectoryList());
     90     }
     91 }
     92