1 /*
2  * Copyright 2017 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 androidx.recyclerview.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 androidx.test.ext.junit.runners.AndroidJUnit4;
24 import androidx.test.filters.SmallTest;
25 
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.HashSet;
33 import java.util.LinkedHashSet;
34 import java.util.List;
35 import java.util.Set;
36 
37 @RunWith(AndroidJUnit4.class)
38 @SmallTest
39 public class SelectionTest {
40 
41     private final String[] mIds = new String[] {
42             "foo",
43             "43",
44             "auth|id=@53di*/f3#d"
45     };
46 
47     private Selection<String> mSelection;
48 
49     @Before
setUp()50     public void setUp() throws Exception {
51         mSelection = new Selection<>();
52         mSelection.add(mIds[0]);
53         mSelection.add(mIds[1]);
54         mSelection.add(mIds[2]);
55     }
56 
57     @Test
testAdd()58     public void testAdd() {
59         // We added in setUp.
60         assertEquals(3, mSelection.size());
61         assertContains(mIds[0]);
62         assertContains(mIds[1]);
63         assertContains(mIds[2]);
64     }
65 
66     @Test
testSelectionOrder_Primary()67     public void testSelectionOrder_Primary() {
68         // We added in setUp.
69         mSelection.clear();
70         mSelection.add("a");
71         mSelection.add("b");
72         mSelection.add("c");
73 
74         List<String> elements = new ArrayList<>();
75         for (String key : mSelection) {
76             elements.add(key);
77         }
78         assertEquals("a", elements.get(0));
79         assertEquals("b", elements.get(1));
80         assertEquals("c", elements.get(2));
81     }
82 
83     @Test
testSelectionOrder_Provisional()84     public void testSelectionOrder_Provisional() {
85         // We added in setUp.
86         mSelection.clear();
87         mSelection.add("a");
88         mSelection.add("b");
89         mSelection.add("c");
90 
91         Set<String> provisionalSelection = new LinkedHashSet<>();
92         provisionalSelection.add("z");
93         provisionalSelection.add("y");
94         provisionalSelection.add("a");
95         mSelection.setProvisionalSelection(provisionalSelection);
96         mSelection.mergeProvisionalSelection();
97 
98         List<String> elements = new ArrayList<>();
99         for (String key : mSelection) {
100             elements.add(key);
101         }
102         assertEquals("a", elements.get(0));
103         assertEquals("b", elements.get(1));
104         assertEquals("c", elements.get(2));
105         assertEquals("z", elements.get(3));
106         assertEquals("y", elements.get(4));
107     }
108 
109     @Test
testAdd_NewItemReturnsTrue()110     public void testAdd_NewItemReturnsTrue() {
111         assertTrue(mSelection.add("poodles"));
112     }
113 
114     @Test
testAdd_ExistingReturnsFalse()115     public void testAdd_ExistingReturnsFalse() {
116         assertFalse(mSelection.add(mIds[0]));
117     }
118 
119     @Test
testRemove()120     public void testRemove() {
121         mSelection.remove(mIds[0]);
122         mSelection.remove(mIds[2]);
123         assertEquals(1, mSelection.size());
124         assertContains(mIds[1]);
125     }
126 
127     @Test
testRemove_ExistingItemReturnsTrue()128     public void testRemove_ExistingItemReturnsTrue() {
129         assertTrue(mSelection.remove(mIds[0]));
130     }
131 
132     @Test
testRemove_NewItemReturnsFalse()133     public void testRemove_NewItemReturnsFalse() {
134         assertFalse(mSelection.remove("poodles"));
135     }
136 
137     @Test
testClear()138     public void testClear() {
139         mSelection.clear();
140         assertEquals(0, mSelection.size());
141     }
142 
143     @Test
testIsEmpty()144     public void testIsEmpty() {
145         assertTrue(new Selection<>().isEmpty());
146         mSelection.clear();
147         assertTrue(mSelection.isEmpty());
148     }
149 
150     @Test
testSize()151     public void testSize() {
152         Selection<String> other = new Selection<>();
153         for (int i = 0; i < mSelection.size(); i++) {
154             other.add(mIds[i]);
155         }
156         assertEquals(mSelection.size(), other.size());
157     }
158 
159     @Test
testEqualsSelf()160     public void testEqualsSelf() {
161         assertEquals(mSelection, mSelection);
162     }
163 
164     @Test
testEqualsOther()165     public void testEqualsOther() {
166         Selection<String> other = new Selection<>();
167         other.add(mIds[0]);
168         other.add(mIds[1]);
169         other.add(mIds[2]);
170         assertEquals(mSelection, other);
171         assertEquals(mSelection.hashCode(), other.hashCode());
172     }
173 
174     @Test
testEqualsCopy()175     public void testEqualsCopy() {
176         Selection<String> other = new Selection<>();
177         other.copyFrom(mSelection);
178         assertEquals(mSelection, other);
179         assertEquals(mSelection.hashCode(), other.hashCode());
180     }
181 
182     @Test
testNotEquals()183     public void testNotEquals() {
184         Selection<String> other = new Selection<>();
185         other.add("foobar");
186         assertFalse(mSelection.equals(other));
187     }
188 
assertContains(String id)189     private void assertContains(String id) {
190         String err = String.format("Selection %s does not contain %s", mSelection, id);
191         assertTrue(err, mSelection.contains(id));
192     }
193 
194     @SuppressWarnings("unchecked")
newSet(E... elements)195     public static <E> Set<E> newSet(E... elements) {
196         HashSet<E> set = new HashSet<>(elements.length);
197         Collections.addAll(set, elements);
198         return set;
199     }
200 }
201