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

Commit 8fc9d89b authored by Steve McKay's avatar Steve McKay
Browse files

Add test for DocsSelectionManager.

Move constants to DefaultSelectionManager (off of SelectionManager interface).

Bug: 64847011
Test: Added a new one! And it passes!
Change-Id: I1955f268db3849a70049ac4acdda32fa8ab12ab9
parent fa48ea40
Loading
Loading
Loading
Loading
+38 −4
Original line number Diff line number Diff line
@@ -19,8 +19,8 @@ package com.android.documentsui;
import android.support.annotation.VisibleForTesting;
import android.support.v7.widget.RecyclerView;

import com.android.documentsui.selection.BandController;
import com.android.documentsui.selection.DefaultSelectionManager;
import com.android.documentsui.selection.DefaultSelectionManager.SelectionMode;
import com.android.documentsui.selection.Selection;
import com.android.documentsui.selection.SelectionManager;

@@ -34,10 +34,14 @@ import javax.annotation.Nullable;
 */
public final class DocsSelectionManager implements SelectionManager {

    private final DelegateFactory mFactory;
    private final @SelectionMode int mSelectionMode;
    private @Nullable DefaultSelectionManager mDelegate;

    public DocsSelectionManager(@SelectionMode int mode) {
    private @Nullable SelectionManager mDelegate;

    @VisibleForTesting
    DocsSelectionManager(DelegateFactory factory, @SelectionMode int mode) {
        mFactory = factory;
        mSelectionMode = mode;
    }

@@ -50,7 +54,7 @@ public final class DocsSelectionManager implements SelectionManager {
            mDelegate.clearSelection();
        }

        mDelegate = new DefaultSelectionManager(mSelectionMode, adapter, stableIds, canSetState);
        mDelegate = mFactory.create(mSelectionMode, adapter, stableIds, canSetState);
        return this;
    }

@@ -144,4 +148,34 @@ public final class DocsSelectionManager implements SelectionManager {
    public void setSelectionRangeBegin(int position) {
        mDelegate.setSelectionRangeBegin(position);
    }

    public static DocsSelectionManager createMultiSelect() {
        return new DocsSelectionManager(
                DelegateFactory.INSTANCE,
                DefaultSelectionManager.MODE_MULTIPLE);
    }

    public static DocsSelectionManager createSingleSelect() {
        return new DocsSelectionManager(
                DelegateFactory.INSTANCE,
                DefaultSelectionManager.MODE_SINGLE);
    }

    /**
     * Use of a factory to create selection manager instances allows testable instances to
     * be inject from tests.
     */
    @VisibleForTesting
    static class DelegateFactory {
        static final DelegateFactory INSTANCE = new DelegateFactory();

        SelectionManager create(
                @SelectionMode int mode,
                RecyclerView.Adapter<?> adapter,
                StableIdProvider stableIds,
                SelectionPredicate canSetState) {

            return new DefaultSelectionManager(mode, adapter, stableIds, canSetState);
        }
    }
}
+1 −2
Original line number Diff line number Diff line
@@ -57,7 +57,6 @@ import com.android.documentsui.clipping.DocumentClipper;
import com.android.documentsui.dirlist.AnimationView.AnimationType;
import com.android.documentsui.dirlist.DirectoryFragment;
import com.android.documentsui.prefs.ScopedPreferences;
import com.android.documentsui.selection.SelectionManager;
import com.android.documentsui.services.FileOperationService;
import com.android.documentsui.sidebar.RootsFragment;
import com.android.documentsui.ui.DialogController;
@@ -105,7 +104,7 @@ public class FilesActivity extends BaseActivity implements ActionHandler.Addons
        super.onCreate(icicle);

        DocumentClipper clipper = DocumentsApplication.getDocumentClipper(this);
        mInjector.selectionMgr = new DocsSelectionManager(SelectionManager.MODE_MULTIPLE);
        mInjector.selectionMgr = DocsSelectionManager.createMultiSelect();

        mInjector.focusManager = new FocusManager(
                mInjector.features,
+3 −5
Original line number Diff line number Diff line
@@ -50,7 +50,6 @@ import com.android.documentsui.base.Shared;
import com.android.documentsui.base.State;
import com.android.documentsui.dirlist.DirectoryFragment;
import com.android.documentsui.prefs.ScopedPreferences;
import com.android.documentsui.selection.SelectionManager;
import com.android.documentsui.services.FileOperationService;
import com.android.documentsui.sidebar.RootsFragment;
import com.android.documentsui.ui.DialogController;
@@ -95,10 +94,9 @@ public class PickActivity extends BaseActivity implements ActionHandler.Addons {

        super.onCreate(icicle);

        mInjector.selectionMgr = new DocsSelectionManager(
                mState.allowMultiple
                        ? SelectionManager.MODE_MULTIPLE
                        : SelectionManager.MODE_SINGLE);
        mInjector.selectionMgr = mState.allowMultiple
                ? DocsSelectionManager.createMultiSelect()
                : DocsSelectionManager.createSingleSelect();

        mInjector.focusManager = new FocusManager(
                mInjector.features,
+34 −0
Original line number Diff line number Diff line
@@ -19,10 +19,13 @@ package com.android.documentsui.selection;
import static com.android.documentsui.selection.Shared.DEBUG;
import static com.android.documentsui.selection.Shared.TAG;

import android.support.annotation.IntDef;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.util.Log;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -37,6 +40,37 @@ import javax.annotation.Nullable;
 */
public final class DefaultSelectionManager implements SelectionManager {

    public static final int MODE_MULTIPLE = 0;
    public static final int MODE_SINGLE = 1;
    @IntDef(flag = true, value = {
            MODE_MULTIPLE,
            MODE_SINGLE
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface SelectionMode {}

    private static final int RANGE_REGULAR = 0;
    /**
     * "Provisional" selection represents a overlay on the primary selection. A provisional
     * selection maybe be eventually added to the primary selection, or it may be abandoned.
     *
     *  <p>E.g. BandController creates a provisional selection while a user is actively
     *  selecting items with the band. Provisionally selected items are considered to be
     *  selected in {@link Selection#contains(String)} and related methods. A provisional
     *  may be abandoned or applied by selection components (like {@link BandController}).
     *
     *  <p>A provisional selection may intersect the primary selection, however clearing
     *  the provisional selection will not affect the primary selection where the two may
     *  intersect.
     */
    private static final int RANGE_PROVISIONAL = 1;
    @IntDef({
        RANGE_REGULAR,
        RANGE_PROVISIONAL
    })
    @Retention(RetentionPolicy.SOURCE)
    @interface RangeType {}

    private final Selection mSelection = new Selection();
    private final List<EventListener> mEventListeners = new ArrayList<>(1);
    private final RecyclerView.Adapter<?> mAdapter;
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import static com.android.documentsui.selection.Shared.TAG;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.android.documentsui.selection.SelectionManager.RangeType;
import com.android.documentsui.selection.DefaultSelectionManager.RangeType;

/**
 * Class providing support for managing range selections.
Loading