modules/graphics/src/test/java/test/javafx/scene/layout/BackgroundTest.java

Print this page
rev 9250 : 8134762: Refactor Javafx graphics module tests for clear separation of tests
Reviewed-by:


   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 javafx.geometry.Insets;
  29 import javafx.geometry.Side;
  30 import javafx.scene.image.Image;






  31 import javafx.scene.paint.Color;
  32 import org.junit.Test;
  33 
  34 import static javafx.scene.layout.BackgroundRepeat.*;
  35 import static org.junit.Assert.*;
  36 
  37 /**
  38  */
  39 public class BackgroundTest {
  40     private static final BackgroundFill[] FILLS_1 = new BackgroundFill[] {
  41             new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY),
  42     };
  43     private static final BackgroundFill[] FILLS_2 = new BackgroundFill[] {
  44             new BackgroundFill(Color.GREEN, new CornerRadii(3), new Insets(4)),
  45             new BackgroundFill(Color.BLUE, new CornerRadii(6), new Insets(8)),
  46     };
  47 
  48     private static final Image IMAGE_1 = new Image("javafx/scene/layout/red.png");
  49     private static final Image IMAGE_2 = new Image("javafx/scene/layout/blue.png");
  50     private static final Image IMAGE_3 = new Image("javafx/scene/layout/green.png");
  51     private static final Image IMAGE_4 = new Image("javafx/scene/layout/yellow.png");
  52 
  53     private static final BackgroundImage[] IMAGES_1 = new BackgroundImage[] {
  54             new BackgroundImage(IMAGE_1, null, null, null, null)
  55     };
  56 
  57     private static final BackgroundImage[] IMAGES_2 = new BackgroundImage[] {
  58             new BackgroundImage(IMAGE_2, SPACE, SPACE, null, null),
  59             new BackgroundImage(IMAGE_3, ROUND, ROUND, null, null),
  60             new BackgroundImage(IMAGE_4, NO_REPEAT, NO_REPEAT, null, null)
  61     };
  62 
  63     @Test public void instanceCreation() {
  64         Background b = new Background(FILLS_1, IMAGES_1);
  65         assertEquals(FILLS_1.length, b.getFills().size(), 0);
  66         assertEquals(FILLS_1[0], b.getFills().get(0));
  67         assertEquals(IMAGES_1.length, b.getImages().size(), 0);
  68         assertEquals(IMAGES_1[0], b.getImages().get(0));
  69     }
  70 
  71     @Test public void instanceCreation2() {


 330     @Test public void notEqualWithNull() {
 331         Background a = new Background((BackgroundFill[])null, null);
 332         assertFalse(a.equals(null));
 333     }
 334 
 335     @Test public void notEqualWithRandom() {
 336         Background a = new Background((BackgroundFill[])null, null);
 337         assertFalse(a.equals("Some random String"));
 338     }
 339 
 340     /**************************************************************************
 341      *                                                                        *
 342      * Tests for getting the computed opaque insets. Like normal insets,      *
 343      * these are positive going inwards.                                      *
 344      *                                                                        *
 345      *************************************************************************/
 346 
 347     @Test public void opaqueInsets_nullFillsResultsInNaN() {
 348         Background b = new Background((BackgroundFill[])null, null);
 349         final double[] trbl = new double[4];
 350         b.computeOpaqueInsets(100, 50, trbl);
 351         assertTrue(Double.isNaN(trbl[0]));
 352         assertTrue(Double.isNaN(trbl[1]));
 353         assertTrue(Double.isNaN(trbl[2]));
 354         assertTrue(Double.isNaN(trbl[3]));
 355     }
 356 
 357     @Test public void opaqueInsets_transparentFillsResultsInNaN() {
 358         BackgroundFill f = new BackgroundFill(Color.TRANSPARENT, null, null);
 359         Background b = new Background(new BackgroundFill[] { f }, null);
 360         final double[] trbl = new double[4];
 361         b.computeOpaqueInsets(100, 50, trbl);
 362         assertTrue(Double.isNaN(trbl[0]));
 363         assertTrue(Double.isNaN(trbl[1]));
 364         assertTrue(Double.isNaN(trbl[2]));
 365         assertTrue(Double.isNaN(trbl[3]));
 366     }
 367 
 368     @Test public void opaqueInsets_transparentFillsResultsInNaN2() {
 369         BackgroundFill f = new BackgroundFill(Color.rgb(255, 0, 0, 0), null, null);
 370         Background b = new Background(new BackgroundFill[] { f }, null);
 371         final double[] trbl = new double[4];
 372         b.computeOpaqueInsets(100, 50, trbl);
 373         assertTrue(Double.isNaN(trbl[0]));
 374         assertTrue(Double.isNaN(trbl[1]));
 375         assertTrue(Double.isNaN(trbl[2]));
 376         assertTrue(Double.isNaN(trbl[3]));
 377     }
 378 
 379     @Test public void opaqueInsets_transparentFillsResultsInNaN3() {
 380         BackgroundFill f = new BackgroundFill(Color.TRANSPARENT, null, null);
 381         BackgroundFill f2 = new BackgroundFill(Color.rgb(255, 0, 0, 0), null, null);
 382         Background b = new Background(new BackgroundFill[] { f, f2 }, null);
 383         final double[] trbl = new double[4];
 384         b.computeOpaqueInsets(100, 50, trbl);
 385         assertTrue(Double.isNaN(trbl[0]));
 386         assertTrue(Double.isNaN(trbl[1]));
 387         assertTrue(Double.isNaN(trbl[2]));
 388         assertTrue(Double.isNaN(trbl[3]));
 389     }
 390 
 391     @Test public void opaqueInsets_transparentFillsMixedWithNonTransparentFills() {
 392         BackgroundFill f = new BackgroundFill(Color.TRANSPARENT, null, null);
 393         BackgroundFill f2 = new BackgroundFill(Color.RED, null, new Insets(1));
 394         Background b = new Background(new BackgroundFill[] { f, f2 }, null);
 395         final double[] trbl = new double[4];
 396         b.computeOpaqueInsets(100, 50, trbl);
 397         assertEquals(1, trbl[0], 0);
 398         assertEquals(1, trbl[1], 0);
 399         assertEquals(1, trbl[2], 0);
 400         assertEquals(1, trbl[3], 0);
 401     }
 402 
 403     @Test public void opaqueInsets_transparentFillsMixedWithNonTransparentFills2() {
 404         BackgroundFill f = new BackgroundFill(Color.TRANSPARENT, null, null);
 405         BackgroundFill f2 = new BackgroundFill(Color.RED, null, new Insets(-1));
 406         Background b = new Background(new BackgroundFill[] { f, f2 }, null);
 407         final double[] trbl = new double[4];
 408         b.computeOpaqueInsets(100, 50, trbl);
 409         assertEquals(-1, trbl[0], 0);
 410         assertEquals(-1, trbl[1], 0);
 411         assertEquals(-1, trbl[2], 0);
 412         assertEquals(-1, trbl[3], 0);
 413     }
 414 
 415     @Test public void opaqueInsets_nestedOpaqueRectangles_LargestRectangleUsed() {
 416         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(0));
 417         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(1));
 418         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(2));
 419         Background b = new Background(new BackgroundFill[] { f, f2, f3 }, null);
 420         final double[] trbl = new double[4];
 421         b.computeOpaqueInsets(100, 50, trbl);
 422         assertEquals(0, trbl[0], 0);
 423         assertEquals(0, trbl[1], 0);
 424         assertEquals(0, trbl[2], 0);
 425         assertEquals(0, trbl[3], 0);
 426     }
 427 
 428     @Test public void opaqueInsets_nestedOpaqueRectangles_LargestRectangleUsed2() {
 429         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(-1));
 430         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(0));
 431         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(1));
 432         Background b = new Background(new BackgroundFill[] { f, f2, f3 }, null);
 433         final double[] trbl = new double[4];
 434         b.computeOpaqueInsets(100, 50, trbl);
 435         assertEquals(-1, trbl[0], 0);
 436         assertEquals(-1, trbl[1], 0);
 437         assertEquals(-1, trbl[2], 0);
 438         assertEquals(-1, trbl[3], 0);
 439     }
 440 
 441     @Test public void opaqueInsets_nestedOpaqueRectangles_LargestRectangleUsed3() {
 442         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(10));
 443         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(1));
 444         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(2));
 445         Background b = new Background(new BackgroundFill[] { f, f2, f3 }, null);
 446         final double[] trbl = new double[4];
 447         b.computeOpaqueInsets(100, 50, trbl);
 448         assertEquals(1, trbl[0], 0);
 449         assertEquals(1, trbl[1], 0);
 450         assertEquals(1, trbl[2], 0);
 451         assertEquals(1, trbl[3], 0);
 452     }
 453 
 454     @Test public void opaqueInsets_nestedOpaqueRectangles_LargestRectangleUsed4() {
 455         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(0));
 456         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(1));
 457         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(-2));
 458         Background b = new Background(new BackgroundFill[] { f, f2, f3 }, null);
 459         final double[] trbl = new double[4];
 460         b.computeOpaqueInsets(100, 50, trbl);
 461         assertEquals(-2, trbl[0], 0);
 462         assertEquals(-2, trbl[1], 0);
 463         assertEquals(-2, trbl[2], 0);
 464         assertEquals(-2, trbl[3], 0);
 465     }
 466 
 467     @Test public void opaqueInsets_offsetOpaqueRectangles_completelyContained_LargestRectangleUsed() {
 468         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(0));
 469         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(1, 0, 0, 0));
 470         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(0, 1, 0, 0));
 471         BackgroundFill f4 = new BackgroundFill(Color.YELLOW, null, new Insets(0, 0, 1, 0));
 472         BackgroundFill f5 = new BackgroundFill(Color.CYAN, null, new Insets(0, 0, 0, 1));
 473         Background b = new Background(new BackgroundFill[] { f, f2, f3, f4, f5 }, null);
 474         final double[] trbl = new double[4];
 475         b.computeOpaqueInsets(100, 50, trbl);
 476         assertEquals(0, trbl[0], 0);
 477         assertEquals(0, trbl[1], 0);
 478         assertEquals(0, trbl[2], 0);
 479         assertEquals(0, trbl[3], 0);
 480     }
 481 
 482     // Even when the big rectangle is not the first, does it still work?
 483     @Test public void opaqueInsets_offsetOpaqueRectangles_completelyContained_LargestRectangleUsed2() {
 484         BackgroundFill f = new BackgroundFill(Color.YELLOW, null, new Insets(0, 0, 1, 0));
 485         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(1, 0, 0, 0));
 486         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(0, 1, 0, 0));
 487         BackgroundFill f4 = new BackgroundFill(Color.RED, null, new Insets(0));
 488         BackgroundFill f5 = new BackgroundFill(Color.CYAN, null, new Insets(0, 0, 0, 1));
 489         Background b = new Background(new BackgroundFill[] { f, f2, f3, f4, f5 }, null);
 490         final double[] trbl = new double[4];
 491         b.computeOpaqueInsets(100, 50, trbl);
 492         assertEquals(0, trbl[0], 0);
 493         assertEquals(0, trbl[1], 0);
 494         assertEquals(0, trbl[2], 0);
 495         assertEquals(0, trbl[3], 0);
 496     }
 497 
 498     @Test public void opaqueInsets_offsetOpaqueRectangles_UnionUsed() {
 499         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(10, 0, 0, 0));
 500         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(0, 10, 0, 0));
 501         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(0, 0, 10, 0));
 502         BackgroundFill f4 = new BackgroundFill(Color.YELLOW, null, new Insets(0, 0, 0, 10));
 503         Background b = new Background(new BackgroundFill[] { f, f2, f3, f4 }, null);
 504         final double[] trbl = new double[4];
 505         b.computeOpaqueInsets(100, 50, trbl);
 506         assertEquals(0, trbl[0], 0);
 507         assertEquals(0, trbl[1], 0);
 508         assertEquals(0, trbl[2], 0);
 509         assertEquals(0, trbl[3], 0);
 510     }
 511 
 512     @Test public void opaqueInsets_offsetOpaqueRectangles_UnionUsed2() {
 513         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(10, 0, 0, 0));
 514         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(10, 10, 0, 0));
 515         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(10, 10, 10, 0));
 516         BackgroundFill f4 = new BackgroundFill(Color.YELLOW, null, new Insets(10, 10, 10, 10));
 517         Background b = new Background(new BackgroundFill[] { f, f2, f3, f4 }, null);
 518         final double[] trbl = new double[4];
 519         b.computeOpaqueInsets(100, 50, trbl);
 520         assertEquals(10, trbl[0], 0);
 521         assertEquals(0, trbl[1], 0);
 522         assertEquals(0, trbl[2], 0);
 523         assertEquals(0, trbl[3], 0);
 524     }
 525 
 526     // There are actually 3 possible outcomes from this test, and the one being tested is
 527     // the one currently being used. We could use the bounds of f, the bounds of f2, or the
 528     // intersection of the bounds of f and f2. It turns out in this case, the intersection
 529     // would be smaller, but the bounds of f and f2 are equal in size.
 530     @Test public void opaqueInsets_offsetOpaqueRectangles_LargestUsed() {
 531         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(10));
 532         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(20, 0, 0, 20));
 533         Background b = new Background(new BackgroundFill[] { f, f2 }, null);
 534         final double[] trbl = new double[4];
 535         b.computeOpaqueInsets(100, 50, trbl);
 536         assertEquals(10, trbl[0], 0);
 537         assertEquals(10, trbl[1], 0);
 538         assertEquals(10, trbl[2], 0);
 539         assertEquals(10, trbl[3], 0);
 540     }
 541 
 542     // NOTE: For these corner radii tests, I only need to know that the opaque region
 543     // of a single fill is correct for any given set of corner radii, because after
 544     // the opaque region for a single fill is computed, thereafter the rest of the
 545     // implementation is all the same
 546     @Test public void opaqueInsets_uniformCornerRadii() {
 547         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(3), null);
 548         Background b = new Background(new BackgroundFill[] { f }, null);
 549         final double[] trbl = new double[4];
 550         b.computeOpaqueInsets(100, 50, trbl);
 551         assertEquals(1.5, trbl[0], 0);
 552         assertEquals(1.5, trbl[1], 0);
 553         assertEquals(1.5, trbl[2], 0);
 554         assertEquals(1.5, trbl[3], 0);
 555     }
 556 
 557     @Test public void opaqueInsets_nonUniformCornerRadii() {
 558         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(1, 2, 3, 4, false), null);
 559         Background b = new Background(new BackgroundFill[] { f }, null);
 560         final double[] trbl = new double[4];
 561         b.computeOpaqueInsets(100, 50, trbl);
 562         assertEquals(1, trbl[0], 0);
 563         assertEquals(1.5, trbl[1], 0);
 564         assertEquals(2, trbl[2], 0);
 565         assertEquals(2, trbl[3], 0);
 566     }
 567 
 568     @Test public void opaqueInsets_nonUniformCornerRadii2() {
 569         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(1, 2, 3, 4, 5, 6, 7, 8,
 570                                                                          false, false, false, false,
 571                                                                          false, false, false, false), null);
 572         Background b = new Background(new BackgroundFill[] { f }, null);
 573         final double[] trbl = new double[4];
 574         b.computeOpaqueInsets(100, 50, trbl);
 575         assertEquals(1.5, trbl[0], 0);
 576         assertEquals(2.5, trbl[1], 0);
 577         assertEquals(3.5, trbl[2], 0);
 578         assertEquals(4, trbl[3], 0);
 579     }
 580 
 581     @Test public void opaqueInsetsPercent_uniformCornerRadii() {
 582         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(.1, true), null);
 583         Background b = new Background(new BackgroundFill[] { f }, null);
 584         final double[] trbl = new double[4];
 585         b.computeOpaqueInsets(100, 50, trbl);
 586         assertEquals(2.5, trbl[0], 0);
 587         assertEquals(5, trbl[1], 0);
 588         assertEquals(2.5, trbl[2], 0);
 589         assertEquals(5, trbl[3], 0);
 590     }
 591 
 592     @Test public void opaqueInsetsPercent_nonUniformCornerRadii() {
 593         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(.1, .2, .3, .4, true), null);
 594         Background b = new Background(new BackgroundFill[] { f }, null);
 595         final double[] trbl = new double[4];
 596         b.computeOpaqueInsets(100, 50, trbl);
 597         assertEquals(5, trbl[0], 0); // top-left-vertical is 2.5, but top-right-vertical is 5
 598         assertEquals(15, trbl[1], 0); // top-right-horizontal is 5, but bottom-right-horizontal is 15
 599         assertEquals(10, trbl[2], 0); // bottom-right-vertical is 7.5, but bottom-left-vertical is 10
 600         assertEquals(20, trbl[3], 0); // bottom-left-horizontal dominates at 20
 601     }
 602 
 603     @Test public void opaqueInsetsPercent_nonUniformCornerRadii2() {
 604         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(.1, .15, .2, .25, .3, .35, .4, .45,
 605                                                                          true, true, true, true,
 606                                                                          true, true, true, true), null);
 607         Background b = new Background(new BackgroundFill[] { f }, null);
 608         final double[] trbl = new double[4];
 609         b.computeOpaqueInsets(100, 50, trbl);
 610         assertEquals(5, trbl[0], 0); // top-left-vertical is 3.75, but top-right-vertical is 5
 611         assertEquals(15, trbl[1], 0); // top-right-horizontal is 12.5, but bottom-right-horizontal is 15
 612         assertEquals(10, trbl[2], 0); // bottom-right-vertical is 8.75, but bottom-left-vertical is 10
 613         assertEquals(22.5, trbl[3], 0); // bottom-left-horizontal dominates at 22.5
 614     }
 615 
 616     @Test public void opaqueInsetsPercent_nonUniformCornerRadii3() {
 617         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(.1, 15, .2, 25, .3, 35, .4, 45,
 618                                                                          true, false, true, false,
 619                                                                          true, false, true, false), null);
 620         Background b = new Background(new BackgroundFill[] { f }, null);
 621         final double[] trbl = new double[4];
 622         b.computeOpaqueInsets(100, 50, trbl);
 623         assertEquals(7.5, trbl[0], 0); // top-left-vertical is 7.5, and top-right-vertical is 5
 624         assertEquals(15, trbl[1], 0); // top-right-horizontal is 12.5, but bottom-right-horizontal is 15
 625         assertEquals(17.5, trbl[2], 0); // bottom-right-vertical is 17.5, and bottom-left-vertical is 10
 626         assertEquals(22.5, trbl[3], 0); // bottom-left-horizontal dominates at 22.5
 627     }
 628 
 629     @Test public void opaqueInsetsPercent_nonUniformCornerRadii4() {
 630         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(10, .15, 20, .25, 30, .35, 40, .45,
 631                                                                          false, true, false, true,
 632                                                                          false, true, false, true), null);
 633         Background b = new Background(new BackgroundFill[] { f }, null);
 634         final double[] trbl = new double[4];
 635         b.computeOpaqueInsets(100, 50, trbl);
 636         assertEquals(10, trbl[0], 0); // top-left-vertical is 3.75, but top-right-vertical is 10
 637         assertEquals(15, trbl[1], 0); // top-right-horizontal is 12.5, but bottom-right-horizontal is 15
 638         assertEquals(20, trbl[2], 0); // bottom-right-vertical is 8.75, but bottom-left-vertical is 20
 639         assertEquals(22.5, trbl[3], 0); // bottom-left-horizontal dominates at 22.5
 640     }
 641 
 642     @Test public void backgroundFillsArePercentageBased_AllPercentageBased() {
 643         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(.5, true), new Insets(10, 0, 0, 0));
 644         BackgroundFill f2 = new BackgroundFill(Color.GREEN, new CornerRadii(.4, true), new Insets(0, 10, 0, 0));
 645         BackgroundFill f3 = new BackgroundFill(Color.BLUE, new CornerRadii(.3, true), new Insets(0, 0, 10, 0));
 646         BackgroundFill f4 = new BackgroundFill(Color.YELLOW, new CornerRadii(.2, true), new Insets(0, 0, 0, 10));
 647         Background b = new Background(new BackgroundFill[] { f, f2, f3, f4 }, null);
 648         assertTrue(b.isFillPercentageBased());
 649     }
 650 
 651     @Test public void backgroundFillsArePercentageBased_OnePercentageBased() {
 652         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(5), new Insets(10, 0, 0, 0));
 653         BackgroundFill f2 = new BackgroundFill(Color.GREEN, new CornerRadii(4), new Insets(0, 10, 0, 0));
 654         BackgroundFill f3 = new BackgroundFill(Color.BLUE, new CornerRadii(.3, true), new Insets(0, 0, 10, 0));
 655         BackgroundFill f4 = new BackgroundFill(Color.YELLOW, new CornerRadii(2), new Insets(0, 0, 0, 10));




   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 test.javafx.scene.layout;
  27 
  28 import javafx.geometry.Insets;
  29 import javafx.geometry.Side;
  30 import javafx.scene.image.Image;
  31 import javafx.scene.layout.Background;
  32 import javafx.scene.layout.BackgroundFill;
  33 import javafx.scene.layout.BackgroundImage;
  34 import javafx.scene.layout.BackgroundPosition;
  35 import javafx.scene.layout.BackgroundShim;
  36 import javafx.scene.layout.CornerRadii;
  37 import javafx.scene.paint.Color;
  38 import org.junit.Test;
  39 
  40 import static javafx.scene.layout.BackgroundRepeat.*;
  41 import static org.junit.Assert.*;
  42 
  43 /**
  44  */
  45 public class BackgroundTest {
  46     private static final BackgroundFill[] FILLS_1 = new BackgroundFill[] {
  47             new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY),
  48     };
  49     private static final BackgroundFill[] FILLS_2 = new BackgroundFill[] {
  50             new BackgroundFill(Color.GREEN, new CornerRadii(3), new Insets(4)),
  51             new BackgroundFill(Color.BLUE, new CornerRadii(6), new Insets(8)),
  52     };
  53 
  54     private static final Image IMAGE_1 = new Image("test/javafx/scene/layout/red.png");
  55     private static final Image IMAGE_2 = new Image("test/javafx/scene/layout/blue.png");
  56     private static final Image IMAGE_3 = new Image("test/javafx/scene/layout/green.png");
  57     private static final Image IMAGE_4 = new Image("test/javafx/scene/layout/yellow.png");
  58 
  59     private static final BackgroundImage[] IMAGES_1 = new BackgroundImage[] {
  60             new BackgroundImage(IMAGE_1, null, null, null, null)
  61     };
  62 
  63     private static final BackgroundImage[] IMAGES_2 = new BackgroundImage[] {
  64             new BackgroundImage(IMAGE_2, SPACE, SPACE, null, null),
  65             new BackgroundImage(IMAGE_3, ROUND, ROUND, null, null),
  66             new BackgroundImage(IMAGE_4, NO_REPEAT, NO_REPEAT, null, null)
  67     };
  68 
  69     @Test public void instanceCreation() {
  70         Background b = new Background(FILLS_1, IMAGES_1);
  71         assertEquals(FILLS_1.length, b.getFills().size(), 0);
  72         assertEquals(FILLS_1[0], b.getFills().get(0));
  73         assertEquals(IMAGES_1.length, b.getImages().size(), 0);
  74         assertEquals(IMAGES_1[0], b.getImages().get(0));
  75     }
  76 
  77     @Test public void instanceCreation2() {


 336     @Test public void notEqualWithNull() {
 337         Background a = new Background((BackgroundFill[])null, null);
 338         assertFalse(a.equals(null));
 339     }
 340 
 341     @Test public void notEqualWithRandom() {
 342         Background a = new Background((BackgroundFill[])null, null);
 343         assertFalse(a.equals("Some random String"));
 344     }
 345 
 346     /**************************************************************************
 347      *                                                                        *
 348      * Tests for getting the computed opaque insets. Like normal insets,      *
 349      * these are positive going inwards.                                      *
 350      *                                                                        *
 351      *************************************************************************/
 352 
 353     @Test public void opaqueInsets_nullFillsResultsInNaN() {
 354         Background b = new Background((BackgroundFill[])null, null);
 355         final double[] trbl = new double[4];
 356         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 357         assertTrue(Double.isNaN(trbl[0]));
 358         assertTrue(Double.isNaN(trbl[1]));
 359         assertTrue(Double.isNaN(trbl[2]));
 360         assertTrue(Double.isNaN(trbl[3]));
 361     }
 362 
 363     @Test public void opaqueInsets_transparentFillsResultsInNaN() {
 364         BackgroundFill f = new BackgroundFill(Color.TRANSPARENT, null, null);
 365         Background b = new Background(new BackgroundFill[] { f }, null);
 366         final double[] trbl = new double[4];
 367         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 368         assertTrue(Double.isNaN(trbl[0]));
 369         assertTrue(Double.isNaN(trbl[1]));
 370         assertTrue(Double.isNaN(trbl[2]));
 371         assertTrue(Double.isNaN(trbl[3]));
 372     }
 373 
 374     @Test public void opaqueInsets_transparentFillsResultsInNaN2() {
 375         BackgroundFill f = new BackgroundFill(Color.rgb(255, 0, 0, 0), null, null);
 376         Background b = new Background(new BackgroundFill[] { f }, null);
 377         final double[] trbl = new double[4];
 378         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 379         assertTrue(Double.isNaN(trbl[0]));
 380         assertTrue(Double.isNaN(trbl[1]));
 381         assertTrue(Double.isNaN(trbl[2]));
 382         assertTrue(Double.isNaN(trbl[3]));
 383     }
 384 
 385     @Test public void opaqueInsets_transparentFillsResultsInNaN3() {
 386         BackgroundFill f = new BackgroundFill(Color.TRANSPARENT, null, null);
 387         BackgroundFill f2 = new BackgroundFill(Color.rgb(255, 0, 0, 0), null, null);
 388         Background b = new Background(new BackgroundFill[] { f, f2 }, null);
 389         final double[] trbl = new double[4];
 390         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 391         assertTrue(Double.isNaN(trbl[0]));
 392         assertTrue(Double.isNaN(trbl[1]));
 393         assertTrue(Double.isNaN(trbl[2]));
 394         assertTrue(Double.isNaN(trbl[3]));
 395     }
 396 
 397     @Test public void opaqueInsets_transparentFillsMixedWithNonTransparentFills() {
 398         BackgroundFill f = new BackgroundFill(Color.TRANSPARENT, null, null);
 399         BackgroundFill f2 = new BackgroundFill(Color.RED, null, new Insets(1));
 400         Background b = new Background(new BackgroundFill[] { f, f2 }, null);
 401         final double[] trbl = new double[4];
 402         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 403         assertEquals(1, trbl[0], 0);
 404         assertEquals(1, trbl[1], 0);
 405         assertEquals(1, trbl[2], 0);
 406         assertEquals(1, trbl[3], 0);
 407     }
 408 
 409     @Test public void opaqueInsets_transparentFillsMixedWithNonTransparentFills2() {
 410         BackgroundFill f = new BackgroundFill(Color.TRANSPARENT, null, null);
 411         BackgroundFill f2 = new BackgroundFill(Color.RED, null, new Insets(-1));
 412         Background b = new Background(new BackgroundFill[] { f, f2 }, null);
 413         final double[] trbl = new double[4];
 414         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 415         assertEquals(-1, trbl[0], 0);
 416         assertEquals(-1, trbl[1], 0);
 417         assertEquals(-1, trbl[2], 0);
 418         assertEquals(-1, trbl[3], 0);
 419     }
 420 
 421     @Test public void opaqueInsets_nestedOpaqueRectangles_LargestRectangleUsed() {
 422         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(0));
 423         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(1));
 424         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(2));
 425         Background b = new Background(new BackgroundFill[] { f, f2, f3 }, null);
 426         final double[] trbl = new double[4];
 427         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 428         assertEquals(0, trbl[0], 0);
 429         assertEquals(0, trbl[1], 0);
 430         assertEquals(0, trbl[2], 0);
 431         assertEquals(0, trbl[3], 0);
 432     }
 433 
 434     @Test public void opaqueInsets_nestedOpaqueRectangles_LargestRectangleUsed2() {
 435         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(-1));
 436         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(0));
 437         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(1));
 438         Background b = new Background(new BackgroundFill[] { f, f2, f3 }, null);
 439         final double[] trbl = new double[4];
 440         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 441         assertEquals(-1, trbl[0], 0);
 442         assertEquals(-1, trbl[1], 0);
 443         assertEquals(-1, trbl[2], 0);
 444         assertEquals(-1, trbl[3], 0);
 445     }
 446 
 447     @Test public void opaqueInsets_nestedOpaqueRectangles_LargestRectangleUsed3() {
 448         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(10));
 449         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(1));
 450         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(2));
 451         Background b = new Background(new BackgroundFill[] { f, f2, f3 }, null);
 452         final double[] trbl = new double[4];
 453         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 454         assertEquals(1, trbl[0], 0);
 455         assertEquals(1, trbl[1], 0);
 456         assertEquals(1, trbl[2], 0);
 457         assertEquals(1, trbl[3], 0);
 458     }
 459 
 460     @Test public void opaqueInsets_nestedOpaqueRectangles_LargestRectangleUsed4() {
 461         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(0));
 462         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(1));
 463         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(-2));
 464         Background b = new Background(new BackgroundFill[] { f, f2, f3 }, null);
 465         final double[] trbl = new double[4];
 466         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 467         assertEquals(-2, trbl[0], 0);
 468         assertEquals(-2, trbl[1], 0);
 469         assertEquals(-2, trbl[2], 0);
 470         assertEquals(-2, trbl[3], 0);
 471     }
 472 
 473     @Test public void opaqueInsets_offsetOpaqueRectangles_completelyContained_LargestRectangleUsed() {
 474         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(0));
 475         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(1, 0, 0, 0));
 476         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(0, 1, 0, 0));
 477         BackgroundFill f4 = new BackgroundFill(Color.YELLOW, null, new Insets(0, 0, 1, 0));
 478         BackgroundFill f5 = new BackgroundFill(Color.CYAN, null, new Insets(0, 0, 0, 1));
 479         Background b = new Background(new BackgroundFill[] { f, f2, f3, f4, f5 }, null);
 480         final double[] trbl = new double[4];
 481         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 482         assertEquals(0, trbl[0], 0);
 483         assertEquals(0, trbl[1], 0);
 484         assertEquals(0, trbl[2], 0);
 485         assertEquals(0, trbl[3], 0);
 486     }
 487 
 488     // Even when the big rectangle is not the first, does it still work?
 489     @Test public void opaqueInsets_offsetOpaqueRectangles_completelyContained_LargestRectangleUsed2() {
 490         BackgroundFill f = new BackgroundFill(Color.YELLOW, null, new Insets(0, 0, 1, 0));
 491         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(1, 0, 0, 0));
 492         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(0, 1, 0, 0));
 493         BackgroundFill f4 = new BackgroundFill(Color.RED, null, new Insets(0));
 494         BackgroundFill f5 = new BackgroundFill(Color.CYAN, null, new Insets(0, 0, 0, 1));
 495         Background b = new Background(new BackgroundFill[] { f, f2, f3, f4, f5 }, null);
 496         final double[] trbl = new double[4];
 497         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 498         assertEquals(0, trbl[0], 0);
 499         assertEquals(0, trbl[1], 0);
 500         assertEquals(0, trbl[2], 0);
 501         assertEquals(0, trbl[3], 0);
 502     }
 503 
 504     @Test public void opaqueInsets_offsetOpaqueRectangles_UnionUsed() {
 505         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(10, 0, 0, 0));
 506         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(0, 10, 0, 0));
 507         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(0, 0, 10, 0));
 508         BackgroundFill f4 = new BackgroundFill(Color.YELLOW, null, new Insets(0, 0, 0, 10));
 509         Background b = new Background(new BackgroundFill[] { f, f2, f3, f4 }, null);
 510         final double[] trbl = new double[4];
 511         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 512         assertEquals(0, trbl[0], 0);
 513         assertEquals(0, trbl[1], 0);
 514         assertEquals(0, trbl[2], 0);
 515         assertEquals(0, trbl[3], 0);
 516     }
 517 
 518     @Test public void opaqueInsets_offsetOpaqueRectangles_UnionUsed2() {
 519         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(10, 0, 0, 0));
 520         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(10, 10, 0, 0));
 521         BackgroundFill f3 = new BackgroundFill(Color.BLUE, null, new Insets(10, 10, 10, 0));
 522         BackgroundFill f4 = new BackgroundFill(Color.YELLOW, null, new Insets(10, 10, 10, 10));
 523         Background b = new Background(new BackgroundFill[] { f, f2, f3, f4 }, null);
 524         final double[] trbl = new double[4];
 525         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 526         assertEquals(10, trbl[0], 0);
 527         assertEquals(0, trbl[1], 0);
 528         assertEquals(0, trbl[2], 0);
 529         assertEquals(0, trbl[3], 0);
 530     }
 531 
 532     // There are actually 3 possible outcomes from this test, and the one being tested is
 533     // the one currently being used. We could use the bounds of f, the bounds of f2, or the
 534     // intersection of the bounds of f and f2. It turns out in this case, the intersection
 535     // would be smaller, but the bounds of f and f2 are equal in size.
 536     @Test public void opaqueInsets_offsetOpaqueRectangles_LargestUsed() {
 537         BackgroundFill f = new BackgroundFill(Color.RED, null, new Insets(10));
 538         BackgroundFill f2 = new BackgroundFill(Color.GREEN, null, new Insets(20, 0, 0, 20));
 539         Background b = new Background(new BackgroundFill[] { f, f2 }, null);
 540         final double[] trbl = new double[4];
 541         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 542         assertEquals(10, trbl[0], 0);
 543         assertEquals(10, trbl[1], 0);
 544         assertEquals(10, trbl[2], 0);
 545         assertEquals(10, trbl[3], 0);
 546     }
 547 
 548     // NOTE: For these corner radii tests, I only need to know that the opaque region
 549     // of a single fill is correct for any given set of corner radii, because after
 550     // the opaque region for a single fill is computed, thereafter the rest of the
 551     // implementation is all the same
 552     @Test public void opaqueInsets_uniformCornerRadii() {
 553         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(3), null);
 554         Background b = new Background(new BackgroundFill[] { f }, null);
 555         final double[] trbl = new double[4];
 556         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 557         assertEquals(1.5, trbl[0], 0);
 558         assertEquals(1.5, trbl[1], 0);
 559         assertEquals(1.5, trbl[2], 0);
 560         assertEquals(1.5, trbl[3], 0);
 561     }
 562 
 563     @Test public void opaqueInsets_nonUniformCornerRadii() {
 564         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(1, 2, 3, 4, false), null);
 565         Background b = new Background(new BackgroundFill[] { f }, null);
 566         final double[] trbl = new double[4];
 567         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 568         assertEquals(1, trbl[0], 0);
 569         assertEquals(1.5, trbl[1], 0);
 570         assertEquals(2, trbl[2], 0);
 571         assertEquals(2, trbl[3], 0);
 572     }
 573 
 574     @Test public void opaqueInsets_nonUniformCornerRadii2() {
 575         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(1, 2, 3, 4, 5, 6, 7, 8,
 576                                                                          false, false, false, false,
 577                                                                          false, false, false, false), null);
 578         Background b = new Background(new BackgroundFill[] { f }, null);
 579         final double[] trbl = new double[4];
 580         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 581         assertEquals(1.5, trbl[0], 0);
 582         assertEquals(2.5, trbl[1], 0);
 583         assertEquals(3.5, trbl[2], 0);
 584         assertEquals(4, trbl[3], 0);
 585     }
 586 
 587     @Test public void opaqueInsetsPercent_uniformCornerRadii() {
 588         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(.1, true), null);
 589         Background b = new Background(new BackgroundFill[] { f }, null);
 590         final double[] trbl = new double[4];
 591         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 592         assertEquals(2.5, trbl[0], 0);
 593         assertEquals(5, trbl[1], 0);
 594         assertEquals(2.5, trbl[2], 0);
 595         assertEquals(5, trbl[3], 0);
 596     }
 597 
 598     @Test public void opaqueInsetsPercent_nonUniformCornerRadii() {
 599         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(.1, .2, .3, .4, true), null);
 600         Background b = new Background(new BackgroundFill[] { f }, null);
 601         final double[] trbl = new double[4];
 602         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 603         assertEquals(5, trbl[0], 0); // top-left-vertical is 2.5, but top-right-vertical is 5
 604         assertEquals(15, trbl[1], 0); // top-right-horizontal is 5, but bottom-right-horizontal is 15
 605         assertEquals(10, trbl[2], 0); // bottom-right-vertical is 7.5, but bottom-left-vertical is 10
 606         assertEquals(20, trbl[3], 0); // bottom-left-horizontal dominates at 20
 607     }
 608 
 609     @Test public void opaqueInsetsPercent_nonUniformCornerRadii2() {
 610         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(.1, .15, .2, .25, .3, .35, .4, .45,
 611                                                                          true, true, true, true,
 612                                                                          true, true, true, true), null);
 613         Background b = new Background(new BackgroundFill[] { f }, null);
 614         final double[] trbl = new double[4];
 615         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 616         assertEquals(5, trbl[0], 0); // top-left-vertical is 3.75, but top-right-vertical is 5
 617         assertEquals(15, trbl[1], 0); // top-right-horizontal is 12.5, but bottom-right-horizontal is 15
 618         assertEquals(10, trbl[2], 0); // bottom-right-vertical is 8.75, but bottom-left-vertical is 10
 619         assertEquals(22.5, trbl[3], 0); // bottom-left-horizontal dominates at 22.5
 620     }
 621 
 622     @Test public void opaqueInsetsPercent_nonUniformCornerRadii3() {
 623         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(.1, 15, .2, 25, .3, 35, .4, 45,
 624                                                                          true, false, true, false,
 625                                                                          true, false, true, false), null);
 626         Background b = new Background(new BackgroundFill[] { f }, null);
 627         final double[] trbl = new double[4];
 628         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 629         assertEquals(7.5, trbl[0], 0); // top-left-vertical is 7.5, and top-right-vertical is 5
 630         assertEquals(15, trbl[1], 0); // top-right-horizontal is 12.5, but bottom-right-horizontal is 15
 631         assertEquals(17.5, trbl[2], 0); // bottom-right-vertical is 17.5, and bottom-left-vertical is 10
 632         assertEquals(22.5, trbl[3], 0); // bottom-left-horizontal dominates at 22.5
 633     }
 634 
 635     @Test public void opaqueInsetsPercent_nonUniformCornerRadii4() {
 636         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(10, .15, 20, .25, 30, .35, 40, .45,
 637                                                                          false, true, false, true,
 638                                                                          false, true, false, true), null);
 639         Background b = new Background(new BackgroundFill[] { f }, null);
 640         final double[] trbl = new double[4];
 641         BackgroundShim.computeOpaqueInsets(b, 100, 50, trbl);
 642         assertEquals(10, trbl[0], 0); // top-left-vertical is 3.75, but top-right-vertical is 10
 643         assertEquals(15, trbl[1], 0); // top-right-horizontal is 12.5, but bottom-right-horizontal is 15
 644         assertEquals(20, trbl[2], 0); // bottom-right-vertical is 8.75, but bottom-left-vertical is 20
 645         assertEquals(22.5, trbl[3], 0); // bottom-left-horizontal dominates at 22.5
 646     }
 647 
 648     @Test public void backgroundFillsArePercentageBased_AllPercentageBased() {
 649         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(.5, true), new Insets(10, 0, 0, 0));
 650         BackgroundFill f2 = new BackgroundFill(Color.GREEN, new CornerRadii(.4, true), new Insets(0, 10, 0, 0));
 651         BackgroundFill f3 = new BackgroundFill(Color.BLUE, new CornerRadii(.3, true), new Insets(0, 0, 10, 0));
 652         BackgroundFill f4 = new BackgroundFill(Color.YELLOW, new CornerRadii(.2, true), new Insets(0, 0, 0, 10));
 653         Background b = new Background(new BackgroundFill[] { f, f2, f3, f4 }, null);
 654         assertTrue(b.isFillPercentageBased());
 655     }
 656 
 657     @Test public void backgroundFillsArePercentageBased_OnePercentageBased() {
 658         BackgroundFill f = new BackgroundFill(Color.RED, new CornerRadii(5), new Insets(10, 0, 0, 0));
 659         BackgroundFill f2 = new BackgroundFill(Color.GREEN, new CornerRadii(4), new Insets(0, 10, 0, 0));
 660         BackgroundFill f3 = new BackgroundFill(Color.BLUE, new CornerRadii(.3, true), new Insets(0, 0, 10, 0));
 661         BackgroundFill f4 = new BackgroundFill(Color.YELLOW, new CornerRadii(2), new Insets(0, 0, 0, 10));