• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 com.android.documentsui.selection.testing;
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 com.android.documentsui.selection.SelectionHelper.SelectionObserver;
24 
25 import java.util.HashSet;
26 import java.util.Set;
27 
28 public class TestSelectionObserver extends SelectionObserver {
29 
30     private final Set<String> mSelected = new HashSet<>();
31     private boolean mSelectionChanged = false;
32     private boolean mSelectionReset = false;
33     private boolean mSelectionRestored = false;
34 
reset()35     public void reset() {
36         mSelected.clear();
37         mSelectionChanged = false;
38         mSelectionReset = false;
39     }
40 
41     @Override
onItemStateChanged(String id, boolean selected)42     public void onItemStateChanged(String id, boolean selected) {
43         if (selected) {
44             assertNotSelected(id);
45             mSelected.add(id);
46         } else {
47             assertSelected(id);
48             mSelected.remove(id);
49         }
50     }
51 
52     @Override
onSelectionReset()53     public void onSelectionReset() {
54         mSelectionReset = true;
55         mSelected.clear();
56     }
57 
58     @Override
onSelectionChanged()59     public void onSelectionChanged() {
60         mSelectionChanged = true;
61     }
62 
63     @Override
onSelectionRestored()64     public void onSelectionRestored() {
65         mSelectionRestored = true;
66     }
67 
assertNoSelection()68     void assertNoSelection() {
69         assertTrue(mSelected.isEmpty());
70     }
71 
assertSelectionSize(int expected)72     void assertSelectionSize(int expected) {
73         assertEquals(expected, mSelected.size());
74     }
75 
assertSelected(String id)76     void assertSelected(String id) {
77         assertTrue(id + " is not selected.", mSelected.contains(id));
78     }
79 
assertNotSelected(String id)80     void assertNotSelected(String id) {
81         assertFalse(id + " is already selected", mSelected.contains(id));
82     }
83 
assertSelectionChanged()84     public void assertSelectionChanged() {
85         assertTrue(mSelectionChanged);
86     }
87 
assertSelectionUnchanged()88     public void assertSelectionUnchanged() {
89         assertFalse(mSelectionChanged);
90     }
91 
assertSelectionReset()92     public void assertSelectionReset() {
93         assertTrue(mSelectionReset);
94     }
95 
assertSelectionRestored()96     public void assertSelectionRestored() {
97         assertTrue(mSelectionRestored);
98     }
99 }
100