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 org.xmlpull.v1.XmlPullParser; 20 21 import android.app.Activity; 22 import android.content.Context; 23 import android.test.ActivityInstrumentationTestCase; 24 import android.test.ViewAsserts; 25 import android.util.AttributeSet; 26 import android.util.Xml; 27 import android.view.ViewGroup; 28 import android.widget.LinearLayout.LayoutParams; 29 import android.widget.AbsoluteLayout; 30 import android.widget.LinearLayout; 31 import android.widget.ListView; 32 import android.widget.TextView; 33 34 import com.android.cts.widget.R; 35 36 /** 37 * Test {@link LinearLayout}. 38 */ 39 public class LinearLayoutTest extends ActivityInstrumentationTestCase<LinearLayoutCtsActivity> { 40 private Context mContext; 41 private Activity mActivity; 42 LinearLayoutTest()43 public LinearLayoutTest() { 44 super("com.android.cts.widget", LinearLayoutCtsActivity.class); 45 } 46 47 @Override setUp()48 protected void setUp() throws Exception { 49 super.setUp(); 50 mActivity = getActivity(); 51 mContext = getInstrumentation().getTargetContext(); 52 } 53 testConstructor()54 public void testConstructor() { 55 new LinearLayout(mContext); 56 57 new LinearLayout(mContext, null); 58 59 XmlPullParser parser = mContext.getResources().getXml(R.layout.linearlayout_layout); 60 AttributeSet attrs = Xml.asAttributeSet(parser); 61 new LinearLayout(mContext, attrs); 62 63 try { 64 new LinearLayout(null, null); 65 fail("should throw NullPointerException."); 66 } catch (NullPointerException e) { 67 } 68 } 69 testAccessBaselineAligned()70 public void testAccessBaselineAligned() { 71 LinearLayout linearLayout = new LinearLayout(mContext); 72 linearLayout.setBaselineAligned(true); 73 assertTrue(linearLayout.isBaselineAligned()); 74 75 linearLayout.setBaselineAligned(false); 76 assertFalse(linearLayout.isBaselineAligned()); 77 78 // android:baselineAligned="false" in LinearLayout weightsum 79 linearLayout = (LinearLayout) mActivity.findViewById(R.id.weightsum); 80 assertFalse(linearLayout.isBaselineAligned()); 81 82 // default mBaselineAligned is true. 83 linearLayout = (LinearLayout) mActivity.findViewById(R.id.horizontal); 84 assertTrue(linearLayout.isBaselineAligned()); 85 86 // default mBaselineAligned is true. 87 // Only applicable if {@link #mOrientation} is horizontal 88 linearLayout = (LinearLayout) mActivity.findViewById(R.id.vertical); 89 assertTrue(linearLayout.isBaselineAligned()); 90 } 91 testGetBaseline()92 public void testGetBaseline() { 93 LinearLayout linearLayout = new LinearLayout(mContext); 94 95 ListView lv1 = new ListView(mContext); 96 linearLayout.addView(lv1); 97 assertEquals(-1, linearLayout.getBaseline()); 98 99 ListView lv2 = new ListView(mContext); 100 linearLayout.addView(lv2); 101 linearLayout.setBaselineAlignedChildIndex(1); 102 try { 103 linearLayout.getBaseline(); 104 fail("LinearLayout.getBaseline() should throw exception here."); 105 } catch (RuntimeException e) { 106 } 107 108 MockListView lv3 = new MockListView(mContext); 109 linearLayout.addView(lv3); 110 linearLayout.setBaselineAlignedChildIndex(2); 111 assertEquals(lv3.getBaseline(), linearLayout.getBaseline()); 112 } 113 testAccessBaselineAlignedChildIndex()114 public void testAccessBaselineAlignedChildIndex() { 115 LinearLayout linearLayout = new LinearLayout(mContext); 116 // set BaselineAlignedChildIndex 117 ListView lv1 = new ListView(mContext); 118 ListView lv2 = new ListView(mContext); 119 ListView lv3 = new ListView(mContext); 120 linearLayout.addView(lv1); 121 linearLayout.addView(lv2); 122 linearLayout.addView(lv3); 123 linearLayout.setBaselineAlignedChildIndex(1); 124 assertEquals(1, linearLayout.getBaselineAlignedChildIndex()); 125 126 linearLayout.setBaselineAlignedChildIndex(2); 127 assertEquals(2, linearLayout.getBaselineAlignedChildIndex()); 128 129 try { 130 linearLayout.setBaselineAlignedChildIndex(-1); 131 fail("LinearLayout should throw IllegalArgumentException here."); 132 } catch (IllegalArgumentException e) { 133 } 134 try { 135 linearLayout.setBaselineAlignedChildIndex(3); 136 fail("LinearLayout should throw IllegalArgumentException here."); 137 } catch (IllegalArgumentException e) { 138 } 139 140 linearLayout = (LinearLayout) mActivity.findViewById(R.id.baseline_aligned_child_index); 141 assertEquals(1, linearLayout.getBaselineAlignedChildIndex()); 142 } 143 144 /** 145 * weightsum is a horizontal LinearLayout. There are three children in it. 146 */ testAccessWeightSum()147 public void testAccessWeightSum() { 148 LinearLayout parent = (LinearLayout) mActivity.findViewById(R.id.weightsum); 149 TextView weight02 = (TextView) mActivity.findViewById(R.id.weight_0_2); 150 TextView weight05 = (TextView) mActivity.findViewById(R.id.weight_0_5); 151 TextView weight03 = (TextView) mActivity.findViewById(R.id.weight_0_3); 152 153 assertNotNull(parent); 154 assertNotNull(weight02); 155 assertNotNull(weight05); 156 assertNotNull(weight03); 157 158 assertEquals(mContext.getResources().getString(R.string.horizontal_text_1), 159 weight02.getText().toString()); 160 assertEquals(mContext.getResources().getString(R.string.horizontal_text_2), 161 weight05.getText().toString()); 162 assertEquals(mContext.getResources().getString(R.string.horizontal_text_3), 163 weight03.getText().toString()); 164 165 assertEquals(LinearLayout.HORIZONTAL, parent.getOrientation()); 166 assertEquals(1.0f, parent.getWeightSum()); 167 168 int parentWidth = parent.getWidth(); 169 assertEquals(Math.ceil(parentWidth * 0.2), weight02.getWidth(), 1.0); 170 assertEquals(Math.ceil(parentWidth * 0.5), weight05.getWidth(), 1.0); 171 assertEquals(Math.ceil(parentWidth * 0.3), weight03.getWidth(), 1.0); 172 } 173 testGenerateLayoutParams()174 public void testGenerateLayoutParams() { 175 ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(320, 240); 176 MockLinearLayout mockLinearLayout = new MockLinearLayout(mContext); 177 LayoutParams layoutParams1 = mockLinearLayout.generateLayoutParams(lp); 178 assertEquals(320, layoutParams1.width); 179 assertEquals(240, layoutParams1.height); 180 181 // generateLayoutParams() always throw a RuntimeException. 182 // XmlPullParser parser = mContext.getResources().getXml(R.layout.linearlayout_layout); 183 // AttributeSet attrs = Xml.asAttributeSet(parser); 184 // LinearLayout linearLayout = new LinearLayout(mContext, attrs); 185 // LayoutParams layoutParams2 = linearLayout.generateLayoutParams(attrs); 186 // assertEquals(LayoutParams.MATCH_PARENT, layoutParams2.width); 187 // assertEquals(LayoutParams.WRAP_CONTENT, layoutParams2.height); 188 } 189 testCheckLayoutParams()190 public void testCheckLayoutParams() { 191 MockLinearLayout mockLinearLayout = new MockLinearLayout(mContext); 192 193 ViewGroup.LayoutParams params = new AbsoluteLayout.LayoutParams(240, 320, 0, 0); 194 assertFalse(mockLinearLayout.checkLayoutParams(params)); 195 196 params = new LinearLayout.LayoutParams(240, 320); 197 assertTrue(mockLinearLayout.checkLayoutParams(params)); 198 } 199 testGenerateDefaultLayoutParams()200 public void testGenerateDefaultLayoutParams() { 201 MockLinearLayout mockLinearLayout = new MockLinearLayout(mContext); 202 203 mockLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 204 ViewGroup.LayoutParams param = mockLinearLayout.generateDefaultLayoutParams(); 205 assertNotNull(param); 206 assertTrue(param instanceof LinearLayout.LayoutParams); 207 assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, param.width); 208 assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, param.height); 209 210 mockLinearLayout.setOrientation(LinearLayout.VERTICAL); 211 param = mockLinearLayout.generateDefaultLayoutParams(); 212 assertNotNull(param); 213 assertTrue(param instanceof LinearLayout.LayoutParams); 214 assertEquals(ViewGroup.LayoutParams.MATCH_PARENT, param.width); 215 assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, param.height); 216 217 mockLinearLayout.setOrientation(-1); 218 assertNull(mockLinearLayout.generateDefaultLayoutParams()); 219 } 220 221 /** 222 * layout of horizontal LinearLayout. 223 * ---------------------------------------------------- 224 * | ------------ | | | 225 * | | top view | | --------------- | | 226 * | | | | | center view | | --------------- | 227 * | ------------ | | | | | bottom view | | 228 * | | --------------- | | | | 229 * | parent | | --------------- | 230 * ---------------------------------------------------- 231 */ testLayoutHorizontal()232 public void testLayoutHorizontal() { 233 LinearLayout parent = (LinearLayout) mActivity.findViewById(R.id.horizontal); 234 TextView topView = (TextView) mActivity.findViewById(R.id.gravity_top); 235 TextView centerView = (TextView) mActivity.findViewById(R.id.gravity_center_vertical); 236 TextView bottomView = (TextView) mActivity.findViewById(R.id.gravity_bottom); 237 238 assertNotNull(parent); 239 assertNotNull(topView); 240 assertNotNull(centerView); 241 assertNotNull(bottomView); 242 243 assertEquals(mContext.getResources().getString(R.string.horizontal_text_1), 244 topView.getText().toString()); 245 assertEquals(mContext.getResources().getString(R.string.horizontal_text_2), 246 centerView.getText().toString()); 247 assertEquals(mContext.getResources().getString(R.string.horizontal_text_3), 248 bottomView.getText().toString()); 249 250 assertEquals(LinearLayout.HORIZONTAL, parent.getOrientation()); 251 252 ViewAsserts.assertTopAligned(parent, topView); 253 ViewAsserts.assertVerticalCenterAligned(parent, centerView); 254 ViewAsserts.assertBottomAligned(parent, bottomView); 255 256 assertEquals(0, topView.getTop()); 257 assertEquals(topView.getHeight(), topView.getBottom()); 258 assertEquals(0, topView.getLeft()); 259 assertEquals(centerView.getLeft(), topView.getRight()); 260 261 int offset = (parent.getHeight() - centerView.getHeight()) / 2; 262 assertEquals(offset, centerView.getTop()); 263 assertEquals(offset + centerView.getHeight(), centerView.getBottom()); 264 assertEquals(topView.getRight(), centerView.getLeft()); 265 assertEquals(bottomView.getLeft(), centerView.getRight()); 266 267 assertEquals(parent.getHeight() - bottomView.getHeight(), bottomView.getTop()); 268 assertEquals(parent.getHeight(), bottomView.getBottom()); 269 assertEquals(centerView.getRight(), bottomView.getLeft()); 270 assertEquals(parent.getWidth(), bottomView.getRight()); 271 } 272 273 /** 274 * layout of vertical LinearLayout. 275 * ----------------------------------- 276 * | ------------- | 277 * | | left view | | 278 * | ------------- | 279 * | - - - - - - - - - - - - - - - - | 280 * | --------------- | 281 * | | center view | | 282 * | --------------- | 283 * | - - - - - - - - - - - - - - - - | 284 * | -------------- | 285 * | parent | right view | | 286 * | -------------- | 287 * ----------------------------------- 288 */ testLayoutVertical()289 public void testLayoutVertical() { 290 LinearLayout parent = (LinearLayout) mActivity.findViewById(R.id.vertical); 291 TextView leftView = (TextView) mActivity.findViewById(R.id.gravity_left); 292 TextView centerView = (TextView) mActivity.findViewById(R.id.gravity_center_horizontal); 293 TextView rightView = (TextView) mActivity.findViewById(R.id.gravity_right); 294 295 assertNotNull(parent); 296 assertNotNull(leftView); 297 assertNotNull(centerView); 298 assertNotNull(rightView); 299 300 assertEquals(mContext.getResources().getString(R.string.vertical_text_1), 301 leftView.getText().toString()); 302 assertEquals(mContext.getResources().getString(R.string.vertical_text_2), 303 centerView.getText().toString()); 304 assertEquals(mContext.getResources().getString(R.string.vertical_text_3), 305 rightView.getText().toString()); 306 307 assertEquals(LinearLayout.VERTICAL, parent.getOrientation()); 308 309 ViewAsserts.assertLeftAligned(parent, leftView); 310 ViewAsserts.assertHorizontalCenterAligned(parent, centerView); 311 ViewAsserts.assertRightAligned(parent, rightView); 312 313 assertEquals(0, leftView.getTop()); 314 assertEquals(centerView.getTop(), leftView.getBottom()); 315 assertEquals(0, leftView.getLeft()); 316 assertEquals(leftView.getWidth(), leftView.getRight()); 317 318 int offset = (parent.getWidth() - centerView.getWidth()) / 2; 319 assertEquals(leftView.getBottom(), centerView.getTop()); 320 assertEquals(rightView.getTop(), centerView.getBottom()); 321 assertEquals(offset, centerView.getLeft()); 322 assertEquals(offset + centerView.getWidth(), centerView.getRight()); 323 324 assertEquals(centerView.getBottom(), rightView.getTop()); 325 assertEquals(parent.getHeight(), rightView.getBottom()); 326 assertEquals(parent.getWidth() - rightView.getWidth(), rightView.getLeft()); 327 assertEquals(parent.getWidth(), rightView.getRight()); 328 } 329 330 private class MockListView extends ListView { 331 private final static int DEFAULT_CHILD_BASE_LINE = 1; 332 MockListView(Context context)333 public MockListView(Context context) { 334 super(context); 335 } 336 getBaseline()337 public int getBaseline() { 338 return DEFAULT_CHILD_BASE_LINE; 339 } 340 } 341 342 /** 343 * Add MockLinearLayout to help for testing protected methods in LinearLayout. 344 * Because we can not access protected methods in LinearLayout directly, we have to 345 * extends from it and override protected methods so that we can access them in 346 * our test codes. 347 */ 348 private class MockLinearLayout extends LinearLayout { MockLinearLayout(Context c)349 public MockLinearLayout(Context c) { 350 super(c); 351 } 352 353 @Override checkLayoutParams(ViewGroup.LayoutParams p)354 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { 355 return super.checkLayoutParams(p); 356 } 357 358 @Override generateDefaultLayoutParams()359 protected LinearLayout.LayoutParams generateDefaultLayoutParams() { 360 return super.generateDefaultLayoutParams(); 361 } 362 363 @Override generateLayoutParams(ViewGroup.LayoutParams p)364 protected LinearLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { 365 return super.generateLayoutParams(p); 366 } 367 } 368 } 369