SelectionProbe.java revision b8874fd0df55113d472c6704b91bd493c577caeb
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
17package com.android.documentsui.selection;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
22
23import com.android.documentsui.selection.SelectionManager;
24import com.android.documentsui.selection.Selection;
25
26/**
27 * Helper class for making assertions against the state of a {@link SelectionManager} instance and
28 * the consistency of states between {@link SelectionManager} and
29 * {@link SelectionManager.ItemCallback}.
30 */
31public final class SelectionProbe {
32
33    private final SelectionManager mMgr;
34    private final TestItemSelectionListener mTestCallback;
35
36    public SelectionProbe(SelectionManager mgr) {
37        mMgr = mgr;
38        mTestCallback = new TestItemSelectionListener();
39
40        mMgr.addItemCallback(mTestCallback);
41    }
42
43    public SelectionProbe(SelectionManager mgr, TestItemSelectionListener testCallback) {
44        mMgr = mgr;
45        mTestCallback = testCallback;
46    }
47
48    public void assertRangeSelected(int begin, int end) {
49        for (int i = begin; i <= end; i++) {
50            assertSelected(i);
51        }
52    }
53
54    public void assertRangeNotSelected(int begin, int end) {
55        for (int i = begin; i <= end; i++) {
56            assertNotSelected(i);
57        }
58    }
59
60    public void assertRangeSelection(int begin, int end) {
61        assertSelectionSize(end - begin + 1);
62        assertRangeSelected(begin, end);
63    }
64
65    public void assertSelectionSize(int expected) {
66        Selection selection = mMgr.getSelection();
67        assertEquals(selection.toString(), expected, selection.size());
68
69        mTestCallback.assertSelectionSize(expected);
70    }
71
72    public void assertNoSelection() {
73        assertSelectionSize(0);
74
75        mTestCallback.assertNoSelection();
76    }
77
78    public void assertSelection(int... ids) {
79        assertSelected(ids);
80        assertEquals(ids.length, mMgr.getSelection().size());
81
82        mTestCallback.assertSelectionSize(ids.length);
83    }
84
85    public void assertSelected(int... ids) {
86        Selection sel = mMgr.getSelection();
87        for (int id : ids) {
88            String sid = String.valueOf(id);
89            assertTrue(sid + " is not in selection " + sel, sel.contains(sid));
90
91            mTestCallback.assertSelected(sid);
92        }
93    }
94
95    public void assertNotSelected(int... ids) {
96        Selection sel = mMgr.getSelection();
97        for (int id : ids) {
98            String sid = String.valueOf(id);
99            assertFalse(sid + " is in selection " + sel, sel.contains(sid));
100
101            mTestCallback.assertNotSelected(sid);
102        }
103    }
104
105    public void select(int...positions) {
106        for (int position : positions) {
107            mMgr.toggleSelection(String.valueOf(position));
108        }
109    }
110}
111