1 /* 2 * Copyright (C) 2019 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 android.widget; 18 19 import static android.view.View.MeasureSpec.EXACTLY; 20 import static android.view.View.MeasureSpec.makeMeasureSpec; 21 22 import static org.hamcrest.Matchers.hasItem; 23 import static org.hamcrest.Matchers.hasSize; 24 import static org.junit.Assert.assertEquals; 25 import static org.junit.Assert.assertThat; 26 27 import android.content.Context; 28 import android.graphics.Point; 29 import android.graphics.Rect; 30 import android.graphics.drawable.ShapeDrawable; 31 import android.graphics.drawable.shapes.RectShape; 32 import android.platform.test.annotations.Presubmit; 33 34 import androidx.test.ext.junit.runners.AndroidJUnit4; 35 import androidx.test.filters.SmallTest; 36 import androidx.test.platform.app.InstrumentationRegistry; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 import java.util.Arrays; 43 import java.util.List; 44 45 @RunWith(AndroidJUnit4.class) 46 @SmallTest 47 @Presubmit 48 public class AbsSeekBarTest { 49 50 public static final int PADDING = 10; 51 private Context mContext; 52 private AbsSeekBar mBar; 53 54 @Before setUp()55 public void setUp() throws Exception { 56 mContext = InstrumentationRegistry.getInstrumentation().getContext(); 57 mBar = new SeekBar(mContext); 58 } 59 60 @Test testExclusionForThumb_limitedTo48dp()61 public void testExclusionForThumb_limitedTo48dp() { 62 mBar.setPadding(PADDING, PADDING, PADDING, PADDING); 63 mBar.setThumb(newThumb(dpToPxSize(20))); 64 mBar.setMin(0); 65 mBar.setMax(100); 66 mBar.setProgress(50); 67 68 final int thumbOffset = mBar.getThumbOffset(); 69 70 measureAndLayout(dpToPxSize(200), dpToPxSize(100)); 71 List<Rect> exclusions = mBar.getSystemGestureExclusionRects(); 72 73 assertEquals("exclusions should be size 1, but was " + exclusions, 1, exclusions.size()); 74 assertEquals("exclusion should be centered on thumb", 75 center(offset(mBar.getThumb().getBounds(), PADDING - thumbOffset, PADDING)), 76 center(exclusions.get(0))); 77 assertEquals("exclusion should be 48dp high", dpToPxSize(48), exclusions.get(0).height()); 78 assertEquals("exclusion should be 48dp wide", dpToPxSize(48), exclusions.get(0).width()); 79 } 80 81 @Test testExclusionForThumb_limitedToHeight()82 public void testExclusionForThumb_limitedToHeight() { 83 mBar.setPadding(PADDING, PADDING, PADDING, PADDING); 84 mBar.setThumb(newThumb(dpToPxSize(20))); 85 mBar.setMin(0); 86 mBar.setMax(100); 87 mBar.setProgress(50); 88 89 final int thumbOffset = mBar.getThumbOffset(); 90 91 measureAndLayout(dpToPxSize(200), dpToPxSize(32)); 92 List<Rect> exclusions = mBar.getSystemGestureExclusionRects(); 93 94 assertEquals("exclusions should be size 1, but was " + exclusions, 1, exclusions.size()); 95 assertEquals("exclusion should be centered on thumb", 96 center(offset(mBar.getThumb().getBounds(), PADDING - thumbOffset, PADDING)), 97 center(exclusions.get(0))); 98 assertEquals("exclusion should be 32dp high", dpToPxSize(32), exclusions.get(0).height()); 99 assertEquals("exclusion should be 32dp wide", dpToPxSize(32), exclusions.get(0).width()); 100 } 101 102 @Test testExclusionForThumb_passesThroughUserExclusions()103 public void testExclusionForThumb_passesThroughUserExclusions() { 104 mBar.setSystemGestureExclusionRects(Arrays.asList(new Rect(1, 2, 3, 4))); 105 106 mBar.setPadding(PADDING, PADDING, PADDING, PADDING); 107 mBar.setThumb(newThumb(dpToPxSize(20))); 108 mBar.setMin(0); 109 mBar.setMax(100); 110 mBar.setProgress(50); 111 measureAndLayout(dpToPxSize(200), dpToPxSize(32)); 112 113 assertThat(mBar.getSystemGestureExclusionRects(), hasItem(new Rect(1, 2, 3, 4))); 114 assertThat(mBar.getSystemGestureExclusionRects(), hasSize(2)); 115 116 mBar.setSystemGestureExclusionRects(Arrays.asList(new Rect(3, 4, 5, 6))); 117 assertThat(mBar.getSystemGestureExclusionRects(), hasItem(new Rect(3, 4, 5, 6))); 118 assertThat(mBar.getSystemGestureExclusionRects(), hasSize(2)); 119 } 120 121 @Test testGrowRectTo_evenInitialDifference()122 public void testGrowRectTo_evenInitialDifference() { 123 doGrowRectTest(new Rect(0, 0, 0, 0), 10, new Rect(-5, -5, 5, 5)); 124 } 125 126 @Test testGrowRectTo_unevenInitialDifference()127 public void testGrowRectTo_unevenInitialDifference() { 128 doGrowRectTest(new Rect(0, 0, 1, 1), 10, new Rect(-5, -5, 5, 5)); 129 } 130 131 @Test testGrowRectTo_unevenInitialDifference_unevenSize()132 public void testGrowRectTo_unevenInitialDifference_unevenSize() { 133 doGrowRectTest(new Rect(0, 0, 0, 0), 9, new Rect(-5, -5, 4, 4)); 134 } 135 doGrowRectTest(Rect in, int minimumSize, Rect expected)136 public void doGrowRectTest(Rect in, int minimumSize, Rect expected) { 137 Rect result = new Rect(in); 138 mBar.growRectTo(result, minimumSize); 139 140 assertEquals("grown rect", expected, result); 141 assertEquals("grown rect center point", center(expected), center(result)); 142 } 143 center(Rect rect)144 private Point center(Rect rect) { 145 return new Point(rect.centerX(), rect.centerY()); 146 } 147 offset(Rect rect, int dx, int dy)148 private Rect offset(Rect rect, int dx, int dy) { 149 Rect result = new Rect(rect); 150 result.offset(dx, dy); 151 return result; 152 } 153 newThumb(int size)154 private ShapeDrawable newThumb(int size) { 155 final ShapeDrawable thumb = new ShapeDrawable(new RectShape()); 156 thumb.setIntrinsicWidth(size); 157 thumb.setIntrinsicHeight(size); 158 return thumb; 159 } 160 measureAndLayout(int wPx, int hPx)161 private void measureAndLayout(int wPx, int hPx) { 162 mBar.measure(makeMeasureSpec(wPx, EXACTLY), makeMeasureSpec(hPx, EXACTLY)); 163 mBar.layout(0, 0, wPx, hPx); 164 } 165 dpToPxSize(int dp)166 private int dpToPxSize(int dp) { 167 return (int) (mContext.getResources().getDisplayMetrics().density * dp + 0.5f); 168 } 169 } 170