1 /* 2 * Copyright (C) 2008 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.cts; 18 19 import android.widget.cts.R; 20 21 import android.app.Activity; 22 import android.content.Context; 23 import android.content.res.ColorStateList; 24 import android.cts.util.PollingCheck; 25 import android.graphics.Canvas; 26 import android.graphics.Color; 27 import android.graphics.ColorFilter; 28 import android.graphics.PorterDuff; 29 import android.graphics.drawable.Drawable; 30 import android.test.ActivityInstrumentationTestCase2; 31 import android.test.UiThreadTest; 32 import android.util.AttributeSet; 33 import android.view.KeyEvent; 34 import android.widget.AbsSeekBar; 35 import android.widget.SeekBar; 36 37 /** 38 * Test {@link AbsSeekBar}. 39 */ 40 public class AbsSeekBarTest extends ActivityInstrumentationTestCase2<ProgressBarCtsActivity> { AbsSeekBarTest()41 public AbsSeekBarTest() { 42 super("android.widget.cts", ProgressBarCtsActivity.class); 43 } 44 45 private Activity mActivity; 46 47 @Override setUp()48 protected void setUp() throws Exception { 49 super.setUp(); 50 51 mActivity = getActivity(); 52 } 53 testConstructor()54 public void testConstructor() { 55 new MyAbsSeekBar(mActivity); 56 57 new MyAbsSeekBar(mActivity, null); 58 59 new MyAbsSeekBar(mActivity, null, android.R.attr.progressBarStyle); 60 } 61 testAccessThumbOffset()62 public void testAccessThumbOffset() { 63 AbsSeekBar myAbsSeekBar = new MyAbsSeekBar(mActivity); 64 final int positive = 5; 65 final int negative = -5; 66 final int zero = 0; 67 68 myAbsSeekBar.setThumbOffset(positive); 69 assertEquals(positive, myAbsSeekBar.getThumbOffset()); 70 71 myAbsSeekBar.setThumbOffset(zero); 72 assertEquals(zero, myAbsSeekBar.getThumbOffset()); 73 74 myAbsSeekBar.setThumbOffset(negative); 75 assertEquals(negative, myAbsSeekBar.getThumbOffset()); 76 } 77 testSetThumb()78 public void testSetThumb() { 79 MyAbsSeekBar myAbsSeekBar = new MyAbsSeekBar(mActivity); 80 Drawable drawable1 = mActivity.getDrawable(R.drawable.scenery); 81 Drawable drawable2 = mActivity.getDrawable(R.drawable.pass); 82 83 assertFalse(myAbsSeekBar.verifyDrawable(drawable1)); 84 assertFalse(myAbsSeekBar.verifyDrawable(drawable2)); 85 86 myAbsSeekBar.setThumb(drawable1); 87 assertSame(drawable1, myAbsSeekBar.getThumb()); 88 assertTrue(myAbsSeekBar.verifyDrawable(drawable1)); 89 assertFalse(myAbsSeekBar.verifyDrawable(drawable2)); 90 91 myAbsSeekBar.setThumb(drawable2); 92 assertSame(drawable2, myAbsSeekBar.getThumb()); 93 assertFalse(myAbsSeekBar.verifyDrawable(drawable1)); 94 assertTrue(myAbsSeekBar.verifyDrawable(drawable2)); 95 } 96 testSetTickMark()97 public void testSetTickMark() { 98 MyAbsSeekBar myAbsSeekBar = new MyAbsSeekBar(mActivity); 99 Drawable drawable1 = mActivity.getDrawable(R.drawable.black); 100 Drawable drawable2 = mActivity.getDrawable(R.drawable.black); 101 102 assertFalse(myAbsSeekBar.verifyDrawable(drawable1)); 103 assertFalse(myAbsSeekBar.verifyDrawable(drawable2)); 104 105 myAbsSeekBar.setTickMark(drawable1); 106 assertSame(drawable1, myAbsSeekBar.getTickMark()); 107 assertTrue(myAbsSeekBar.verifyDrawable(drawable1)); 108 assertFalse(myAbsSeekBar.verifyDrawable(drawable2)); 109 110 myAbsSeekBar.setTickMark(drawable2); 111 assertSame(drawable2, myAbsSeekBar.getTickMark()); 112 assertFalse(myAbsSeekBar.verifyDrawable(drawable1)); 113 assertTrue(myAbsSeekBar.verifyDrawable(drawable2)); 114 } 115 testDrawableStateChanged()116 public void testDrawableStateChanged() { 117 MyAbsSeekBar myAbsSeekBar = new MyAbsSeekBar(mActivity); 118 MockDrawable drawable = new MockDrawable(); 119 myAbsSeekBar.setProgressDrawable(drawable); 120 121 myAbsSeekBar.setEnabled(false); 122 myAbsSeekBar.drawableStateChanged(); 123 assertEquals(0, drawable.getAlpha()); 124 125 myAbsSeekBar.setEnabled(true); 126 myAbsSeekBar.drawableStateChanged(); 127 assertEquals(0xFF, drawable.getAlpha()); 128 } 129 testVerifyDrawable()130 public void testVerifyDrawable() { 131 MyAbsSeekBar myAbsSeekBar = new MyAbsSeekBar(mActivity); 132 Drawable drawable1 = mActivity.getDrawable(R.drawable.scenery); 133 Drawable drawable2 = mActivity.getDrawable(R.drawable.pass); 134 Drawable drawable3 = mActivity.getDrawable(R.drawable.blue); 135 Drawable drawable4 = mActivity.getDrawable(R.drawable.black); 136 137 assertFalse(myAbsSeekBar.verifyDrawable(drawable1)); 138 assertFalse(myAbsSeekBar.verifyDrawable(drawable2)); 139 assertFalse(myAbsSeekBar.verifyDrawable(drawable3)); 140 assertFalse(myAbsSeekBar.verifyDrawable(drawable4)); 141 142 myAbsSeekBar.setThumb(drawable1); 143 assertTrue(myAbsSeekBar.verifyDrawable(drawable1)); 144 assertFalse(myAbsSeekBar.verifyDrawable(drawable2)); 145 assertFalse(myAbsSeekBar.verifyDrawable(drawable3)); 146 assertFalse(myAbsSeekBar.verifyDrawable(drawable4)); 147 148 myAbsSeekBar.setThumb(drawable2); 149 assertFalse(myAbsSeekBar.verifyDrawable(drawable1)); 150 assertTrue(myAbsSeekBar.verifyDrawable(drawable2)); 151 assertFalse(myAbsSeekBar.verifyDrawable(drawable3)); 152 assertFalse(myAbsSeekBar.verifyDrawable(drawable4)); 153 154 myAbsSeekBar.setBackgroundDrawable(drawable2); 155 myAbsSeekBar.setProgressDrawable(drawable3); 156 myAbsSeekBar.setIndeterminateDrawable(drawable4); 157 assertFalse(myAbsSeekBar.verifyDrawable(drawable1)); 158 assertTrue(myAbsSeekBar.verifyDrawable(drawable2)); 159 assertTrue(myAbsSeekBar.verifyDrawable(drawable3)); 160 assertTrue(myAbsSeekBar.verifyDrawable(drawable4)); 161 } 162 testAccessKeyProgressIncrement()163 public void testAccessKeyProgressIncrement() throws Throwable { 164 // AbsSeekBar is an abstract class, use its subclass: SeekBar to do this test. 165 runTestOnUiThread(new Runnable() { 166 public void run() { 167 mActivity.setContentView(R.layout.seekbar_layout); 168 } 169 }); 170 getInstrumentation().waitForIdleSync(); 171 172 final SeekBar seekBar = (SeekBar) mActivity.findViewById(R.id.seekBar); 173 final int keyProgressIncrement = 2; 174 runTestOnUiThread(new Runnable() { 175 public void run() { 176 seekBar.setKeyProgressIncrement(keyProgressIncrement); 177 seekBar.setFocusable(true); 178 seekBar.requestFocus(); 179 } 180 }); 181 new PollingCheck(1000) { 182 @Override 183 protected boolean check() { 184 return seekBar.hasWindowFocus(); 185 } 186 }.run(); 187 assertEquals(keyProgressIncrement, seekBar.getKeyProgressIncrement()); 188 189 int oldProgress = seekBar.getProgress(); 190 KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT); 191 getInstrumentation().sendKeySync(keyEvent); 192 assertEquals(oldProgress + keyProgressIncrement, seekBar.getProgress()); 193 oldProgress = seekBar.getProgress(); 194 keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT); 195 getInstrumentation().sendKeySync(keyEvent); 196 assertEquals(oldProgress - keyProgressIncrement, seekBar.getProgress()); 197 } 198 testSetMax()199 public void testSetMax() { 200 MyAbsSeekBar myAbsSeekBar = new MyAbsSeekBar(mActivity, null, R.style.TestProgressBar); 201 202 int progress = 10; 203 myAbsSeekBar.setProgress(progress); 204 int max = progress + 1; 205 myAbsSeekBar.setMax(max); 206 assertEquals(max, myAbsSeekBar.getMax()); 207 assertEquals(progress, myAbsSeekBar.getProgress()); 208 assertEquals(1, myAbsSeekBar.getKeyProgressIncrement()); 209 210 max = progress - 1; 211 myAbsSeekBar.setMax(max); 212 assertEquals(max, myAbsSeekBar.getMax()); 213 assertEquals(max, myAbsSeekBar.getProgress()); 214 assertEquals(1, myAbsSeekBar.getKeyProgressIncrement()); 215 216 int keyProgressIncrement = 10; 217 myAbsSeekBar.setKeyProgressIncrement(keyProgressIncrement); 218 assertEquals(keyProgressIncrement, myAbsSeekBar.getKeyProgressIncrement()); 219 max = (keyProgressIncrement - 1) * 20; 220 myAbsSeekBar.setMax(max); 221 assertEquals(keyProgressIncrement, myAbsSeekBar.getKeyProgressIncrement()); 222 max = (keyProgressIncrement + 1) * 20; 223 myAbsSeekBar.setMax(max); 224 assertEquals(keyProgressIncrement + 1, myAbsSeekBar.getKeyProgressIncrement()); 225 } 226 227 @UiThreadTest testThumbTint()228 public void testThumbTint() { 229 mActivity.setContentView(R.layout.seekbar_layout); 230 231 SeekBar inflatedView = (SeekBar) mActivity.findViewById(R.id.thumb_tint); 232 233 assertEquals("Thumb tint inflated correctly", 234 Color.WHITE, inflatedView.getThumbTintList().getDefaultColor()); 235 assertEquals("Thumb tint mode inflated correctly", 236 PorterDuff.Mode.SRC_OVER, inflatedView.getThumbTintMode()); 237 238 MockDrawable thumb = new MockDrawable(); 239 SeekBar view = new SeekBar(mActivity); 240 241 view.setThumb(thumb); 242 assertFalse("No thumb tint applied by default", thumb.hasCalledSetTint()); 243 244 view.setThumbTintList(ColorStateList.valueOf(Color.WHITE)); 245 assertTrue("Thumb tint applied when setThumbTintList() called after setThumb()", 246 thumb.hasCalledSetTint()); 247 248 thumb.reset(); 249 view.setThumb(null); 250 view.setThumb(thumb); 251 assertTrue("Thumb tint applied when setThumbTintList() called before setThumb()", 252 thumb.hasCalledSetTint()); 253 } 254 255 @UiThreadTest testTickMarkTint()256 public void testTickMarkTint() { 257 mActivity.setContentView(R.layout.seekbar_layout); 258 259 SeekBar inflatedView = (SeekBar) mActivity.findViewById(R.id.tick_mark_tint); 260 261 assertEquals("TickMark tint inflated correctly", 262 Color.WHITE, inflatedView.getTickMarkTintList().getDefaultColor()); 263 assertEquals("TickMark tint mode inflated correctly", 264 PorterDuff.Mode.SRC_OVER, inflatedView.getTickMarkTintMode()); 265 266 MockDrawable tickMark = new MockDrawable(); 267 SeekBar view = new SeekBar(mActivity); 268 269 view.setTickMark(tickMark); 270 assertFalse("No tickMark tint applied by default", tickMark.hasCalledSetTint()); 271 272 view.setTickMarkTintList(ColorStateList.valueOf(Color.WHITE)); 273 assertTrue("TickMark tint applied when setTickMarkTintList() called after setTickMark()", 274 tickMark.hasCalledSetTint()); 275 276 tickMark.reset(); 277 view.setTickMark(null); 278 view.setTickMark(tickMark); 279 assertTrue("TickMark tint applied when setTickMarkTintList() called before setTickMark()", 280 tickMark.hasCalledSetTint()); 281 } 282 283 private static class MyAbsSeekBar extends AbsSeekBar { MyAbsSeekBar(Context context)284 public MyAbsSeekBar(Context context) { 285 super(context); 286 } 287 MyAbsSeekBar(Context context, AttributeSet attrs)288 public MyAbsSeekBar(Context context, AttributeSet attrs) { 289 super(context, attrs); 290 } 291 MyAbsSeekBar(Context context, AttributeSet attrs, int defStyle)292 public MyAbsSeekBar(Context context, AttributeSet attrs, int defStyle) { 293 super(context, attrs, defStyle); 294 } 295 296 @Override drawableStateChanged()297 protected void drawableStateChanged() { 298 super.drawableStateChanged(); 299 } 300 301 @Override verifyDrawable(Drawable who)302 protected boolean verifyDrawable(Drawable who) { 303 return super.verifyDrawable(who); 304 } 305 } 306 307 private static class MockDrawable extends Drawable { 308 private int mAlpha; 309 private boolean mCalledDraw = false; 310 private boolean mCalledSetTint = false; 311 312 @Override draw(Canvas canvas)313 public void draw(Canvas canvas) { } 314 reset()315 public void reset() { 316 mCalledDraw = false; 317 mCalledSetTint = false; 318 } 319 320 @Override getOpacity()321 public int getOpacity() { 322 return 0; 323 } 324 325 @Override setAlpha(int alpha)326 public void setAlpha(int alpha) { 327 mAlpha = alpha; 328 } 329 getAlpha()330 public int getAlpha() { 331 return mAlpha; 332 } 333 334 @Override setColorFilter(ColorFilter cf)335 public void setColorFilter(ColorFilter cf) { } 336 337 @Override setTintList(ColorStateList tint)338 public void setTintList(ColorStateList tint) { 339 super.setTintList(tint); 340 mCalledSetTint = true; 341 } 342 hasCalledSetTint()343 public boolean hasCalledSetTint() { 344 return mCalledSetTint; 345 } 346 } 347 } 348