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