blob: aabec9abcaabf65ba5167f5d4f84a5b84e033de9 [file] [log] [blame]
Steve McKayef280152015-06-11 10:10:49 -07001/*
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;
18
19import static org.junit.Assert.*;
20
21import android.support.v7.widget.RecyclerView;
Steve McKay8c1b8c82015-07-28 19:20:01 -070022import android.view.KeyEvent;
Steve McKayef280152015-06-11 10:10:49 -070023import android.view.MotionEvent;
24import android.view.View;
25import android.view.ViewGroup;
26
27import com.android.documentsui.MultiSelectManager.RecyclerViewHelper;
28import com.android.documentsui.MultiSelectManager.Selection;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.mockito.Mockito;
33
34import java.util.ArrayList;
35import java.util.HashSet;
36import java.util.List;
37import java.util.Set;
38
39public class MultiSelectManagerTest {
40
41 private static final List<String> items;
42 static {
43 items = new ArrayList<String>();
44 items.add("aaa");
45 items.add("bbb");
46 items.add("ccc");
47 items.add("111");
48 items.add("222");
49 items.add("333");
50 }
51
52 private MultiSelectManager mManager;
53 private TestAdapter mAdapter;
54 private TestCallback mCallback;
55
56 private EventHelper mEventHelper;
57
58 @Before
59 public void setUp() throws Exception {
60 mAdapter = new TestAdapter(items);
61 mCallback = new TestCallback();
62 mEventHelper = new EventHelper();
63 mManager = new MultiSelectManager(mAdapter, mEventHelper);
64 mManager.addCallback(mCallback);
65 }
66
67 @Test
Steve McKay9459a7c2015-07-24 13:14:20 -070068 public void longPress_StartsSelectionMode() {
Steve McKayef280152015-06-11 10:10:49 -070069 mManager.onLongPress(7);
70 assertSelection(7);
71 }
72
73 @Test
Steve McKay9459a7c2015-07-24 13:14:20 -070074 public void longPress_SecondPressExtendsSelection() {
Steve McKayef280152015-06-11 10:10:49 -070075 mManager.onLongPress(7);
76 mManager.onLongPress(99);
77 assertSelection(7, 99);
78 }
79
80 @Test
Steve McKay9459a7c2015-07-24 13:14:20 -070081 public void singleTapUp_DoesNotSelectBeforeLongPress() {
Steve McKay8c1b8c82015-07-28 19:20:01 -070082 mManager.onSingleTapUp(99, 0);
Steve McKay9459a7c2015-07-24 13:14:20 -070083 assertSelection();
84 }
85
86 @Test
87 public void singleTapUp_UnselectsSelectedItem() {
Steve McKayef280152015-06-11 10:10:49 -070088 mManager.onLongPress(7);
Steve McKay8c1b8c82015-07-28 19:20:01 -070089 mManager.onSingleTapUp(7, 0);
Steve McKayef280152015-06-11 10:10:49 -070090 assertSelection();
91 }
92
93 @Test
Steve McKay9459a7c2015-07-24 13:14:20 -070094 public void singleTapUp_NoPositionClearsSelection() {
95 mManager.onLongPress(7);
Steve McKay8c1b8c82015-07-28 19:20:01 -070096 mManager.onSingleTapUp(11, 0);
97 mManager.onSingleTapUp(RecyclerView.NO_POSITION, 0);
Steve McKay9459a7c2015-07-24 13:14:20 -070098 assertSelection();
99 }
100
101 @Test
102 public void singleTapUp_ExtendsSelection() {
Steve McKayef280152015-06-11 10:10:49 -0700103 mManager.onLongPress(99);
Steve McKay8c1b8c82015-07-28 19:20:01 -0700104 mManager.onSingleTapUp(7, 0);
105 mManager.onSingleTapUp(13, 0);
106 mManager.onSingleTapUp(129899, 0);
Steve McKayef280152015-06-11 10:10:49 -0700107 assertSelection(7, 99, 13, 129899);
108 }
109
Steve McKay8c1b8c82015-07-28 19:20:01 -0700110 @Test
111 public void singleTapUp_ShiftCreatesRangeSelection() {
112 mManager.onLongPress(7);
113 mManager.onSingleTapUp(17, KeyEvent.META_SHIFT_ON);
114 assertRangeSelection(7, 17);
115 }
116
117 @Test
118 public void singleTapUp_ShiftCreatesRangeSeletion_Backwards() {
119 mManager.onLongPress(17);
120 mManager.onSingleTapUp(7, KeyEvent.META_SHIFT_ON);
121 assertRangeSelection(7, 17);
122 }
123
124 @Test
125 public void singleTapUp_SecondShiftClickExtendsSelection() {
126 mManager.onLongPress(7);
127 mManager.onSingleTapUp(11, KeyEvent.META_SHIFT_ON);
128 mManager.onSingleTapUp(17, KeyEvent.META_SHIFT_ON);
129 assertRangeSelection(7, 17);
130 }
131
132 @Test
133 public void singleTapUp_MultipleContiguousRangesSelected() {
134 mManager.onLongPress(7);
135 mManager.onSingleTapUp(11, KeyEvent.META_SHIFT_ON);
136 mManager.onSingleTapUp(20, 0);
137 mManager.onSingleTapUp(25, KeyEvent.META_SHIFT_ON);
138 assertRangeSelected(7, 11);
139 assertRangeSelected(20, 25);
140 assertSelectionSize(11);
141 }
142
143 @Test
144 public void singleTapUp_ShiftReducesSelectionRange_FromPreviousShiftClick() {
145 mManager.onLongPress(7);
146 mManager.onSingleTapUp(17, KeyEvent.META_SHIFT_ON);
147 mManager.onSingleTapUp(10, KeyEvent.META_SHIFT_ON);
148 assertRangeSelection(7, 10);
149 }
150
151 @Test
152 public void singleTapUp_ShiftReducesSelectionRange_FromPreviousShiftClick_Backwards() {
153 mManager.onLongPress(17);
154 mManager.onSingleTapUp(7, KeyEvent.META_SHIFT_ON);
155 mManager.onSingleTapUp(14, KeyEvent.META_SHIFT_ON);
156 assertRangeSelection(14, 17);
157 }
158
159
160 @Test
161 public void singleTapUp_ShiftReversesSelectionDirection() {
162 mManager.onLongPress(7);
163 mManager.onSingleTapUp(17, KeyEvent.META_SHIFT_ON);
164 mManager.onSingleTapUp(0, KeyEvent.META_SHIFT_ON);
165 assertRangeSelection(0, 7);
166 }
167
Steve McKayef280152015-06-11 10:10:49 -0700168 private void assertSelected(int... expected) {
169 for (int i = 0; i < expected.length; i++) {
170 Selection selection = mManager.getSelection();
171 String err = String.format(
172 "Selection %s does not contain %d", selection, expected[i]);
173 assertTrue(err, selection.contains(expected[i]));
174 }
175 }
176
177 private void assertSelection(int... expected) {
178 assertSelectionSize(expected.length);
179 assertSelected(expected);
180 }
181
Steve McKay8c1b8c82015-07-28 19:20:01 -0700182 private void assertRangeSelected(int begin, int end) {
183 for (int i = begin; i <= end; i++) {
184 assertSelected(i);
185 }
186 }
187
188 private void assertRangeSelection(int begin, int end) {
189 assertSelectionSize(end - begin + 1);
190 assertRangeSelected(begin, end);
191 }
192
Steve McKayef280152015-06-11 10:10:49 -0700193 private void assertSelectionSize(int expected) {
194 Selection selection = mManager.getSelection();
Steve McKay8c1b8c82015-07-28 19:20:01 -0700195 assertEquals(selection.toString(), expected, selection.size());
Steve McKayef280152015-06-11 10:10:49 -0700196 }
197
198 private static final class EventHelper implements RecyclerViewHelper {
199 @Override
200 public int findEventPosition(MotionEvent e) {
201 throw new UnsupportedOperationException();
202 }
203 }
204
205 private static final class TestCallback implements MultiSelectManager.Callback {
206
207 Set<Integer> ignored = new HashSet<>();
208 private int mLastChangedPosition;
209 private boolean mLastChangedSelected;
210
211 @Override
212 public void onItemStateChanged(int position, boolean selected) {
213 this.mLastChangedPosition = position;
214 this.mLastChangedSelected = selected;
215 }
216
217 @Override
218 public boolean onBeforeItemStateChange(int position, boolean selected) {
219 return !ignored.contains(position);
220 }
221 }
222
223 private static final class TestHolder extends RecyclerView.ViewHolder {
224 // each data item is just a string in this case
225 public View view;
226 public String string;
227 public TestHolder(View view) {
228 super(view);
229 this.view = view;
230 }
231 }
232
233 private static final class TestAdapter extends RecyclerView.Adapter<TestHolder> {
234
235 private List<String> mItems;
236
237 public TestAdapter(List<String> items) {
238 mItems = items;
239 }
240
241 @Override
242 public TestHolder onCreateViewHolder(ViewGroup parent, int viewType) {
243 return new TestHolder(Mockito.mock(ViewGroup.class));
244 }
245
246 @Override
247 public void onBindViewHolder(TestHolder holder, int position) {}
248
249 @Override
250 public int getItemCount() {
251 return mItems.size();
252 }
253 }
254}