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.assertSimilar; 30 import static test.com.sun.javafx.test.TestHelper.box; 31 import static org.junit.Assert.assertEquals; 32 import javafx.geometry.BoundingBox; 33 import javafx.scene.Group; 34 import javafx.scene.shape.Rectangle; 35 import javafx.scene.transform.Rotate; 36 import javafx.scene.transform.Scale; 37 import javafx.scene.transform.Translate; 38 39 import org.junit.Test; 40 41 public class TransformedBoundsTest { 42 43 /*************************************************************************** 44 * * Transform Tests * * These are tests related to transforming the basic 45 * node types including * Groups and Regions. It is assumed that the 46 * getBoundsInLocal() values * for these nodes are correct. * * / 47 **************************************************************************/ 48 49 public @Test 50 void testBoundsForTranslatedRectangle() { 51 Rectangle rect = new Rectangle(100, 100); 52 rect.setTranslateX(10); 53 rect.setTranslateY(10); 54 55 assertEquals(box(0, 0, 100, 100), rect.getBoundsInLocal()); 56 assertEquals(box(10, 10, 100, 100), rect.getBoundsInParent()); 57 assertEquals(rect.getBoundsInLocal(), rect.getLayoutBounds()); 58 } 59 60 // this test specifically checks to make sure the minX,, width, height 61 // invariants are correct in this case 62 public @Test 63 void testBoundsForNegativeScaledRectangle() { 64 Rectangle rect = new Rectangle(100, 100); 65 rect.setScaleX(-1); 66 rect.setScaleY(-1); 67 68 assertEquals(box(0, 0, 100, 100), rect.getBoundsInLocal()); 69 assertEquals(box(0, 0, 100, 100), rect.getBoundsInParent()); 70 assertEquals(rect.getBoundsInLocal(), rect.getLayoutBounds()); 71 } 72 73 public @Test 74 void testBoundsForRotatedRectangle() { 75 Rectangle rect = new Rectangle(100, 100); 76 rect.setRotate(45); 77 78 assertEquals(box(0, 0, 100, 100), rect.getBoundsInLocal()); 79 assertSimilar(box(-20.71, -20.71, 141.42, 141.42), 80 rect.getBoundsInParent()); 81 assertEquals(rect.getBoundsInLocal(), rect.getLayoutBounds()); 82 } 83 84 public @Test 85 void testBoundsForRotatedGroupOfRectangles() { 86 Group group = new Group(new Rectangle(100, 100), new Rectangle(2, 2, 87 96, 96)); 88 group.setRotate(45); 89 90 assertEquals(box(0, 0, 100, 100), group.getBoundsInLocal()); 91 assertSimilar(box(-20.71, -20.71, 141.42, 141.42), 92 group.getBoundsInParent()); 93 assertEquals(group.getBoundsInLocal(), group.getLayoutBounds()); 94 } 95 96 public @Test 97 void testBoundsForRotatedRectangleUsingTransforms() { 98 Rectangle rect = new Rectangle(100, 100); 99 rect.getTransforms().add(new Rotate(45)); // rotates about 0, 0 rather 100 // than the center! 101 102 assertEquals(box(0, 0, 100, 100), rect.getBoundsInLocal()); 103 assertSimilar(box(-70.71, 0, 141.42, 141.42), rect.getBoundsInParent()); 104 assertEquals(rect.getBoundsInLocal(), rect.getLayoutBounds()); 105 } 106 107 public @Test 108 void testBoundsForTransformedRectangleInTransformedGroup() { 109 Rectangle rect = new Rectangle(100, 100); 110 rect.setTranslateX(100); 111 Group group = new Group(rect); 112 group.setTranslateX(50); 113 114 assertEquals(box(0, 0, 100, 100), rect.getBoundsInLocal()); 115 assertEquals(box(100, 0, 100, 100), rect.getBoundsInParent()); 116 assertEquals(box(100, 0, 100, 100), group.getBoundsInLocal()); 117 assertEquals(box(150, 0, 100, 100), group.getBoundsInParent()); 118 assertEquals(rect.getBoundsInLocal(), rect.getLayoutBounds()); 119 assertEquals(rect.getBoundsInParent(), group.getLayoutBounds()); 120 } 121 122 public @Test 123 void testUnTransformedBounds() { 124 Rectangle rect = new Rectangle(3, 7, 17, 19); 125 BoundingBox bb = box(3, 7, 17, 19); 126 127 assertSimilar(bb, rect.getBoundsInLocal()); 128 assertSimilar(bb, rect.localToParent(rect.getBoundsInLocal())); 129 assertSimilar(bb, rect.localToScene(rect.getBoundsInLocal())); 130 assertSimilar(bb, rect.parentToLocal(rect.getBoundsInLocal())); 131 assertSimilar(bb, rect.sceneToLocal(rect.getBoundsInLocal())); 132 } 133 134 public @Test 135 void testTranslateTxBounds() { 136 Rectangle rect = new Rectangle(3, 7, 17, 19); 137 rect.getTransforms().add(new Translate(20, 30)); 138 BoundingBox tbb = box(23, 37, 17, 19); 139 140 assertSimilar(tbb, rect.getBoundsInParent()); 141 assertSimilar(rect.getBoundsInParent(), 142 rect.localToParent(rect.getBoundsInLocal())); 143 assertSimilar(tbb, rect.localToScene(rect.getBoundsInLocal())); 144 } 145 146 public @Test 147 void testTranslatedBounds() { 148 Rectangle rect = new Rectangle(3, 7, 17, 19); 149 rect.setTranslateX(20); 150 rect.setTranslateY(30); 151 BoundingBox bb = box(3, 7, 17, 19); 152 BoundingBox tbb = box(23, 37, 17, 19); 153 154 assertSimilar(tbb, rect.getBoundsInParent()); 155 assertSimilar(rect.getBoundsInParent(), 156 rect.localToParent(rect.getBoundsInLocal())); 157 assertSimilar(tbb, rect.localToScene(rect.getBoundsInLocal())); 158 assertSimilar(bb, 159 rect.parentToLocal(rect.localToParent(rect.getBoundsInLocal()))); 160 assertSimilar(bb, 161 rect.sceneToLocal(rect.localToScene(rect.getBoundsInLocal()))); 162 } 163 164 public @Test 165 void testScaleTxBounds() { 166 Rectangle rect = new Rectangle(3, 7, 17, 19); 167 rect.getTransforms().add(new Scale(2, 3)); 168 169 BoundingBox bb = box(3, 7, 17, 19); 170 BoundingBox tbb = box(6, 21, 34, 57); 171 172 assertSimilar(tbb, rect.getBoundsInParent()); 173 assertSimilar(rect.getBoundsInParent(), 174 rect.localToParent(rect.getBoundsInLocal())); 175 assertSimilar(tbb, rect.localToScene(rect.getBoundsInLocal())); 176 assertSimilar(bb, 177 rect.parentToLocal(rect.localToParent(rect.getBoundsInLocal()))); 178 assertSimilar(bb, 179 rect.sceneToLocal(rect.localToScene(rect.getBoundsInLocal()))); 180 } 181 182 public @Test 183 void testScaledBounds() { 184 Rectangle rect = new Rectangle(3, 7, 17, 19); 185 rect.setScaleX(2); 186 rect.setScaleY(3); 187 188 BoundingBox bb = box(3, 7, 17, 19); 189 BoundingBox tbb = box(-5.5, -12, 34, 57); 190 191 assertSimilar(tbb, rect.getBoundsInParent()); 192 assertSimilar(rect.getBoundsInParent(), 193 rect.localToParent(rect.getBoundsInLocal())); 194 assertSimilar(tbb, rect.localToScene(rect.getBoundsInLocal())); 195 assertSimilar(bb, 196 rect.parentToLocal(rect.localToParent(rect.getBoundsInLocal()))); 197 assertSimilar(bb, 198 rect.sceneToLocal(rect.localToScene(rect.getBoundsInLocal()))); 199 } 200 201 public @Test 202 void testRotateTx11Bounds() { 203 Rectangle rect = new Rectangle(3, 7, 17, 19); 204 BoundingBox bb = box(3, 7, 17, 19); 205 rect.getTransforms().add(new Rotate(11)); 206 207 BoundingBox tbb = box(-2.0161524, 7.443817, 20.313034, 21.89467); 208 209 assertSimilar(bb, rect.getBoundsInLocal()); 210 assertSimilar(tbb, rect.getBoundsInParent()); 211 assertSimilar(tbb, rect.localToParent(rect.getBoundsInLocal())); 212 assertSimilar(tbb, rect.localToScene(rect.getBoundsInLocal())); 213 } 214 215 public @Test 216 void testRotateTx90Bounds() { 217 Rectangle rect = new Rectangle(3, 7, 17, 19); 218 BoundingBox bb = box(3, 7, 17, 19); 219 rect.getTransforms().add(new Rotate(90)); 220 221 BoundingBox tbb = box(-26, 3, 19, 17); 222 223 assertSimilar(tbb, rect.getBoundsInParent()); 224 assertSimilar(rect.getBoundsInParent(), 225 rect.localToParent(rect.getBoundsInLocal())); 226 assertSimilar(tbb, rect.localToScene(rect.getBoundsInLocal())); 227 } 228 229 public @Test 230 void testRotated90Bounds() { 231 Rectangle rect = new Rectangle(3, 7, 17, 19); 232 BoundingBox bb = box(3, 7, 17, 19); 233 rect.setRotate(90); 234 235 BoundingBox tbb = box(2.0, 8.0, 19, 17); 236 237 assertSimilar(bb, rect.getBoundsInLocal()); 238 assertSimilar(tbb, rect.getBoundsInParent()); 239 assertSimilar(rect.getBoundsInParent(), 240 rect.localToParent(rect.getBoundsInLocal())); 241 assertSimilar(tbb, rect.localToScene(rect.getBoundsInLocal())); 242 } 243 244 public @Test 245 void testRotated120Bounds() { 246 Rectangle rect = new Rectangle(3, 7, 17, 19); 247 BoundingBox bb = box(3, 7, 17, 19); 248 rect.setRotate(120); 249 250 BoundingBox tbb = box(-0.97724134, 4.388784, 24.954483, 24.222431); 251 252 assertSimilar(bb, rect.getBoundsInLocal()); 253 assertSimilar(tbb, rect.getBoundsInParent()); 254 assertSimilar(tbb, rect.localToParent(rect.getBoundsInLocal())); 255 assertSimilar(tbb, rect.localToScene(rect.getBoundsInLocal())); 256 } 257 258 public @Test 259 void testNotificationOnBoundsChangeForLeafNode() { 260 Rectangle rect = new Rectangle(); 261 262 rect.setX(50); 263 assertBoundsEqual(box(50, 0, 0, 0), rect.getBoundsInLocal()); 264 265 rect.setY(50); 266 rect.setWidth(100); 267 rect.setHeight(30); 268 assertBoundsEqual(box(50, 50, 100, 30), rect.getBoundsInLocal()); 269 } 270 271 @Test 272 public void testNotificationOnBoundsChangeForTransformedLeafNode() { 273 final Rectangle rect = new Rectangle(-50, -50, 100, 100); 274 rect.getTransforms().add(new Rotate(-45)); 275 276 assertSimilar( 277 box(-Math.sqrt(2) * 50, 278 -Math.sqrt(2) * 50, 279 Math.sqrt(2) * 100, 280 Math.sqrt(2) * 100), 281 rect.getBoundsInParent()); 282 283 rect.setX(-100); 284 rect.setY(-100); 285 rect.setWidth(200); 286 rect.setHeight(200); 287 288 assertSimilar( 289 box(-Math.sqrt(2) * 100, 290 -Math.sqrt(2) * 100, 291 Math.sqrt(2) * 200, 292 Math.sqrt(2) * 200), 293 rect.getBoundsInParent()); 294 } 295 296 public @Test 297 void testBoundsWithTransform() { 298 Rectangle rect = new Rectangle(); 299 rect.setScaleX(0.5f); 300 rect.setScaleY(0.5f); 301 302 assertBoundsEqual(box(0, 0, 0, 0), rect.getBoundsInLocal()); 303 assertBoundsEqual(box(0, 0, 0, 0), rect.getBoundsInParent()); 304 305 rect.setX(50); 306 rect.setY(50); 307 rect.setWidth(100); 308 rect.setHeight(30); 309 310 assertBoundsEqual(box(50, 50, 100, 30), rect.getBoundsInLocal()); 311 assertBoundsEqual(box(75, 57.5, 50, 15), rect.getBoundsInParent()); 312 } 313 314 public @Test 315 void testNotificationOnBoundsChangeForTransforms() { 316 Rectangle rect = new Rectangle(); 317 rect.setScaleX(0.5f); 318 rect.setScaleY(0.5f); 319 320 // changes to either the rectangle OR the scale should cause events 321 // to fire 322 323 rect.setX(50); 324 rect.setY(50); 325 rect.setWidth(100); 326 rect.setHeight(30); 327 assertBoundsEqual(box(75, 57.5, 50, 15), rect.getBoundsInParent()); 328 329 rect.setScaleX(1); 330 rect.setScaleY(1); 331 assertBoundsEqual(box(50, 50, 100, 30), rect.getBoundsInParent()); 332 } 333 334 }