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.content.res.ColorStateList; 20 import android.graphics.Color; 21 import android.graphics.PorterDuff; 22 import android.view.LayoutInflater; 23 24 import android.widget.cts.R; 25 26 27 import android.content.Context; 28 import android.graphics.Canvas; 29 import android.graphics.ColorFilter; 30 import android.graphics.drawable.Drawable; 31 import android.os.Parcelable; 32 import android.test.InstrumentationTestCase; 33 import android.view.View; 34 import android.view.animation.AccelerateDecelerateInterpolator; 35 import android.view.animation.Interpolator; 36 import android.view.animation.LinearInterpolator; 37 import android.widget.ProgressBar; 38 39 public class ProgressBarTest extends InstrumentationTestCase { 40 // The target context. 41 private Context mContext; 42 43 @Override 44 protected void setUp() throws Exception { 45 super.setUp(); 46 mContext = getInstrumentation().getTargetContext(); 47 } 48 49 public void testConstructor() { 50 new ProgressBar(mContext); 51 52 new ProgressBar(mContext, null); 53 54 new ProgressBar(mContext, null, android.R.attr.progressBarStyle); 55 } 56 57 public void testSetIndeterminate() { 58 ProgressBar progressBar = new ProgressBar(mContext); 59 assertTrue(progressBar.isIndeterminate()); 60 61 progressBar.setIndeterminate(true); 62 assertTrue(progressBar.isIndeterminate()); 63 64 progressBar.setIndeterminate(false); 65 // because default is Indeterminate only progressBar, can't change the status 66 assertTrue(progressBar.isIndeterminate()); 67 68 progressBar = new ProgressBar(mContext, null, android.R.attr.progressBarStyleHorizontal); 69 assertFalse(progressBar.isIndeterminate()); 70 71 progressBar.setIndeterminate(true); 72 assertTrue(progressBar.isIndeterminate()); 73 74 progressBar.setIndeterminate(false); 75 assertFalse(progressBar.isIndeterminate()); 76 } 77 78 public void testAccessIndeterminateDrawable() { 79 ProgressBar progressBar = new ProgressBar(mContext); 80 81 // set IndeterminateDrawable 82 // normal value 83 MockDrawable mockDrawable = new MockDrawable(); 84 progressBar.setIndeterminateDrawable(mockDrawable); 85 assertSame(mockDrawable, progressBar.getIndeterminateDrawable()); 86 assertFalse(mockDrawable.hasCalledDraw()); 87 progressBar.draw(new Canvas()); 88 assertTrue(mockDrawable.hasCalledDraw()); 89 90 // exceptional value 91 progressBar.setIndeterminateDrawable(null); 92 assertNull(progressBar.getIndeterminateDrawable()); 93 } 94 95 public void testAccessProgressDrawable() { 96 ProgressBar progressBar = new ProgressBar(mContext, null, 97 android.R.attr.progressBarStyleHorizontal); 98 99 // set ProgressDrawable 100 // normal value 101 MockDrawable mockDrawable = new MockDrawable(); 102 progressBar.setProgressDrawable(mockDrawable); 103 assertSame(mockDrawable, progressBar.getProgressDrawable()); 104 assertFalse(mockDrawable.hasCalledDraw()); 105 progressBar.draw(new Canvas()); 106 assertTrue(mockDrawable.hasCalledDraw()); 107 108 // exceptional value 109 progressBar.setProgressDrawable(null); 110 assertNull(progressBar.getProgressDrawable()); 111 } 112 113 public void testAccessProgress() { 114 ProgressBar progressBar = new ProgressBar(mContext, null, 115 android.R.attr.progressBarStyleHorizontal); 116 assertEquals(0, progressBar.getProgress()); 117 118 final int maxProgress = progressBar.getMax(); 119 // set Progress 120 // normal value 121 progressBar.setProgress(maxProgress >> 1); 122 assertEquals(maxProgress >> 1, progressBar.getProgress()); 123 124 // exceptional values 125 progressBar.setProgress(-1); 126 assertEquals(0, progressBar.getProgress()); 127 128 progressBar.setProgress(maxProgress + 1); 129 assertEquals(maxProgress, progressBar.getProgress()); 130 131 progressBar.setProgress(Integer.MAX_VALUE); 132 assertEquals(maxProgress, progressBar.getProgress()); 133 134 // when in indeterminate mode 135 progressBar.setIndeterminate(true); 136 progressBar.setProgress(maxProgress >> 1); 137 assertEquals(0, progressBar.getProgress()); 138 } 139 140 public void testAccessSecondaryProgress() { 141 ProgressBar progressBar = new ProgressBar(mContext, null, 142 android.R.attr.progressBarStyleHorizontal); 143 assertEquals(0, progressBar.getSecondaryProgress()); 144 145 final int maxProgress = progressBar.getMax(); 146 // set SecondaryProgress 147 // normal value 148 progressBar.setSecondaryProgress(maxProgress >> 1); 149 assertEquals(maxProgress >> 1, progressBar.getSecondaryProgress()); 150 151 // exceptional value 152 progressBar.setSecondaryProgress(-1); 153 assertEquals(0, progressBar.getSecondaryProgress()); 154 155 progressBar.setSecondaryProgress(maxProgress + 1); 156 assertEquals(maxProgress, progressBar.getSecondaryProgress()); 157 158 progressBar.setSecondaryProgress(Integer.MAX_VALUE); 159 assertEquals(maxProgress, progressBar.getSecondaryProgress()); 160 161 // when in indeterminate mode 162 progressBar.setIndeterminate(true); 163 progressBar.setSecondaryProgress(maxProgress >> 1); 164 assertEquals(0, progressBar.getSecondaryProgress()); 165 } 166 167 public void testIncrementProgressBy() { 168 ProgressBar progressBar = new ProgressBar(mContext, null, 169 android.R.attr.progressBarStyleHorizontal); 170 171 // normal value 172 int increment = 1; 173 int oldProgress = progressBar.getProgress(); 174 progressBar.incrementProgressBy(increment); 175 assertEquals(oldProgress + increment, progressBar.getProgress()); 176 177 increment = progressBar.getMax() >> 1; 178 oldProgress = progressBar.getProgress(); 179 progressBar.incrementProgressBy(increment); 180 assertEquals(oldProgress + increment, progressBar.getProgress()); 181 182 // exceptional values 183 progressBar.setProgress(0); 184 progressBar.incrementProgressBy(Integer.MAX_VALUE); 185 assertEquals(progressBar.getMax(), progressBar.getProgress()); 186 187 progressBar.setProgress(0); 188 progressBar.incrementProgressBy(Integer.MIN_VALUE); 189 assertEquals(0, progressBar.getProgress()); 190 } 191 192 public void testIncrementSecondaryProgressBy() { 193 ProgressBar progressBar = new ProgressBar(mContext, null, 194 android.R.attr.progressBarStyleHorizontal); 195 196 // normal value 197 int increment = 1; 198 int oldSecondaryProgress = progressBar.getSecondaryProgress(); 199 progressBar.incrementSecondaryProgressBy(increment); 200 assertEquals(oldSecondaryProgress + increment, progressBar.getSecondaryProgress()); 201 202 increment = progressBar.getMax() >> 1; 203 oldSecondaryProgress = progressBar.getSecondaryProgress(); 204 progressBar.incrementSecondaryProgressBy(increment); 205 assertEquals(oldSecondaryProgress + increment, progressBar.getSecondaryProgress()); 206 207 // exceptional values 208 progressBar.setSecondaryProgress(0); 209 progressBar.incrementSecondaryProgressBy(Integer.MAX_VALUE); 210 assertEquals(progressBar.getMax(), progressBar.getSecondaryProgress()); 211 212 progressBar.setSecondaryProgress(0); 213 progressBar.incrementSecondaryProgressBy(Integer.MIN_VALUE); 214 assertEquals(0, progressBar.getSecondaryProgress()); 215 } 216 217 public void testAccessInterpolator() { 218 ProgressBar progressBar = new ProgressBar(mContext); 219 220 // default should be LinearInterpolator 221 assertTrue(progressBar.getInterpolator() instanceof LinearInterpolator); 222 223 // normal value 224 Interpolator i = new AccelerateDecelerateInterpolator(); 225 progressBar.setInterpolator(i); 226 assertEquals(i, progressBar.getInterpolator()); 227 } 228 229 public void testSetVisibility() { 230 ProgressBar progressBar = new ProgressBar(mContext, null, 231 android.R.attr.progressBarStyleHorizontal); 232 233 // set visibility 234 // normal value 235 int visibility = View.VISIBLE; 236 progressBar.setVisibility(visibility); 237 assertEquals(visibility, progressBar.getVisibility()); 238 239 visibility = View.GONE; 240 progressBar.setVisibility(visibility); 241 assertEquals(visibility, progressBar.getVisibility()); 242 243 // exceptional value 244 visibility = 0xfffffff5; // -11 245 int mask = 0x0000000C; // View.VISIBILITY_MASK 246 int expected = (progressBar.getVisibility() & ~mask) | (visibility & mask); 247 progressBar.setVisibility(visibility); 248 assertEquals(expected, progressBar.getVisibility()); 249 250 visibility = 0x7fffffff; // Integer.MAX_VALUE; 251 expected = (progressBar.getVisibility() & ~mask) | (visibility & mask); 252 progressBar.setVisibility(Integer.MAX_VALUE); 253 assertEquals(expected, progressBar.getVisibility()); 254 } 255 256 public void testInvalidateDrawable() { 257 MockProgressBar mockProgressBar = new MockProgressBar(mContext); 258 259 MockDrawable mockDrawable1 = new MockDrawable(); 260 MockDrawable mockDrawable2 = new MockDrawable(); 261 mockProgressBar.setBackgroundDrawable(mockDrawable1); 262 263 mockProgressBar.invalidateDrawable(mockDrawable1); 264 assertTrue(mockProgressBar.hasCalledInvalidate()); 265 266 mockProgressBar.reset(); 267 mockProgressBar.invalidateDrawable(mockDrawable2); 268 assertFalse(mockProgressBar.hasCalledInvalidate()); 269 270 mockProgressBar.setIndeterminateDrawable(mockDrawable1); 271 mockProgressBar.setProgressDrawable(mockDrawable2); 272 } 273 274 public void testPostInvalidate() { 275 MockProgressBar mockProgressBar = new MockProgressBar(mContext); 276 mockProgressBar.postInvalidate(); 277 } 278 279 public void testAccessMax() { 280 ProgressBar progressBar = new ProgressBar(mContext, null, 281 android.R.attr.progressBarStyleHorizontal); 282 283 // set Progress 284 int progress = 10; 285 progressBar.setProgress(progress); 286 287 // normal value 288 int max = progress + 1; 289 progressBar.setMax(max); 290 assertEquals(max, progressBar.getMax()); 291 assertEquals(progress, progressBar.getProgress()); 292 293 max = progress - 1; 294 progressBar.setMax(max); 295 assertEquals(max, progressBar.getMax()); 296 assertEquals(max, progressBar.getProgress()); 297 298 // exceptional values 299 progressBar.setMax(-1); 300 assertEquals(0, progressBar.getMax()); 301 assertEquals(0, progressBar.getProgress()); 302 303 progressBar.setMax(Integer.MAX_VALUE); 304 assertEquals(Integer.MAX_VALUE, progressBar.getMax()); 305 assertEquals(0, progressBar.getProgress()); 306 } 307 308 public void testOnDraw() { 309 // Do not test, it's controlled by View. Implementation details 310 } 311 312 public void testProgressTint() { 313 LayoutInflater inflater = LayoutInflater.from(mContext); 314 View layout = inflater.inflate(R.layout.progressbar_layout, null); 315 ProgressBar inflatedView = (ProgressBar) layout.findViewById(R.id.progress_tint); 316 317 assertEquals("Progress tint inflated correctly", 318 Color.WHITE, inflatedView.getProgressTintList().getDefaultColor()); 319 assertEquals("Progress tint mode inflated correctly", 320 PorterDuff.Mode.SRC_OVER, inflatedView.getProgressTintMode()); 321 322 assertEquals("Progress background tint inflated correctly", 323 Color.WHITE, inflatedView.getProgressBackgroundTintList().getDefaultColor()); 324 assertEquals("Progress background tint mode inflated correctly", 325 PorterDuff.Mode.SRC_OVER, inflatedView.getProgressBackgroundTintMode()); 326 327 assertEquals("Secondary progress tint inflated correctly", 328 Color.WHITE, inflatedView.getSecondaryProgressTintList().getDefaultColor()); 329 assertEquals("Secondary progress tint mode inflated correctly", 330 PorterDuff.Mode.SRC_OVER, inflatedView.getSecondaryProgressTintMode()); 331 332 MockDrawable progress = new MockDrawable(); 333 ProgressBar view = new ProgressBar(mContext); 334 335 view.setProgressDrawable(progress); 336 assertFalse("No progress tint applied by default", progress.hasCalledSetTint()); 337 338 view.setProgressBackgroundTintList(ColorStateList.valueOf(Color.WHITE)); 339 assertFalse("Progress background tint not applied when layer missing", 340 progress.hasCalledSetTint()); 341 342 view.setSecondaryProgressTintList(ColorStateList.valueOf(Color.WHITE)); 343 assertFalse("Secondary progress tint not applied when layer missing", 344 progress.hasCalledSetTint()); 345 346 view.setProgressTintList(ColorStateList.valueOf(Color.WHITE)); 347 assertTrue("Progress tint applied when setProgressTintList() called after setProgress()", 348 progress.hasCalledSetTint()); 349 350 progress.reset(); 351 view.setProgressDrawable(null); 352 view.setProgressDrawable(progress); 353 assertTrue("Progress tint applied when setProgressTintList() called before setProgress()", 354 progress.hasCalledSetTint()); 355 } 356 357 public void testIndeterminateTint() { 358 LayoutInflater inflater = LayoutInflater.from(mContext); 359 View layout = inflater.inflate(R.layout.progressbar_layout, null); 360 ProgressBar inflatedView = (ProgressBar) layout.findViewById(R.id.indeterminate_tint); 361 362 assertEquals("Indeterminate tint inflated correctly", 363 Color.WHITE, inflatedView.getIndeterminateTintList().getDefaultColor()); 364 assertEquals("Indeterminate tint mode inflated correctly", 365 PorterDuff.Mode.SRC_OVER, inflatedView.getIndeterminateTintMode()); 366 367 MockDrawable indeterminate = new MockDrawable(); 368 ProgressBar view = new ProgressBar(mContext); 369 370 view.setIndeterminateDrawable(indeterminate); 371 assertFalse("No indeterminate tint applied by default", indeterminate.hasCalledSetTint()); 372 373 view.setIndeterminateTintList(ColorStateList.valueOf(Color.WHITE)); 374 assertTrue("Indeterminate tint applied when setIndeterminateTintList() called after " 375 + "setIndeterminate()", indeterminate.hasCalledSetTint()); 376 377 indeterminate.reset(); 378 view.setIndeterminateDrawable(null); 379 view.setIndeterminateDrawable(indeterminate); 380 assertTrue("Indeterminate tint applied when setIndeterminateTintList() called before " 381 + "setIndeterminate()", indeterminate.hasCalledSetTint()); 382 } 383 384 private class MockDrawable extends Drawable { 385 private boolean mCalledDraw = false; 386 private boolean mCalledSetTint = false; 387 388 @Override 389 public void draw(Canvas canvas) { 390 mCalledDraw = true; 391 } 392 393 @Override 394 public int getOpacity() { 395 return 0; 396 } 397 398 @Override 399 public void setAlpha(int alpha) { 400 } 401 402 @Override 403 public void setColorFilter(ColorFilter cf) { 404 } 405 406 @Override 407 public void setTintList(ColorStateList tint) { 408 super.setTintList(tint); 409 mCalledSetTint = true; 410 } 411 412 public boolean hasCalledSetTint() { 413 return mCalledSetTint; 414 } 415 416 public boolean hasCalledDraw() { 417 return mCalledDraw; 418 } 419 420 public void reset() { 421 mCalledDraw = false; 422 mCalledSetTint = false; 423 } 424 425 } 426 427 public void testOnMeasure() { 428 // onMeasure() is implementation details, do NOT test 429 } 430 431 public void testOnSizeChange() { 432 // onSizeChanged() is implementation details, do NOT test 433 } 434 435 public void testVerifyDrawable() { 436 MockProgressBar mockProgressBar = new MockProgressBar(mContext); 437 assertTrue(mockProgressBar.verifyDrawable(null)); 438 439 Drawable d1 = mContext.getResources().getDrawable(R.drawable.blue); 440 Drawable d2 = mContext.getResources().getDrawable(R.drawable.red); 441 Drawable d3 = mContext.getResources().getDrawable(R.drawable.yellow); 442 443 mockProgressBar.setBackgroundDrawable(d1); 444 assertTrue(mockProgressBar.verifyDrawable(null)); 445 assertTrue(mockProgressBar.verifyDrawable(d1)); 446 assertFalse(mockProgressBar.verifyDrawable(d2)); 447 assertFalse(mockProgressBar.verifyDrawable(d3)); 448 449 mockProgressBar.setIndeterminateDrawable(d2); 450 assertTrue(mockProgressBar.verifyDrawable(null)); 451 assertTrue(mockProgressBar.verifyDrawable(d1)); 452 assertTrue(mockProgressBar.verifyDrawable(d2)); 453 assertFalse(mockProgressBar.verifyDrawable(d3)); 454 455 mockProgressBar.setProgressDrawable(d3); 456 assertFalse(mockProgressBar.verifyDrawable(null)); 457 assertTrue(mockProgressBar.verifyDrawable(d1)); 458 assertTrue(mockProgressBar.verifyDrawable(d2)); 459 assertTrue(mockProgressBar.verifyDrawable(d3)); 460 } 461 462 public void testDrawableStateChanged() { 463 // drawableStateChanged() is implementation details, do NOT test 464 } 465 466 public void testOnSaveAndRestoreInstanceState() { 467 ProgressBar progressBar = new ProgressBar(mContext, null, 468 android.R.attr.progressBarStyleHorizontal); 469 int oldProgress = 1; 470 int oldSecondaryProgress = progressBar.getMax() - 1; 471 progressBar.setProgress(oldProgress); 472 progressBar.setSecondaryProgress(oldSecondaryProgress); 473 assertEquals(oldProgress, progressBar.getProgress()); 474 assertEquals(oldSecondaryProgress, progressBar.getSecondaryProgress()); 475 476 Parcelable state = progressBar.onSaveInstanceState(); 477 478 int newProgress = 2; 479 int newSecondaryProgress = progressBar.getMax() - 2; 480 progressBar.setProgress(newProgress); 481 progressBar.setSecondaryProgress(newSecondaryProgress); 482 assertEquals(newProgress, progressBar.getProgress()); 483 assertEquals(newSecondaryProgress, progressBar.getSecondaryProgress()); 484 485 progressBar.onRestoreInstanceState(state); 486 assertEquals(oldProgress, progressBar.getProgress()); 487 assertEquals(oldSecondaryProgress, progressBar.getSecondaryProgress()); 488 } 489 490 /* 491 * Mock class for ProgressBar to test protected methods 492 */ 493 private class MockProgressBar extends ProgressBar { 494 private boolean mCalledInvalidate = false; 495 496 /** 497 * @param context 498 */ 499 public MockProgressBar(Context context) { 500 super(context); 501 } 502 503 @Override 504 protected boolean verifyDrawable(Drawable who) { 505 return super.verifyDrawable(who); 506 } 507 508 @Override 509 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 510 super.onSizeChanged(w, h, oldw, oldh); 511 } 512 513 @Override 514 protected synchronized void onMeasure(int widthMeasureSpec, 515 int heightMeasureSpec) { 516 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 517 } 518 519 @Override 520 protected synchronized void onDraw(Canvas canvas) { 521 super.onDraw(canvas); 522 } 523 524 @Override 525 protected void drawableStateChanged() { 526 super.drawableStateChanged(); 527 } 528 529 public void invalidate(int l, int t, int r, int b) { 530 mCalledInvalidate = true; 531 super.invalidate(l, t, r, b); 532 } 533 534 public void invalidate() { 535 mCalledInvalidate = true; 536 super.invalidate(); 537 } 538 539 public boolean hasCalledInvalidate() { 540 return mCalledInvalidate; 541 } 542 543 public void reset() { 544 mCalledInvalidate = false; 545 } 546 } 547 } 548