Fix Drawable, PopupWindow CTS breakages

Makes ScaleDrawableTest more like ClipDrawableTest, since they have the
same base class. This fixes the state change test, which was incorrect.

Fixes the ClipDrawableTest's opacity check to correctly reflect level.

Uses the correct default values and root view for PopupWindow layout
params.

Bug: 21406104
Bug: 20563211
Change-Id: I2301f3fb71bb53a8df5a704d0d7255da4f6cd20c
diff --git a/tests/tests/graphics/src/android/graphics/drawable/cts/ClipDrawableTest.java b/tests/tests/graphics/src/android/graphics/drawable/cts/ClipDrawableTest.java
index da96cdc..e727350 100644
--- a/tests/tests/graphics/src/android/graphics/drawable/cts/ClipDrawableTest.java
+++ b/tests/tests/graphics/src/android/graphics/drawable/cts/ClipDrawableTest.java
@@ -68,19 +68,21 @@
     }
 
     public void testGetChangingConfigurations() {
+        final int SUPER_CONFIG = 1;
+        final int CONTAINED_DRAWABLE_CONFIG = 2;
+
         MockDrawable mockDrawable = new MockDrawable();
         ClipDrawable clipDrawable = new ClipDrawable(mockDrawable,
                 Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
+
         assertEquals(0, clipDrawable.getChangingConfigurations());
 
-        clipDrawable.setChangingConfigurations(1);
-        assertEquals(1, clipDrawable.getChangingConfigurations());
+        mockDrawable.setChangingConfigurations(CONTAINED_DRAWABLE_CONFIG);
+        assertEquals(CONTAINED_DRAWABLE_CONFIG, clipDrawable.getChangingConfigurations());
 
-        mockDrawable.setChangingConfigurations(2);
-        clipDrawable = new ClipDrawable(mockDrawable,
-                Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
-        clipDrawable.setChangingConfigurations(1);
-        assertEquals(3, clipDrawable.getChangingConfigurations());
+        clipDrawable.setChangingConfigurations(SUPER_CONFIG);
+        assertEquals(SUPER_CONFIG | CONTAINED_DRAWABLE_CONFIG,
+                clipDrawable.getChangingConfigurations());
     }
 
     public void testGetConstantState() {
@@ -126,16 +128,28 @@
 
     @SuppressWarnings("deprecation")
     public void testGetOpacity() {
-        BitmapDrawable bmpDrawable =
-            new BitmapDrawable(Bitmap.createBitmap(100, 50, Config.RGB_565));
-        ClipDrawable clipDrawable = new ClipDrawable(bmpDrawable,
-                Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
-        assertEquals(PixelFormat.OPAQUE, clipDrawable.getOpacity());
+        MockDrawable dr;
+        ClipDrawable clipDrawable;
 
-        bmpDrawable = new BitmapDrawable(Bitmap.createBitmap(100, 50, Config.RGB_565));
-        bmpDrawable.setGravity(Gravity.CENTER);
-        clipDrawable = new ClipDrawable(bmpDrawable, Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
-        assertEquals(PixelFormat.TRANSLUCENT, clipDrawable.getOpacity());
+        dr = new MockDrawable();
+        dr.setOpacity(PixelFormat.OPAQUE);
+        clipDrawable = new ClipDrawable(dr, Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
+        clipDrawable.setLevel(0);
+        assertEquals("Fully-clipped opaque drawable is transparent",
+                PixelFormat.TRANSPARENT, clipDrawable.getOpacity());
+        clipDrawable.setLevel(5000);
+        assertEquals("Partially-clipped opaque drawable is translucent",
+                PixelFormat.TRANSLUCENT, clipDrawable.getOpacity());
+        clipDrawable.setLevel(10000);
+        assertEquals("Unclipped opaque drawable is opaque",
+                PixelFormat.OPAQUE, clipDrawable.getOpacity());
+
+        dr = new MockDrawable();
+        dr.setOpacity(PixelFormat.TRANSLUCENT);
+        clipDrawable = new ClipDrawable(dr, Gravity.BOTTOM, ClipDrawable.HORIZONTAL);
+        clipDrawable.setLevel(10000);
+        assertEquals("Unclipped translucent drawable is translucent",
+                PixelFormat.TRANSLUCENT, clipDrawable.getOpacity());
     }
 
     public void testGetPadding() {
@@ -220,7 +234,7 @@
         MockCallback callback = new MockCallback();
         mockClipDrawable.setCallback(callback);
 
-        assertEquals(0, mockDrawable.getLevel());
+        assertEquals("Default level is 0", 0, mockDrawable.getLevel());
         mockClipDrawable.onLevelChange(1000);
         assertEquals(1000, mockDrawable.getLevel());
         assertSame(mockClipDrawable, callback.getInvalidateDrawable());
@@ -344,6 +358,7 @@
     private class MockDrawable extends Drawable {
         private ColorFilter mColorFilter;
         private ConstantState mConstantState;
+        private int mOpacity;
         private boolean mCalledDraw = false;
         private int mAlpha;
 
@@ -372,7 +387,11 @@
         }
 
         public int getOpacity() {
-            return 0;
+            return mOpacity;
+        }
+
+        public void setOpacity(int opacity) {
+            mOpacity = opacity;
         }
 
         protected void onBoundsChange(Rect bounds) {
diff --git a/tests/tests/graphics/src/android/graphics/drawable/cts/ScaleDrawableTest.java b/tests/tests/graphics/src/android/graphics/drawable/cts/ScaleDrawableTest.java
index 3d758fe..b58e40f 100644
--- a/tests/tests/graphics/src/android/graphics/drawable/cts/ScaleDrawableTest.java
+++ b/tests/tests/graphics/src/android/graphics/drawable/cts/ScaleDrawableTest.java
@@ -21,6 +21,7 @@
 import org.xmlpull.v1.XmlPullParserException;
 
 import java.io.IOException;
+import java.util.Arrays;
 
 import android.content.res.Resources;
 import android.content.res.XmlResourceParser;
@@ -28,6 +29,7 @@
 import android.graphics.ColorFilter;
 import android.graphics.Rect;
 import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.ClipDrawable;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.Drawable.ConstantState;
 import android.graphics.drawable.ScaleDrawable;
@@ -275,19 +277,23 @@
     }
 
     public void testOnStateChange() {
-        MockDrawable mockDrawable = new MockDrawable();
-        MockScaleDrawable mockScaleDrawable = new MockScaleDrawable(
-                mockDrawable, Gravity.CENTER, 100, 200);
+        Drawable d = new MockDrawable();
+        MockScaleDrawable scaleDrawable = new MockScaleDrawable(d, Gravity.CENTER, 100, 200);
+        assertEquals("initial child state is empty", d.getState(), StateSet.WILD_CARD);
 
-        assertFalse(mockScaleDrawable.onStateChange(StateSet.WILD_CARD));
-        assertTrue(mockDrawable.hasCalledSetState());
-        assertTrue(mockScaleDrawable.hasCalledOnBoundsChange());
+        int[] state = new int[] {1, 2, 3};
+        assertFalse("child did not change", scaleDrawable.onStateChange(state));
+        assertEquals("child state did not change", d.getState(), StateSet.WILD_CARD);
 
-        mockDrawable.reset();
-        mockScaleDrawable.reset();
-        assertFalse(mockScaleDrawable.onStateChange(null));
-        assertTrue(mockDrawable.hasCalledSetState());
-        assertTrue(mockScaleDrawable.hasCalledOnBoundsChange());
+        d = mContext.getDrawable(R.drawable.statelistdrawable);
+        scaleDrawable = new MockScaleDrawable(d, Gravity.CENTER, 100, 200);
+        assertEquals("initial child state is empty", d.getState(), StateSet.WILD_CARD);
+        scaleDrawable.onStateChange(state);
+        assertTrue("child state changed", Arrays.equals(state, d.getState()));
+
+        // input null as param
+        scaleDrawable.onStateChange(null);
+        // expected, no Exception thrown out, test success
     }
 
     public void testOnLevelChange() {
@@ -413,25 +419,25 @@
         parser = res.getXml(R.xml.scaledrawable);
         attrs = DrawableTestUtils.getAttributeSet(parser, "scale_nodrawable");
         try {
-            scaleDrawable.inflate(res, parser, attrs);
-            fail("Should throw XmlPullParserException");
+            Drawable.createFromXmlInner(res, parser, attrs);
+            fail("Should throw XmlPullParserException if missing drawable");
         } catch (XmlPullParserException e) {
         }
 
         try {
-            scaleDrawable.inflate(null, parser, attrs);
+            Drawable.createFromXmlInner(null, parser, attrs);
             fail("Should throw NullPointerException if resource is null");
         } catch (NullPointerException e) {
         }
 
         try {
-            scaleDrawable.inflate(res, null, attrs);
+            Drawable.createFromXmlInner(res, null, attrs);
             fail("Should throw NullPointerException if parser is null");
         } catch (NullPointerException e) {
         }
 
         try {
-            scaleDrawable.inflate(res, parser, null);
+            Drawable.createFromXmlInner(res, parser, null);
             fail("Should throw NullPointerException if attribute set is null");
         } catch (NullPointerException e) {
         }
diff --git a/tests/tests/widget/src/android/widget/cts/PopupWindowTest.java b/tests/tests/widget/src/android/widget/cts/PopupWindowTest.java
index c14bb03..706ad8d 100644
--- a/tests/tests/widget/src/android/widget/cts/PopupWindowTest.java
+++ b/tests/tests/widget/src/android/widget/cts/PopupWindowTest.java
@@ -189,8 +189,7 @@
 
     public void testAccessHeight() {
         mPopupWindow = new PopupWindow(mActivity);
-        // default is 0
-        assertEquals(0, mPopupWindow.getHeight());
+        assertEquals(WindowManager.LayoutParams.WRAP_CONTENT, mPopupWindow.getHeight());
 
         int height = getDisplay().getHeight() / 2;
         mPopupWindow.setHeight(height);
@@ -224,7 +223,7 @@
 
     public void testAccessWidth() {
         mPopupWindow = new PopupWindow(mActivity);
-        assertEquals(0, mPopupWindow.getWidth());
+        assertEquals(WindowManager.LayoutParams.WRAP_CONTENT, mPopupWindow.getWidth());
 
         int width = getDisplay().getWidth() / 2;
         mPopupWindow.setWidth(width);
@@ -418,7 +417,7 @@
         mPopupWindow.setOutsideTouchable(true);
 
         WindowManager.LayoutParams p = (WindowManager.LayoutParams)
-                mPopupWindow.getContentView().getLayoutParams();
+                mPopupWindow.getContentView().getRootView().getLayoutParams();
 
         assertEquals(0, WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES & p.flags);
         assertEquals(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
@@ -748,7 +747,7 @@
         mPopupWindow = new PopupWindow(new TextView(mActivity));
         showPopup();
 
-        ViewGroup.LayoutParams p = mPopupWindow.getContentView().getLayoutParams();
+        ViewGroup.LayoutParams p = mPopupWindow.getContentView().getRootView().getLayoutParams();
         assertEquals(0, p.width);
         assertEquals(0, p.height);