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