am c05d403: AI 147566: CTS: add test cases for android.widget.SeekBar.

Merge commit 'c05d403dd75e1fa84f328c6acfbed70c497390e6' into donut

* commit 'c05d403dd75e1fa84f328c6acfbed70c497390e6':
  AI 147566: CTS: add test cases for android.widget.SeekBar.
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 401d3d0..b4db3f6 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -128,6 +128,14 @@
             </intent-filter>
         </activity>
 
+        <activity android:name="android.widget.cts.SeekBarStubActivity"
+            android:label="SeekBarStubActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" />
+            </intent-filter>
+        </activity>
+
         <activity android:name="android.widget.cts.ScrollViewStubActivity"
             android:label="ScrollViewStubActivity">
             <intent-filter>
diff --git a/tests/src/android/widget/cts/SeekBarStubActivity.java b/tests/src/android/widget/cts/SeekBarStubActivity.java
new file mode 100644
index 0000000..84274dc
--- /dev/null
+++ b/tests/src/android/widget/cts/SeekBarStubActivity.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.widget.cts;
+
+import com.android.cts.stub.R;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.SeekBar;
+
+/**
+ * Stub activity for testing {@link SeekBar}
+ */
+public class SeekBarStubActivity extends Activity {
+    /**
+     * Called with the activity is first created.
+     */
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.seekbar);
+
+        View v = findViewById(R.id.seekBar);
+        v.setEnabled(true);
+        v.setFocusable(true);
+        v.setFocusableInTouchMode(true);
+        v.requestFocus();
+    }
+}
diff --git a/tests/src/android/widget/cts/WidgetTestUtils.java b/tests/src/android/widget/cts/WidgetTestUtils.java
index 45214cb..2dda0d6 100644
--- a/tests/src/android/widget/cts/WidgetTestUtils.java
+++ b/tests/src/android/widget/cts/WidgetTestUtils.java
@@ -16,8 +16,13 @@
 
 package android.widget.cts;
 
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+
 import android.graphics.Bitmap;
 
+import java.io.IOException;
+
 import junit.framework.Assert;
 
 /**
@@ -59,4 +64,29 @@
             }
         }
     }
+
+    /**
+     * Find beginning of the special element.
+     * @param parser XmlPullParser will be parsed.
+     * @param firstElementName the target element name.
+     *
+     * @throws XmlPullParserException if XML Pull Parser related faults occur.
+     * @throws IOException if I/O-related error occur when parsing.
+     */
+    public static final void beginDocument(XmlPullParser parser, String firstElementName)
+            throws XmlPullParserException, IOException {
+        Assert.assertNotNull(parser);
+        Assert.assertNotNull(firstElementName);
+
+        int type;
+        while ((type = parser.next()) != XmlPullParser.START_TAG
+                && type != XmlPullParser.END_DOCUMENT) {
+            ;
+        }
+
+        if (!parser.getName().equals(firstElementName)) {
+            throw new XmlPullParserException("Unexpected start tag: found " + parser.getName()
+                    + ", expected " + firstElementName);
+        }
+    }
 }
diff --git a/tests/tests/widget/src/android/widget/cts/SeekBarTest.java b/tests/tests/widget/src/android/widget/cts/SeekBarTest.java
new file mode 100644
index 0000000..7ce0148
--- /dev/null
+++ b/tests/tests/widget/src/android/widget/cts/SeekBarTest.java
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.widget.cts;
+
+import com.android.cts.stub.R;
+
+import dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+import dalvik.annotation.TestTargets;
+
+import android.app.Activity;
+import android.app.Instrumentation;
+import android.os.SystemClock;
+import android.test.ActivityInstrumentationTestCase2;
+import android.view.MotionEvent;
+import android.widget.SeekBar;
+import android.widget.SeekBar.OnSeekBarChangeListener;
+
+/**
+ * Test {@link SeekBar}.
+ */
+@TestTargetClass(SeekBar.class)
+public class SeekBarTest extends ActivityInstrumentationTestCase2<SeekBarStubActivity> {
+    private SeekBar mSeekBar;
+
+    private Activity mActivity;
+
+    private Instrumentation mInstrumentation;
+
+    public SeekBarTest() {
+        super("com.android.cts.stub", SeekBarStubActivity.class);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mInstrumentation = getInstrumentation();
+        mActivity = getActivity();
+        mSeekBar = (SeekBar) mActivity.findViewById(R.id.seekBar);
+    }
+
+    @TestTargets({
+        @TestTargetNew(
+            level = TestLevel.COMPLETE,
+            method = "SeekBar",
+            args = {android.content.Context.class}
+        ),
+        @TestTargetNew(
+            level = TestLevel.COMPLETE,
+            method = "SeekBar",
+            args = {android.content.Context.class, android.util.AttributeSet.class}
+        ),
+        @TestTargetNew(
+            level = TestLevel.COMPLETE,
+            method = "SeekBar",
+            args = {android.content.Context.class, android.util.AttributeSet.class, int.class}
+        )
+    })
+    public void testConstructor() {
+        new SeekBar(mActivity);
+
+        new SeekBar(mActivity, null);
+
+        new SeekBar(mActivity, null, com.android.internal.R.attr.seekBarStyle);
+    }
+
+    @TestTargetNew(
+        level = TestLevel.COMPLETE,
+        method = "setOnSeekBarChangeListener",
+        args = {android.widget.SeekBar.OnSeekBarChangeListener.class}
+    )
+    public void testSetOnSeekBarChangeListener() {
+        MockOnSeekBarListener listener = new MockOnSeekBarListener();
+
+        mSeekBar.setOnSeekBarChangeListener(listener);
+        listener.reset();
+        long downTime = SystemClock.uptimeMillis();
+        long eventTime = SystemClock.uptimeMillis();
+        int seekBarXY[] = new int[2];
+        mSeekBar.getLocationInWindow(seekBarXY);
+        MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN,
+                seekBarXY[0], seekBarXY[1], 0);
+        mInstrumentation.sendPointerSync(event);
+        mInstrumentation.waitForIdleSync();
+        assertTrue(listener.hasCalledOnStartTrackingTouch());
+        // while starting to track, the progress is changed also
+        assertTrue(listener.hasCalledOnProgressChanged());
+
+        listener.reset();
+        downTime = SystemClock.uptimeMillis();
+        eventTime = SystemClock.uptimeMillis();
+        event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE,
+                seekBarXY[0] + (mSeekBar.getWidth() >> 1), seekBarXY[1], 0);
+        mInstrumentation.sendPointerSync(event);
+        mInstrumentation.waitForIdleSync();
+        assertTrue(listener.hasCalledOnProgressChanged());
+
+        listener.reset();
+        downTime = SystemClock.uptimeMillis();
+        eventTime = SystemClock.uptimeMillis();
+        event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP,
+                seekBarXY[0] + (mSeekBar.getWidth() >> 1), seekBarXY[1], 0);
+        mInstrumentation.sendPointerSync(event);
+        mInstrumentation.waitForIdleSync();
+        assertTrue(listener.hasCalledOnStopTrackingTouch());
+
+        mSeekBar.setOnSeekBarChangeListener(null);
+    }
+
+    private class MockOnSeekBarListener implements OnSeekBarChangeListener {
+        private boolean mHasCalledOnProgressChanged;
+
+        private boolean mHasCalledOnStartTrackingTouch;
+
+        private boolean mHasCalledOnStopTrackingTouch;
+
+        public boolean hasCalledOnProgressChanged() {
+            return mHasCalledOnProgressChanged;
+        }
+
+        public boolean hasCalledOnStartTrackingTouch() {
+            return mHasCalledOnStartTrackingTouch;
+        }
+
+        public boolean hasCalledOnStopTrackingTouch() {
+            return mHasCalledOnStopTrackingTouch;
+        }
+
+        public void reset(){
+            mHasCalledOnProgressChanged = false;
+            mHasCalledOnStartTrackingTouch = false;
+            mHasCalledOnStopTrackingTouch = false;
+        }
+
+        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
+            mHasCalledOnProgressChanged = true;
+        }
+
+        public void onStartTrackingTouch(SeekBar seekBar) {
+            mHasCalledOnStartTrackingTouch = true;
+        }
+
+        public void onStopTrackingTouch(SeekBar seekBar) {
+            mHasCalledOnStopTrackingTouch = true;
+        }
+    }
+}