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.transform.BaseTransform; 29 import com.sun.javafx.sg.prism.NGNode; 30 import com.sun.javafx.sg.prism.NGRectangle; 31 import com.sun.javafx.test.TestHelper; 32 import javafx.geometry.Bounds; 33 import javafx.scene.NodeTest; 34 import javafx.scene.paint.Paint; 35 import org.junit.Test; 36 37 import static com.sun.javafx.test.TestHelper.assertSimilar; 38 import static org.junit.Assert.*; 39 40 public class RectangleTest { 41 42 @Test public void testPropertyPropagation_visible() throws Exception { 43 final Rectangle node = new StubRectangle(); 44 NodeTest.testBooleanPropertyPropagation(node, "visible", false, true); 45 } 46 47 @Test public void testPropertyPropagation_x() throws Exception { 48 final Rectangle node = new StubRectangle(); 49 NodeTest.testDoublePropertyPropagation(node, "x", 100, 200); 50 } 51 52 @Test public void testPropertyPropagation_y() throws Exception { 53 final Rectangle node = new StubRectangle(); 54 NodeTest.testDoublePropertyPropagation(node, "y", 100, 200); 55 } 56 57 @Test public void testPropertyPropagation_width() throws Exception { 58 final Rectangle node = new StubRectangle(); 59 NodeTest.testDoublePropertyPropagation(node, "width", 100, 200); 60 } 61 62 @Test public void testPropertyPropagation_height() throws Exception { 63 final Rectangle node = new StubRectangle(); 64 NodeTest.testDoublePropertyPropagation(node, "height", 100, 200); 65 } 66 67 @Test public void testPropertyPropagation_arcWidth() throws Exception { 68 final Rectangle node = new StubRectangle(); 69 NodeTest.testDoublePropertyPropagation(node, "arcWidth", 100, 200); 70 } 71 72 @Test public void testPropertyPropagation_arcHeight() throws Exception { 73 final Rectangle node = new StubRectangle(); 74 NodeTest.testDoublePropertyPropagation(node, "arcHeight", 100, 200); 75 } 76 77 @Test public void testBoundPropertySync_X() throws Exception { 78 NodeTest.assertDoublePropertySynced( 79 new StubRectangle(200.0, 100.0), 80 "x", "x", 10.0); 81 } 82 83 @Test public void testBoundPropertySync_Y() throws Exception { 84 NodeTest.assertDoublePropertySynced( 85 new StubRectangle(200.0, 100.0), 86 "y", "y", 20.0); 87 } 88 89 @Test public void testBoundPropertySync_Width() throws Exception { 90 NodeTest.assertDoublePropertySynced( 91 new StubRectangle(200.0, 100.0), 92 "width", "width", 300.0); 93 } 94 95 @Test public void testBoundPropertySync_Height() throws Exception { 96 NodeTest.assertDoublePropertySynced( 97 new StubRectangle(200.0, 100.0), 98 "height", "height", 200.0); 99 } 100 101 @Test public void testBoundPropertySync_ArcWidth() throws Exception { 102 NodeTest.assertDoublePropertySynced( 103 new StubRectangle(200.0, 100.0), 104 "arcWidth", "arcWidth", 10.0); 105 } 106 107 @Test public void testBoundPropertySync_ArcHeight() throws Exception { 108 NodeTest.assertDoublePropertySynced( 109 new StubRectangle(200.0, 100.0), 110 "arcHeight", "arcHeight", 30.0); 111 } 112 113 114 @Test 115 public void testTransformedBounds_rotation() { 116 Rectangle r = new StubRectangle(50, 100, 10, 20); 117 r.setArcHeight(5); 118 r.setArcWidth(10); 119 Bounds original = r.getBoundsInParent(); 120 r.setRotate(90); 121 assertSimilar(TestHelper.box(45, 105, 122 original.getHeight(), original.getWidth()), r.getBoundsInParent()); 123 } 124 125 @Test public void toStringShouldReturnNonEmptyString() { 126 String s = new StubRectangle().toString(); 127 assertNotNull(s); 128 assertFalse(s.isEmpty()); 129 } 130 131 public static final class StubRectangle extends Rectangle { 132 public StubRectangle() { 133 super(); 134 } 135 136 public StubRectangle(double width, double height) { 137 super(width, height); 138 } 139 140 public StubRectangle(double width, double height, Paint fill) { 141 super(width, height, fill); 142 } 143 144 public StubRectangle(double x, double y, double width, double height) { 145 super(x, y, width, height); 146 } 147 148 @Override 149 protected NGNode impl_createPeer() { 150 return new StubNGRectangle(); 151 } 152 } 153 154 public static final class StubNGRectangle extends NGRectangle { 155 // for tests 156 private float x; 157 private float y; 158 private float width; 159 private float height; 160 private float arcWidth; 161 private float arcHeight; 162 163 public void setX(float x) {this.x = x;} 164 public void setY(float y) {this.y = y;} 165 public void setWidth(float width) {this.width = width;} 166 public void setHeight(float height) {this.height = height;} 167 public void setArcWidth(float arcWidth) {this.arcWidth = arcWidth;} 168 public void setArcHeight(float arcHeight) {this.arcHeight = arcHeight;} 169 public float getArcHeight() {return arcHeight;} 170 public float getArcWidth() {return arcWidth;} 171 public float getHeight() {return height;} 172 public float getWidth() {return width;} 173 public float getX() {return x;} 174 public float getY() {return y;} 175 176 @Override 177 public void updateRectangle(float x, float y, float width, float height, float arcWidth, float arcHeight) { 178 this.x = x; 179 this.y = y; 180 this.width = width; 181 this.height = height; 182 this.arcWidth = arcWidth; 183 this.arcHeight = arcHeight; 184 } 185 186 private BaseTransform transformMatrix; 187 @Override 188 public void setTransformMatrix(BaseTransform tx) { 189 super.setTransformMatrix(tx); 190 this.transformMatrix = tx; 191 } 192 193 public BaseTransform getTransformMatrix() { 194 return transformMatrix; 195 } 196 } 197 }