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