SelectionManagerTest.java revision 75b7b9039cf0efcb188e916c6f510328bfe099a8
1/*
2 * Copyright (C) 2015 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
17package com.android.documentsui.selection;
18
19import android.support.test.filters.SmallTest;
20import android.support.test.runner.AndroidJUnit4;
21import android.util.SparseBooleanArray;
22
23import com.android.documentsui.selection.SelectionManager;
24import com.android.documentsui.dirlist.TestData;
25import com.android.documentsui.selection.Selection;
26import com.android.documentsui.testing.SelectionManagers;
27
28import org.junit.Before;
29import org.junit.Test;
30import org.junit.runner.RunWith;
31
32import java.util.ArrayList;
33import java.util.HashSet;
34import java.util.List;
35import java.util.Set;
36
37@RunWith(AndroidJUnit4.class)
38@SmallTest
39public class SelectionManagerTest {
40
41    private static final List<String> ITEMS = TestData.create(100);
42
43    private final Set<String> mIgnored = new HashSet<>();
44    private SelectionManager mManager;
45    private TestSelectionListener mCallback;
46    private SelectionProbe mSelection;
47
48    @Before
49    public void setUp() throws Exception {
50        mCallback = new TestSelectionListener();
51        mManager = SelectionManagers.createTestInstance(
52                ITEMS,
53                SelectionManager.MODE_MULTIPLE,
54                (String id, boolean nextState) -> (!nextState || !mIgnored.contains(id)));
55        mManager.addCallback(mCallback);
56
57        mSelection = new SelectionProbe(mManager);
58
59        mIgnored.clear();
60    }
61
62    @Test
63    public void testSelection() {
64        // Check selection.
65        mManager.toggleSelection(ITEMS.get(7));
66        mSelection.assertSelection(7);
67        // Check deselection.
68        mManager.toggleSelection(ITEMS.get(7));
69        mSelection.assertNoSelection();
70    }
71
72    @Test
73    public void testSelection_DoNothingOnUnselectableItem() {
74        mIgnored.add(ITEMS.get(7));
75
76        mManager.toggleSelection(ITEMS.get(7));
77        mSelection.assertNoSelection();
78    }
79
80    @Test
81    public void testSelection_NotifiesSelectionChanged() {
82        // Selection should notify.
83        mManager.toggleSelection(ITEMS.get(7));
84        mCallback.assertSelectionChanged();
85        // Deselection should notify.
86        mManager.toggleSelection(ITEMS.get(7));
87        mCallback.assertSelectionChanged();
88    }
89
90    @Test
91    public void testSetItemsSelected() {
92        mManager.setItemsSelected(getStringIds(6, 7, 8), true);
93
94        mSelection.assertRangeSelected(6, 8);
95    }
96
97    @Test
98    public void testSetItemsSelected_SkipUnselectableItem() {
99        mIgnored.add(ITEMS.get(7));
100
101        mManager.setItemsSelected(getStringIds(6, 7, 8), true);
102
103        mSelection.assertSelected(6);
104        mSelection.assertNotSelected(7);
105        mSelection.assertSelected(8);
106    }
107
108    @Test
109    public void testRangeSelection() {
110        mManager.startRangeSelection(15);
111        mManager.snapRangeSelection(19);
112        mSelection.assertRangeSelection(15, 19);
113    }
114
115    @Test
116    public void testRangeSelection_SkipUnselectableItem() {
117        mIgnored.add(ITEMS.get(17));
118
119        mManager.startRangeSelection(15);
120        mManager.snapRangeSelection(19);
121
122        mSelection.assertRangeSelected(15, 16);
123        mSelection.assertNotSelected(17);
124        mSelection.assertRangeSelected(18, 19);
125    }
126
127    @Test
128    public void testRangeSelection_snapExpand() {
129        mManager.startRangeSelection(15);
130        mManager.snapRangeSelection(19);
131        mManager.snapRangeSelection(27);
132        mSelection.assertRangeSelection(15, 27);
133    }
134
135    @Test
136    public void testRangeSelection_snapContract() {
137        mManager.startRangeSelection(15);
138        mManager.snapRangeSelection(27);
139        mManager.snapRangeSelection(19);
140        mSelection.assertRangeSelection(15, 19);
141    }
142
143    @Test
144    public void testRangeSelection_snapInvert() {
145        mManager.startRangeSelection(15);
146        mManager.snapRangeSelection(27);
147        mManager.snapRangeSelection(3);
148        mSelection.assertRangeSelection(3, 15);
149    }
150
151    @Test
152    public void testRangeSelection_multiple() {
153        mManager.startRangeSelection(15);
154        mManager.snapRangeSelection(27);
155        mManager.endRangeSelection();
156        mManager.startRangeSelection(42);
157        mManager.snapRangeSelection(57);
158        mSelection.assertSelectionSize(29);
159        mSelection.assertRangeSelected(15, 27);
160        mSelection.assertRangeSelected(42, 57);
161    }
162
163    @Test
164    public void testProvisionalRangeSelection() {
165        mManager.startRangeSelection(13);
166        mManager.snapProvisionalRangeSelection(15);
167        mSelection.assertRangeSelection(13, 15);
168        mManager.getSelection().applyProvisionalSelection();
169        mManager.endRangeSelection();
170        mSelection.assertSelectionSize(3);
171    }
172
173    @Test
174    public void testProvisionalRangeSelection_endEarly() {
175        mManager.startRangeSelection(13);
176        mManager.snapProvisionalRangeSelection(15);
177        mSelection.assertRangeSelection(13, 15);
178
179        mManager.endRangeSelection();
180        // If we end range selection prematurely for provision selection, nothing should be selected
181        // except the first item
182        mSelection.assertSelectionSize(1);
183    }
184
185    @Test
186    public void testProvisionalRangeSelection_snapExpand() {
187        mManager.startRangeSelection(13);
188        mManager.snapProvisionalRangeSelection(15);
189        mSelection.assertRangeSelection(13, 15);
190        mManager.getSelection().applyProvisionalSelection();
191        mManager.snapRangeSelection(18);
192        mSelection.assertRangeSelection(13, 18);
193    }
194
195    @Test
196    public void testCombinationRangeSelection_IntersectsOldSelection() {
197        mManager.startRangeSelection(13);
198        mManager.snapRangeSelection(15);
199        mSelection.assertRangeSelection(13, 15);
200
201        mManager.startRangeSelection(11);
202        mManager.snapProvisionalRangeSelection(18);
203        mSelection.assertRangeSelected(11, 18);
204        mManager.endRangeSelection();
205        mSelection.assertRangeSelected(13, 15);
206        mSelection.assertRangeSelected(11, 11);
207        mSelection.assertSelectionSize(4);
208    }
209
210    @Test
211    public void testProvisionalSelection() {
212        Selection s = mManager.getSelection();
213        mSelection.assertNoSelection();
214
215        SparseBooleanArray provisional = new SparseBooleanArray();
216        provisional.append(1, true);
217        provisional.append(2, true);
218        s.setProvisionalSelection(getItemIds(provisional));
219        mSelection.assertSelection(1, 2);
220    }
221
222    @Test
223    public void testProvisionalSelection_Replace() {
224        Selection s = mManager.getSelection();
225
226        SparseBooleanArray provisional = new SparseBooleanArray();
227        provisional.append(1, true);
228        provisional.append(2, true);
229        s.setProvisionalSelection(getItemIds(provisional));
230
231        provisional.clear();
232        provisional.append(3, true);
233        provisional.append(4, true);
234        s.setProvisionalSelection(getItemIds(provisional));
235        mSelection.assertSelection(3, 4);
236    }
237
238    @Test
239    public void testProvisionalSelection_IntersectsExistingProvisionalSelection() {
240        Selection s = mManager.getSelection();
241
242        SparseBooleanArray provisional = new SparseBooleanArray();
243        provisional.append(1, true);
244        provisional.append(2, true);
245        s.setProvisionalSelection(getItemIds(provisional));
246
247        provisional.clear();
248        provisional.append(1, true);
249        s.setProvisionalSelection(getItemIds(provisional));
250        mSelection.assertSelection(1);
251    }
252
253    @Test
254    public void testProvisionalSelection_Apply() {
255        Selection s = mManager.getSelection();
256
257        SparseBooleanArray provisional = new SparseBooleanArray();
258        provisional.append(1, true);
259        provisional.append(2, true);
260        s.setProvisionalSelection(getItemIds(provisional));
261        s.applyProvisionalSelection();
262        mSelection.assertSelection(1, 2);
263    }
264
265    @Test
266    public void testProvisionalSelection_Cancel() {
267        mManager.toggleSelection(ITEMS.get(1));
268        mManager.toggleSelection(ITEMS.get(2));
269        Selection s = mManager.getSelection();
270
271        SparseBooleanArray provisional = new SparseBooleanArray();
272        provisional.append(3, true);
273        provisional.append(4, true);
274        s.setProvisionalSelection(getItemIds(provisional));
275        s.cancelProvisionalSelection();
276
277        // Original selection should remain.
278        mSelection.assertSelection(1, 2);
279    }
280
281    @Test
282    public void testProvisionalSelection_IntersectsAppliedSelection() {
283        mManager.toggleSelection(ITEMS.get(1));
284        mManager.toggleSelection(ITEMS.get(2));
285        Selection s = mManager.getSelection();
286
287        SparseBooleanArray provisional = new SparseBooleanArray();
288        provisional.append(2, true);
289        provisional.append(3, true);
290        s.setProvisionalSelection(getItemIds(provisional));
291        mSelection.assertSelection(1, 2, 3);
292    }
293
294    private static Set<String> getItemIds(SparseBooleanArray selection) {
295        Set<String> ids = new HashSet<>();
296
297        int count = selection.size();
298        for (int i = 0; i < count; ++i) {
299            ids.add(ITEMS.get(selection.keyAt(i)));
300        }
301
302        return ids;
303    }
304
305    private static Iterable<String> getStringIds(int... ids) {
306        List<String> stringIds = new ArrayList<>(ids.length);
307        for (int id : ids) {
308            stringIds.add(ITEMS.get(id));
309        }
310        return stringIds;
311    }
312}
313