• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.selection;
18 
19 import static junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.assertFalse;
21 import static junit.framework.Assert.assertTrue;
22 
23 import android.support.test.filters.SmallTest;
24 import android.support.test.runner.AndroidJUnit4;
25 
26 import com.google.common.collect.Sets;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 import java.util.HashSet;
33 import java.util.Set;
34 
35 @RunWith(AndroidJUnit4.class)
36 @SmallTest
37 public class SelectionTest {
38 
39     private Selection selection;
40 
41     private String[] ids = new String[]{
42             "foo",
43             "43",
44             "auth|id=@53di*/f3#d"
45     };
46 
47     @Before
setUp()48     public void setUp() throws Exception {
49         selection = new Selection();
50         selection.add(ids[0]);
51         selection.add(ids[1]);
52         selection.add(ids[2]);
53     }
54 
55     @Test
testAdd()56     public void testAdd() {
57         // We added in setUp.
58         assertEquals(3, selection.size());
59         assertContains(ids[0]);
60         assertContains(ids[1]);
61         assertContains(ids[2]);
62     }
63 
64     @Test
testRemove()65     public void testRemove() {
66         selection.remove(ids[0]);
67         selection.remove(ids[2]);
68         assertEquals(1, selection.size());
69         assertContains(ids[1]);
70     }
71 
72     @Test
testClear()73     public void testClear() {
74         selection.clear();
75         assertEquals(0, selection.size());
76     }
77 
78     @Test
testIsEmpty()79     public void testIsEmpty() {
80         assertTrue(new Selection().isEmpty());
81         selection.clear();
82         assertTrue(selection.isEmpty());
83     }
84 
85     @Test
testSize()86     public void testSize() {
87         Selection other = new Selection();
88         for (int i = 0; i < selection.size(); i++) {
89             other.add(ids[i]);
90         }
91         assertEquals(selection.size(), other.size());
92     }
93 
94     @Test
testEqualsSelf()95     public void testEqualsSelf() {
96         assertEquals(selection, selection);
97     }
98 
99     @Test
testEqualsOther()100     public void testEqualsOther() {
101         Selection other = new Selection();
102         other.add(ids[0]);
103         other.add(ids[1]);
104         other.add(ids[2]);
105         assertEquals(selection, other);
106         assertEquals(selection.hashCode(), other.hashCode());
107     }
108 
109     @Test
testEqualsCopy()110     public void testEqualsCopy() {
111         Selection other = new Selection();
112         other.copyFrom(selection);
113         assertEquals(selection, other);
114         assertEquals(selection.hashCode(), other.hashCode());
115     }
116 
117     @Test
testNotEquals()118     public void testNotEquals() {
119         Selection other = new Selection();
120         other.add("foobar");
121         assertFalse(selection.equals(other));
122     }
123 
124     @Test
testIntersection_empty0()125     public void testIntersection_empty0() {
126         Selection testSelection = new Selection();
127         testSelection.intersect(new HashSet<String>());
128         assertTrue(testSelection.isEmpty());
129     }
130 
131     @Test
testIntersection_empty1()132     public void testIntersection_empty1() {
133         Selection testSelection = new Selection();
134         testSelection.intersect(Sets.newHashSet("foo"));
135         assertTrue(testSelection.isEmpty());
136     }
137 
138     @Test
testIntersection_empty2()139     public void testIntersection_empty2() {
140         assertFalse(selection.isEmpty());
141         selection.intersect(new HashSet<String>());
142         assertTrue(selection.isEmpty());
143     }
144 
145     @Test
testIntersection_exclusive()146     public void testIntersection_exclusive() {
147         String[] ids0 = new String[]{"foo", "bar", "baz"};
148         String[] ids1 = new String[]{"0", "1", "2"};
149 
150         Selection testSelection = new Selection();
151         testSelection.add(ids0[0]);
152         testSelection.add(ids0[1]);
153         testSelection.add(ids0[2]);
154 
155         Set<String> set = Sets.newHashSet(ids1);
156         testSelection.intersect(set);
157 
158         assertTrue(testSelection.isEmpty());
159     }
160 
161     @Test
testIntersection_subset()162     public void testIntersection_subset() {
163         String[] ids0 = new String[]{"foo", "bar", "baz"};
164         String[] ids1 = new String[]{"0", "baz", "1", "foo", "2"};
165 
166         Selection testSelection = new Selection();
167         testSelection.add(ids0[0]);
168         testSelection.add(ids0[1]);
169         testSelection.add(ids0[2]);
170 
171         testSelection.intersect(Sets.newHashSet(ids1));
172 
173         assertTrue(testSelection.contains("foo"));
174         assertFalse(testSelection.contains("bar"));
175         assertTrue(testSelection.contains("baz"));
176     }
177 
178     @Test
testIntersection_all()179     public void testIntersection_all() {
180         String[] ids0 = new String[]{"foo", "bar", "baz"};
181         String[] ids1 = new String[]{"0", "baz", "1", "foo", "2", "bar"};
182 
183         Selection testSelection = new Selection();
184         testSelection.add(ids0[0]);
185         testSelection.add(ids0[1]);
186         testSelection.add(ids0[2]);
187 
188         Selection control = new Selection();
189         control.copyFrom(testSelection);
190 
191         testSelection.intersect(Sets.newHashSet(ids1));
192 
193         assertTrue(testSelection.equals(control));
194     }
195 
assertContains(String id)196     private void assertContains(String id) {
197         String err = String.format("Selection %s does not contain %s", selection, id);
198         assertTrue(err, selection.contains(id));
199     }
200 }
201