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