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 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 
 158     /**
 159      * Make sure minHeight is based on pref height. By overriding prefHeight to
 160      * be something nonsensical, we know in the test that the pref height is being used
 161      * and not the layout bounds height.
 162      */
 163     @Test public void Node_minHeight_SameAsPrefHeight() {
 164         Rectangle node = new Rectangle(10, 10, 100, 100) {
 165             @Override public double prefHeight(double height) {
 166                 return 500;
 167             }
 168         };
 169 
 170         assertEquals(500, node.minHeight(-1), 0);
 171         assertEquals(500, node.minHeight(5), 0);
 172     }
 173 
 174     /**
 175      * Make sure maxWidth is based on pref width. By overriding prefWidth to
 176      * be something nonsensical, we know in the test that the pref width is being used
 177      * and not the layout bounds width.
 178      */
 179     @Test public void Node_maxWidth_SameAsPrefWidth() {
 180         Rectangle node = new Rectangle(10, 10, 100, 100) {
 181             @Override public double prefWidth(double height) {
 182                 return 500;
 183             }
 184         };
 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 }