Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
| 19 | import static org.junit.Assert.*; |
| 20 | |
| 21 | import android.support.v7.widget.RecyclerView; |
| 22 | import android.view.MotionEvent; |
| 23 | import android.view.View; |
| 24 | import android.view.ViewGroup; |
| 25 | |
| 26 | import com.android.documentsui.MultiSelectManager.RecyclerViewHelper; |
| 27 | import com.android.documentsui.MultiSelectManager.Selection; |
| 28 | |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| 31 | import org.mockito.Mockito; |
| 32 | |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.HashSet; |
| 35 | import java.util.List; |
| 36 | import java.util.Set; |
| 37 | |
| 38 | public class MultiSelectManagerTest { |
| 39 | |
| 40 | private static final List<String> items; |
| 41 | static { |
| 42 | items = new ArrayList<String>(); |
| 43 | items.add("aaa"); |
| 44 | items.add("bbb"); |
| 45 | items.add("ccc"); |
| 46 | items.add("111"); |
| 47 | items.add("222"); |
| 48 | items.add("333"); |
| 49 | } |
| 50 | |
| 51 | private MultiSelectManager mManager; |
| 52 | private TestAdapter mAdapter; |
| 53 | private TestCallback mCallback; |
| 54 | |
| 55 | private EventHelper mEventHelper; |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() throws Exception { |
| 59 | mAdapter = new TestAdapter(items); |
| 60 | mCallback = new TestCallback(); |
| 61 | mEventHelper = new EventHelper(); |
| 62 | mManager = new MultiSelectManager(mAdapter, mEventHelper); |
| 63 | mManager.addCallback(mCallback); |
| 64 | } |
| 65 | |
| 66 | @Test |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame^] | 67 | public void longPress_StartsSelectionMode() { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 68 | mManager.onLongPress(7); |
| 69 | assertSelection(7); |
| 70 | } |
| 71 | |
| 72 | @Test |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame^] | 73 | public void longPress_SecondPressExtendsSelection() { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 74 | mManager.onLongPress(7); |
| 75 | mManager.onLongPress(99); |
| 76 | assertSelection(7, 99); |
| 77 | } |
| 78 | |
| 79 | @Test |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame^] | 80 | public void singleTapUp_DoesNotSelectBeforeLongPress() { |
| 81 | mManager.onSingleTapUp(99); |
| 82 | assertSelection(); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void singleTapUp_UnselectsSelectedItem() { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 87 | mManager.onLongPress(7); |
| 88 | mManager.onSingleTapUp(7); |
| 89 | assertSelection(); |
| 90 | } |
| 91 | |
| 92 | @Test |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame^] | 93 | public void singleTapUp_NoPositionClearsSelection() { |
| 94 | mManager.onLongPress(7); |
| 95 | mManager.onSingleTapUp(11); |
| 96 | mManager.onSingleTapUp(RecyclerView.NO_POSITION); |
| 97 | assertSelection(); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void singleTapUp_ExtendsSelection() { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 102 | mManager.onLongPress(99); |
| 103 | mManager.onSingleTapUp(7); |
| 104 | mManager.onSingleTapUp(13); |
| 105 | mManager.onSingleTapUp(129899); |
| 106 | assertSelection(7, 99, 13, 129899); |
| 107 | } |
| 108 | |
| 109 | private void assertSelected(int... expected) { |
| 110 | for (int i = 0; i < expected.length; i++) { |
| 111 | Selection selection = mManager.getSelection(); |
| 112 | String err = String.format( |
| 113 | "Selection %s does not contain %d", selection, expected[i]); |
| 114 | assertTrue(err, selection.contains(expected[i])); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private void assertSelection(int... expected) { |
| 119 | assertSelectionSize(expected.length); |
| 120 | assertSelected(expected); |
| 121 | } |
| 122 | |
| 123 | private void assertSelectionSize(int expected) { |
| 124 | Selection selection = mManager.getSelection(); |
| 125 | assertEquals(expected, selection.size()); |
| 126 | } |
| 127 | |
| 128 | private static final class EventHelper implements RecyclerViewHelper { |
| 129 | @Override |
| 130 | public int findEventPosition(MotionEvent e) { |
| 131 | throw new UnsupportedOperationException(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private static final class TestCallback implements MultiSelectManager.Callback { |
| 136 | |
| 137 | Set<Integer> ignored = new HashSet<>(); |
| 138 | private int mLastChangedPosition; |
| 139 | private boolean mLastChangedSelected; |
| 140 | |
| 141 | @Override |
| 142 | public void onItemStateChanged(int position, boolean selected) { |
| 143 | this.mLastChangedPosition = position; |
| 144 | this.mLastChangedSelected = selected; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public boolean onBeforeItemStateChange(int position, boolean selected) { |
| 149 | return !ignored.contains(position); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private static final class TestHolder extends RecyclerView.ViewHolder { |
| 154 | // each data item is just a string in this case |
| 155 | public View view; |
| 156 | public String string; |
| 157 | public TestHolder(View view) { |
| 158 | super(view); |
| 159 | this.view = view; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | private static final class TestAdapter extends RecyclerView.Adapter<TestHolder> { |
| 164 | |
| 165 | private List<String> mItems; |
| 166 | |
| 167 | public TestAdapter(List<String> items) { |
| 168 | mItems = items; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public TestHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 173 | return new TestHolder(Mockito.mock(ViewGroup.class)); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void onBindViewHolder(TestHolder holder, int position) {} |
| 178 | |
| 179 | @Override |
| 180 | public int getItemCount() { |
| 181 | return mItems.size(); |
| 182 | } |
| 183 | } |
| 184 | } |