Home | History | Annotate | Download | only in testing
      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.testing;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertTrue;
     22 
     23 import androidx.recyclerview.selection.DefaultSelectionTracker;
     24 import androidx.recyclerview.selection.Selection;
     25 import androidx.recyclerview.selection.SelectionTracker;
     26 
     27 /**
     28  * Helper class for making assertions against the state of a {@link DefaultSelectionTracker}
     29  * instance and the consistency of states between {@link DefaultSelectionTracker} and
     30  * {@link DefaultSelectionTracker.SelectionObserver}.
     31  */
     32 public final class SelectionProbe {
     33 
     34     private final SelectionTracker<String> mMgr;
     35     private final TestSelectionObserver<String> mSelectionListener;
     36 
     37     public SelectionProbe(SelectionTracker<String> mgr) {
     38         mMgr = mgr;
     39         mSelectionListener = new TestSelectionObserver<String>();
     40         mMgr.addObserver(mSelectionListener);
     41     }
     42 
     43     public SelectionProbe(
     44             SelectionTracker<String> mgr, TestSelectionObserver<String> selectionListener) {
     45         mMgr = mgr;
     46         mSelectionListener = selectionListener;
     47     }
     48 
     49     public void assertRangeSelected(int begin, int end) {
     50         for (int i = begin; i <= end; i++) {
     51             assertSelected(i);
     52         }
     53     }
     54 
     55     public void assertRangeNotSelected(int begin, int end) {
     56         for (int i = begin; i <= end; i++) {
     57             assertNotSelected(i);
     58         }
     59     }
     60 
     61     public void assertRangeSelection(int begin, int end) {
     62         assertSelectionSize(end - begin + 1);
     63         assertRangeSelected(begin, end);
     64     }
     65 
     66     public void assertSelectionSize(int expected) {
     67         Selection<String> selection = mMgr.getSelection();
     68         assertEquals(selection.toString(), expected, selection.size());
     69 
     70         mSelectionListener.assertSelectionSize(expected);
     71     }
     72 
     73     public void assertNoSelection() {
     74         assertSelectionSize(0);
     75 
     76         mSelectionListener.assertNoSelection();
     77     }
     78 
     79     public void assertSelection(int... ids) {
     80         assertSelected(ids);
     81         assertEquals(ids.length, mMgr.getSelection().size());
     82 
     83         mSelectionListener.assertSelectionSize(ids.length);
     84     }
     85 
     86     public void assertSelected(int... ids) {
     87         Selection<String> sel = mMgr.getSelection();
     88         for (int id : ids) {
     89             String sid = String.valueOf(id);
     90             assertTrue(sid + " is not in selection " + sel, sel.contains(sid));
     91 
     92             mSelectionListener.assertSelected(sid);
     93         }
     94     }
     95 
     96     public void assertNotSelected(int... ids) {
     97         Selection<String> sel = mMgr.getSelection();
     98         for (int id : ids) {
     99             String sid = String.valueOf(id);
    100             assertFalse(sid + " is in selection " + sel, sel.contains(sid));
    101 
    102             mSelectionListener.assertNotSelected(sid);
    103         }
    104     }
    105 }
    106