Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 028b2630 authored by Geoffrey Pitsch's avatar Geoffrey Pitsch
Browse files

Fix crash when attempting to focus when no items are visible.

Judging from stack, monkey test is somehow getting into a state
where no items are currently visible. Safeguard this edge case
from crashing.

Test: docsui unittest
Change-Id: I10dee1dfd8373012f4a1bedcbda851653c12f5ee
Fixes: 64589752
parent 9ea7f88e
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -153,6 +153,10 @@ public final class FocusManager implements FocusHandler {
        final int focusPos = (mScope.lastFocusPosition != RecyclerView.NO_POSITION)
                ? mScope.lastFocusPosition
                : mScope.layout.findFirstVisibleItemPosition();
        if (focusPos == RecyclerView.NO_POSITION) {
            return false;
        }

        focusItem(focusPos);
        return true;
    }
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.documentsui.testing;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;

import org.mockito.Mockito;

public class TestGridLayoutManager extends GridLayoutManager {

    private int mFirstVisibleItemPosition;

    public static TestGridLayoutManager create() {
        final TestGridLayoutManager manager = Mockito.mock(TestGridLayoutManager.class,
                Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
        return manager;
    }

    @Override
    public int findFirstVisibleItemPosition() { return mFirstVisibleItemPosition; }

    public void setFirstVisibleItemPosition(int position) { mFirstVisibleItemPosition = position; }

    private TestGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
    }
}
+14 −2
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.documentsui.testing;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.v7.widget.RecyclerView;
import android.view.View;

@@ -32,6 +33,7 @@ public class TestRecyclerView extends RecyclerView {

    private List<RecyclerView.ViewHolder> holders = new ArrayList<>();
    private TestDocumentsAdapter adapter;
    private RecyclerView.LayoutManager mLayoutManager;

    public TestRecyclerView(Context context) {
        super(context);
@@ -55,6 +57,16 @@ public class TestRecyclerView extends RecyclerView {
        return adapter;
    }

    @Override
    public void setLayoutManager(LayoutManager manager) {
        mLayoutManager = manager;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return mLayoutManager;
    }

    public void setItems(List<String> modelIds) {
        holders = new ArrayList<>();
        for (String modelId: modelIds) {
@@ -64,8 +76,8 @@ public class TestRecyclerView extends RecyclerView {
    }

    public static TestRecyclerView create(List<String> modelIds) {
        final TestRecyclerView view = Mockito.mock(TestRecyclerView.class,
                Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
        final TestRecyclerView view =
                new TestRecyclerView(InstrumentationRegistry.getTargetContext());
        view.holders = new ArrayList<>();
        for (String modelId: modelIds) {
            view.holders.add(new TestViewHolder(Views.createTestView()));
+11 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.documentsui;

import android.support.v7.widget.RecyclerView;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

@@ -25,6 +26,7 @@ import com.android.documentsui.selection.SelectionHelper;
import com.android.documentsui.testing.TestModel;
import com.android.documentsui.testing.SelectionHelpers;
import com.android.documentsui.testing.TestFeatures;
import com.android.documentsui.testing.TestGridLayoutManager;
import com.android.documentsui.testing.TestRecyclerView;

import java.util.ArrayList;
@@ -39,12 +41,16 @@ public class FocusManagerTest extends AndroidTestCase {

    private FocusManager mManager;
    private TestRecyclerView mView;
    private TestGridLayoutManager mTestGridLayoutManager;
    private SelectionHelper mSelectionMgr;
    private TestFeatures mFeatures;

    @Override
    public void setUp() throws Exception {
        mView = TestRecyclerView.create(ITEMS);
        mTestGridLayoutManager = TestGridLayoutManager.create();
        mView.setLayoutManager(mTestGridLayoutManager);

        mSelectionMgr = SelectionHelpers.createTestInstance(ITEMS);
        mFeatures = new TestFeatures();
        mManager = new FocusManager(mFeatures, mSelectionMgr, null, null, 0)
@@ -73,6 +79,11 @@ public class FocusManagerTest extends AndroidTestCase {
        assertFalse(mManager.focusDirectoryList());
    }

    public void testFocusDirectoryList_noVisibleItems() {
        mTestGridLayoutManager.setFirstVisibleItemPosition(RecyclerView.NO_POSITION);
        assertFalse(mManager.focusDirectoryList());
    }

    public void testFocusDirectoryList_hasSelection() {
        mSelectionMgr.select("0");
        assertFalse(mManager.focusDirectoryList());