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 22 import android.app.Activity; 23 import android.app.Instrumentation; 24 import android.os.SystemClock; 25 import android.test.ActivityInstrumentationTestCase2; 26 import android.view.MotionEvent; 27 import android.widget.SeekBar; 28 import android.widget.SeekBar.OnSeekBarChangeListener; 29 30 /** 31 * Test {@link SeekBar}. 32 */ 33 public class SeekBarTest extends ActivityInstrumentationTestCase2<SeekBarCtsActivity> { 34 private SeekBar mSeekBar; 35 36 private Activity mActivity; 37 38 private Instrumentation mInstrumentation; 39 SeekBarTest()40 public SeekBarTest() { 41 super("android.widget.cts", SeekBarCtsActivity.class); 42 } 43 44 @Override setUp()45 protected void setUp() throws Exception { 46 super.setUp(); 47 mInstrumentation = getInstrumentation(); 48 mActivity = getActivity(); 49 mSeekBar = (SeekBar) mActivity.findViewById(R.id.seekBar); 50 } 51 testConstructor()52 public void testConstructor() { 53 new SeekBar(mActivity); 54 55 new SeekBar(mActivity, null); 56 57 new SeekBar(mActivity, null, android.R.attr.seekBarStyle); 58 } 59 testSetOnSeekBarChangeListener()60 public void testSetOnSeekBarChangeListener() { 61 MockOnSeekBarListener listener = new MockOnSeekBarListener(); 62 63 mSeekBar.setOnSeekBarChangeListener(listener); 64 listener.reset(); 65 long downTime = SystemClock.uptimeMillis(); 66 long eventTime = SystemClock.uptimeMillis(); 67 int seekBarXY[] = new int[2]; 68 mSeekBar.getLocationInWindow(seekBarXY); 69 MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 70 seekBarXY[0], seekBarXY[1], 0); 71 mInstrumentation.sendPointerSync(event); 72 mInstrumentation.waitForIdleSync(); 73 assertTrue(listener.hasCalledOnStartTrackingTouch()); 74 // while starting to track, the progress is changed also 75 assertTrue(listener.hasCalledOnProgressChanged()); 76 77 listener.reset(); 78 downTime = SystemClock.uptimeMillis(); 79 eventTime = SystemClock.uptimeMillis(); 80 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 81 seekBarXY[0] + (mSeekBar.getWidth() >> 1), seekBarXY[1], 0); 82 mInstrumentation.sendPointerSync(event); 83 mInstrumentation.waitForIdleSync(); 84 assertTrue(listener.hasCalledOnProgressChanged()); 85 86 listener.reset(); 87 downTime = SystemClock.uptimeMillis(); 88 eventTime = SystemClock.uptimeMillis(); 89 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, 90 seekBarXY[0] + (mSeekBar.getWidth() >> 1), seekBarXY[1], 0); 91 mInstrumentation.sendPointerSync(event); 92 mInstrumentation.waitForIdleSync(); 93 assertTrue(listener.hasCalledOnStopTrackingTouch()); 94 95 mSeekBar.setOnSeekBarChangeListener(null); 96 } 97 98 private class MockOnSeekBarListener implements OnSeekBarChangeListener { 99 private boolean mHasCalledOnProgressChanged; 100 101 private boolean mHasCalledOnStartTrackingTouch; 102 103 private boolean mHasCalledOnStopTrackingTouch; 104 hasCalledOnProgressChanged()105 public boolean hasCalledOnProgressChanged() { 106 return mHasCalledOnProgressChanged; 107 } 108 hasCalledOnStartTrackingTouch()109 public boolean hasCalledOnStartTrackingTouch() { 110 return mHasCalledOnStartTrackingTouch; 111 } 112 hasCalledOnStopTrackingTouch()113 public boolean hasCalledOnStopTrackingTouch() { 114 return mHasCalledOnStopTrackingTouch; 115 } 116 reset()117 public void reset(){ 118 mHasCalledOnProgressChanged = false; 119 mHasCalledOnStartTrackingTouch = false; 120 mHasCalledOnStopTrackingTouch = false; 121 } 122 onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch)123 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { 124 mHasCalledOnProgressChanged = true; 125 } 126 onStartTrackingTouch(SeekBar seekBar)127 public void onStartTrackingTouch(SeekBar seekBar) { 128 mHasCalledOnStartTrackingTouch = true; 129 } 130 onStopTrackingTouch(SeekBar seekBar)131 public void onStopTrackingTouch(SeekBar seekBar) { 132 mHasCalledOnStopTrackingTouch = true; 133 } 134 } 135 } 136