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 test.javafx.scene.shape;
  27 
  28 import com.sun.javafx.geom.Path2D;
  29 import test.com.sun.javafx.pgstub.SVGPathImpl;
  30 import com.sun.javafx.sg.prism.NGNode;
  31 import com.sun.javafx.sg.prism.NGSVGPath;
  32 import javafx.beans.property.SimpleStringProperty;
  33 import javafx.beans.property.StringProperty;
  34 import test.javafx.scene.NodeTest;
  35 import javafx.scene.shape.FillRule;
  36 import javafx.scene.shape.SVGPath;
  37 import org.junit.Test;
  38 
  39 import static org.junit.Assert.*;
  40 
  41 public class SVGPathTest {
  42     
  43     @Test public void testBoundPropertySync_Content() throws Exception {
  44         StubSVGPath svgPath = new StubSVGPath();
  45         svgPath.setContent("M40,60 C42,48 44,30 25,32");
  46 
  47         StringProperty content = new SimpleStringProperty();
  48         svgPath.contentProperty().bind(content);
  49         String s = "M11,11 C22,22 33,33";
  50         content.set(s);
  51         NodeTest.syncNode(svgPath);
  52 
  53         SVGPathImpl geometry = ((StubNGSVGPath) svgPath.impl_getPeer()).geometry;
  54         assertEquals(s, geometry.getContent());
  55     }
  56 
  57     @Test
  58     public void testDefaultValues() {
  59         StubSVGPath svgPath = new StubSVGPath();
  60         assertEquals("", svgPath.getContent());
  61         assertEquals("", svgPath.contentProperty().get());
  62         assertEquals(FillRule.NON_ZERO, svgPath.getFillRule());
  63         assertEquals(FillRule.NON_ZERO, svgPath.fillRuleProperty().get());
  64     }
  65 
  66     @Test public void testFillRuleSync() {
  67         StubSVGPath svgPath = new StubSVGPath();
  68         svgPath.setContent("M40,60 C42,48 44,30 25,32");
  69         svgPath.setFillRule(FillRule.EVEN_ODD);
  70         StubNGSVGPath peer = svgPath.impl_getPeer();
  71         peer.setAcceptsPath2dOnUpdate(true);
  72 
  73         NodeTest.syncNode(svgPath);
  74         Path2D path = peer.path;
  75         assertEquals(Path2D.WIND_EVEN_ODD, path.getWindingRule());
  76 
  77         svgPath.setFillRule(FillRule.NON_ZERO);
  78         NodeTest.syncNode(svgPath);
  79         // internal shape might have changed, getting it again
  80         path = peer.path;
  81         assertEquals(Path2D.WIND_NON_ZERO, path.getWindingRule());
  82     }
  83 
  84     @Test public void toStringShouldReturnNonEmptyString() {
  85         String s = new StubSVGPath().toString();
  86         assertNotNull(s);
  87         assertFalse(s.isEmpty());
  88     }
  89 
  90     private class StubSVGPath extends SVGPath {
  91         @Override
  92         protected NGNode impl_createPeer() {
  93             return new StubNGSVGPath();
  94         }
  95     }
  96 
  97     private class StubNGSVGPath extends NGSVGPath {
  98         private SVGPathImpl geometry;
  99         private Path2D path;
 100         private boolean acceptsPath2dOnUpdate = false;
 101 
 102         @Override
 103         public void setContent(Object content) {
 104             if (acceptsPath2dOnUpdate) {
 105                 path = (Path2D) content;
 106             } else {
 107                 geometry = (SVGPathImpl) content;
 108             }
 109         }
 110 
 111         public void setAcceptsPath2dOnUpdate(boolean acceptsPath2dOnUpdate) {
 112             this.acceptsPath2dOnUpdate = acceptsPath2dOnUpdate;
 113         }
 114 
 115         @Override
 116         public boolean acceptsPath2dOnUpdate() {
 117             return acceptsPath2dOnUpdate;
 118         }
 119     }
 120 }