1 /* 2 * Copyright (C) 2008 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 */ 16 17 package android.widget.cts; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.content.Context; 22 import android.util.AttributeSet; 23 import android.util.Xml; 24 import android.view.Gravity; 25 import android.view.ViewGroup; 26 import android.view.ViewGroup.MarginLayoutParams; 27 import android.widget.FrameLayout; 28 import android.widget.FrameLayout.LayoutParams; 29 30 import androidx.test.InstrumentationRegistry; 31 import androidx.test.filters.SmallTest; 32 import androidx.test.runner.AndroidJUnit4; 33 34 import com.android.compatibility.common.util.WidgetTestUtils; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.xmlpull.v1.XmlPullParser; 40 import org.xmlpull.v1.XmlPullParserException; 41 42 import java.io.IOException; 43 44 @SmallTest 45 @RunWith(AndroidJUnit4.class) 46 public class FrameLayout_LayoutParamsTest { 47 private Context mContext; 48 49 @Before 50 public void setup() { 51 mContext = InstrumentationRegistry.getTargetContext(); 52 } 53 54 private AttributeSet getAttributeSet() throws XmlPullParserException, IOException { 55 XmlPullParser parser = mContext.getResources().getLayout(R.layout.framelayout_layout); 56 WidgetTestUtils.beginDocument(parser, "LinearLayout"); 57 return Xml.asAttributeSet(parser); 58 } 59 60 @Test 61 public void testConstructor() throws XmlPullParserException, IOException { 62 AttributeSet attrs = getAttributeSet(); 63 64 new LayoutParams(mContext, attrs); 65 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 66 new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0); 67 new LayoutParams(new ViewGroup.LayoutParams(mContext, attrs)); 68 new LayoutParams(new LayoutParams(mContext, attrs)); 69 new LayoutParams(new MarginLayoutParams(mContext, attrs)); 70 71 new LayoutParams(-1, -1); 72 new LayoutParams(-1, -1, -1); 73 } 74 75 @Test(expected=NullPointerException.class) 76 public void testConstructorNullContext() { 77 new LayoutParams(null, null); 78 } 79 80 @Test(expected=NullPointerException.class) 81 public void testConstructorNullViewGroupParams() { 82 new LayoutParams((ViewGroup.LayoutParams) null); 83 } 84 85 @Test(expected=NullPointerException.class) 86 public void testConstructorNullViewGroupMarginParams() { 87 new LayoutParams((ViewGroup.MarginLayoutParams) null); 88 } 89 90 @Test 91 public void testCopyConstructor() { 92 FrameLayout.LayoutParams copy; 93 94 final FrameLayout.LayoutParams fllp = new FrameLayout.LayoutParams( 95 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 96 fllp.gravity = Gravity.BOTTOM; 97 fllp.leftMargin = 5; 98 fllp.topMargin = 10; 99 fllp.rightMargin = 15; 100 fllp.bottomMargin = 20; 101 102 copy = new FrameLayout.LayoutParams(fllp); 103 assertEquals("Width", fllp.width, copy.width); 104 assertEquals("Height", fllp.height, copy.height); 105 assertEquals("Gravity", fllp.gravity, copy.gravity); 106 assertEquals("Left margin", fllp.leftMargin, copy.leftMargin); 107 assertEquals("Top margin", fllp.topMargin, copy.topMargin); 108 assertEquals("Right margin", fllp.rightMargin, copy.rightMargin); 109 assertEquals("Bottom margin", fllp.bottomMargin, copy.bottomMargin); 110 111 final MarginLayoutParams mlp = new MarginLayoutParams( 112 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 113 mlp.leftMargin = 5; 114 mlp.topMargin = 10; 115 mlp.rightMargin = 15; 116 mlp.bottomMargin = 20; 117 118 copy = new FrameLayout.LayoutParams(mlp); 119 assertEquals("Width", mlp.width, copy.width); 120 assertEquals("Height", mlp.height, copy.height); 121 assertEquals("Left margin", fllp.leftMargin, copy.leftMargin); 122 assertEquals("Top margin", fllp.topMargin, copy.topMargin); 123 assertEquals("Right margin", fllp.rightMargin, copy.rightMargin); 124 assertEquals("Bottom margin", fllp.bottomMargin, copy.bottomMargin); 125 126 final ViewGroup.LayoutParams vglp = new ViewGroup.LayoutParams( 127 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 128 129 copy = new FrameLayout.LayoutParams(vglp); 130 assertEquals("Width", vglp.width, copy.width); 131 assertEquals("Height", vglp.height, copy.height); 132 } 133 } 134