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.geom.Path2D;
  29 import com.sun.javafx.geom.PathIterator;
  30 import com.sun.javafx.sg.prism.NGPath;
  31 import javafx.beans.property.DoubleProperty;
  32 import javafx.beans.property.SimpleDoubleProperty;
  33 import javafx.scene.Group;
  34 import javafx.scene.NodeTest;
  35 import javafx.scene.Scene;
  36 import org.junit.Test;
  37 
  38 import java.lang.reflect.Method;
  39 
  40 import static org.junit.Assert.*;
  41 
  42 public class ArcToTest {
  43 
  44     @Test public void testSetGetX() throws Exception {
  45         TestUtils.testDoublePropertyGetterSetter(new ArcTo(), "x", 123.2, 0.0);
  46     }
  47     
  48     @Test public void testSetGetY() throws Exception {
  49         TestUtils.testDoublePropertyGetterSetter(new ArcTo(), "y", 123.2, 0.0);
  50     }
  51     
  52     @Test public void testSetGetRadiusX() throws Exception {
  53         TestUtils.testDoublePropertyGetterSetter(new ArcTo(), "radiusX", 123.2, 0.0);
  54     }
  55     
  56     @Test public void testSetGetRadiusY() throws Exception {
  57         TestUtils.testDoublePropertyGetterSetter(new ArcTo(), "radiusY", 123.2, 0.0);
  58     }
  59     
  60     
  61     @Test public void testSetGetXAxisRotation() throws Exception {
  62         TestUtils.testDoublePropertyGetterSetter(new ArcTo(), "xAxisRotation", 123.2, 0.0);
  63     }
  64     
  65     @Test public void testSetGetLargeArcFlag() throws Exception {
  66         TestUtils.testBooleanPropertyGetterSetter(new ArcTo(), "largeArcFlag");
  67     }
  68     
  69     @Test public void testSetGetSweepFlag() throws Exception {
  70         TestUtils.testBooleanPropertyGetterSetter(new ArcTo(), "sweepFlag");
  71     }
  72 
  73     //TODO test addTo
  74 
  75     @Test public void testDoublePropertySynced_X() throws Exception {
  76         checkSyncedProperty("x", Coords.X, 200.0);
  77     }
  78 
  79     @Test public void testDoublePropertySynced_Y() throws Exception {
  80         checkSyncedProperty("y", Coords.Y, 200.0);
  81     }
  82 
  83     @Test public void toStringShouldReturnNonEmptyString() {
  84         String s = new ArcTo().toString();
  85         assertNotNull(s);
  86         assertFalse(s.isEmpty());
  87     }
  88 
  89     private void checkSyncedProperty(String propertyName, Coords coord, double expected)
  90             throws Exception {
  91         
  92         ArcTo arcTo = new ArcTo();
  93         arcTo.setRadiusX(40.0);arcTo.setRadiusY(50.0);
  94         arcTo.setX(100.0); arcTo.setY(90.0);
  95         DoubleProperty v = new SimpleDoubleProperty(100.0);
  96         Method m = ArcTo.class.getMethod(propertyName + "Property", new Class[] {});
  97         ((DoubleProperty)m.invoke(arcTo)).bind(v);
  98 
  99         Path path = new Path();
 100         path.getElements().addAll(new MoveTo(1.0, 1.0), arcTo);
 101         ((Group)new Scene(new Group()).getRoot()).getChildren().add(path);
 102 
 103         v.set(expected);
 104         NodeTest.syncNode(path);
 105 
 106         //check
 107         NGPath pgPath = path.impl_getPeer();
 108         Path2D geometry = pgPath.getGeometry();
 109         float[] coords = new float[6];
 110         PathIterator it = (PathIterator)geometry.getPathIterator(null);
 111         it.next(); it.next(); //path contains [MoveTo], [CubicTo], [CubicTo], [MoveTo]
 112         int segType = it.currentSegment(coords);
 113         assertEquals(PathIterator.SEG_CUBICTO, segType);
 114         assertEquals(expected, coords[coord.ordinal()], 0.001);
 115     }
 116 
 117     static enum Coords {
 118         CONTROL_X1,
 119         CONTROL_Y1,
 120         CONTROL_X2,
 121         CONTROL_Y2,
 122         X,
 123         Y
 124     }
 125 }