ShadowOverlayContainer Fix MATCH_PARENT handling

Added test cases

Bug 23625951

Change-Id: I66430f35d3c8997f9dfed84f3295d37aa5313a64
diff --git a/v17/leanback/src/android/support/v17/leanback/widget/ShadowOverlayContainer.java b/v17/leanback/src/android/support/v17/leanback/widget/ShadowOverlayContainer.java
index 3be5929..fdb7c71 100644
--- a/v17/leanback/src/android/support/v17/leanback/widget/ShadowOverlayContainer.java
+++ b/v17/leanback/src/android/support/v17/leanback/widget/ShadowOverlayContainer.java
@@ -283,12 +283,15 @@
         ViewGroup.LayoutParams lp = view.getLayoutParams();
         if (lp != null) {
             // if wrapped view has layout params, inherit everything but width/height.
-            // Wrapped view is assigned a FrameLayout.LayoutParams with width
-            // and height only.  User can still change wrapped view width/height afterwards.
+            // Wrapped view is assigned a FrameLayout.LayoutParams with width and height only.
             // Margins, etc are assigned to the wrapper and take effect in parent container.
             ViewGroup.LayoutParams wrapped_lp = new FrameLayout.LayoutParams(lp.width, lp.height);
-            lp.width = LayoutParams.WRAP_CONTENT;
-            lp.height = LayoutParams.WRAP_CONTENT;
+            // Uses MATCH_PARENT for MATCH_PARENT, WRAP_CONTENT for WRAP_CONTENT and fixed size,
+            // App can still change wrapped view fixed width/height afterwards.
+            lp.width = lp.width == LayoutParams.MATCH_PARENT ?
+                    LayoutParams.MATCH_PARENT : LayoutParams.WRAP_CONTENT;
+            lp.height = lp.height == LayoutParams.MATCH_PARENT ?
+                    LayoutParams.MATCH_PARENT : LayoutParams.WRAP_CONTENT;
             this.setLayoutParams(lp);
             addView(view, wrapped_lp);
         } else {
diff --git a/v17/tests/src/android/support/v17/leanback/widget/ShadowOverlayContainerTest.java b/v17/tests/src/android/support/v17/leanback/widget/ShadowOverlayContainerTest.java
new file mode 100644
index 0000000..e7ec4bf
--- /dev/null
+++ b/v17/tests/src/android/support/v17/leanback/widget/ShadowOverlayContainerTest.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2015 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.support.v17.leanback.widget;
+
+import android.test.AndroidTestCase;
+import android.view.View.MeasureSpec;
+import android.view.ViewGroup.LayoutParams;
+import android.widget.FrameLayout;
+import android.widget.TextView;
+
+
+public class ShadowOverlayContainerTest extends AndroidTestCase {
+
+    public void testWrapContent() {
+        FrameLayout frameLayout = new FrameLayout(getContext());
+        TextView textView = new TextView(getContext());
+        textView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
+                LayoutParams.WRAP_CONTENT));
+        textView.setText("abc");
+        ShadowOverlayContainer container = new ShadowOverlayContainer(getContext());
+        container.initialize(true, true, true);
+        container.wrap(textView);
+        frameLayout.addView(container);
+        frameLayout.measure(MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY),
+                MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY));
+        frameLayout.layout(0, 0, 500, 500);
+        assertTrue(textView.getWidth() > 0);
+        assertTrue(textView.getWidth() < 500);
+        assertTrue(textView.getHeight() > 0);
+        assertTrue(textView.getHeight() < 500);
+        assertEquals(container.getWidth(), textView.getWidth());
+        assertEquals(container.getHeight(), textView.getHeight());
+
+        // change layout size of textView after wrap()
+        textView.setLayoutParams(new FrameLayout.LayoutParams(123, 123));
+        frameLayout.measure(MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY),
+                MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY));
+        frameLayout.layout(0, 0, 500, 500);
+        assertTrue(textView.getWidth() == 123);
+        assertTrue(textView.getHeight() == 123);
+        assertEquals(container.getWidth(), textView.getWidth());
+        assertEquals(container.getHeight(), textView.getHeight());
+    }
+
+    public void testFixedSize() {
+        FrameLayout frameLayout = new FrameLayout(getContext());
+        TextView textView = new TextView(getContext());
+        textView.setLayoutParams(new FrameLayout.LayoutParams(200, LayoutParams.WRAP_CONTENT));
+        textView.setText("abc");
+        ShadowOverlayContainer container = new ShadowOverlayContainer(getContext());
+        container.initialize(true, true, true);
+        container.wrap(textView);
+        frameLayout.addView(container);
+        frameLayout.measure(MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY),
+                MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY));
+        frameLayout.layout(0, 0, 500, 500);
+        assertTrue(textView.getWidth() == 200);
+        assertTrue(textView.getHeight() > 0);
+        assertTrue(textView.getHeight() < 500);
+        assertEquals(container.getWidth(), textView.getWidth());
+        assertEquals(container.getHeight(), textView.getHeight());
+
+        // change layout size of textView after wrap()
+        textView.setLayoutParams(new FrameLayout.LayoutParams(123, 123));
+        frameLayout.measure(MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY),
+                MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY));
+        frameLayout.layout(0, 0, 500, 500);
+        assertTrue(textView.getWidth() == 123);
+        assertTrue(textView.getHeight() == 123);
+        assertEquals(container.getWidth(), textView.getWidth());
+        assertEquals(container.getHeight(), textView.getHeight());
+    }
+
+    public void testMatchParent() {
+        FrameLayout frameLayout = new FrameLayout(getContext());
+        TextView textView = new TextView(getContext());
+        textView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
+                LayoutParams.WRAP_CONTENT));
+        textView.setText("abc");
+        ShadowOverlayContainer container = new ShadowOverlayContainer(getContext());
+        container.initialize(true, true, true);
+        container.wrap(textView);
+        frameLayout.addView(container);
+        frameLayout.measure(MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY),
+                MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY));
+        frameLayout.layout(0, 0, 500, 500);
+        assertTrue(textView.getWidth() == 500);
+        assertTrue(textView.getHeight() > 0);
+        assertTrue(textView.getHeight() < 500);
+        assertEquals(container.getWidth(), textView.getWidth());
+        assertEquals(container.getHeight(), textView.getHeight());
+    }
+}