• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 com.android.documentsui.selection;
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 com.android.documentsui.selection.SelectionManager;
24 import com.android.documentsui.selection.Selection;
25 
26 /**
27  * Helper class for making assertions against the state of a {@link SelectionManager} instance and
28  * the consistency of states between {@link SelectionManager} and
29  * {@link SelectionManager.ItemCallback}.
30  */
31 public final class SelectionProbe {
32 
33     private final SelectionManager mMgr;
34     private final TestItemSelectionListener mTestCallback;
35 
SelectionProbe(SelectionManager mgr)36     public SelectionProbe(SelectionManager mgr) {
37         mMgr = mgr;
38         mTestCallback = new TestItemSelectionListener();
39 
40         mMgr.addItemCallback(mTestCallback);
41     }
42 
SelectionProbe(SelectionManager mgr, TestItemSelectionListener testCallback)43     public SelectionProbe(SelectionManager mgr, TestItemSelectionListener testCallback) {
44         mMgr = mgr;
45         mTestCallback = testCallback;
46     }
47 
assertRangeSelected(int begin, int end)48     public void assertRangeSelected(int begin, int end) {
49         for (int i = begin; i <= end; i++) {
50             assertSelected(i);
51         }
52     }
53 
assertRangeNotSelected(int begin, int end)54     public void assertRangeNotSelected(int begin, int end) {
55         for (int i = begin; i <= end; i++) {
56             assertNotSelected(i);
57         }
58     }
59 
assertRangeSelection(int begin, int end)60     public void assertRangeSelection(int begin, int end) {
61         assertSelectionSize(end - begin + 1);
62         assertRangeSelected(begin, end);
63     }
64 
assertSelectionSize(int expected)65     public void assertSelectionSize(int expected) {
66         Selection selection = mMgr.getSelection();
67         assertEquals(selection.toString(), expected, selection.size());
68 
69         mTestCallback.assertSelectionSize(expected);
70     }
71 
assertNoSelection()72     public void assertNoSelection() {
73         assertSelectionSize(0);
74 
75         mTestCallback.assertNoSelection();
76     }
77 
assertSelection(int... ids)78     public void assertSelection(int... ids) {
79         assertSelected(ids);
80         assertEquals(ids.length, mMgr.getSelection().size());
81 
82         mTestCallback.assertSelectionSize(ids.length);
83     }
84 
assertSelected(int... ids)85     public void assertSelected(int... ids) {
86         Selection sel = mMgr.getSelection();
87         for (int id : ids) {
88             String sid = String.valueOf(id);
89             assertTrue(sid + " is not in selection " + sel, sel.contains(sid));
90 
91             mTestCallback.assertSelected(sid);
92         }
93     }
94 
assertNotSelected(int... ids)95     public void assertNotSelected(int... ids) {
96         Selection sel = mMgr.getSelection();
97         for (int id : ids) {
98             String sid = String.valueOf(id);
99             assertFalse(sid + " is in selection " + sel, sel.contains(sid));
100 
101             mTestCallback.assertNotSelected(sid);
102         }
103     }
104 
select(int...positions)105     public void select(int...positions) {
106         for (int position : positions) {
107             mMgr.toggleSelection(String.valueOf(position));
108         }
109     }
110 }
111