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 import static org.junit.Assert.assertNotNull; 21 22 import android.app.Activity; 23 import android.content.res.XmlResourceParser; 24 import android.test.ViewAsserts; 25 import android.util.LayoutDirection; 26 import android.view.View; 27 import android.view.ViewGroup; 28 import android.view.ViewGroup.MarginLayoutParams; 29 import android.widget.RelativeLayout; 30 import android.widget.cts.util.XmlUtils; 31 32 import androidx.test.filters.MediumTest; 33 import androidx.test.rule.ActivityTestRule; 34 import androidx.test.runner.AndroidJUnit4; 35 36 import org.junit.Before; 37 import org.junit.Rule; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.xmlpull.v1.XmlPullParserException; 41 42 import java.io.IOException; 43 44 /** 45 * Test {@link RelativeLayout.LayoutParams}. 46 */ 47 @MediumTest 48 @RunWith(AndroidJUnit4.class) 49 public class RelativeLayout_LayoutParamsTest { 50 private Activity mActivity; 51 52 @Rule 53 public ActivityTestRule<RelativeLayoutCtsActivity> mActivityRule = 54 new ActivityTestRule<>(RelativeLayoutCtsActivity.class); 55 56 @Before 57 public void setup() { 58 mActivity = mActivityRule.getActivity(); 59 } 60 61 @Test 62 public void testConstructor() throws XmlPullParserException, IOException { 63 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200, 300); 64 assertEquals(200, layoutParams.width); 65 assertEquals(300, layoutParams.height); 66 67 RelativeLayout.LayoutParams tempLayoutParams = layoutParams; 68 layoutParams = new RelativeLayout.LayoutParams(tempLayoutParams); 69 assertEquals(200, layoutParams.width); 70 assertEquals(300, layoutParams.height); 71 72 ViewGroup.LayoutParams tempViewGroupLayoutParams = new ViewGroup.LayoutParams(300, 400); 73 layoutParams = new RelativeLayout.LayoutParams(tempViewGroupLayoutParams); 74 assertEquals(300, layoutParams.width); 75 assertEquals(400, layoutParams.height); 76 77 MarginLayoutParams tempMarginLayoutParams = new MarginLayoutParams(400, 500); 78 layoutParams = new RelativeLayout.LayoutParams(tempMarginLayoutParams); 79 assertEquals(400, layoutParams.width); 80 assertEquals(500, layoutParams.height); 81 82 XmlResourceParser p = mActivity.getResources().getLayout(R.layout.relative_layout); 83 XmlUtils.beginDocument(p, "RelativeLayout"); 84 layoutParams = new RelativeLayout.LayoutParams(mActivity, p); 85 assertEquals(ViewGroup.LayoutParams.MATCH_PARENT, layoutParams.width); 86 assertEquals(ViewGroup.LayoutParams.MATCH_PARENT, layoutParams.height); 87 88 // Test RelativeLayout.Params which generated from the xml file. 89 int rules[]; 90 91 // test attributes used in RelativeLayout. 92 RelativeLayout relativeLayout = (RelativeLayout) mActivity.findViewById( 93 R.id.relative_sublayout_attrs); 94 95 // view1, centered within its parent. 96 // TEST: android:layout_centerInParent 97 View view1 = mActivity.findViewById(R.id.relative_view1); 98 ViewAsserts.assertHorizontalCenterAligned(relativeLayout, view1); 99 ViewAsserts.assertVerticalCenterAligned(relativeLayout, view1); 100 layoutParams = (RelativeLayout.LayoutParams) (view1.getLayoutParams()); 101 rules = layoutParams.getRules(); 102 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.CENTER_IN_PARENT]); 103 104 // view2, below view1 and has same left position with view1. 105 // TEST: android:layout_below; android:layout_alignLeft 106 View view2 = mActivity.findViewById(R.id.relative_view2); 107 ViewAsserts.assertLeftAligned(view1, view2); 108 assertEquals(view1.getBottom(), view2.getTop()); 109 layoutParams = (RelativeLayout.LayoutParams) (view2.getLayoutParams()); 110 rules = layoutParams.getRules(); 111 assertEquals(R.id.relative_view1, rules[RelativeLayout.BELOW]); 112 assertEquals(R.id.relative_view1, rules[RelativeLayout.ALIGN_LEFT]); 113 114 // view3, has same top position with view1 and same bottom position with view2, 115 // and on the right of view1.1. 116 // TEST: android:layout_alignTop; android:layout_alignBottom; android:layout_toRightOf 117 View view3 = mActivity.findViewById(R.id.relative_view3); 118 ViewAsserts.assertTopAligned(view1, view3); 119 ViewAsserts.assertBottomAligned(view2, view3); 120 assertEquals(view1.getRight(), view3.getLeft()); 121 layoutParams = (RelativeLayout.LayoutParams) (view3.getLayoutParams()); 122 rules = layoutParams.getRules(); 123 assertEquals(R.id.relative_view1, rules[RelativeLayout.ALIGN_TOP]); 124 assertEquals(R.id.relative_view2, rules[RelativeLayout.ALIGN_BOTTOM]); 125 assertEquals(R.id.relative_view1, rules[RelativeLayout.RIGHT_OF]); 126 127 // view4, has same right position with view3 and above view3. 128 // TEST: android:layout_alignRight; android:layout_above 129 View view4 = mActivity.findViewById(R.id.relative_view4); 130 ViewAsserts.assertRightAligned(view3, view4); 131 assertEquals(view3.getTop(), view4.getBottom()); 132 layoutParams = (RelativeLayout.LayoutParams) (view4.getLayoutParams()); 133 rules = layoutParams.getRules(); 134 assertEquals(R.id.relative_view3, rules[RelativeLayout.ALIGN_RIGHT]); 135 assertEquals(R.id.relative_view3, rules[RelativeLayout.ABOVE]); 136 137 // view5 goes on the left-bottom. 138 // TEST: android:layout_alignParentBottom; android:layout_alignParentLeft 139 View view5 = mActivity.findViewById(R.id.relative_view5); 140 ViewAsserts.assertLeftAligned(relativeLayout, view5); 141 ViewAsserts.assertBottomAligned(relativeLayout, view5); 142 layoutParams = (RelativeLayout.LayoutParams) (view5.getLayoutParams()); 143 rules = layoutParams.getRules(); 144 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_BOTTOM]); 145 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_LEFT]); 146 147 // view6 goes on the top-right. 148 // TEST: android:layout_alignParentTop; android:layout_alignParentRight 149 View view6 = mActivity.findViewById(R.id.relative_view6); 150 ViewAsserts.assertTopAligned(relativeLayout, view6); 151 ViewAsserts.assertRightAligned(relativeLayout, view6); 152 layoutParams = (RelativeLayout.LayoutParams) (view6.getLayoutParams()); 153 rules = layoutParams.getRules(); 154 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_TOP]); 155 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_RIGHT]); 156 157 // view7, has same baseline with view6 and centered horizontally within its parent. 158 // TEST: android:layout_alignBaseline; android:layout_centerHorizontal 159 View view7 = mActivity.findViewById(R.id.relative_view7); 160 ViewAsserts.assertBaselineAligned(view6, view7); 161 ViewAsserts.assertHorizontalCenterAligned(relativeLayout, view7); 162 layoutParams = (RelativeLayout.LayoutParams) (view7.getLayoutParams()); 163 rules = layoutParams.getRules(); 164 assertEquals(R.id.relative_view6, rules[RelativeLayout.ALIGN_BASELINE]); 165 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.CENTER_HORIZONTAL]); 166 167 // view8, centered vertically within its parent and on the left of view1. 168 // TEST: android:layout_toLeftOf; android:layout_centerVertical 169 View view8 = mActivity.findViewById(R.id.relative_view8); 170 ViewAsserts.assertVerticalCenterAligned(relativeLayout, view8); 171 assertEquals(view1.getLeft(), view8.getRight()); 172 layoutParams = (RelativeLayout.LayoutParams) (view8.getLayoutParams()); 173 rules = layoutParams.getRules(); 174 assertEquals(R.id.relative_view1, rules[RelativeLayout.LEFT_OF]); 175 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.CENTER_VERTICAL]); 176 177 // view9, has same top and bottom position with view3 and same left position with its parent 178 // TEST: android:layout_alignLeft; android:layout_alignTop; android:layout_alignBottom; 179 // android:layout_alignWithParentIfMissing 180 View view9 = mActivity.findViewById(R.id.relative_view9); 181 ViewAsserts.assertTopAligned(view3, view9); 182 ViewAsserts.assertBottomAligned(view3, view9); 183 ViewAsserts.assertLeftAligned(relativeLayout, view9); 184 layoutParams = (RelativeLayout.LayoutParams) (view9.getLayoutParams()); 185 rules = layoutParams.getRules(); 186 assertEquals(R.id.gravity_bottom, rules[RelativeLayout.ALIGN_LEFT]); 187 assertEquals(R.id.relative_view3, rules[RelativeLayout.ALIGN_TOP]); 188 assertEquals(R.id.relative_view3, rules[RelativeLayout.ALIGN_BOTTOM]); 189 } 190 191 @Test 192 public void testStartEnd() { 193 RelativeLayout.LayoutParams layoutParams; 194 195 // Test RelativeLayout.Params which generated from the xml file. 196 int rules[]; 197 198 // test attributes used in RelativeLayout. 199 RelativeLayout relativeLayout = (RelativeLayout) mActivity.findViewById( 200 R.id.relative_sublayout_attrs_2); 201 202 // view1, centered within its parent. 203 // TEST: android:layout_centerInParent 204 View view1 = mActivity.findViewById(R.id.relative_view21); 205 ViewAsserts.assertHorizontalCenterAligned(relativeLayout, view1); 206 ViewAsserts.assertVerticalCenterAligned(relativeLayout, view1); 207 layoutParams = (RelativeLayout.LayoutParams) (view1.getLayoutParams()); 208 rules = layoutParams.getRules(); 209 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.CENTER_IN_PARENT]); 210 211 // view2, below view1 and has same left position with view1. 212 // TEST: android:layout_below; android:layout_alignStart 213 View view2 = mActivity.findViewById(R.id.relative_view22); 214 ViewAsserts.assertLeftAligned(view1, view2); 215 assertEquals(view1.getBottom(), view2.getTop()); 216 layoutParams = (RelativeLayout.LayoutParams) (view2.getLayoutParams()); 217 rules = layoutParams.getRules(); 218 assertEquals(R.id.relative_view21, rules[RelativeLayout.BELOW]); 219 assertEquals(0, rules[RelativeLayout.ALIGN_START]); 220 assertEquals(R.id.relative_view21, rules[RelativeLayout.ALIGN_LEFT]); 221 assertEquals(0, rules[RelativeLayout.ALIGN_RIGHT]); 222 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_RTL); 223 rules = layoutParams.getRules(); 224 assertEquals(R.id.relative_view21, rules[RelativeLayout.BELOW]); 225 assertEquals(0, rules[RelativeLayout.ALIGN_START]); 226 assertEquals(0, rules[RelativeLayout.ALIGN_LEFT]); 227 assertEquals(R.id.relative_view21, rules[RelativeLayout.ALIGN_RIGHT]); 228 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_LTR); 229 230 // view3, has same top position with view1 and same bottom position with view2, 231 // and on the right of view1.1. 232 // TEST: android:layout_alignTop; android:layout_alignBottom; android:layout_toEndOf 233 View view3 = mActivity.findViewById(R.id.relative_view23); 234 ViewAsserts.assertTopAligned(view1, view3); 235 ViewAsserts.assertBottomAligned(view2, view3); 236 assertEquals(view1.getRight(), view3.getLeft()); 237 layoutParams = (RelativeLayout.LayoutParams) (view3.getLayoutParams()); 238 rules = layoutParams.getRules(); 239 assertEquals(R.id.relative_view21, rules[RelativeLayout.ALIGN_TOP]); 240 assertEquals(R.id.relative_view22, rules[RelativeLayout.ALIGN_BOTTOM]); 241 assertEquals(0, rules[RelativeLayout.END_OF]); 242 assertEquals(0, rules[RelativeLayout.LEFT_OF]); 243 assertEquals(R.id.relative_view21, rules[RelativeLayout.RIGHT_OF]); 244 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_RTL); 245 rules = layoutParams.getRules(); 246 assertEquals(R.id.relative_view21, rules[RelativeLayout.ALIGN_TOP]); 247 assertEquals(R.id.relative_view22, rules[RelativeLayout.ALIGN_BOTTOM]); 248 assertEquals(0, rules[RelativeLayout.END_OF]); 249 assertEquals(R.id.relative_view21, rules[RelativeLayout.LEFT_OF]); 250 assertEquals(0, rules[RelativeLayout.RIGHT_OF]); 251 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_LTR); 252 253 // view4, has same right position with view3 and above view3. 254 // TEST: android:layout_alignEnd; android:layout_above 255 View view4 = mActivity.findViewById(R.id.relative_view24); 256 ViewAsserts.assertRightAligned(view3, view4); 257 assertEquals(view3.getTop(), view4.getBottom()); 258 layoutParams = (RelativeLayout.LayoutParams) (view4.getLayoutParams()); 259 rules = layoutParams.getRules(); 260 assertEquals(0, rules[RelativeLayout.ALIGN_END]); 261 assertEquals(0, rules[RelativeLayout.ALIGN_LEFT]); 262 assertEquals(R.id.relative_view23, rules[RelativeLayout.ALIGN_RIGHT]); 263 assertEquals(R.id.relative_view23, rules[RelativeLayout.ABOVE]); 264 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_RTL); 265 rules = layoutParams.getRules(); 266 assertEquals(0, rules[RelativeLayout.ALIGN_END]); 267 assertEquals(R.id.relative_view23, rules[RelativeLayout.ALIGN_LEFT]); 268 assertEquals(0, rules[RelativeLayout.ALIGN_RIGHT]); 269 assertEquals(R.id.relative_view23, rules[RelativeLayout.ABOVE]); 270 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_LTR); 271 272 // view5 goes on the left-bottom. 273 // TEST: android:layout_alignParentBottom; android:layout_alignParentStart 274 View view5 = mActivity.findViewById(R.id.relative_view25); 275 ViewAsserts.assertLeftAligned(relativeLayout, view5); 276 ViewAsserts.assertBottomAligned(relativeLayout, view5); 277 layoutParams = (RelativeLayout.LayoutParams) (view5.getLayoutParams()); 278 rules = layoutParams.getRules(); 279 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_BOTTOM]); 280 assertEquals(0, rules[RelativeLayout.ALIGN_PARENT_START]); 281 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_LEFT]); 282 assertEquals(0, rules[RelativeLayout.ALIGN_PARENT_RIGHT]); 283 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_RTL); 284 rules = layoutParams.getRules(); 285 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_BOTTOM]); 286 assertEquals(0, rules[RelativeLayout.ALIGN_PARENT_START]); 287 assertEquals(0, rules[RelativeLayout.ALIGN_PARENT_LEFT]); 288 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_RIGHT]); 289 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_LTR); 290 291 // view6 goes on the top-right. 292 // TEST: android:layout_alignParentTop; android:layout_alignParentEnd 293 View view6 = mActivity.findViewById(R.id.relative_view26); 294 ViewAsserts.assertTopAligned(relativeLayout, view6); 295 ViewAsserts.assertRightAligned(relativeLayout, view6); 296 layoutParams = (RelativeLayout.LayoutParams) (view6.getLayoutParams()); 297 rules = layoutParams.getRules(); 298 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_TOP]); 299 assertEquals(0, rules[RelativeLayout.ALIGN_PARENT_END]); 300 assertEquals(0, rules[RelativeLayout.ALIGN_PARENT_LEFT]); 301 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_RIGHT]); 302 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_RTL); 303 rules = layoutParams.getRules(); 304 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_TOP]); 305 assertEquals(0, rules[RelativeLayout.ALIGN_PARENT_END]); 306 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_LEFT]); 307 assertEquals(0, rules[RelativeLayout.ALIGN_PARENT_RIGHT]); 308 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_LTR); 309 310 // view7, has same baseline with view6 and centered horizontally within its parent. 311 // TEST: android:layout_alignBaseline; android:layout_centerHorizontal 312 View view7 = mActivity.findViewById(R.id.relative_view27); 313 ViewAsserts.assertBaselineAligned(view6, view7); 314 ViewAsserts.assertHorizontalCenterAligned(relativeLayout, view7); 315 layoutParams = (RelativeLayout.LayoutParams) (view7.getLayoutParams()); 316 rules = layoutParams.getRules(); 317 assertEquals(R.id.relative_view26, rules[RelativeLayout.ALIGN_BASELINE]); 318 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.CENTER_HORIZONTAL]); 319 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_RTL); 320 rules = layoutParams.getRules(); 321 assertEquals(R.id.relative_view26, rules[RelativeLayout.ALIGN_BASELINE]); 322 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.CENTER_HORIZONTAL]); 323 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_LTR); 324 325 // view8, centered vertically within its parent and on the left of view1. 326 // TEST: android:layout_toStartOf; android:layout_centerVertical 327 View view8 = mActivity.findViewById(R.id.relative_view28); 328 ViewAsserts.assertVerticalCenterAligned(relativeLayout, view8); 329 assertEquals(view1.getLeft(), view8.getRight()); 330 layoutParams = (RelativeLayout.LayoutParams) (view8.getLayoutParams()); 331 rules = layoutParams.getRules(); 332 assertEquals(0, rules[RelativeLayout.START_OF]); 333 assertEquals(R.id.relative_view21, rules[RelativeLayout.LEFT_OF]); 334 assertEquals(0, rules[RelativeLayout.RIGHT_OF]); 335 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.CENTER_VERTICAL]); 336 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_RTL); 337 rules = layoutParams.getRules(); 338 assertEquals(0, rules[RelativeLayout.START_OF]); 339 assertEquals(0, rules[RelativeLayout.LEFT_OF]); 340 assertEquals(R.id.relative_view21, rules[RelativeLayout.RIGHT_OF]); 341 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.CENTER_VERTICAL]); 342 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_LTR); 343 344 // view9, has same top and bottom position with view3 and same left position with its parent 345 // TEST: android:layout_alignStart; android:layout_alignTop; android:layout_alignBottom; 346 // android:layout_alignWithParentIfMissing 347 View view9 = mActivity.findViewById(R.id.relative_view29); 348 ViewAsserts.assertTopAligned(view3, view9); 349 ViewAsserts.assertBottomAligned(view3, view9); 350 ViewAsserts.assertLeftAligned(relativeLayout, view9); 351 layoutParams = (RelativeLayout.LayoutParams) (view9.getLayoutParams()); 352 rules = layoutParams.getRules(); 353 assertEquals(0, rules[RelativeLayout.ALIGN_START]); 354 assertEquals(R.id.gravity_bottom, rules[RelativeLayout.ALIGN_LEFT]); 355 assertEquals(0, rules[RelativeLayout.ALIGN_RIGHT]); 356 assertEquals(R.id.relative_view23, rules[RelativeLayout.ALIGN_TOP]); 357 assertEquals(R.id.relative_view23, rules[RelativeLayout.ALIGN_BOTTOM]); 358 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_RTL); 359 rules = layoutParams.getRules(); 360 assertEquals(0, rules[RelativeLayout.ALIGN_START]); 361 assertEquals(0, rules[RelativeLayout.ALIGN_LEFT]); 362 assertEquals(R.id.gravity_bottom, rules[RelativeLayout.ALIGN_RIGHT]); 363 assertEquals(R.id.relative_view23, rules[RelativeLayout.ALIGN_TOP]); 364 assertEquals(R.id.relative_view23, rules[RelativeLayout.ALIGN_BOTTOM]); 365 layoutParams.resolveLayoutDirection(View.LAYOUT_DIRECTION_LTR); 366 } 367 368 @Test 369 public void testAccessRuleVerb() { 370 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200, 300); 371 int rules[] = layoutParams.getRules(); 372 373 // normal value 374 assertEquals(0, rules[RelativeLayout.CENTER_IN_PARENT]); 375 layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); 376 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.CENTER_IN_PARENT]); 377 378 // issue 1695243 379 // not clear what is supposed to happen if verb is must refer to another sibling. 380 assertEquals(0, rules[RelativeLayout.ALIGN_LEFT]); 381 layoutParams.addRule(RelativeLayout.ALIGN_LEFT); 382 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_LEFT]); 383 } 384 385 @Test(expected=ArrayIndexOutOfBoundsException.class) 386 public void testAddRuleVerbTooLow() { 387 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200, 300); 388 389 layoutParams.addRule(-1); 390 } 391 392 @Test(expected=ArrayIndexOutOfBoundsException.class) 393 public void testAddRuleVerbTooHigh() { 394 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200, 300); 395 396 layoutParams.addRule(Integer.MAX_VALUE); 397 } 398 399 @Test 400 public void testAccessRuleVerbSubject() { 401 int rules[]; 402 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200, 300); 403 404 // normal value 405 layoutParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.relative_view1); 406 rules = layoutParams.getRules(); 407 assertEquals(R.id.relative_view1, rules[RelativeLayout.ALIGN_LEFT]); 408 409 layoutParams.addRule(RelativeLayout.ALIGN_LEFT, 0); 410 rules = layoutParams.getRules(); 411 assertEquals(0, rules[RelativeLayout.ALIGN_LEFT]); 412 413 layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); 414 rules = layoutParams.getRules(); 415 assertEquals(0, rules[RelativeLayout.ALIGN_PARENT_LEFT]); 416 417 layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 418 rules = layoutParams.getRules(); 419 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_PARENT_LEFT]); 420 421 // exceptional value 422 layoutParams.addRule(RelativeLayout.ALIGN_LEFT, RelativeLayout.TRUE); 423 rules = layoutParams.getRules(); 424 assertEquals(RelativeLayout.TRUE, rules[RelativeLayout.ALIGN_LEFT]); 425 426 layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, R.id.relative_view1); 427 rules = layoutParams.getRules(); 428 assertEquals(R.id.relative_view1, rules[RelativeLayout.ALIGN_PARENT_LEFT]); 429 430 layoutParams.addRule(RelativeLayout.ALIGN_LEFT, Integer.MAX_VALUE); 431 rules = layoutParams.getRules(); 432 assertEquals(Integer.MAX_VALUE, rules[RelativeLayout.ALIGN_LEFT]); 433 434 layoutParams.addRule(RelativeLayout.ALIGN_LEFT, Integer.MIN_VALUE); 435 rules = layoutParams.getRules(); 436 assertEquals(Integer.MIN_VALUE, rules[RelativeLayout.ALIGN_LEFT]); 437 } 438 439 @Test(expected=ArrayIndexOutOfBoundsException.class) 440 public void testAddRuleVerbSubjectTooLow() { 441 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200, 300); 442 443 layoutParams.addRule(-1, 0); 444 } 445 446 @Test(expected=ArrayIndexOutOfBoundsException.class) 447 public void testAddRuleVerbSubjectTooHigh() { 448 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200, 300); 449 450 layoutParams.addRule(Integer.MAX_VALUE, 0); 451 } 452 453 @Test 454 public void testRemoveRule() { 455 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200, 300); 456 457 layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE); 458 layoutParams.resolveLayoutDirection(LayoutDirection.LTR); 459 assertEquals(0, layoutParams.getRule(RelativeLayout.ALIGN_PARENT_START)); 460 assertEquals(RelativeLayout.TRUE, layoutParams.getRule(RelativeLayout.ALIGN_PARENT_LEFT)); 461 assertEquals(0, layoutParams.getRule(RelativeLayout.CENTER_HORIZONTAL)); 462 463 layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START); 464 layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 465 layoutParams.resolveLayoutDirection(LayoutDirection.LTR); 466 assertEquals(0, layoutParams.getRule(RelativeLayout.ALIGN_PARENT_START)); 467 assertEquals(0, layoutParams.getRule(RelativeLayout.ALIGN_PARENT_LEFT)); 468 assertEquals(RelativeLayout.TRUE, layoutParams.getRule(RelativeLayout.CENTER_HORIZONTAL)); 469 470 layoutParams.removeRule(RelativeLayout.CENTER_HORIZONTAL); 471 layoutParams.resolveLayoutDirection(LayoutDirection.LTR); 472 assertEquals(0, layoutParams.getRule(RelativeLayout.ALIGN_PARENT_START)); 473 assertEquals(0, layoutParams.getRule(RelativeLayout.ALIGN_PARENT_LEFT)); 474 assertEquals(0, layoutParams.getRule(RelativeLayout.CENTER_HORIZONTAL)); 475 } 476 477 @Test 478 public void testDebug() { 479 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200, 300); 480 assertNotNull(layoutParams.debug("test: ")); 481 } 482 } 483