1 /* 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.scene.layout; 27 28 import static org.junit.Assert.assertEquals; 29 import static org.junit.Assert.assertNull; 30 import static org.junit.Assert.assertTrue; 31 import javafx.geometry.Insets; 32 import javafx.geometry.Orientation; 33 import javafx.geometry.Pos; 34 import javafx.scene.Node; 35 import javafx.scene.shape.Rectangle; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 40 41 public class HBoxTest { 42 HBox hbox; 43 44 @Before public void setUp() { 45 this.hbox = new HBox(); 46 } 47 48 49 @Test public void testHBoxDefaults() { 50 assertEquals(0, hbox.getSpacing(), 1e-100); 51 assertTrue(hbox.isFillHeight()); 52 assertEquals(Pos.TOP_LEFT, hbox.getAlignment()); 53 } 54 55 @Test public void testHBoxNulls() { 56 hbox.setAlignment(null); 57 58 // this musn't throw NPE 59 hbox.autosize(); 60 hbox.layout(); 61 62 assertNull(null, hbox.getAlignment()); 63 assertNull(null, hbox.alignmentProperty().get()); 64 } 65 66 @Test public void testSimpleHBox() { 67 MockResizable child1 = new MockResizable(100,200, 300,400, 500,600); 68 Rectangle child2 = new Rectangle(100, 100); 69 hbox.getChildren().addAll(child1, child2); 70 71 assertEquals(200, hbox.minWidth(-1), 1e-100); 72 assertEquals(200, hbox.minHeight(-1), 1e-100); 73 assertEquals(400, hbox.prefWidth(-1), 1e-100); 74 assertEquals(400, hbox.prefHeight(-1), 1e-100); 75 76 hbox.autosize(); 77 hbox.layout(); 78 assertEquals(0, child1.getLayoutX(), 1e-100); 79 assertEquals(0, child1.getLayoutY(), 1e-100); 80 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 81 assertEquals(400, child1.getLayoutBounds().getHeight(), 1e-100); 82 assertEquals(300, child2.getLayoutX(), 1e-100); 83 assertEquals(0, child2.getLayoutY(), 1e-100); 84 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 85 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 86 87 hbox.resize(500,500); 88 hbox.layout(); 89 assertEquals(0, child1.getLayoutX(), 1e-100); 90 assertEquals(0, child1.getLayoutY(), 1e-100); 91 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 92 assertEquals(500, child1.getLayoutBounds().getHeight(), 1e-100); 93 assertEquals(300, child2.getLayoutX(), 1e-100); 94 assertEquals(0, child2.getLayoutY(), 1e-100); 95 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 96 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 97 } 98 99 @Test public void testHBoxSpacing() { 100 hbox.setSpacing(10); 101 MockResizable child1 = new MockResizable(100,200, 300,400, 500,600); 102 Rectangle child2 = new Rectangle(100, 100); 103 hbox.getChildren().addAll(child1, child2); 104 105 assertEquals(210, hbox.minWidth(-1), 1e-100); 106 assertEquals(200, hbox.minHeight(-1), 1e-100); 107 assertEquals(410, hbox.prefWidth(-1), 1e-100); 108 assertEquals(400, hbox.prefHeight(-1), 1e-100); 109 110 hbox.autosize(); 111 hbox.layout(); 112 assertEquals(0, child1.getLayoutX(), 1e-100); 113 assertEquals(0, child1.getLayoutY(), 1e-100); 114 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 115 assertEquals(400, child1.getLayoutBounds().getHeight(), 1e-100); 116 assertEquals(310, child2.getLayoutX(), 1e-100); 117 assertEquals(0, child2.getLayoutY(), 1e-100); 118 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 119 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 120 121 hbox.resize(500,500); 122 hbox.layout(); 123 assertEquals(0, child1.getLayoutX(), 1e-100); 124 assertEquals(0, child1.getLayoutY(), 1e-100); 125 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 126 assertEquals(500, child1.getLayoutBounds().getHeight(), 1e-100); 127 assertEquals(310, child2.getLayoutX(), 1e-100); 128 assertEquals(0, child2.getLayoutY(), 1e-100); 129 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 130 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 131 } 132 133 @Test public void testHBoxFillHeight() { 134 hbox.setFillHeight(false); 135 hbox.setAlignment(Pos.TOP_CENTER); 136 MockResizable child1 = new MockResizable(100,100, 200,300, 500,600); 137 MockResizable child2 = new MockResizable(100,100, 100, 400, 800, 800); 138 hbox.getChildren().addAll(child1, child2); 139 140 assertEquals(200, hbox.minWidth(-1), 1e-100); 141 assertEquals(100, hbox.minHeight(-1), 1e-100); 142 assertEquals(300, hbox.prefWidth(-1), 1e-100); 143 assertEquals(400, hbox.prefHeight(-1), 1e-100); 144 145 hbox.autosize(); 146 hbox.layout(); 147 assertEquals(0, child1.getLayoutX(), 1e-100); 148 assertEquals(0, child1.getLayoutY(), 1e-100); 149 assertEquals(200, child1.getLayoutBounds().getWidth(), 1e-100); 150 assertEquals(300, child1.getLayoutBounds().getHeight(), 1e-100); 151 assertEquals(200, child2.getLayoutX(), 1e-100); 152 assertEquals(0, child2.getLayoutY(), 1e-100); 153 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 154 assertEquals(400, child2.getLayoutBounds().getHeight(), 1e-100); 155 156 hbox.resize(500,500); 157 hbox.layout(); 158 assertEquals(100, child1.getLayoutX(), 1e-100); 159 assertEquals(0, child1.getLayoutY(), 1e-100); 160 assertEquals(200, child1.getLayoutBounds().getWidth(), 1e-100); 161 assertEquals(300, child1.getLayoutBounds().getHeight(), 1e-100); 162 assertEquals(300, child2.getLayoutX(), 1e-100); 163 assertEquals(0, child2.getLayoutY(), 1e-100); 164 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 165 assertEquals(400, child2.getLayoutBounds().getHeight(), 1e-100); 166 } 167 168 @Test public void testHBoxAlignmentTopLeft() { 169 MockResizable child1 = new MockResizable(300,400); 170 Rectangle child2 = new Rectangle(100, 100); 171 hbox.getChildren().addAll(child1, child2); 172 173 hbox.setAlignment(Pos.TOP_LEFT); 174 hbox.resize(500,500); 175 hbox.layout(); 176 assertEquals(0, child1.getLayoutX(), 1e-100); 177 assertEquals(0, child1.getLayoutY(), 1e-100); 178 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 179 assertEquals(500, child1.getLayoutBounds().getHeight(), 1e-100); 180 assertEquals(300, child2.getLayoutX(), 1e-100); 181 assertEquals(0, child2.getLayoutY(), 1e-100); 182 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 183 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 184 } 185 186 @Test public void testHBoxAlignmentTopCenter() { 187 MockResizable child1 = new MockResizable(300,400); 188 Rectangle child2 = new Rectangle(100, 100); 189 hbox.getChildren().addAll(child1, child2); 190 191 hbox.setAlignment(Pos.TOP_CENTER); 192 hbox.resize(500,500); 193 hbox.layout(); 194 assertEquals(50, child1.getLayoutX(), 1e-100); 195 assertEquals(0, child1.getLayoutY(), 1e-100); 196 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 197 assertEquals(500, child1.getLayoutBounds().getHeight(), 1e-100); 198 assertEquals(350, child2.getLayoutX(), 1e-100); 199 assertEquals(0, child2.getLayoutY(), 1e-100); 200 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 201 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 202 } 203 204 @Test public void testHBoxAlignmentTopRight() { 205 MockResizable child1 = new MockResizable(300,400); 206 Rectangle child2 = new Rectangle(100, 100); 207 hbox.getChildren().addAll(child1, child2); 208 209 hbox.setAlignment(Pos.TOP_RIGHT); 210 hbox.resize(500,500); 211 hbox.layout(); 212 assertEquals(100, child1.getLayoutX(), 1e-100); 213 assertEquals(0, child1.getLayoutY(), 1e-100); 214 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 215 assertEquals(500, child1.getLayoutBounds().getHeight(), 1e-100); 216 assertEquals(400, child2.getLayoutX(), 1e-100); 217 assertEquals(0, child2.getLayoutY(), 1e-100); 218 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 219 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 220 } 221 222 @Test public void testHBoxAlignmentCenterLeft() { 223 MockResizable child1 = new MockResizable(300,400); 224 Rectangle child2 = new Rectangle(100, 100); 225 hbox.getChildren().addAll(child1, child2); 226 227 hbox.setAlignment(Pos.CENTER_LEFT); 228 hbox.resize(500,500); 229 hbox.layout(); 230 assertEquals(0, child1.getLayoutX(), 1e-100); 231 assertEquals(0, child1.getLayoutY(), 1e-100); 232 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 233 assertEquals(500, child1.getLayoutBounds().getHeight(), 1e-100); 234 assertEquals(300, child2.getLayoutX(), 1e-100); 235 assertEquals(200, child2.getLayoutY(), 1e-100); 236 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 237 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 238 } 239 240 @Test public void testHBoxAlignmentCenter() { 241 MockResizable child1 = new MockResizable(300,400); 242 Rectangle child2 = new Rectangle(100, 100); 243 hbox.getChildren().addAll(child1, child2); 244 245 hbox.setAlignment(Pos.CENTER); 246 hbox.resize(500,500); 247 hbox.layout(); 248 assertEquals(50, child1.getLayoutX(), 1e-100); 249 assertEquals(0, child1.getLayoutY(), 1e-100); 250 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 251 assertEquals(500, child1.getLayoutBounds().getHeight(), 1e-100); 252 assertEquals(350, child2.getLayoutX(), 1e-100); 253 assertEquals(200, child2.getLayoutY(), 1e-100); 254 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 255 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 256 } 257 258 @Test public void testHBoxAlignmentCenterRight() { 259 MockResizable child1 = new MockResizable(300,400); 260 Rectangle child2 = new Rectangle(100, 100); 261 hbox.getChildren().addAll(child1, child2); 262 263 hbox.setAlignment(Pos.CENTER_RIGHT); 264 hbox.resize(500,500); 265 hbox.layout(); 266 assertEquals(100, child1.getLayoutX(), 1e-100); 267 assertEquals(0, child1.getLayoutY(), 1e-100); 268 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 269 assertEquals(500, child1.getLayoutBounds().getHeight(), 1e-100); 270 assertEquals(400, child2.getLayoutX(), 1e-100); 271 assertEquals(200, child2.getLayoutY(), 1e-100); 272 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 273 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 274 } 275 276 @Test public void testHBoxAlignmentBottomLeft() { 277 MockResizable child1 = new MockResizable(300,400); 278 Rectangle child2 = new Rectangle(100, 100); 279 hbox.getChildren().addAll(child1, child2); 280 281 hbox.setAlignment(Pos.BOTTOM_LEFT); 282 hbox.resize(500,500); 283 hbox.layout(); 284 assertEquals(0, child1.getLayoutX(), 1e-100); 285 assertEquals(0, child1.getLayoutY(), 1e-100); 286 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 287 assertEquals(500, child1.getLayoutBounds().getHeight(), 1e-100); 288 assertEquals(300, child2.getLayoutX(), 1e-100); 289 assertEquals(400, child2.getLayoutY(), 1e-100); 290 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 291 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 292 } 293 294 @Test public void testHBoxAlignmentBottomCenter() { 295 MockResizable child1 = new MockResizable(300,400); 296 Rectangle child2 = new Rectangle(100, 100); 297 hbox.getChildren().addAll(child1, child2); 298 299 hbox.setAlignment(Pos.BOTTOM_CENTER); 300 hbox.resize(500,500); 301 hbox.layout(); 302 assertEquals(50, child1.getLayoutX(), 1e-100); 303 assertEquals(0, child1.getLayoutY(), 1e-100); 304 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 305 assertEquals(500, child1.getLayoutBounds().getHeight(), 1e-100); 306 assertEquals(350, child2.getLayoutX(), 1e-100); 307 assertEquals(400, child2.getLayoutY(), 1e-100); 308 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 309 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 310 } 311 312 @Test public void testHBoxAlignmentBottomRight() { 313 MockResizable child1 = new MockResizable(300,400); 314 Rectangle child2 = new Rectangle(100, 100); 315 hbox.getChildren().addAll(child1, child2); 316 317 hbox.setAlignment(Pos.BOTTOM_RIGHT); 318 hbox.resize(500,500); 319 hbox.layout(); 320 assertEquals(100, child1.getLayoutX(), 1e-100); 321 assertEquals(0, child1.getLayoutY(), 1e-100); 322 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 323 assertEquals(500, child1.getLayoutBounds().getHeight(), 1e-100); 324 assertEquals(400, child2.getLayoutX(), 1e-100); 325 assertEquals(400, child2.getLayoutY(), 1e-100); 326 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 327 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 328 } 329 330 @Test public void testHBoxAlignmentBaselineLeft() { 331 MockResizable child1 = new MockResizable(300,300); // baseline=290 332 Rectangle child2 = new Rectangle(100, 100); 333 hbox.getChildren().addAll(child1, child2); 334 335 hbox.setFillHeight(false); 336 hbox.setAlignment(Pos.BASELINE_LEFT); 337 hbox.resize(500,500); 338 hbox.layout(); 339 assertEquals(0, child1.getLayoutX(), 1e-100); 340 assertEquals(0, child1.getLayoutY(), 1e-100); 341 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 342 assertEquals(300, child1.getLayoutBounds().getHeight(), 1e-100); 343 assertEquals(300, child2.getLayoutX(), 1e-100); 344 assertEquals(190, child2.getLayoutY(), 1e-100); 345 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 346 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 347 } 348 349 @Test public void testHBoxAlignmentBaselineCenter() { 350 MockResizable child1 = new MockResizable(300,300); // baseline=290 351 Rectangle child2 = new Rectangle(100, 100); 352 hbox.getChildren().addAll(child1, child2); 353 354 hbox.setFillHeight(false); 355 hbox.setAlignment(Pos.BASELINE_CENTER); 356 hbox.resize(500,500); 357 hbox.layout(); 358 assertEquals(50, child1.getLayoutX(), 1e-100); 359 assertEquals(0, child1.getLayoutY(), 1e-100); 360 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 361 assertEquals(300, child1.getLayoutBounds().getHeight(), 1e-100); 362 assertEquals(350, child2.getLayoutX(), 1e-100); 363 assertEquals(190, child2.getLayoutY(), 1e-100); 364 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 365 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 366 } 367 368 @Test public void testHBoxAlignmentBaselineRight() { 369 MockResizable child1 = new MockResizable(300,300); // baseline=290 370 Rectangle child2 = new Rectangle(100, 100); 371 hbox.getChildren().addAll(child1, child2); 372 373 hbox.setFillHeight(false); 374 hbox.setAlignment(Pos.BASELINE_RIGHT); 375 hbox.resize(500,500); 376 hbox.layout(); 377 assertEquals(100, child1.getLayoutX(), 1e-100); 378 assertEquals(0, child1.getLayoutY(), 1e-100); 379 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 380 assertEquals(300, child1.getLayoutBounds().getHeight(), 1e-100); 381 assertEquals(400, child2.getLayoutX(), 1e-100); 382 assertEquals(190, child2.getLayoutY(), 1e-100); 383 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 384 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 385 } 386 387 @Test public void testHBoxAlignmentBaselineLeftComputed() { 388 MockResizable child1 = new MockResizable(300,300) { 389 @Override public double getBaselineOffset() { 390 return BASELINE_OFFSET_SAME_AS_HEIGHT; // should be prefHeight 391 } 392 }; 393 Rectangle child2 = new Rectangle(100, 100); 394 hbox.getChildren().addAll(child1, child2); 395 396 hbox.setFillHeight(false); 397 hbox.setAlignment(Pos.BASELINE_LEFT); 398 hbox.resize(500,500); 399 hbox.layout(); 400 assertEquals(0, child1.getLayoutX(), 1e-100); 401 assertEquals(0, child1.getLayoutY(), 1e-100); 402 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 403 assertEquals(300, child1.getLayoutBounds().getHeight(), 1e-100); 404 assertEquals(300, child2.getLayoutX(), 1e-100); 405 assertEquals(200, child2.getLayoutY(), 1e-100); 406 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 407 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 408 } 409 410 @Test public void testHBoxAlignmentBaselineCenterComputed() { 411 MockResizable child1 = new MockResizable(300,300) { 412 @Override public double getBaselineOffset() { 413 return BASELINE_OFFSET_SAME_AS_HEIGHT; // should be prefHeight 414 } 415 }; 416 Rectangle child2 = new Rectangle(100, 100); 417 hbox.getChildren().addAll(child1, child2); 418 419 hbox.setFillHeight(false); 420 hbox.setAlignment(Pos.BASELINE_CENTER); 421 hbox.resize(500,500); 422 hbox.layout(); 423 assertEquals(50, child1.getLayoutX(), 1e-100); 424 assertEquals(0, child1.getLayoutY(), 1e-100); 425 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 426 assertEquals(300, child1.getLayoutBounds().getHeight(), 1e-100); 427 assertEquals(350, child2.getLayoutX(), 1e-100); 428 assertEquals(200, child2.getLayoutY(), 1e-100); 429 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 430 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 431 } 432 433 @Test public void testHBoxAlignmentBaselineRightComputed() { 434 MockResizable child1 = new MockResizable(300,300) { 435 @Override public double getBaselineOffset() { 436 return BASELINE_OFFSET_SAME_AS_HEIGHT; // should be prefHeight 437 } 438 }; 439 Rectangle child2 = new Rectangle(100, 100); 440 hbox.getChildren().addAll(child1, child2); 441 442 hbox.setFillHeight(false); 443 hbox.setAlignment(Pos.BASELINE_RIGHT); 444 hbox.resize(500,500); 445 hbox.layout(); 446 447 assertEquals(100, child1.getLayoutX(), 1e-100); 448 assertEquals(0, child1.getLayoutY(), 1e-100); 449 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 450 assertEquals(300, child1.getLayoutBounds().getHeight(), 1e-100); 451 assertEquals(400, child2.getLayoutX(), 1e-100); 452 assertEquals(200, child2.getLayoutY(), 1e-100); 453 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 454 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 455 } 456 457 /* Content Bias */ 458 459 @Test public void testHBoxContentBiasNullNoChildHasContentBias() { 460 Rectangle r = new Rectangle(100,100); 461 MockResizable child = new MockResizable(200,100); 462 hbox.getChildren().addAll(r, child); 463 464 assertNull(hbox.getContentBias()); 465 assertEquals(300, hbox.prefWidth(-1), 0); 466 assertEquals(100, hbox.prefHeight(-1), 0); 467 assertEquals(300, hbox.prefWidth(200), 0); 468 } 469 470 @Test public void testHBoxContentBiasVERTICALIfChildVERTICAL() { 471 Rectangle r = new Rectangle(100,50); 472 MockBiased biased = new MockBiased(Orientation.VERTICAL, 200, 100); 473 MockResizable child = new MockResizable(100,100); 474 hbox.getChildren().addAll(r, biased, child); 475 476 assertEquals(Orientation.VERTICAL, hbox.getContentBias()); 477 478 } 479 480 @Test public void testHBoxWithVerticalContentBiasAtPrefSize() { 481 Rectangle rect1 = new Rectangle(100,50); 482 MockBiased biased2 = new MockBiased(Orientation.VERTICAL, 200, 100); 483 MockResizable resizable3 = new MockResizable(100,100); 484 hbox.getChildren().addAll(rect1, biased2, resizable3); 485 486 assertEquals(100, hbox.prefHeight(-1), 0); 487 assertEquals(400, hbox.prefWidth(-1), 0); 488 489 hbox.autosize(); // 400 x 100 490 hbox.layout(); 491 assertEquals(0, rect1.getLayoutX(), 1e-100); 492 assertEquals(0, rect1.getLayoutY(), 1e-100); 493 assertEquals(200, biased2.getLayoutBounds().getWidth(), 1e-100); 494 assertEquals(100, biased2.getLayoutBounds().getHeight(), 1e-100); 495 assertEquals(100, biased2.getLayoutX(), 1e-100); 496 assertEquals(0, biased2.getLayoutY(), 1e-100); 497 assertEquals(100, resizable3.getLayoutBounds().getWidth(), 1e-100); 498 assertEquals(100, resizable3.getLayoutBounds().getHeight(), 1e-100); 499 assertEquals(300, resizable3.getLayoutX(), 1e-100); 500 assertEquals(0, resizable3.getLayoutY(), 1e-100); 501 } 502 503 @Test public void testHBoxWithVerticalContentBiasWithVerticalShrinking() { 504 Rectangle rect1 = new Rectangle(100,50); 505 MockBiased biased2 = new MockBiased(Orientation.VERTICAL, 200, 100); 506 MockResizable resizable3 = new MockResizable(100,100); 507 hbox.getChildren().addAll(rect1, biased2, resizable3); 508 509 assertEquals(600, hbox.prefWidth(50), 0); 510 511 hbox.resize(600,50); // height smaller than pref 512 hbox.layout(); 513 assertEquals(0, rect1.getLayoutX(), 1e-100); 514 assertEquals(0, rect1.getLayoutY(), 1e-100); 515 // Only the width of the child that has a content bias should change. 516 assertEquals(400, biased2.getLayoutBounds().getWidth(), 1e-100); 517 assertEquals(50, biased2.getLayoutBounds().getHeight(), 1e-100); 518 assertEquals(100, biased2.getLayoutX(), 1e-100); 519 assertEquals(0, biased2.getLayoutY(), 1e-100); 520 assertEquals(100, resizable3.getLayoutBounds().getWidth(), 1e-100); 521 assertEquals(50, resizable3.getLayoutBounds().getHeight(), 1e-100); 522 assertEquals(500, resizable3.getLayoutX(), 1e-100); 523 assertEquals(0, resizable3.getLayoutY(), 1e-100); 524 } 525 526 @Test public void testHBoxWithVerticalContentBiasWithVerticalGrowing() { 527 Rectangle rect1 = new Rectangle(100,50); 528 MockBiased biased2 = new MockBiased(Orientation.VERTICAL, 200, 100); 529 MockResizable resizable3 = new MockResizable(100,100); 530 hbox.getChildren().addAll(rect1, biased2, resizable3); 531 532 assertEquals(300, hbox.prefWidth(200), 0); 533 534 hbox.resize(300,200); // height larger than pref 535 hbox.layout(); 536 assertEquals(0, rect1.getLayoutX(), 1e-100); 537 assertEquals(0, rect1.getLayoutY(), 1e-100); 538 assertEquals(100, biased2.getLayoutBounds().getWidth(), 1e-100); 539 assertEquals(200, biased2.getLayoutBounds().getHeight(), 1e-100); 540 assertEquals(100, biased2.getLayoutX(), 1e-100); 541 assertEquals(0, biased2.getLayoutY(), 1e-100); 542 assertEquals(100, resizable3.getLayoutBounds().getWidth(), 1e-100); 543 assertEquals(200, resizable3.getLayoutBounds().getHeight(), 1e-100); 544 assertEquals(200, resizable3.getLayoutX(), 1e-100); 545 assertEquals(0, resizable3.getLayoutY(), 1e-100); 546 } 547 548 @Test public void testHBoxContentBiasHORIZONTALIfChildHORIZONTAL() { 549 Rectangle r = new Rectangle(100,100); 550 MockBiased biased = new MockBiased(Orientation.HORIZONTAL, 200, 100); 551 MockResizable child = new MockResizable(100,100); 552 hbox.getChildren().addAll(r, biased, child); 553 554 assertEquals(Orientation.HORIZONTAL, hbox.getContentBias()); 555 } 556 557 @Test public void testHBoxWithHorizontalContentBiasAtPrefSize() { 558 Rectangle rect1 = new Rectangle(100,50); 559 MockBiased biased2 = new MockBiased(Orientation.HORIZONTAL, 200, 100); 560 MockResizable resizable3 = new MockResizable(100,100); 561 hbox.getChildren().addAll(rect1, biased2, resizable3); 562 563 assertEquals(400, hbox.prefWidth(-1), 0); 564 assertEquals(100, hbox.prefHeight(-1), 0); 565 566 hbox.autosize(); // 400 x 100 567 hbox.layout(); 568 assertEquals(0, rect1.getLayoutX(), 1e-100); 569 assertEquals(0, rect1.getLayoutY(), 1e-100); 570 assertEquals(200, biased2.getLayoutBounds().getWidth(), 1e-100); 571 assertEquals(100, biased2.getLayoutBounds().getHeight(), 1e-100); 572 assertEquals(100, biased2.getLayoutX(), 1e-100); 573 assertEquals(0, biased2.getLayoutY(), 1e-100); 574 assertEquals(100, resizable3.getLayoutBounds().getWidth(), 1e-100); 575 assertEquals(100, resizable3.getLayoutBounds().getHeight(), 1e-100); 576 assertEquals(300, resizable3.getLayoutX(), 1e-100); 577 assertEquals(0, resizable3.getLayoutY(), 1e-100); 578 } 579 580 @Test public void testHBoxWithHorizontalContentBiasWithHorizontalShrinking() { 581 Rectangle rect1 = new Rectangle(100,50); 582 MockBiased biased2 = new MockBiased(Orientation.HORIZONTAL, 200, 100); 583 MockResizable resizable3 = new MockResizable(100,100); 584 hbox.getChildren().addAll(rect1, biased2, resizable3); 585 586 assertEquals(134, hbox.prefHeight(300), 0); 587 588 hbox.resize(300,134); // width less than preferred 589 hbox.layout(); 590 assertEquals(0, rect1.getLayoutX(), 1e-100); 591 assertEquals(0, rect1.getLayoutY(), 1e-100); 592 assertEquals(150, biased2.getLayoutBounds().getWidth(), 1e-100); 593 assertEquals(134, biased2.getLayoutBounds().getHeight(), 1e-100); 594 assertEquals(100, biased2.getLayoutX(), 1e-100); 595 assertEquals(0, biased2.getLayoutY(), 1e-100); 596 assertEquals(50, resizable3.getLayoutBounds().getWidth(), 1e-100); 597 assertEquals(134, resizable3.getLayoutBounds().getHeight(), 1e-100); 598 assertEquals(250, resizable3.getLayoutX(), 1e-100); 599 assertEquals(0, resizable3.getLayoutY(), 1e-100); 600 } 601 602 @Test public void testHBoxWithHorizontalContentBiasWithHorizontalGrowingFillHeightFalse() { 603 hbox.setFillHeight(false); 604 605 Rectangle rect1 = new Rectangle(100,50); 606 MockBiased biased2 = new MockBiased(Orientation.HORIZONTAL, 200, 100); 607 MockResizable resizable3 = new MockResizable(100,100); 608 hbox.getChildren().addAll(rect1, biased2, resizable3); 609 610 HBox.setHgrow(biased2, Priority.ALWAYS); 611 612 assertEquals(100, hbox.prefHeight(500), 0); 613 614 hbox.resize(500,100); // width greater than preferred 615 hbox.layout(); 616 assertEquals(0, rect1.getLayoutX(), 1e-100); 617 assertEquals(0, rect1.getLayoutY(), 1e-100); 618 assertEquals(300, biased2.getLayoutBounds().getWidth(), 1e-100); 619 assertEquals(67, biased2.getLayoutBounds().getHeight(), 1e-100); 620 assertEquals(100, biased2.getLayoutX(), 1e-100); 621 assertEquals(0, biased2.getLayoutY(), 1e-100); 622 assertEquals(100, resizable3.getLayoutBounds().getWidth(), 1e-100); 623 assertEquals(100, resizable3.getLayoutBounds().getHeight(), 1e-100); 624 assertEquals(400, resizable3.getLayoutX(), 1e-100); 625 assertEquals(0, resizable3.getLayoutY(), 1e-100); 626 } 627 628 @Test public void testHBoxWithHorizontalContentBiasWithHorizontalGrowingFillHeightTrue() { 629 hbox.setFillHeight(true); 630 631 Rectangle rect1 = new Rectangle(100,50); 632 MockBiased biased2 = new MockBiased(Orientation.HORIZONTAL, 200, 100); 633 MockResizable resizable3 = new MockResizable(100,100); 634 hbox.getChildren().addAll(rect1, biased2, resizable3); 635 636 HBox.setHgrow(biased2, Priority.ALWAYS); 637 638 assertEquals(100, hbox.prefHeight(500), 0); 639 640 hbox.resize(500,100); // width greater than preferred 641 hbox.layout(); 642 assertEquals(0, rect1.getLayoutX(), 1e-100); 643 assertEquals(0, rect1.getLayoutY(), 1e-100); 644 assertEquals(300, biased2.getLayoutBounds().getWidth(), 1e-100); 645 assertEquals(67, biased2.getLayoutBounds().getHeight(), 1e-100); 646 assertEquals(100, biased2.getLayoutX(), 1e-100); 647 assertEquals(0, biased2.getLayoutY(), 1e-100); 648 assertEquals(100, resizable3.getLayoutBounds().getWidth(), 1e-100); 649 assertEquals(100, resizable3.getLayoutBounds().getHeight(), 1e-100); 650 assertEquals(400, resizable3.getLayoutX(), 1e-100); 651 assertEquals(0, resizable3.getLayoutY(), 1e-100); 652 } 653 654 655 656 657 // @Test public void testHBoxContentBiasNullIfFillHeightFalse() { 658 // hbox.setFillHeight(false); 659 // Rectangle r = new Rectangle(100,100); 660 // MockBiased biased = new MockBiased(Orientation.VERTICAL, 200, 100); 661 // MockResizable child = new MockResizable(100,100); 662 // hbox.getChildren().addAll(r, biased, child); 663 // 664 // assertNull(hbox.getContentBias()); 665 // assertEquals(100, hbox.prefHeight(-1), 0); 666 // assertEquals(400, hbox.prefWidth(-1), 0); 667 // assertEquals(400, hbox.prefWidth(200), 0); 668 // } 669 670 // @Test public void testHBoxContentBiasNullIfChildHORIZONTALAndFillHeightTrue() { 671 // hbox.setFillHeight(true); 672 // Rectangle r = new Rectangle(100,100); 673 // MockBiased biased = new MockBiased(Orientation.HORIZONTAL, 100, 200); 674 // MockResizable child = new MockResizable(100,100); 675 // hbox.getChildren().addAll(r, biased, child); 676 // 677 // assertNull(hbox.getContentBias()); 678 // assertEquals(300, hbox.prefWidth(-1), 0); 679 // assertEquals(200, hbox.prefHeight(-1), 0); 680 // assertEquals(200, hbox.prefHeight(200), 0); 681 // } 682 // 683 // @Test public void testHBoxContentBiasNullIfChildHORIZONTALAndFillHeightFalse() { 684 // hbox.setFillHeight(false); 685 // Rectangle r = new Rectangle(100,100); 686 // MockBiased biased = new MockBiased(Orientation.HORIZONTAL, 100, 200); 687 // MockResizable child = new MockResizable(100,100); 688 // hbox.getChildren().addAll(r, biased, child); 689 // 690 // assertNull(hbox.getContentBias()); 691 // assertEquals(300, hbox.prefWidth(-1), 0); 692 // assertEquals(200, hbox.prefHeight(-1), 0); 693 // assertEquals(200, hbox.prefHeight(200), 0); 694 // } 695 696 @Test public void testHBoxSetMarginConstraint() { 697 MockResizable child1 = new MockResizable(100,200, 300,400, 500,600); 698 699 assertNull(HBox.getMargin(child1)); 700 701 Insets margin = new Insets(10,20,30,40); 702 HBox.setMargin(child1, margin); 703 assertEquals(margin, HBox.getMargin(child1)); 704 705 HBox.setMargin(child1, null); 706 assertNull(HBox.getMargin(child1)); 707 } 708 709 @Test public void testHBoxMarginConstraint() { 710 HBox hbox = new HBox(); 711 MockResizable child1 = new MockResizable(100,200, 300,400, 500,600); 712 Rectangle child2 = new Rectangle(100, 100); 713 hbox.getChildren().addAll(child1, child2); 714 715 HBox.setMargin(child1, new Insets(10,20,30,40)); 716 717 assertEquals(260, hbox.minWidth(-1), 1e-100); 718 assertEquals(240, hbox.minHeight(-1), 1e-100); 719 assertEquals(460, hbox.prefWidth(-1), 1e-100); 720 assertEquals(440, hbox.prefHeight(-1), 1e-100); 721 722 hbox.autosize(); 723 hbox.layout(); 724 assertEquals(40, child1.getLayoutX(), 1e-100); 725 assertEquals(10, child1.getLayoutY(), 1e-100); 726 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 727 assertEquals(400, child1.getLayoutBounds().getHeight(), 1e-100); 728 assertEquals(360, child2.getLayoutX(), 1e-100); 729 assertEquals(0, child2.getLayoutY(), 1e-100); 730 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 731 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 732 733 hbox.resize(500,500); 734 hbox.layout(); 735 assertEquals(40, child1.getLayoutX(), 1e-100); 736 assertEquals(10, child1.getLayoutY(), 1e-100); 737 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 738 assertEquals(460, child1.getLayoutBounds().getHeight(), 1e-100); 739 assertEquals(360, child2.getLayoutX(), 1e-100); 740 assertEquals(0, child2.getLayoutY(), 1e-100); 741 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 742 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 743 } 744 745 @Test public void testHBoxSetHgrowConstraint() { 746 MockResizable child1 = new MockResizable(100,200, 300,400, 500,600); 747 748 assertNull(HBox.getHgrow(child1)); 749 750 HBox.setHgrow(child1, Priority.ALWAYS); 751 assertEquals(Priority.ALWAYS, HBox.getHgrow(child1)); 752 753 HBox.setHgrow(child1, null); 754 assertNull(HBox.getHgrow(child1)); 755 } 756 757 @Test public void testHBoxHgrowConstraint() { 758 HBox hbox = new HBox(); 759 MockResizable child1 = new MockResizable(100,200, 300,400, 500,600); 760 Rectangle child2 = new Rectangle(100, 100); 761 hbox.getChildren().addAll(child1, child2); 762 763 HBox.setHgrow(child1, Priority.ALWAYS); 764 765 assertEquals(200, hbox.minWidth(-1), 1e-100); 766 assertEquals(200, hbox.minHeight(-1), 1e-100); 767 assertEquals(400, hbox.prefWidth(-1), 1e-100); 768 assertEquals(400, hbox.prefHeight(-1), 1e-100); 769 770 hbox.autosize(); 771 hbox.layout(); 772 assertEquals(0, child1.getLayoutX(), 1e-100); 773 assertEquals(0, child1.getLayoutY(), 1e-100); 774 assertEquals(300, child1.getLayoutBounds().getWidth(), 1e-100); 775 assertEquals(400, child1.getLayoutBounds().getHeight(), 1e-100); 776 assertEquals(300, child2.getLayoutX(), 1e-100); 777 assertEquals(0, child2.getLayoutY(), 1e-100); 778 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 779 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 780 781 hbox.resize(500,500); 782 hbox.layout(); 783 assertEquals(0, child1.getLayoutX(), 1e-100); 784 assertEquals(0, child1.getLayoutY(), 1e-100); 785 assertEquals(400, child1.getLayoutBounds().getWidth(), 1e-100); 786 assertEquals(500, child1.getLayoutBounds().getHeight(), 1e-100); 787 assertEquals(400, child2.getLayoutX(), 1e-100); 788 assertEquals(0, child2.getLayoutY(), 1e-100); 789 assertEquals(100, child2.getLayoutBounds().getWidth(), 1e-100); 790 assertEquals(100, child2.getLayoutBounds().getHeight(), 1e-100); 791 } 792 793 @Test public void testHBoxWithHorizontalBiasedChild() { 794 HBox hbox = new HBox(); 795 System.out.println("************************************"); 796 797 MockBiased biased = new MockBiased(Orientation.HORIZONTAL, 100,100); 798 biased.setId("test"); 799 MockBiased biased2 = new MockBiased(Orientation.HORIZONTAL, 200, 200); 800 biased2.setId("test"); 801 HBox.setHgrow(biased, Priority.ALWAYS); 802 803 hbox.getChildren().addAll(biased, biased2); 804 805 assertEquals(300, hbox.prefWidth(-1), 1e-100); 806 assertEquals(200, hbox.prefHeight(-1), 1e-100); 807 assertEquals(20, hbox.minWidth(-1), 1e-100); //MockBias minWidth == 10 808 assertEquals(1, hbox.minHeight(-1), 1e-100); // If both have max width, min height can be indeed 1 809 assertEquals(Math.ceil(Math.max(100 * 100 / 50.0, 200 * 200 / 150.0)), hbox.minHeight(200), 1e-100); 810 System.out.println("************************************"); 811 812 hbox.autosize(); 813 hbox.layout(); 814 assertEquals(0, biased.getLayoutX(), 1e-100); 815 assertEquals(0, biased.getLayoutY(), 1e-100); 816 assertEquals(100, biased.getLayoutBounds().getWidth(), 1e-100); 817 assertEquals(100, biased.getLayoutBounds().getHeight(), 1e-100); 818 assertEquals(100, biased2.getLayoutX(), 1e-100); 819 assertEquals(0, biased2.getLayoutY(), 1e-100); 820 assertEquals(200, biased2.getLayoutBounds().getWidth(), 1e-100); 821 assertEquals(200, biased2.getLayoutBounds().getHeight(), 1e-100); 822 823 hbox.resize(400, 400); 824 hbox.layout(); 825 assertEquals(0, biased.getLayoutX(), 1e-100); 826 assertEquals(0, biased.getLayoutY(), 1e-100); 827 assertEquals(200, biased.getLayoutBounds().getWidth(), 1e-100); 828 assertEquals(50, biased.getLayoutBounds().getHeight(), 1e-100); 829 assertEquals(200, biased2.getLayoutX(), 1e-100); 830 assertEquals(0, biased2.getLayoutY(), 1e-100); 831 assertEquals(200, biased2.getLayoutBounds().getWidth(), 1e-100); 832 assertEquals(200, biased2.getLayoutBounds().getHeight(), 1e-100); 833 834 835 } 836 837 @Test public void testHBoxWithVerticalBiasedChild() { 838 HBox hbox = new HBox(); 839 840 MockBiased biased = new MockBiased(Orientation.VERTICAL, 100,100); 841 MockBiased biased2 = new MockBiased(Orientation.VERTICAL, 200, 200); 842 HBox.setHgrow(biased, Priority.ALWAYS); 843 844 hbox.getChildren().addAll(biased, biased2); 845 846 assertEquals(300, hbox.prefWidth(-1), 1e-100); 847 assertEquals(200, hbox.prefHeight(-1), 1e-100); 848 849 hbox.autosize(); 850 hbox.layout(); 851 assertEquals(0, biased.getLayoutX(), 1e-100); 852 assertEquals(0, biased.getLayoutY(), 1e-100); 853 assertEquals(50, biased.getLayoutBounds().getWidth(), 1e-100); 854 assertEquals(200, biased.getLayoutBounds().getHeight(), 1e-100); 855 assertEquals(50, biased2.getLayoutX(), 1e-100); 856 assertEquals(0, biased2.getLayoutY(), 1e-100); 857 assertEquals(200, biased2.getLayoutBounds().getWidth(), 1e-100); 858 assertEquals(200, biased2.getLayoutBounds().getHeight(), 1e-100); 859 860 hbox.resize(400, 400); 861 hbox.layout(); 862 assertEquals(0, biased.getLayoutX(), 1e-100); 863 assertEquals(0, biased.getLayoutY(), 1e-100); 864 assertEquals(25, biased.getLayoutBounds().getWidth(), 1e-100); 865 assertEquals(400, biased.getLayoutBounds().getHeight(), 1e-100); 866 assertEquals(25, biased2.getLayoutX(), 1e-100); // 867 assertEquals(0, biased2.getLayoutY(), 1e-100); 868 assertEquals(100, biased2.getLayoutBounds().getWidth(), 1e-100); 869 assertEquals(400, biased2.getLayoutBounds().getHeight(), 1e-100); 870 871 } 872 873 @Test public void testMaxWidthHonoredWhenGrowing() { 874 MockRegion child = new MockRegion(10,10,100,100,300,300); 875 MockRegion child2 = new MockRegion(10,10,100,100,200,200); 876 HBox.setHgrow(child, Priority.ALWAYS); 877 HBox.setHgrow(child2, Priority.ALWAYS); 878 879 hbox.getChildren().addAll(child, child2); 880 881 hbox.resize(600,600); 882 hbox.layout(); 883 assertEquals(300, child.getWidth(), 0); 884 assertEquals(0, child.getLayoutX(), 0); 885 assertEquals(200, child2.getWidth(), 0); 886 assertEquals(300, child2.getLayoutX(), 0); 887 } 888 889 @Test public void testBaselineOffset() { 890 hbox.setAlignment(Pos.BASELINE_CENTER); 891 MockResizable child = new MockResizable(100, 100); 892 MockResizable child2 = new MockResizable(100, 150); 893 894 HBox.setMargin(child, new Insets(55, 0, 0, 0)); 895 896 assertEquals(Node.BASELINE_OFFSET_SAME_AS_HEIGHT, hbox.getBaselineOffset(), 1e-100); 897 898 hbox.getChildren().addAll(child, child2); 899 900 assertEquals(145, hbox.getBaselineOffset(), 1e-100); 901 } 902 903 }