1 /*
   2  * Copyright (c) 2010, 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 com.sun.javafx.sg.prism.NGCircle;
  29 import com.sun.javafx.sg.prism.NGNode;
  30 import com.sun.javafx.test.TestHelper;
  31 import javafx.geometry.Bounds;
  32 import javafx.scene.NodeTest;
  33 import javafx.scene.paint.Color;
  34 import javafx.scene.paint.Paint;
  35 import javafx.scene.transform.Rotate;
  36 import javafx.scene.transform.Scale;
  37 import org.junit.Test;
  38 
  39 import static com.sun.javafx.test.TestHelper.assertSimilar;
  40 import static org.junit.Assert.assertFalse;
  41 import static org.junit.Assert.assertNotNull;
  42 
  43 
  44 public class CircleTest {
  45 
  46     @Test public void testPropertyPropagation_visible() throws Exception {
  47         final Circle node = new StubCircle();
  48         NodeTest.testBooleanPropertyPropagation(node, "visible", false, true);
  49     }
  50 
  51     @Test public void testPropertyPropagation_centerX() throws Exception {
  52         final Circle node = new StubCircle();
  53         NodeTest.testDoublePropertyPropagation(node, "centerX", 100, 200);
  54     }
  55 
  56     @Test public void testPropertyPropagation_centerY() throws Exception {
  57         final Circle node = new StubCircle();
  58         NodeTest.testDoublePropertyPropagation(node, "centerY", 100, 200);
  59     }
  60 
  61     @Test public void testPropertyPropagation_radius() throws Exception {
  62         final Circle node = new StubCircle();
  63         NodeTest.testDoublePropertyPropagation(node, "radius", 100, 200);
  64     }
  65     
  66     @Test public void testBoundPropertySync_centerX() throws Exception {
  67         NodeTest.assertDoublePropertySynced(
  68                 new StubCircle(100, 200, 50),
  69                 "centerX", "centerX",
  70                 350.0);
  71     }
  72 
  73     @Test public void testBoundPropertySync_centerY() throws Exception {
  74         NodeTest.assertDoublePropertySynced(
  75                 new StubCircle(100, 200, 50),
  76                 "centerY", "centerY",
  77                 250.0);
  78     }
  79 
  80     @Test public void testBoundPropertySync_radius() throws Exception {
  81         NodeTest.assertDoublePropertySynced(
  82                 new StubCircle(100, 200, 50),
  83                 "radius", "radius",
  84                 100.0);
  85     }
  86 
  87     @Test
  88     public void testTransformedBounds_rotation() {
  89         Circle c = new StubCircle(50, 100, 10, Color.RED);
  90         Bounds original = c.getBoundsInParent();
  91         c.setRotate(15);
  92         assertSimilar(original, c.getBoundsInParent());
  93     }
  94     
  95     @Test
  96     public void testTransformedBounds_rotation2() {
  97         final int centerX = 50;
  98         final int centerY = 200;
  99         Circle c = new StubCircle(centerX, centerY, 10, Color.RED);
 100         Bounds original = c.getBoundsInParent();
 101         // Using integer isosceles triangle of (38, 181, 181)
 102         Rotate r = new Rotate();
 103         final double angle = Math.asin((38.0/2)/181)*2;
 104         r.setAngle(Math.toDegrees(angle));
 105         r.setPivotX(centerX + 181);
 106         r.setPivotY(centerY);
 107         c.getTransforms().add(r);
 108 
 109         final double centerXDelta = Math.cos((Math.PI - angle)/2) * 38;
 110         final double centerYDelta = - (Math.sin((Math.PI - angle)/2) * 38);
 111 
 112         assertSimilar(TestHelper.box(original.getMinX() + centerXDelta, original.getMinY() + centerYDelta,
 113                 original.getWidth(), original.getHeight()), c.getBoundsInParent());
 114     }
 115 
 116     @Test
 117     public void testTransformedBounds_translate() {
 118         Circle c = new StubCircle(50, 100, 10, Color.RED);
 119         Bounds original = c.getBoundsInParent();
 120         c.setTranslateX(10);
 121         c.setTranslateY(20);
 122         assertSimilar(TestHelper.box(original.getMinX() + 10, original.getMinY() + 20,
 123                 original.getWidth(), original.getHeight()), c.getBoundsInParent());
 124     }
 125 
 126 
 127     @Test public void testTransformedBounds_scale() {
 128         Circle c = new StubCircle(50, 100, 10, Color.RED);
 129         double scalePivotX = (c.getCenterX() + c.getRadius()) / 2;
 130         double scalePivotY = (c.getCenterY() + c.getRadius()) / 2;
 131         Bounds original = c.getBoundsInParent();
 132         Scale s = new Scale(2.0, 1.5, scalePivotX, scalePivotY);
 133         c.getTransforms().setAll(s);
 134         assertSimilar(TestHelper.box(2 * original.getMinX() - scalePivotX, 1.5 * original.getMinY() - 0.5 * scalePivotY,
 135                 2 * original.getWidth(), 1.5 * original.getHeight()), c.getBoundsInParent());
 136     }
 137 
 138     @Test public void toStringShouldReturnNonEmptyString() {
 139         String s = new StubCircle().toString();
 140         assertNotNull(s);
 141         assertFalse(s.isEmpty());
 142     }
 143 
 144     public static final class StubCircle extends Circle {
 145         public StubCircle() {
 146             super();
 147         }
 148 
 149         public StubCircle(double radius) {
 150             super(radius);
 151         }
 152 
 153         public StubCircle(double centerX, double centerY, double radius) {
 154             super(centerX, centerY, radius);
 155         }
 156 
 157         public StubCircle(double centerX, double centerY, double radius, Paint fill) {
 158             super(centerX, centerY, radius, fill);
 159         }
 160 
 161         @Override
 162         protected NGNode impl_createPeer() {
 163             return new StubNGCircle();
 164         }
 165     }
 166 
 167     public static final class StubNGCircle extends NGCircle {
 168         private float centerX;
 169         private float centerY;
 170         private float radius;
 171 
 172         public float getCenterX() {return centerX;}
 173         public float getCenterY() {return centerY;}
 174         public float getRadius() {return radius;}
 175 
 176         @Override
 177         public void updateCircle(float cx, float cy, float radius) {
 178             this.centerX = cx;
 179             this.centerY = cy;
 180             this.radius = radius;
 181         }
 182     }
 183 }