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