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.shape;
  27 
  28 import static com.sun.javafx.test.TestHelper.assertSimilar;
  29 import static com.sun.javafx.test.TestHelper.box;
  30 import static org.junit.Assert.assertEquals;
  31 
  32 import org.junit.Test;
  33 
  34 public class BoundsTest {
  35 
  36     /***************************************************************************
  37      *                                                                         *
  38      *                      Simple Bounds Sanity Tests                         *
  39      *                                                                         *
  40      *  These are simple tests that basic geometry works. These are simple     *
  41      *  tests for the basic primitives in the scene graph to make sure that    *
  42      *  they all compute their geometric bounds properly.                      *
  43      *                                                                         *
  44     /**************************************************************************/
  45 /*
  46     @Test
  47     public void testBoundsForArc() {
  48         Arc arc = new Arc(0, 0, 25, 25, 90, 90);
  49         arc.setType(ArcType.ROUND);
  50 
  51         assertEquals(box(-25, -25, 25, 25), arc.getBoundsInLocal());
  52         assertEquals(arc.getBoundsInLocal(), arc.getLayoutBounds());
  53         assertEquals(arc.getBoundsInLocal(), arc.getBoundsInParent());
  54         
  55         arc.setStroke(Color.BLACK);
  56         assertSimilar(box(-26, -26, 27, 27), arc.getBoundsInLocal());
  57         assertEquals(arc.getBoundsInLocal(), arc.getLayoutBounds());
  58         assertEquals(arc.getBoundsInLocal(), arc.getBoundsInParent());
  59     }
  60 
  61     public @Test void testBoundsForCircle() {
  62         Circle circle = new Circle(50);
  63         
  64         assertEquals(box(-50, -50, 100, 100), circle.getBoundsInLocal());
  65         assertEquals(circle.getBoundsInLocal(), circle.getLayoutBounds());
  66         assertEquals(circle.getBoundsInLocal(), circle.getBoundsInParent());
  67         
  68         circle.setStroke(Color.BLACK);
  69         assertSimilar(box(-51, -51, 102, 102), circle.getBoundsInLocal());
  70         assertEquals(circle.getBoundsInLocal(), circle.getLayoutBounds());
  71         assertEquals(circle.getBoundsInLocal(), circle.getBoundsInParent());
  72     }
  73     
  74     public @Test void testBoundsForCubicCurve() {
  75         CubicCurve cubic = new CubicCurve();
  76         cubic.setStartX(0);
  77         cubic.setStartY(50);
  78         cubic.setControlX1(25);
  79         cubic.setControlX2(75);
  80         cubic.setControlY1(0);
  81         cubic.setControlY2(100);
  82         cubic.setEndX(100);
  83         cubic.setEndY(50);
  84 
  85         assertSimilar(box(0, 36, 100, 28), cubic.getBoundsInLocal());
  86         assertEquals(cubic.getBoundsInLocal(), cubic.getBoundsInParent());
  87     }
  88 
  89     public @Test void testBoundsForEllipse() {
  90         Ellipse ellipse = new Ellipse(50, 100);
  91 
  92         assertEquals(box(-50, -100, 100, 200), ellipse.getBoundsInLocal());
  93         assertEquals(ellipse.getBoundsInLocal(), ellipse.getLayoutBounds());
  94         assertEquals(ellipse.getBoundsInLocal(), ellipse.getBoundsInParent());
  95         
  96         ellipse.setStroke(Color.BLACK);
  97         assertSimilar(box(-51, -101, 102, 202), ellipse.getBoundsInLocal());
  98         assertEquals(ellipse.getBoundsInLocal(), ellipse.getLayoutBounds());
  99         assertEquals(ellipse.getBoundsInLocal(), ellipse.getBoundsInParent());
 100     }
 101 
 102     public @Test void testBoundsForLine() {
 103         Line line = new Line(-10, -10, 10, 10);
 104 
 105         assertSimilar(box(-11, -11, 22, 22), line.getBoundsInLocal());
 106         assertEquals(line.getBoundsInLocal(), line.getLayoutBounds());
 107         assertEquals(line.getBoundsInLocal(), line.getBoundsInParent());
 108     }
 109     */
 110 
 111     public @Test void testBoundsForPath() {
 112         Path path = new Path();
 113         path.getElements().add(new MoveTo(10, 50));
 114         path.getElements().add(new HLineTo(70));
 115         path.getElements().add(new QuadCurveTo(100, 0, 120, 60));
 116         path.getElements().add(new LineTo(175, 55));
 117         path.getElements().add(new ArcTo(100, 100, 0, 10, 50, false, true));
 118 
 119         assertSimilar(box(9, 26, 167, 71), path.getBoundsInLocal());
 120         assertEquals(path.getBoundsInLocal(), path.getBoundsInParent());
 121     }
 122 /*
 123     public @Test void testBoundsForPolygon() {
 124         Polygon polygon = new Polygon(new double[] {0,0,20,10,10,20});
 125 
 126         assertEquals(box(0, 0, 20, 20), polygon.getBoundsInLocal());
 127         assertEquals(polygon.getBoundsInLocal(), polygon.getLayoutBounds());
 128         assertEquals(polygon.getBoundsInLocal(), polygon.getBoundsInParent());
 129         
 130         polygon.setStroke(Color.BLACK);
 131         assertSimilar(box(-1, -1, 22, 22), polygon.getBoundsInLocal());
 132         assertEquals(polygon.getBoundsInLocal(), polygon.getLayoutBounds());
 133         assertEquals(polygon.getBoundsInLocal(), polygon.getBoundsInParent());
 134     }
 135 
 136     public @Test void testBoundsForPolyline() {
 137         Polyline polyline = new Polyline(new double[] {0,0,20,10,10,20});
 138 
 139         assertSimilar(box(-1, -1, 22, 22), polyline.getBoundsInLocal());
 140         assertEquals(polyline.getBoundsInLocal(), polyline.getLayoutBounds());
 141         assertEquals(polyline.getBoundsInLocal(), polyline.getBoundsInParent());
 142     }
 143 
 144     public @Test void testBoundsForQuadCurve() {
 145         QuadCurve quad = new QuadCurve(0, 50, 25, 0, 50, 50);
 146 
 147         assertEquals(box(0, 25, 50, 25), quad.getBoundsInLocal());
 148         assertEquals(quad.getBoundsInLocal(), quad.getBoundsInParent());
 149     }
 150     
 151     public @Test void testBoundsForRectangle() {
 152         Rectangle rect = new Rectangle(100, 100);
 153         
 154         assertEquals(box(0, 0, 100, 100), rect.getBoundsInLocal());
 155         assertEquals(rect.getBoundsInLocal(), rect.getLayoutBounds());
 156         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 157         
 158         rect.setX(50);
 159         rect.setY(50);
 160         assertEquals(box(50, 50, 100, 100), rect.getBoundsInLocal());
 161         assertEquals(rect.getBoundsInLocal(), rect.getLayoutBounds());
 162         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 163         
 164         rect.setStroke(Color.BLACK);
 165         assertSimilar(box(49, 49, 102, 102), rect.getBoundsInLocal());
 166         assertEquals(rect.getBoundsInLocal(), rect.getLayoutBounds());
 167         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 168     }
 169     
 170     // SVGPath
 171     
 172     // we don't have a default impl of this is tests yet, so disabling for
 173     // now
 174     /*
 175     public function testBoundsForText() {
 176 text = Text {
 177             100
 178  x            100
 179  y            TextOrigin.TOP
 180  textOrigin            Font font { 42 size }
 181             "Testing"
 182  content        }
 183         
 184         // this is hard to test. All I'm going to do is a brief sanity check
 185         // to make sure the bounds seem somewhat reasonable.
 186         
 187         // check that the difference is less than 5 pixels here
 188         assertTrue(Math.abs(100 - text.getBoundsInLocal.minX) < 5);
 189         assertTrue(Math.abs(100 - text.getBoundsInLocal.minY) < 5);
 190         assertTrue(text.getBoundsInLocal.width > 120);
 191         assertTrue(text.getBoundsInLocal.height > 40);
 192         assertEquals(text.getBoundsInLocal, text.getLayoutBounds);
 193         assertEquals(text.getBoundsInLocal, text.getBoundsInParent);
 194     }*/
 195 }