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.sg.prism.NGNode;
  30 import com.sun.javafx.sg.prism.NGPolygon;
  31 import javafx.collections.ObservableList;
  32 import javafx.scene.NodeTest;
  33 import org.junit.Test;
  34 
  35 import static com.sun.javafx.test.TestHelper.assertBoundsEqual;
  36 import static com.sun.javafx.test.TestHelper.box;
  37 import static org.junit.Assert.*;
  38 
  39 public class PolygonTest {
  40     @Test public void testPropertyPropagation_emptyPoints() {
  41         final StubPolygon polygon = new StubPolygon();
  42         NodeTest.callSyncPGNode(polygon);
  43         assertPGPolygonPointsEquals(polygon, new double[0]);
  44     }
  45 
  46     @Test public void testPropertyPropagation_pointsEvenLength() {
  47         final double[] initialPoints = { 10, 20, 100, 200, 200, 100, 50, 10 };
  48 
  49         final StubPolygon polygon = new StubPolygon(initialPoints);
  50         NodeTest.callSyncPGNode(polygon);
  51         assertPGPolygonPointsEquals(polygon, initialPoints);
  52 
  53         final ObservableList<Double> polygonPoints = polygon.getPoints();
  54         polygonPoints.remove(1);
  55         polygonPoints.remove(2);
  56 
  57         NodeTest.callSyncPGNode(polygon);
  58         assertPGPolygonPointsEquals(polygon, 10, 100, 200, 100, 50, 10);
  59     }
  60 
  61     @Test public void testPropertyPropagation_pointsOddLength() {
  62         final double[] initialPoints = { 10, 20, 100, 200, 200 };
  63 
  64         final StubPolygon polygon = new StubPolygon(initialPoints);
  65         NodeTest.callSyncPGNode(polygon);
  66         assertPGPolygonPointsEquals(polygon, initialPoints);
  67 
  68         final ObservableList<Double> polygonPoints = polygon.getPoints();
  69         polygonPoints.add(100.0);
  70         polygonPoints.add(50.0);
  71 
  72         NodeTest.callSyncPGNode(polygon);
  73         assertPGPolygonPointsEquals(polygon, 10, 20, 100, 200, 200, 100, 50);
  74     }
  75 
  76     @Test public void testPropertyPropagation_visible() throws Exception {
  77         final StubPolygon polygon = new StubPolygon();
  78         NodeTest.testBooleanPropertyPropagation(polygon, "visible", false, true);
  79     }
  80 
  81     @Test public void testBounds_emptyPoints() {
  82         final StubPolygon polygon = new StubPolygon();
  83         assertBoundsEqual(box(0, 0, -1, -1), polygon.getBoundsInLocal());
  84     }
  85 
  86     @Test public void testBounds_evenPointsLength() {
  87         final double[] initialPoints = { 100, 100, 200, 100, 200, 200 };
  88 
  89         final StubPolygon polygon = new StubPolygon(initialPoints);
  90         assertBoundsEqual(box(100, 100, 100, 100), polygon.getBoundsInLocal());
  91 
  92         final ObservableList<Double> polygonPoints = polygon.getPoints();
  93         polygonPoints.add(150.0);
  94         polygonPoints.add(300.0);
  95 
  96         assertBoundsEqual(box(100, 100, 100, 200), polygon.getBoundsInLocal());
  97     }
  98 
  99     @Test public void testBounds_oddPointsLength() {
 100         final double[] initialPoints = {
 101             100, 100, 200, 100, 200, 200, 150, 300
 102         };
 103 
 104         final StubPolygon polygon = new StubPolygon(initialPoints);
 105         assertBoundsEqual(box(100, 100, 100, 200), polygon.getBoundsInLocal());
 106 
 107         final ObservableList<Double> polygonPoints = polygon.getPoints();
 108         polygonPoints.remove(6);
 109 
 110         assertBoundsEqual(box(100, 100, 100, 100), polygon.getBoundsInLocal());
 111     }
 112     
 113     @Test public void testConfigShape() throws Exception {
 114         final StubPolygon polygon =
 115                 new StubPolygon(new double[] { 0, 0, 0, 1, 1, 1, 1, 0 });
 116         final Path2D path = polygon.impl_configShape();
 117         assertTrue(path.contains(0.5f, 0.5f));
 118         assertFalse(path.contains(0, 2));
 119     }
 120 
 121     private static void assertPGPolygonPointsEquals(
 122             final StubPolygon polygon,
 123             final double... expectedPoints) {
 124         final StubNGPolygon stubPolygon = polygon.impl_getPeer();
 125         final float[] pgPoints = stubPolygon.points;
 126 
 127         final int minLength = expectedPoints.length & ~1;
 128         final int maxLength = expectedPoints.length;
 129 
 130         assertTrue(pgPoints.length >= minLength);
 131         assertTrue(pgPoints.length <= maxLength);
 132 
 133         int i;
 134 
 135         for (i = 0; i < minLength; ++i) {
 136             assertEquals(expectedPoints[i], pgPoints[i], 0);
 137         }
 138 
 139         for (; i < pgPoints.length; ++i) {
 140             assertEquals(expectedPoints[i], pgPoints[i], 0);
 141         }
 142     }
 143 
 144     @Test public void toStringShouldReturnNonEmptyString() {
 145         String s = new Polygon().toString();
 146         assertNotNull(s);
 147         assertFalse(s.isEmpty());
 148     }
 149 
 150     private final class StubPolygon extends Polygon {
 151         public StubPolygon(double... initialPoints) {
 152             super(initialPoints);
 153         }
 154 
 155         public StubPolygon() {
 156             super();
 157         }
 158 
 159         @Override
 160         protected NGNode impl_createPeer() {
 161             return new StubNGPolygon();
 162         }
 163     }
 164 
 165     private final class StubNGPolygon extends NGPolygon {
 166         private float[] points;
 167         @Override
 168         public void updatePolygon(float[] points) {
 169             super.updatePolygon(points);
 170             this.points = points;
 171         }
 172     }
 173 }