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 test.javafx.scene.bounds; 27 28 import static test.com.sun.javafx.test.TestHelper.assertBoundsEqual; 29 import static test.com.sun.javafx.test.TestHelper.box; 30 import javafx.scene.Group; 31 import javafx.scene.effect.DropShadow; 32 import javafx.scene.shape.Rectangle; 33 import javafx.scene.transform.Scale; 34 import javafx.scene.transform.Translate; 35 36 import org.junit.Test; 37 38 public class LayoutBoundsTest { 39 40 public @Test 41 void testLayoutBoundsForLeafNode() { 42 Rectangle rect = new Rectangle(10, 10, 100, 100); 43 44 assertBoundsEqual(box(10, 10, 100, 100), rect.getLayoutBounds()); 45 } 46 47 public @Test 48 void testLayoutBoundsUpdatedForLeafNode() { 49 Rectangle rect = new Rectangle(10, 10, 100, 100); 50 51 rect.setWidth(200); 52 assertBoundsEqual(box(10, 10, 200, 100), rect.getLayoutBounds()); 53 54 rect.setX(20); 55 assertBoundsEqual(box(20, 10, 200, 100), rect.getLayoutBounds()); 56 } 57 58 public @Test 59 void testEffectNotIncludedInLayoutBounds() { 60 Rectangle rect = new Rectangle(10, 10, 100, 100); 61 DropShadow ds = new DropShadow(); 62 ds.setOffsetY(5); 63 ds.setOffsetX(5); 64 rect.setEffect(ds); 65 66 assertBoundsEqual(box(10, 10, 100, 100), rect.getLayoutBounds()); 67 } 68 69 public @Test 70 void testClipNotIncludedInLayoutBounds() { 71 Rectangle rect = new Rectangle(10, 10, 100, 100); 72 rect.setClip(new Rectangle(25, 25, 25, 25)); 73 74 assertBoundsEqual(box(10, 10, 100, 100), rect.getLayoutBounds()); 75 } 76 77 public @Test 78 void testTransformsNotIncludedInLayoutBounds() { 79 Rectangle rect = new Rectangle(10, 10, 100, 100); 80 rect.getTransforms().add(new Scale(2, 15)); 81 rect.getTransforms().add(new Translate(20, 30)); 82 83 assertBoundsEqual(box(10, 10, 100, 100), rect.getLayoutBounds()); 84 85 rect.setTranslateX(50); 86 rect.setTranslateY(50); 87 rect.setScaleX(.5f); 88 rect.setScaleY(.2f); 89 rect.setRotate(45); 90 91 assertBoundsEqual(box(10, 10, 100, 100), rect.getLayoutBounds()); 92 } 93 94 public @Test 95 void testLayoutBoundsForGroup() { 96 Group group = new Group(new Rectangle(10, 10, 100, 100), new Rectangle( 97 110, 10, 200, 100)); 98 assertBoundsEqual(box(10, 10, 300, 100), group.getLayoutBounds()); 99 100 ((Rectangle) group.getChildren().get(0)).setX(50); 101 assertBoundsEqual(box(50, 10, 260, 100), group.getLayoutBounds()); 102 103 ((Rectangle) group.getChildren().get(0)).setHeight(200); 104 assertBoundsEqual(box(50, 10, 260, 200), group.getLayoutBounds()); 105 } 106 107 public @Test 108 void testEffectOnLayoutBoundsForGroup() { 109 Rectangle r1 = new Rectangle(10, 10, 100, 100); 110 DropShadow ds1 = new DropShadow(); 111 ds1.setOffsetX(5); 112 ds1.setOffsetY(5); 113 r1.setEffect(ds1); 114 Group group = new Group(r1, new Rectangle(110, 10, 200, 100)); 115 116 assertBoundsEqual(box(6, 6, 304, 118), group.getLayoutBounds()); 117 118 // setting effect directly on gruop should not affect layout bounds 119 DropShadow ds2 = new DropShadow(); 120 ds2.setOffsetX(5); 121 ds2.setOffsetY(5); 122 group.setEffect(ds2); 123 assertBoundsEqual(box(6, 6, 304, 118), group.getLayoutBounds()); 124 } 125 126 public @Test 127 void testClipOnLayoutBoundsForGroup() { 128 Rectangle r1 = new Rectangle(10, 10, 100, 100); 129 r1.setClip(new Rectangle(60, 60, 50, 100)); 130 Group group = new Group(r1, new Rectangle(110, 10, 200, 100)); 131 132 assertBoundsEqual(box(60, 10, 250, 100), group.getLayoutBounds()); 133 134 // setting clip directly on group should not affect layout bounds 135 group.setClip(new Rectangle(50, 50, 20, 20)); 136 assertBoundsEqual(box(60, 10, 250, 100), group.getLayoutBounds()); 137 } 138 139 public @Test 140 void testTransformsOnLayoutBoundsForGroup() { 141 Rectangle r1 = new Rectangle(10, 10, 100, 100); 142 r1.getTransforms().add(new Scale(2, 2)); 143 Group group = new Group(r1, new Rectangle(120, 20, 200, 100)); 144 145 assertBoundsEqual(box(20, 20, 300, 200), group.getLayoutBounds()); 146 147 // setting clip directly on gruop should not affect layout bounds 148 group.getTransforms().add(new Scale(3, 3)); 149 assertBoundsEqual(box(20, 20, 300, 200), group.getLayoutBounds()); 150 151 r1.getTransforms().clear(); 152 r1.setScaleX(2); 153 r1.setScaleY(2); 154 r1.setTranslateX(10); 155 r1.setTranslateY(10); 156 157 assertBoundsEqual(box(-30, -30, 350, 200), group.getLayoutBounds()); 158 159 group.setScaleX(3); 160 group.setScaleY(3); 161 group.setTranslateX(20); 162 group.setTranslateY(20); 163 assertBoundsEqual(box(-30, -30, 350, 200), group.getLayoutBounds()); 164 } 165 166 }