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 test.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 test.javafx.scene.NodeTest;
  35 import javafx.scene.Scene;
  36 import org.junit.Test;
  37 
  38 import java.lang.reflect.Method;
  39 import javafx.scene.shape.CubicCurveTo;
  40 import javafx.scene.shape.MoveTo;
  41 import javafx.scene.shape.Path;
  42 
  43 import static org.junit.Assert.*;
  44 
  45 
  46 public class CubicCurveToTest {
  47 
  48     @Test public void testSetGetX() throws Exception {
  49         TestUtils.testDoublePropertyGetterSetter(new CubicCurveTo(), "x", 123.2f, 0.0f);
  50     }
  51 
  52     @Test public void testSetGetControlX1() throws Exception {
  53         TestUtils.testDoublePropertyGetterSetter(new CubicCurveTo(), "controlX1", 123.2f, 0.0f);
  54     }
  55 
  56     @Test public void testSetGetControlX2() throws Exception {
  57         TestUtils.testDoublePropertyGetterSetter(new CubicCurveTo(), "controlX2", 123.2f, 0.0f);
  58     }
  59 
  60     @Test public void testSetGetControlY1() throws Exception {
  61         TestUtils.testDoublePropertyGetterSetter(new CubicCurveTo(), "controlY1", 123.2f, 0.0f);
  62     }
  63 
  64     @Test public void testSetGetControlY2() throws Exception {
  65         TestUtils.testDoublePropertyGetterSetter(new CubicCurveTo(), "controlY2", 123.2f, 0.0f);
  66     }
  67     
  68     //TODO testAddTo
  69 
  70     @Test public void testDoublePropertySynced_X() throws Exception {
  71         checkSyncedProperty("x", Coords.X, 123.4);
  72     }
  73 
  74    @Test public void testDoublePropertySynced_Y() throws Exception {
  75         checkSyncedProperty("y", Coords.Y, 432.1);
  76     }
  77 
  78    @Test public void testDoublePropertySynced_ControlX1() throws Exception {
  79         checkSyncedProperty("controlX1", Coords.CONTROL_X1, 11.1);
  80     }
  81 
  82    @Test public void testDoublePropertySynced_ControlY1() throws Exception {
  83         checkSyncedProperty("controlY1", Coords.CONTROL_Y1, 22.2);
  84     }
  85 
  86    @Test public void testDoublePropertySynced_ControlX2() throws Exception {
  87         checkSyncedProperty("controlX2", Coords.CONTROL_X2, 1.1);
  88     }
  89 
  90    @Test public void testDoublePropertySynced_ControlY2() throws Exception {
  91         checkSyncedProperty("controlY2", Coords.CONTROL_Y2, 2.2);
  92     }
  93 
  94     @Test public void toStringShouldReturnNonEmptyString() {
  95         String s = new CubicCurveTo().toString();
  96         assertNotNull(s);
  97         assertFalse(s.isEmpty());
  98     }
  99 
 100     private void checkSyncedProperty(String propertyName, Coords coord, double expected) throws Exception {
 101         CubicCurveTo ccurveTo = new CubicCurveTo(110.0, 190.0, 290.0, 190.0, 300.0, 200.0);
 102         DoubleProperty v = new SimpleDoubleProperty(10.0);
 103         Method m = CubicCurveTo.class.getMethod(propertyName + "Property", new Class[] {});
 104         ((DoubleProperty)m.invoke(ccurveTo)).bind(v);
 105 
 106         Path path = new Path();
 107         path.getElements().addAll(new MoveTo(100.0, 200.0), ccurveTo);
 108         ((Group)new Scene(new Group()).getRoot()).getChildren().add(path);
 109 
 110         v.set(expected);
 111         NodeTest.syncNode(path);
 112 
 113         //check
 114         NGPath pgPath = path.impl_getPeer();
 115         Path2D geometry = pgPath.getGeometry();
 116         float[] coords = new float[6];
 117         PathIterator it = geometry.getPathIterator(null);
 118         it.next(); //next is QuadCurveTo segment
 119         int segType = it.currentSegment(coords);
 120         assertEquals(segType, PathIterator.SEG_CUBICTO);
 121         assertEquals(expected, coords[coord.ordinal()], 0.001);
 122     }
 123 
 124     static enum Coords {
 125         CONTROL_X1,
 126         CONTROL_Y1,
 127         CONTROL_X2,
 128         CONTROL_Y2,
 129         X,
 130         Y
 131     }
 132 }