Merge change 3096 into donut

* changes:
  Integrate unsubmitted cupcake change 149414: 	CTS: added missed test cases for package android.view
diff --git a/tests/tests/view/src/android/view/cts/GravityTest.java b/tests/tests/view/src/android/view/cts/GravityTest.java
index 5e641a8..aee209e 100644
--- a/tests/tests/view/src/android/view/cts/GravityTest.java
+++ b/tests/tests/view/src/android/view/cts/GravityTest.java
@@ -16,14 +16,15 @@
 
 package android.view.cts;
 
+import dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+import dalvik.annotation.TestTargets;
+import dalvik.annotation.ToBeFixed;
+
 import android.graphics.Rect;
 import android.test.AndroidTestCase;
 import android.view.Gravity;
-import dalvik.annotation.TestTargets;
-import dalvik.annotation.TestLevel;
-import dalvik.annotation.TestTargetNew;
-import dalvik.annotation.TestTargetClass;
-import dalvik.annotation.ToBeFixed;
 
 /**
  * Test {@link Gravity}.
@@ -54,13 +55,13 @@
         @TestTargetNew(
             level = TestLevel.COMPLETE,
             method = "apply",
-            args = {int.class, int.class, int.class, android.graphics.Rect.class, 
+            args = {int.class, int.class, int.class, android.graphics.Rect.class,
                     android.graphics.Rect.class}
         ),
         @TestTargetNew(
             level = TestLevel.COMPLETE,
             method = "apply",
-            args = {int.class, int.class, int.class, android.graphics.Rect.class, int.class, 
+            args = {int.class, int.class, int.class, android.graphics.Rect.class, int.class,
                     int.class, android.graphics.Rect.class}
         )
     })
@@ -180,4 +181,27 @@
         assertTrue(Gravity.isHorizontal(Gravity.HORIZONTAL_GRAVITY_MASK));
         assertFalse(Gravity.isHorizontal(Gravity.NO_GRAVITY));
     }
+
+    @TestTargetNew(
+        level = TestLevel.COMPLETE,
+        method = "applyDisplay",
+        args = {int.class, android.graphics.Rect.class, android.graphics.Rect.class}
+    )
+    public void testApplyDisplay() {
+        Rect display = new Rect(20, 30, 40, 50);
+        Rect inoutRect = new Rect(10, 10, 30, 60);
+        Gravity.applyDisplay(Gravity.DISPLAY_CLIP_VERTICAL, display, inoutRect);
+        assertEquals(20, inoutRect.left);
+        assertEquals(40, inoutRect.right);
+        assertEquals(30, inoutRect.top);
+        assertEquals(50, inoutRect.bottom);
+
+        display = new Rect(20, 30, 40, 50);
+        inoutRect = new Rect(10, 10, 30, 60);
+        Gravity.applyDisplay(Gravity.DISPLAY_CLIP_HORIZONTAL, display, inoutRect);
+        assertEquals(20, inoutRect.left);
+        assertEquals(30, inoutRect.right);
+        assertEquals(30, inoutRect.top);
+        assertEquals(50, inoutRect.bottom);
+    }
 }
diff --git a/tests/tests/view/src/android/view/cts/KeyEventTest.java b/tests/tests/view/src/android/view/cts/KeyEventTest.java
index aefa37a..24264d1 100644
--- a/tests/tests/view/src/android/view/cts/KeyEventTest.java
+++ b/tests/tests/view/src/android/view/cts/KeyEventTest.java
@@ -93,6 +93,12 @@
             notes = "Test constructor(s) of {@link KeyEvent}",
             method = "KeyEvent",
             args = {long.class, String.class, int.class, int.class}
+        ),
+        @TestTargetNew(
+            level = TestLevel.COMPLETE,
+            notes = "Test constructor(s) of {@link KeyEvent}",
+            method = "KeyEvent",
+            args = {android.view.KeyEvent.class}
         )
     })
     public void testConstructor() {
@@ -110,6 +116,7 @@
                 KeyEvent.META_SHIFT_ON, 1, 1, KeyEvent.FLAG_SOFT_KEYBOARD);
 
         KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0);
+        new KeyEvent(keyEvent);
         new KeyEvent(keyEvent, mEventTime, 1);
 
         new KeyEvent(mDownTime, "test", 0, KeyEvent.FLAG_SOFT_KEYBOARD);
@@ -651,6 +658,71 @@
         assertEquals(scanCode, mKeyEvent.getScanCode());
     }
 
+    @TestTargetNew(
+        level = TestLevel.COMPLETE,
+        method = "changeAction",
+        args = {android.view.KeyEvent.class, int.class}
+    )
+    public void testChangeAction() {
+        mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN,
+                KeyEvent.KEYCODE_0, 5, KeyEvent.META_SHIFT_ON, 1, 1, KeyEvent.FLAG_WOKE_HERE);
+
+        KeyEvent newEvent = KeyEvent.changeAction(mKeyEvent, KeyEvent.ACTION_UP);
+        assertEquals(KeyEvent.ACTION_UP, newEvent.getAction());
+        assertEquals(mKeyEvent.getFlags(), newEvent.getFlags());
+        assertEquals(mKeyEvent.getCharacters(), newEvent.getCharacters());
+        assertEquals(mKeyEvent.getDisplayLabel(), newEvent.getDisplayLabel());
+        assertEquals(mKeyEvent.getDeviceId(), newEvent.getDeviceId());
+        assertEquals(mKeyEvent.getDownTime(), newEvent.getDownTime());
+        assertEquals(mKeyEvent.getEventTime(), newEvent.getEventTime());
+        assertEquals(mKeyEvent.getKeyCode(), newEvent.getKeyCode());
+        assertEquals(mKeyEvent.getRepeatCount(), newEvent.getRepeatCount());
+    }
+
+    @TestTargetNew(
+        level = TestLevel.COMPLETE,
+        method = "changeFlags",
+        args = {android.view.KeyEvent.class, int.class}
+    )
+    public void testChangeFlags() {
+        mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN,
+                KeyEvent.KEYCODE_0, 5, KeyEvent.META_SHIFT_ON, 1, 1, KeyEvent.FLAG_WOKE_HERE);
+
+        KeyEvent newEvent = KeyEvent.changeFlags(mKeyEvent, KeyEvent.FLAG_FROM_SYSTEM);
+        assertEquals(KeyEvent.FLAG_FROM_SYSTEM, newEvent.getFlags());
+        assertEquals(mKeyEvent.getAction(), newEvent.getAction());
+        assertEquals(mKeyEvent.getCharacters(), newEvent.getCharacters());
+        assertEquals(mKeyEvent.getDisplayLabel(), newEvent.getDisplayLabel());
+        assertEquals(mKeyEvent.getDeviceId(), newEvent.getDeviceId());
+        assertEquals(mKeyEvent.getDownTime(), newEvent.getDownTime());
+        assertEquals(mKeyEvent.getEventTime(), newEvent.getEventTime());
+        assertEquals(mKeyEvent.getKeyCode(), newEvent.getKeyCode());
+        assertEquals(mKeyEvent.getRepeatCount(), newEvent.getRepeatCount());
+    }
+
+    @TestTargetNew(
+        level = TestLevel.COMPLETE,
+        method = "changeTimeRepeat",
+        args = {android.view.KeyEvent.class, long.class, int.class}
+    )
+    public void testChangeTimeRepeat() {
+        mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN,
+                KeyEvent.KEYCODE_0, 5, KeyEvent.META_SHIFT_ON, 1, 1, KeyEvent.FLAG_WOKE_HERE);
+
+        long newEventTime = SystemClock.uptimeMillis();
+        int newRepeat = mKeyEvent.getRepeatCount() + 1;
+        KeyEvent newEvent = KeyEvent.changeTimeRepeat(mKeyEvent, newEventTime, newRepeat);
+        assertEquals(newEventTime, newEvent.getEventTime());
+        assertEquals(newRepeat, newEvent.getRepeatCount());
+        assertEquals(mKeyEvent.getFlags(), newEvent.getFlags());
+        assertEquals(mKeyEvent.getAction(), newEvent.getAction());
+        assertEquals(mKeyEvent.getCharacters(), newEvent.getCharacters());
+        assertEquals(mKeyEvent.getDisplayLabel(), newEvent.getDisplayLabel());
+        assertEquals(mKeyEvent.getDeviceId(), newEvent.getDeviceId());
+        assertEquals(mKeyEvent.getDownTime(), newEvent.getDownTime());
+        assertEquals(mKeyEvent.getKeyCode(), newEvent.getKeyCode());
+    }
+
     private class MockCallback implements Callback {
         private boolean mIsKeyDown;
         private boolean mIsKeyUp;
diff --git a/tests/tests/view/src/android/view/cts/ViewTreeObserverTest.java b/tests/tests/view/src/android/view/cts/ViewTreeObserverTest.java
index e959f26..c0b1b96 100644
--- a/tests/tests/view/src/android/view/cts/ViewTreeObserverTest.java
+++ b/tests/tests/view/src/android/view/cts/ViewTreeObserverTest.java
@@ -16,6 +16,13 @@
 
 package android.view.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.app.cts.MockActivity;
@@ -28,17 +35,12 @@
 import android.view.ViewTreeObserver.OnGlobalFocusChangeListener;
 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
 import android.view.ViewTreeObserver.OnPreDrawListener;
+import android.view.ViewTreeObserver.OnScrollChangedListener;
 import android.view.ViewTreeObserver.OnTouchModeChangeListener;
 import android.widget.Button;
 import android.widget.LinearLayout;
 import android.widget.ListView;
-
-import com.android.cts.stub.R;
-
-import dalvik.annotation.TestTargets;
-import dalvik.annotation.TestTargetNew;
-import dalvik.annotation.TestLevel;
-import dalvik.annotation.TestTargetClass;
+import android.widget.ScrollView;
 
 /**
  * Test {@link ViewTreeObserver}.
@@ -353,6 +355,48 @@
         assertFalse(listener.hasCalledOnTouchModeChanged());
     }
 
+    @TestTargets({
+        @TestTargetNew(
+            level = TestLevel.COMPLETE,
+            method = "addOnScrollChangedListener",
+            args = {android.view.ViewTreeObserver.OnScrollChangedListener.class}
+        ),
+        @TestTargetNew(
+            level = TestLevel.COMPLETE,
+            method = "removeOnScrollChangedListener",
+            args = {android.view.ViewTreeObserver.OnScrollChangedListener.class}
+        )
+    })
+    public void testAccessOnScrollChangedListener() throws Throwable {
+        layout(R.layout.scrollview_layout);
+        final ScrollView scrollView = (ScrollView) mActivity.findViewById(R.id.scroll_view);
+
+        mViewTreeObserver = scrollView.getViewTreeObserver();
+
+        MockOnScrollChangedListener listener = new MockOnScrollChangedListener();
+        assertFalse(listener.hasCalledOnScrollChanged());
+        mViewTreeObserver.addOnScrollChangedListener(listener);
+
+        runTestOnUiThread(new Runnable() {
+            public void run() {
+                scrollView.fullScroll(View.FOCUS_DOWN);
+            }
+        });
+        mInstrumentation.waitForIdleSync();
+        assertTrue(listener.hasCalledOnScrollChanged());
+
+        listener.reset();
+        assertFalse(listener.hasCalledOnScrollChanged());
+
+        mViewTreeObserver.removeOnScrollChangedListener(listener);
+        runTestOnUiThread(new Runnable() {
+            public void run() {
+                scrollView.fullScroll(View.FOCUS_UP);
+            }
+        });
+        assertFalse(listener.hasCalledOnScrollChanged());
+    }
+
     private class MockOnGlobalFocusChangeListener implements OnGlobalFocusChangeListener {
         private boolean mCalledOnGlobalFocusChanged = false;
 
@@ -425,4 +469,20 @@
             return mCalledOnComputeInternalInsets;
         }
     }
+
+    private static class MockOnScrollChangedListener implements OnScrollChangedListener {
+        private boolean mCalledOnScrollChanged = false;
+
+        public boolean hasCalledOnScrollChanged() {
+            return mCalledOnScrollChanged;
+        }
+
+        public void onScrollChanged() {
+            mCalledOnScrollChanged = true;
+        }
+
+        public void reset() {
+            mCalledOnScrollChanged = false;
+        }
+    }
 }