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 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.ObjectProperty;
  32 import javafx.beans.property.SimpleObjectProperty;
  33 import javafx.scene.Group;
  34 import javafx.scene.NodeTest;
  35 import javafx.scene.Scene;
  36 import org.junit.Test;
  37 
  38 import java.util.ArrayList;
  39 import java.util.List;
  40 
  41 import static org.junit.Assert.*;
  42 
  43 
  44 public class PathTest {
  45 
  46     @Test public void testVarargConstructor() {
  47         PathElement one = new MoveTo(10, 10);
  48         PathElement two = new LineTo(20, 20);
  49         PathElement three = new MoveTo(30, 30);
  50         Path path = new Path(one, two, three);
  51         assertEquals(3, path.getElements().size());
  52         assertSame(one, path.getElements().get(0));
  53         assertSame(two, path.getElements().get(1));
  54         assertSame(three, path.getElements().get(2));
  55     }
  56     
  57     @Test public void testListConstructor() {        
  58         PathElement one = new MoveTo(10, 10);
  59         PathElement two = new LineTo(20, 20);
  60         PathElement three = new MoveTo(30, 30);
  61         
  62         List<PathElement> listOfElements = new ArrayList<PathElement>();
  63         listOfElements.add(one);
  64         listOfElements.add(two);
  65         listOfElements.add(three);
  66         Path path = new Path(listOfElements);
  67         assertEquals(3, path.getElements().size());
  68         assertSame(one, path.getElements().get(0));
  69         assertSame(two, path.getElements().get(1));
  70         assertSame(three, path.getElements().get(2));
  71     }
  72     
  73     @Test public void testBoundPropertySync_FillRule() throws Exception {
  74         ObjectProperty<FillRule> v = new SimpleObjectProperty<FillRule>(FillRule.EVEN_ODD);
  75         Path path = new Path();
  76         path.fillRuleProperty().bind(v);
  77         ((Group)new Scene(new Group()).getRoot()).getChildren().add(path);
  78         v.set(FillRule.NON_ZERO);
  79         NodeTest.syncNode(path);
  80 
  81         //check
  82         Path2D geometry = ((NGPath)path.impl_getPeer()).getGeometry();
  83         assertEquals(geometry.getWindingRule(), FillRule.NON_ZERO.ordinal());
  84     }
  85 
  86     @Test public void testFirstRelativeElement_PathIsEmpty() {
  87         Path path = new Path();
  88         final MoveTo moveTo = new MoveTo(10, 10);
  89         moveTo.setAbsolute(false);
  90         path.getElements().add(moveTo);
  91         path.getElements().add(new LineTo(100, 100));
  92         Path2D geometry = ((NGPath)path.impl_getPeer()).getGeometry();
  93         PathIterator piterator = geometry.getPathIterator(null);
  94         assertTrue(piterator.isDone());//path is empty
  95     }
  96 
  97      @Test public void testFirstRelativeElement_BoundsAreEmpty() {
  98         Path path = new Path();
  99          final MoveTo moveTo = new MoveTo(10, 10);
 100          moveTo.setAbsolute(false);
 101          path.getElements().add(moveTo);
 102          path.getElements().add(new LineTo(100, 100));
 103         assertTrue(path.getBoundsInLocal().isEmpty() && path.getBoundsInParent().isEmpty());
 104     }
 105 
 106     @Test public void testFirstElementIsNotMoveTo_PathIsEmpty() {
 107         Path path = new Path();
 108         path.getElements().add(new LineTo(10, 10));
 109         path.getElements().add(new LineTo(100, 100));
 110         Path2D geometry = ((NGPath)path.impl_getPeer()).getGeometry();
 111         PathIterator piterator = geometry.getPathIterator(null);
 112         assertTrue(piterator.isDone());//path is empty
 113     }
 114 
 115     @Test public void testFirstElementIsNotMoveTo_BoundsAreEmpty() {
 116         Path path = new Path();
 117         path.getElements().add(new LineTo(10, 10));
 118         path.getElements().add(new LineTo(100, 100));
 119         assertTrue(path.getBoundsInLocal().isEmpty() && path.getBoundsInParent().isEmpty());
 120     }
 121 
 122     @Test public void testFillRuleSync() {
 123         Path path = new Path();
 124         path.getElements().add(new MoveTo(10, 10));
 125         path.getElements().add(new LineTo(100, 10));
 126         path.getElements().add(new LineTo(100, 100));
 127         path.setFillRule(FillRule.EVEN_ODD);
 128         NodeTest.syncNode(path);
 129         Path2D geometry = ((NGPath)path.impl_getPeer()).getGeometry();
 130         assertEquals(Path2D.WIND_EVEN_ODD, geometry.getWindingRule());
 131 
 132         path.setFillRule(FillRule.NON_ZERO);
 133         NodeTest.syncNode(path);
 134         // internal shape might have changed, getting it again
 135         geometry = ((NGPath)path.impl_getPeer()).getGeometry();
 136         assertEquals(Path2D.WIND_NON_ZERO, geometry.getWindingRule());
 137     }
 138 
 139     @Test public void toStringShouldReturnNonEmptyString() {
 140         String s = new Path().toString();
 141         assertNotNull(s);
 142         assertFalse(s.isEmpty());
 143     }
 144 }