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 javafx.geometry.BoundingBox; 29 import javafx.geometry.Bounds; 30 import javafx.scene.shape.Rectangle; 31 import org.junit.Test; 32 import static org.junit.Assert.assertEquals; 33 34 /** 35 * Tests for the layout size methods on Node. In particular, we need to ensure that 36 * the min/max width/height are both based on pref by default. Also that these methods 37 * never (ever) return NaN or negative values. 38 */ 39 public class Node_layoutSizes_Test { 40 /** 41 * The standard case, where the layout bounds is automatically determined by the bounds of 42 * the rectangle. I've offset the rectangle x/y to make sure we're using the width and 43 * not the maxX for this calculation. 44 */ 45 @Test public void Node_prefWidth_BasedOnLayoutBounds() { 46 Rectangle node = new Rectangle(10, 10, 100, 100); 47 assertEquals(100, node.prefWidth(-1), 0); 48 assertEquals(100, node.prefWidth(5), 0); 49 } 50 51 /** 52 * A specialized computeLayoutBounds implementation, to make sure it is the 53 * layout bounds from which we're gathering this information. 54 */ 55 @Test public void Node_prefWidth_BasedOnLayoutBounds2() { 56 Rectangle node = new Rectangle(10, 10, 100, 100) { 57 @Override protected Bounds impl_computeLayoutBounds() { 58 return new BoundingBox(0, 0, 50, 50); 59 } 60 }; 61 assertEquals(50, node.prefWidth(-1), 0); 62 assertEquals(50, node.prefWidth(5), 0); 63 } 64 65 /** 66 * If the layout bounds has a NaN, it shouldn't leak out through node.prefWidth 67 */ 68 @Test public void Node_prefWidth_BasedOnLayoutBounds_CleansUpAfterBadBounds() { 69 Rectangle node = new Rectangle(10, 10, 100, 100) { 70 @Override protected Bounds impl_computeLayoutBounds() { 71 return new BoundingBox(0, 0, Double.NaN, 50); 72 } 73 }; 74 assertEquals(0, node.prefWidth(-1), 0); 75 assertEquals(0, node.prefWidth(5), 0); 76 } 77 78 /** 79 * If the layout bounds has a negative value, it shouldn't leak out through node.prefWidth 80 */ 81 @Test public void Node_prefWidth_BasedOnLayoutBounds_CleansUpAfterBadBounds2() { 82 Rectangle node = new Rectangle(10, 10, 100, 100) { 83 @Override protected Bounds impl_computeLayoutBounds() { 84 return new BoundingBox(0, 0, -10, 50); 85 } 86 }; 87 assertEquals(0, node.prefWidth(-1), 0); 88 assertEquals(0, node.prefWidth(5), 0); 89 } 90 91 /** 92 * The standard case, where the layout bounds is automatically determined by the bounds of 93 * the rectangle. I've offset the rectangle x/y to make sure we're using the height and 94 * not the maxY for this calculation. 95 */ 96 @Test public void Node_prefHeight_BasedOnLayoutBounds() { 97 Rectangle node = new Rectangle(10, 10, 100, 100); 98 assertEquals(100, node.prefHeight(-1), 0); 99 assertEquals(100, node.prefHeight(5), 0); 100 } 101 102 /** 103 * A specialized computeLayoutBounds implementation, to make sure it is the 104 * layout bounds from which we're gathering this information. 105 */ 106 @Test public void Node_prefHeight_BasedOnLayoutBounds2() { 107 Rectangle node = new Rectangle(10, 10, 100, 100) { 108 @Override protected Bounds impl_computeLayoutBounds() { 109 return new BoundingBox(0, 0, 50, 50); 110 } 111 }; 112 assertEquals(50, node.prefHeight(-1), 0); 113 assertEquals(50, node.prefHeight(5), 0); 114 } 115 116 /** 117 * If the layout bounds has a NaN, it shouldn't leak out through node.prefHeight 118 */ 119 @Test public void Node_prefHeight_BasedOnLayoutBounds_CleansUpAfterBadBounds() { 120 Rectangle node = new Rectangle(10, 10, 100, 100) { 121 @Override protected Bounds impl_computeLayoutBounds() { 122 return new BoundingBox(0, 0, 50, Double.NaN); 123 } 124 }; 125 assertEquals(0, node.prefHeight(-1), 0); 126 assertEquals(0, node.prefHeight(5), 0); 127 } 128 129 /** 130 * If the layout bounds has a negative value, it shouldn't leak out through node.prefHeight 131 */ 132 @Test public void Node_prefHeight_BasedOnLayoutBounds_CleansUpAfterBadBounds2() { 133 Rectangle node = new Rectangle(10, 10, 100, 100) { 134 @Override protected Bounds impl_computeLayoutBounds() { 135 return new BoundingBox(0, 0, 50, -10); 136 } 137 }; 138 assertEquals(0, node.prefHeight(-1), 0); 139 assertEquals(0, node.prefHeight(5), 0); 140 } 141 142 /** 143 * Make sure minWidth is based on pref width. By overriding prefWidth to 144 * be something nonsensical, we know in the test that the pref width is being used 145 * and not the layout bounds width. 146 */ 147 @Test public void Node_minWidth_SameAsPrefWidth() { 148 Rectangle node = new Rectangle(10, 10, 100, 100) { 149 @Override public double prefWidth(double height) { 150 return 500; 151 } 152 }; 153 154 assertEquals(500, node.minWidth(-1), 0); 155 assertEquals(500, node.minWidth(5), 0); 156 } 157 185 186 assertEquals(500, node.maxWidth(-1), 0); 187 assertEquals(500, node.maxWidth(5), 0); 188 } 189 190 /** 191 * Make sure maxHeight is based on pref height. By overriding prefHeight to 192 * be something nonsensical, we know in the test that the pref height is being used 193 * and not the layout bounds height. 194 */ 195 @Test public void Node_maxHeight_SameAsPrefHeight() { 196 Rectangle node = new Rectangle(10, 10, 100, 100) { 197 @Override public double prefHeight(double height) { 198 return 500; 199 } 200 }; 201 202 assertEquals(500, node.maxHeight(-1), 0); 203 assertEquals(500, node.maxHeight(5), 0); 204 } 205 } | 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 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 } |