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