1 /*
2  * Copyright (C) 2016 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.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 
24 import androidx.recyclerview.selection.Selection;
25 import androidx.test.filters.SmallTest;
26 import androidx.test.runner.AndroidJUnit4;
27 
28 import com.android.documentsui.DocsSelectionHelper.DelegateFactory;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 
39 /**
40  * Tests for the specialized behaviors provided by DocsSelectionManager.
41  */
42 @RunWith(AndroidJUnit4.class)
43 @SmallTest
44 public class DocsSelectionHelperTest {
45 
46     private DocsSelectionHelper mSelectionMgr;
47     private List<TestSelectionManager> mCreated;
48     private DelegateFactory mFactory;
49 
50     @Before
setup()51     public void setup() {
52         mCreated = new ArrayList<>();
53         mFactory = new DelegateFactory();
54 
55         mSelectionMgr = new DocsSelectionHelper(mFactory);
56     }
57 
58     @Test
testCallableBeforeReset()59     public void testCallableBeforeReset() {
60         mSelectionMgr.hasSelection();
61         assertNotNull(mSelectionMgr.getSelection());
62         assertFalse(mSelectionMgr.isSelected("poodle"));
63     }
64 
65     @Test
testReset_CreatesNewInstances()66     public void testReset_CreatesNewInstances() {
67         resetSelectionHelper();
68         resetSelectionHelper();
69 
70         assertCreated(2);
71     }
72 
73     @Test
testReset_ClearsPreviousSelection()74     public void testReset_ClearsPreviousSelection() {
75         resetSelectionHelper();
76         resetSelectionHelper();
77 
78         mCreated.get(0).assertCleared(true);
79         mCreated.get(1).assertCleared(false);
80     }
81 
82     @Test
testReplaceSelection()83     public void testReplaceSelection() {
84         resetSelectionHelper();
85 
86         List<String> ids = new ArrayList<>();
87         ids.add("poodles");
88         ids.add("hammy");
89         mSelectionMgr.replaceSelection(ids);
90         mCreated.get(0).assertCleared(true);
91         mCreated.get(0).assertSelected("poodles", "hammy");
92     }
93 
assertCreated(int count)94     void assertCreated(int count) {
95         assertEquals(count, mCreated.size());
96     }
97 
resetSelectionHelper()98     private void resetSelectionHelper() {
99         TestSelectionManager mgr = new TestSelectionManager();
100         mCreated.add(mgr);
101 
102         mSelectionMgr.reset(mgr);
103     }
104 
105     private static final class TestSelectionManager extends DummySelectionTracker<String> {
106 
107         private boolean mCleared;
108         private Map<String, Boolean> mSelected = new HashMap<>();
109 
assertCleared(boolean expected)110         void assertCleared(boolean expected) {
111             assertEquals(expected, mCleared);
112         }
113 
assertSelected(String... expected)114         void assertSelected(String... expected) {
115             for (String id : expected) {
116                 assertTrue(mSelected.containsKey(id));
117                 assertTrue(mSelected.get(id));
118             }
119             assertEquals(expected.length, mSelected.size());
120         }
121 
122         @Override
addObserver(SelectionObserver listener)123         public void addObserver(SelectionObserver listener) {
124             throw new UnsupportedOperationException();
125         }
126 
127         @Override
hasSelection()128         public boolean hasSelection() {
129             throw new UnsupportedOperationException();
130         }
131 
132         @Override
getSelection()133         public Selection<String> getSelection() {
134             throw new UnsupportedOperationException();
135         }
136 
137         @Override
isSelected(String id)138         public boolean isSelected(String id) {
139             throw new UnsupportedOperationException();
140         }
141 
142         @Override
restoreSelection(Selection<String> other)143         public void restoreSelection(Selection<String> other) {
144             throw new UnsupportedOperationException();
145         }
146 
147         @Override
setItemsSelected(Iterable<String> ids, boolean selected)148         public boolean setItemsSelected(Iterable<String> ids, boolean selected) {
149             for (String id : ids) {
150                 mSelected.put(id, selected);
151             }
152             return true;
153         }
154 
155         @Override
clearSelection()156         public boolean clearSelection() {
157             mCleared = true;
158             return true;
159         }
160 
161         @Override
select(String itemId)162         public boolean select(String itemId) {
163             throw new UnsupportedOperationException();
164         }
165 
166         @Override
deselect(String itemId)167         public boolean deselect(String itemId) {
168             throw new UnsupportedOperationException();
169         }
170     }
171 }
172