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.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.DefaultSelectionHelper; 24 import com.android.documentsui.selection.Selection; 25 import com.android.documentsui.selection.SelectionHelper; 26 27 /** 28 * Helper class for making assertions against the state of a {@link DefaultSelectionHelper} instance 29 * and the consistency of states between {@link DefaultSelectionHelper} and 30 * {@link DefaultSelectionHelper.ItemEventCallback}. 31 */ 32 public final class SelectionProbe { 33 34 private final SelectionHelper mMgr; 35 private final TestSelectionObserver mSelectionListener; 36 SelectionProbe(SelectionHelper mgr)37 public SelectionProbe(SelectionHelper mgr) { 38 mMgr = mgr; 39 mSelectionListener = new TestSelectionObserver(); 40 mMgr.addObserver(mSelectionListener); 41 } 42 SelectionProbe(SelectionHelper mgr, TestSelectionObserver selectionListener)43 public SelectionProbe(SelectionHelper mgr, TestSelectionObserver selectionListener) { 44 mMgr = mgr; 45 mSelectionListener = selectionListener; 46 } 47 assertRangeSelected(int begin, int end)48 public void assertRangeSelected(int begin, int end) { 49 for (int i = begin; i <= end; i++) { 50 assertSelected(i); 51 } 52 } 53 assertRangeNotSelected(int begin, int end)54 public void assertRangeNotSelected(int begin, int end) { 55 for (int i = begin; i <= end; i++) { 56 assertNotSelected(i); 57 } 58 } 59 assertRangeSelection(int begin, int end)60 public void assertRangeSelection(int begin, int end) { 61 assertSelectionSize(end - begin + 1); 62 assertRangeSelected(begin, end); 63 } 64 assertSelectionSize(int expected)65 public void assertSelectionSize(int expected) { 66 Selection selection = mMgr.getSelection(); 67 assertEquals(selection.toString(), expected, selection.size()); 68 69 mSelectionListener.assertSelectionSize(expected); 70 } 71 assertNoSelection()72 public void assertNoSelection() { 73 assertSelectionSize(0); 74 75 mSelectionListener.assertNoSelection(); 76 } 77 assertSelection(int... ids)78 public void assertSelection(int... ids) { 79 assertSelected(ids); 80 assertEquals(ids.length, mMgr.getSelection().size()); 81 82 mSelectionListener.assertSelectionSize(ids.length); 83 } 84 assertSelected(int... ids)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 mSelectionListener.assertSelected(sid); 92 } 93 } 94 assertNotSelected(int... ids)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 mSelectionListener.assertNotSelected(sid); 102 } 103 } 104 select(int...positions)105 public void select(int...positions) { 106 for (int position : positions) { 107 mMgr.select(String.valueOf(position)); 108 } 109 } 110 } 111