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