1/*
2 * Copyright (C) 2015 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 */
16package androidx.leanback.widget;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertTrue;
20
21import android.content.Context;
22import android.support.test.InstrumentationRegistry;
23import android.support.test.filters.MediumTest;
24import android.support.test.runner.AndroidJUnit4;
25import android.view.View.MeasureSpec;
26import android.view.ViewGroup.LayoutParams;
27import android.widget.FrameLayout;
28import android.widget.TextView;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34@MediumTest
35@RunWith(AndroidJUnit4.class)
36public class ShadowOverlayContainerTest {
37    private Context mContext;
38
39    @Before
40    public void setup() throws Exception {
41        mContext = InstrumentationRegistry.getTargetContext();
42    }
43
44    @Test
45    public void testWrapContent() {
46        FrameLayout frameLayout = new FrameLayout(mContext);
47        TextView textView = new TextView(mContext);
48        textView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
49                LayoutParams.WRAP_CONTENT));
50        textView.setText("abc");
51        ShadowOverlayContainer container = new ShadowOverlayContainer(mContext);
52        container.initialize(true, true, true);
53        container.wrap(textView);
54        frameLayout.addView(container);
55        frameLayout.measure(MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY),
56                MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY));
57        frameLayout.layout(0, 0, 500, 500);
58        assertTrue(textView.getWidth() > 0);
59        assertTrue(textView.getWidth() < 500);
60        assertTrue(textView.getHeight() > 0);
61        assertTrue(textView.getHeight() < 500);
62        assertEquals(container.getWidth(), textView.getWidth());
63        assertEquals(container.getHeight(), textView.getHeight());
64
65        // change layout size of textView after wrap()
66        textView.setLayoutParams(new FrameLayout.LayoutParams(123, 123));
67        frameLayout.measure(MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY),
68                MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY));
69        frameLayout.layout(0, 0, 500, 500);
70        assertTrue(textView.getWidth() == 123);
71        assertTrue(textView.getHeight() == 123);
72        assertEquals(container.getWidth(), textView.getWidth());
73        assertEquals(container.getHeight(), textView.getHeight());
74    }
75
76    @Test
77    public void testFixedSize() {
78        FrameLayout frameLayout = new FrameLayout(mContext);
79        TextView textView = new TextView(mContext);
80        textView.setLayoutParams(new FrameLayout.LayoutParams(200, LayoutParams.WRAP_CONTENT));
81        textView.setText("abc");
82        ShadowOverlayContainer container = new ShadowOverlayContainer(mContext);
83        container.initialize(true, true, true);
84        container.wrap(textView);
85        frameLayout.addView(container);
86        frameLayout.measure(MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY),
87                MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY));
88        frameLayout.layout(0, 0, 500, 500);
89        assertTrue(textView.getWidth() == 200);
90        assertTrue(textView.getHeight() > 0);
91        assertTrue(textView.getHeight() < 500);
92        assertEquals(container.getWidth(), textView.getWidth());
93        assertEquals(container.getHeight(), textView.getHeight());
94
95        // change layout size of textView after wrap()
96        textView.setLayoutParams(new FrameLayout.LayoutParams(123, 123));
97        frameLayout.measure(MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY),
98                MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY));
99        frameLayout.layout(0, 0, 500, 500);
100        assertTrue(textView.getWidth() == 123);
101        assertTrue(textView.getHeight() == 123);
102        assertEquals(container.getWidth(), textView.getWidth());
103        assertEquals(container.getHeight(), textView.getHeight());
104    }
105
106    @Test
107    public void testMatchParent() {
108        FrameLayout frameLayout = new FrameLayout(mContext);
109        TextView textView = new TextView(mContext);
110        textView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
111                LayoutParams.WRAP_CONTENT));
112        textView.setText("abc");
113        ShadowOverlayContainer container = new ShadowOverlayContainer(mContext);
114        container.initialize(true, true, true);
115        container.wrap(textView);
116        frameLayout.addView(container);
117        frameLayout.measure(MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY),
118                MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY));
119        frameLayout.layout(0, 0, 500, 500);
120        assertTrue(textView.getWidth() == 500);
121        assertTrue(textView.getHeight() > 0);
122        assertTrue(textView.getHeight() < 500);
123        assertEquals(container.getWidth(), textView.getWidth());
124        assertEquals(container.getHeight(), textView.getHeight());
125    }
126}
127