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