Home | History | Annotate | Download | only in selection
      1 /*
      2  * Copyright 2017 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 androidx.recyclerview.selection;
     18 
     19 import static org.junit.Assert.assertFalse;
     20 import static org.junit.Assert.assertTrue;
     21 
     22 import android.graphics.Rect;
     23 import android.support.test.filters.SmallTest;
     24 import android.support.test.runner.AndroidJUnit4;
     25 import android.view.MotionEvent;
     26 
     27 import androidx.recyclerview.selection.testing.TestAdapter;
     28 import androidx.recyclerview.selection.testing.TestAutoScroller;
     29 import androidx.recyclerview.selection.testing.TestBandPredicate;
     30 import androidx.recyclerview.selection.testing.TestData;
     31 import androidx.recyclerview.selection.testing.TestEvents.Builder;
     32 import androidx.recyclerview.selection.testing.TestItemKeyProvider;
     33 import androidx.recyclerview.widget.RecyclerView.OnScrollListener;
     34 
     35 import org.junit.Before;
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 
     39 import java.util.Collections;
     40 import java.util.List;
     41 
     42 @RunWith(AndroidJUnit4.class)
     43 @SmallTest
     44 public class BandSelectionHelperTest {
     45 
     46     private List<String> mItems;
     47     private BandSelectionHelper<String> mBandController;
     48     private boolean mIsActive;
     49     private Builder mStartBuilder;
     50     private Builder mStopBuilder;
     51     private MotionEvent mStartEvent;
     52     private MotionEvent mStopEvent;
     53     private TestBandHost mBandHost;
     54     private TestBandPredicate mBandPredicate;
     55     private TestAdapter<String> mAdapter;
     56 
     57     @Before
     58     public void setup() throws Exception {
     59         mItems = TestData.createStringData(10);
     60         mIsActive = false;
     61         mAdapter = new TestAdapter<String>();
     62         mAdapter.updateTestModelIds(mItems);
     63         mBandHost = new TestBandHost();
     64         mBandPredicate = new TestBandPredicate();
     65         ItemKeyProvider<String> keyProvider =
     66                 new TestItemKeyProvider<>(ItemKeyProvider.SCOPE_MAPPED, mAdapter);
     67         OperationMonitor operationMonitor = new OperationMonitor();
     68 
     69         SelectionTracker<String> helper = new DefaultSelectionTracker<String>(
     70                 "band-selection-test",
     71                 keyProvider,
     72                 SelectionPredicates.createSelectAnything(),
     73                 StorageStrategy.createStringStorage());
     74 
     75         EventBridge.install(mAdapter, helper, keyProvider);
     76         FocusDelegate<String> focusDelegate = FocusDelegate.dummy();
     77 
     78         mBandController = new BandSelectionHelper<String>(
     79                 mBandHost,
     80                 new TestAutoScroller(),
     81                 keyProvider,
     82                 helper,
     83                 mBandPredicate,
     84                 focusDelegate,
     85                 operationMonitor) {
     86                     @Override
     87                     public boolean isActive() {
     88                         return mIsActive;
     89                     }
     90                 };
     91 
     92         mStartBuilder = new Builder().mouse().primary().action(MotionEvent.ACTION_MOVE);
     93         mStopBuilder = new Builder().mouse().action(MotionEvent.ACTION_UP);
     94         mStartEvent = mStartBuilder.build();
     95         mStopEvent = mStopBuilder.build();
     96     }
     97 
     98     @Test
     99     public void testStartsBand() {
    100         assertTrue(mBandController.shouldStart(mStartEvent));
    101     }
    102 
    103     @Test
    104     public void testStartsBand_NoItems() {
    105         mAdapter.updateTestModelIds(Collections.EMPTY_LIST);
    106         assertTrue(mBandController.shouldStart(mStartEvent));
    107     }
    108 
    109     @Test
    110     public void testBadStart_NoButtons() {
    111         assertFalse(mBandController.shouldStart(
    112                 mStartBuilder.releaseButton(MotionEvent.BUTTON_PRIMARY).build()));
    113     }
    114 
    115     @Test
    116     public void testBadStart_SecondaryButton() {
    117         assertFalse(
    118                 mBandController.shouldStart(mStartBuilder.secondary().build()));
    119     }
    120 
    121     @Test
    122     public void testBadStart_TertiaryButton() {
    123         assertFalse(
    124                 mBandController.shouldStart(mStartBuilder.tertiary().build()));
    125     }
    126 
    127     @Test
    128     public void testBadStart_Touch() {
    129         assertFalse(mBandController.shouldStart(
    130                 mStartBuilder.touch().releaseButton(MotionEvent.BUTTON_PRIMARY).build()));
    131     }
    132 
    133     @Test
    134     public void testBadStart_RespectsCanInitiateBand() {
    135         mBandPredicate.setCanInitiate(false);
    136         assertFalse(mBandController.shouldStart(mStartEvent));
    137     }
    138 
    139     @Test
    140     public void testBadStart_ActionDown() {
    141         assertFalse(mBandController
    142                 .shouldStart(mStartBuilder.action(MotionEvent.ACTION_DOWN).build()));
    143     }
    144 
    145     @Test
    146     public void testBadStart_ActionUp() {
    147         assertFalse(mBandController
    148                 .shouldStart(mStartBuilder.action(MotionEvent.ACTION_UP).build()));
    149     }
    150 
    151     @Test
    152     public void testBadStart_ActionPointerDown() {
    153         assertFalse(mBandController.shouldStart(
    154                 mStartBuilder.action(MotionEvent.ACTION_POINTER_DOWN).build()));
    155     }
    156 
    157     @Test
    158     public void testBadStart_ActionPointerUp() {
    159         assertFalse(mBandController.shouldStart(
    160                 mStartBuilder.action(MotionEvent.ACTION_POINTER_UP).build()));
    161     }
    162 
    163     @Test
    164     public void testBadStart_alreadyActive() {
    165         mIsActive = true;
    166         assertFalse(mBandController.shouldStart(mStartEvent));
    167     }
    168 
    169     @Test
    170     public void testGoodStop() {
    171         mIsActive = true;
    172         assertTrue(mBandController.shouldStop(mStopEvent));
    173     }
    174 
    175     @Test
    176     public void testGoodStop_PointerUp() {
    177         mIsActive = true;
    178         assertTrue(mBandController
    179                 .shouldStop(mStopBuilder.action(MotionEvent.ACTION_POINTER_UP).build()));
    180     }
    181 
    182     @Test
    183     public void testGoodStop_Cancel() {
    184         mIsActive = true;
    185         assertTrue(mBandController
    186                 .shouldStop(mStopBuilder.action(MotionEvent.ACTION_CANCEL).build()));
    187     }
    188 
    189     @Test
    190     public void testBadStop_NotActive() {
    191         assertFalse(mBandController.shouldStop(mStopEvent));
    192     }
    193 
    194     @Test
    195     public void testBadStop_Move() {
    196         mIsActive = true;
    197         assertFalse(mBandController.shouldStop(
    198                 mStopBuilder.action(MotionEvent.ACTION_MOVE).touch().build()));
    199     }
    200 
    201     @Test
    202     public void testBadStop_Down() {
    203         mIsActive = true;
    204         assertFalse(mBandController.shouldStop(
    205                 mStopBuilder.action(MotionEvent.ACTION_DOWN).touch().build()));
    206     }
    207 
    208     private final class TestBandHost extends BandSelectionHelper.BandHost<String> {
    209         @Override
    210         GridModel<String> createGridModel() {
    211             throw new UnsupportedOperationException();
    212         }
    213 
    214         @Override
    215         void showBand(Rect rect) {
    216             throw new UnsupportedOperationException();
    217         }
    218 
    219         @Override
    220         void hideBand() {
    221             throw new UnsupportedOperationException();
    222         }
    223 
    224         @Override
    225         void addOnScrollListener(OnScrollListener listener) {
    226         }
    227     }
    228 }
    229