1 /* 2 * Copyright (c) 2013, 2015, 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; 27 28 import com.sun.javafx.scene.shape.RectangleHelper; 29 import javafx.geometry.BoundingBox; 30 import javafx.geometry.Bounds; 31 import javafx.scene.Node; 32 import javafx.scene.shape.Rectangle; 33 import org.junit.Test; 34 import static org.junit.Assert.assertEquals; 35 36 /** 37 * Tests for the layout size methods on Node. In particular, we need to ensure that 38 * the min/max width/height are both based on pref by default. Also that these methods 39 * never (ever) return NaN or negative values. 40 */ 41 public class Node_layoutSizes_Test { 42 /** 43 * The standard case, where the layout bounds is automatically determined by the bounds of 44 * the rectangle. I've offset the rectangle x/y to make sure we're using the width and 45 * not the maxX for this calculation. 46 */ 47 @Test public void Node_prefWidth_BasedOnLayoutBounds() { 48 Rectangle node = new Rectangle(10, 10, 100, 100); 49 assertEquals(100, node.prefWidth(-1), 0); 50 assertEquals(100, node.prefWidth(5), 0); 51 } 52 53 /** 54 * A specialized computeLayoutBounds implementation, to make sure it is the 55 * layout bounds from which we're gathering this information. 56 */ 57 @Test public void Node_prefWidth_BasedOnLayoutBounds2() { 58 SpecialRect node = new SpecialRect(10, 10, 100, 100); 59 node.setTestBB(0, 0, 50, 50); 60 assertEquals(50, node.prefWidth(-1), 0); 61 assertEquals(50, node.prefWidth(5), 0); 62 } 63 64 /** 65 * If the layout bounds has a NaN, it shouldn't leak out through node.prefWidth 66 */ 67 @Test public void Node_prefWidth_BasedOnLayoutBounds_CleansUpAfterBadBounds() { 68 SpecialRect node = new SpecialRect(10, 10, 100, 100); 69 node.setTestBB(0, 0, Double.NaN, 50); 70 assertEquals(0, node.prefWidth(-1), 0); 71 assertEquals(0, node.prefWidth(5), 0); 72 } 73 74 /** 75 * If the layout bounds has a negative value, it shouldn't leak out through node.prefWidth 76 */ 77 @Test public void Node_prefWidth_BasedOnLayoutBounds_CleansUpAfterBadBounds2() { 78 SpecialRect node = new SpecialRect(10, 10, 100, 100); 79 node.setTestBB(0, 0, -10, 50); 80 assertEquals(0, node.prefWidth(-1), 0); 81 assertEquals(0, node.prefWidth(5), 0); 82 } 83 84 /** 85 * The standard case, where the layout bounds is automatically determined by the bounds of 86 * the rectangle. I've offset the rectangle x/y to make sure we're using the height and 87 * not the maxY for this calculation. 88 */ 89 @Test public void Node_prefHeight_BasedOnLayoutBounds() { 90 Rectangle node = new Rectangle(10, 10, 100, 100); 91 assertEquals(100, node.prefHeight(-1), 0); 92 assertEquals(100, node.prefHeight(5), 0); 93 } 94 95 /** 96 * A specialized computeLayoutBounds implementation, to make sure it is the 97 * layout bounds from which we're gathering this information. 98 */ 99 @Test public void Node_prefHeight_BasedOnLayoutBounds2() { 100 SpecialRect node = new SpecialRect(10, 10, 100, 100); 101 node.setTestBB(0, 0, 50, 50); 102 assertEquals(50, node.prefHeight(-1), 0); 103 assertEquals(50, node.prefHeight(5), 0); 104 } 105 106 /** 107 * If the layout bounds has a NaN, it shouldn't leak out through node.prefHeight 108 */ 109 @Test public void Node_prefHeight_BasedOnLayoutBounds_CleansUpAfterBadBounds() { 110 SpecialRect node = new SpecialRect(10, 10, 100, 100); 111 node.setTestBB(0, 0, 50, Double.NaN); 112 assertEquals(0, node.prefHeight(-1), 0); 113 assertEquals(0, node.prefHeight(5), 0); 114 } 115 116 /** 117 * If the layout bounds has a negative value, it shouldn't leak out through node.prefHeight 118 */ 119 @Test public void Node_prefHeight_BasedOnLayoutBounds_CleansUpAfterBadBounds2() { 120 SpecialRect node = new SpecialRect(10, 10, 100, 100); 121 node.setTestBB(0, 0, 50, -10); 122 assertEquals(0, node.prefHeight(-1), 0); 123 assertEquals(0, node.prefHeight(5), 0); 124 } 125 126 /** 127 * Make sure minWidth is based on pref width. By overriding prefWidth to 128 * be something nonsensical, we know in the test that the pref width is being used 129 * and not the layout bounds width. 130 */ 131 @Test public void Node_minWidth_SameAsPrefWidth() { 132 Rectangle node = new Rectangle(10, 10, 100, 100) { 133 @Override public double prefWidth(double height) { 134 return 500; 135 } 136 }; 137 138 assertEquals(500, node.minWidth(-1), 0); 139 assertEquals(500, node.minWidth(5), 0); 140 } 141 142 /** 143 * Make sure minHeight is based on pref height. By overriding prefHeight to 144 * be something nonsensical, we know in the test that the pref height is being used 145 * and not the layout bounds height. 146 */ 147 @Test public void Node_minHeight_SameAsPrefHeight() { 148 Rectangle node = new Rectangle(10, 10, 100, 100) { 149 @Override public double prefHeight(double height) { 150 return 500; 151 } 152 }; 153 154 assertEquals(500, node.minHeight(-1), 0); 155 assertEquals(500, node.minHeight(5), 0); 156 } 157 158 /** 159 * Make sure maxWidth is based on pref width. By overriding prefWidth to 160 * be something nonsensical, we know in the test that the pref width is being used 161 * and not the layout bounds width. 162 */ 163 @Test public void Node_maxWidth_SameAsPrefWidth() { 164 Rectangle node = new Rectangle(10, 10, 100, 100) { 165 @Override public double prefWidth(double height) { 166 return 500; 167 } 168 }; 169 170 assertEquals(500, node.maxWidth(-1), 0); 171 assertEquals(500, node.maxWidth(5), 0); 172 } 173 174 /** 175 * Make sure maxHeight is based on pref height. By overriding prefHeight to 176 * be something nonsensical, we know in the test that the pref height is being used 177 * and not the layout bounds height. 178 */ 179 @Test public void Node_maxHeight_SameAsPrefHeight() { 180 Rectangle node = new Rectangle(10, 10, 100, 100) { 181 @Override public double prefHeight(double height) { 182 return 500; 183 } 184 }; 185 186 assertEquals(500, node.maxHeight(-1), 0); 187 assertEquals(500, node.maxHeight(5), 0); 188 } 189 190 // 191 // A specialized rect that return a user hardcoded computeLayoutBounds 192 // 193 protected class SpecialRect extends Rectangle { 194 195 BoundingBox testBB; 196 197 { 198 // To initialize the class helper at the begining each constructor of this class 199 SpecialRectHelper.initHelper(this); 200 } 201 202 public SpecialRect(double x, double y, double width, double height) { 203 } 204 205 void setTestBB(double x, double y, double width, double height) { 206 testBB = new BoundingBox(x, y, width, height); 207 } 208 209 private Bounds doComputeLayoutBounds() { 210 return testBB; 211 } 212 } 213 214 static final class SpecialRectHelper extends RectangleHelper { 215 216 private static final SpecialRectHelper theInstance; 217 218 static { 219 theInstance = new SpecialRectHelper(); 220 } 221 222 private static SpecialRectHelper getInstance() { 223 return theInstance; 224 } 225 226 public static void initHelper(SpecialRect cssBridge) { 227 setHelper(cssBridge, getInstance()); 228 } 229 230 @Override 231 protected Bounds computeLayoutBoundsImpl(Node node) { 232 return ((SpecialRect) node).doComputeLayoutBounds(); 233 } 234 } 235 236 }