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

Commit 26819d7d authored by Ben Lin's avatar Ben Lin
Browse files

Fix crash regarding Type-to-Focus.

Added functional test also.

Bug: 30875102
Change-Id: I9d76e5698b71c2c605a9e38d7b013432a1e34332
parent cfbc7abe
Loading
Loading
Loading
Loading
+7 −9
Original line number Diff line number Diff line
@@ -147,11 +147,11 @@ public class DirectoryFragment extends Fragment
    private static final int CACHE_EVICT_LIMIT = 100;
    private static final int REFRESH_SPINNER_DISMISS_DELAY = 500;

    private Model mModel;
    private final Model mModel = new Model();
    private final Model.UpdateListener mModelUpdateListener = new ModelUpdateListener();
    private final SelectionModeListener mSelectionModeListener = new SelectionModeListener();
    private MultiSelectManager mSelectionMgr;
    private Model.UpdateListener mModelUpdateListener = new ModelUpdateListener();
    private UserInputHandler<InputEvent> mInputHandler;
    private SelectionModeListener mSelectionModeListener;
    private FocusManager mFocusManager;

    private IconHelper mIconHelper;
@@ -303,7 +303,10 @@ public class DirectoryFragment extends Fragment
                    ? MultiSelectManager.MODE_MULTIPLE
                    : MultiSelectManager.MODE_SINGLE);

        // Make sure this is done after the RecyclerView is set up.
        mModel.addUpdateListener(mAdapter);
        mModel.addUpdateListener(mModelUpdateListener);

        // Make sure this is done after the RecyclerView and the Model are set up.
        mFocusManager = new FocusManager(context, mRecView, mModel);

        mInputHandler = new UserInputHandler<>(
@@ -344,13 +347,8 @@ public class DirectoryFragment extends Fragment
            mBandController = new BandController(mRecView, mAdapter, mSelectionMgr);
        }

        mSelectionModeListener = new SelectionModeListener();
        mSelectionMgr.addCallback(mSelectionModeListener);

        mModel = new Model();
        mModel.addUpdateListener(mAdapter);
        mModel.addUpdateListener(mModelUpdateListener);

        final BaseActivity activity = getBaseActivity();
        mTuner = activity.createFragmentTuner();
        mMenuManager = activity.getMenuManager();
+2 −0
Original line number Diff line number Diff line
@@ -62,6 +62,8 @@ final class FocusManager implements FocusHandler {
    private int mLastFocusPosition = RecyclerView.NO_POSITION;

    public FocusManager(Context context, RecyclerView view, Model model) {
        assert (view != null);
        assert (model != null);
        mView = view;
        mAdapter = (DocumentsAdapter) view.getAdapter();
        mLayout = (GridLayoutManager) view.getLayoutManager();
+26 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.documentsui;

import android.net.Uri;
import android.os.RemoteException;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.Suppress;
import android.view.KeyEvent;
@@ -27,6 +29,17 @@ public class KeyboardNavigationUiTest extends ActivityTest<FilesActivity> {
        super(FilesActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        initTestFiles();
    }

    @Override
    public void initTestFiles() throws RemoteException {
        mDocsHelper.createDocument(rootDir0, "image/png", "file1.png");
    }

    // Tests that pressing tab switches focus between the roots and directory listings.
    @Suppress
    public void testKeyboard_tab() throws Exception {
@@ -49,6 +62,19 @@ public class KeyboardNavigationUiTest extends ActivityTest<FilesActivity> {
        }
    }

    public void testKeyboard_tabFocuses() throws Exception {
        bots.roots.closeDrawer();
        if (bots.main.inFixedLayout()) {
            // Tablet devices need to press one more tab since it focuses on root list first
            bots.keyboard.pressKey(KeyEvent.KEYCODE_TAB);
        }
        bots.keyboard.pressKey(KeyEvent.KEYCODE_TAB);
        bots.directory.assertFirstDocumentHasFocus();

        // This should not cause any exceptions
        bots.keyboard.pressKey(KeyEvent.KEYCODE_F);
    }

    // Tests that arrow keys do not switch focus away from the roots list.
    public void testKeyboard_arrowsRootsList() throws Exception {

+13 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.support.test.uiautomator.Until;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

import com.android.documentsui.R;

@@ -185,6 +186,18 @@ public class DirectoryListBot extends Bots.BaseBot {
        return true;
    }

    public void assertFirstDocumentHasFocus() throws UiObjectNotFoundException {
        final UiSelector docList = new UiSelector().resourceId(
                "com.android.documentsui:id/container_directory").childSelector(
                        new UiSelector().resourceId(DIR_LIST_ID));

        // Wait for the first list item to appear
        UiObject doc = new UiObject(docList.childSelector(new UiSelector()));
        doc.waitForExists(mTimeout);

        assertTrue(doc.isFocused());
    }

    public UiObject findDocumentsList() {
        return findObject(
                "com.android.documentsui:id/container_directory",