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.layout;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertFalse;
  30 import static org.junit.Assert.assertTrue;
  31 import javafx.geometry.Insets;
  32 import javafx.scene.Group;
  33 import javafx.scene.Node;
  34 import javafx.scene.Parent;
  35 import javafx.scene.shape.Rectangle;
  36 import javafx.scene.text.Text;
  37 
  38 import org.junit.Test;
  39 import org.junit.Ignore;
  40 /**
  41  * Tests resizability apis of Node and key subclasses.
  42  *
  43  */
  44 public class ResizabilityTest {
  45 
  46     // test isResizable on key base classes
  47 
  48     @Test public void testNodeNotResizable() {
  49         Node node = new MockNode();
  50         assertFalse(node.isResizable());
  51     }
  52 
  53     @Test public void testShapeNotResizable() {
  54         Rectangle rect = new Rectangle();
  55         assertFalse(rect.isResizable());
  56     }
  57 
  58     @Test public void testTextNotResizable() {
  59         Text text = new Text();
  60         assertFalse(text.isResizable());
  61     }
  62 
  63 // consider making ImageView resizable!
  64 //    @Test public void testImageViewNotResizable() {
  65 //        ImageView imageview = new ImageView();
  66 //        assertFalse(imageview.isResizable());
  67 //    }
  68 
  69     @Test public void testParentNotResizable() {
  70         Parent parent = new MockParent();
  71         assertFalse(parent.isResizable());
  72     }
  73 
  74     @Test public void testGroupNotResizable() {
  75         Group group = new Group();
  76         assertFalse(group.isResizable());
  77     }
  78 
  79     @Test public void testRegionResizable() {
  80         Region container = new Region();
  81         assertTrue(container.isResizable());
  82     }
  83 
  84     //test min, pref, max sizes on key base classes
  85 
  86     @Test public void testShapeMinPrefMaxWidthEqualLayoutBounds() {
  87         Rectangle rect = new Rectangle(100,200);
  88         assertTrue(rect.getLayoutBounds().getWidth() == rect.minWidth(-1) &&
  89                    rect.getLayoutBounds().getWidth() == rect.prefWidth(-1) &&
  90                    rect.getLayoutBounds().getWidth() == rect.maxWidth(-1));
  91     }
  92 
  93     @Test public void testShapeMinPrefMaxHeightEqualLayoutBounds() {
  94         Rectangle rect = new Rectangle(100,200);
  95         assertTrue(rect.getLayoutBounds().getHeight() == rect.minHeight(-1) &&
  96                    rect.getLayoutBounds().getHeight() == rect.prefHeight(-1) &&
  97                    rect.getLayoutBounds().getHeight() == rect.maxHeight(-1));
  98     }
  99 
 100     @Test public void testTextMinPrefMaxWidthEqualLayoutBounds() {
 101         Text text = new Text("something");
 102         assertTrue(text.getLayoutBounds().getWidth() == text.minWidth(-1) &&
 103                    text.getLayoutBounds().getWidth() == text.prefWidth(-1) &&
 104                    text.getLayoutBounds().getWidth() == text.maxWidth(-1));
 105     }
 106 
 107     @Test public void testTextMinPrefMaxHeightEqualLayoutBounds() {
 108         Text text = new Text("something");
 109         assertTrue(text.getLayoutBounds().getHeight() == text.minHeight(-1) &&
 110                    text.getLayoutBounds().getHeight() == text.prefHeight(-1) &&
 111                    text.getLayoutBounds().getHeight() == text.maxHeight(-1));
 112     }
 113 
 114     @Test public void testParentMinPrefMaxWidthAreEqual() {
 115         Parent parent = new MockParent();
 116         assertTrue(parent.prefWidth(-1) == parent.minWidth(-1) &&
 117                    parent.prefWidth(-1) == parent.maxWidth(-1));
 118     }
 119     
 120     @Test public void testParentMinPrefMaxHeightAreEqual() {
 121         Parent parent = new MockParent();
 122         assertTrue(parent.prefHeight(-1) == parent.minHeight(-1) &&
 123                    parent.prefHeight(-1) == parent.maxHeight(-1));
 124     }
 125 
 126     // test that parent computes pref size based on chldren
 127 
 128     @Test public void testParentPrefWidthQueriesChildPrefWidth() {
 129         Parent parent = new MockParent();
 130         assertEquals(110, parent.prefWidth(-1), 1e-100);
 131     }
 132 
 133     @Test public void testParentPrefHeightQueriesChildPrefHeight() {
 134         Parent parent = new MockParent();
 135         assertEquals(220, parent.prefHeight(-1), 1e-100);
 136     }
 137 
 138     @Test public void testPanePrefWidthQueriesChildPrefWidth() {
 139         Pane container = new Pane();
 140         Rectangle r = new Rectangle(-10,-20,100,200);
 141         container.getChildren().add(r);
 142         MockResizable tr = new MockResizable(100,200);
 143         container.getChildren().add(tr);
 144         assertEquals(110, container.prefWidth(-1), 1e-100);
 145     }
 146 
 147     @Test public void testPanePrefHeightQueriesChildPrefHeight() {
 148         Pane container = new Pane();
 149         Rectangle r = new Rectangle(-10,-20,100,200);
 150         container.getChildren().add(r);
 151         MockResizable tr = new MockResizable(100,200);
 152         container.getChildren().add(tr);
 153         assertEquals(220, container.prefHeight(-1), 1e-100);
 154     }
 155 
 156     @Test public void testPanePrefWidthIncludesPadding() {
 157         Pane container = new Pane();
 158         container.setPadding(new Insets(10,20,30,40));
 159         Rectangle r = new Rectangle(-10,-20,100,200);
 160         container.getChildren().add(r);
 161         MockResizable tr = new MockResizable(100,200);
 162         container.getChildren().add(tr);
 163         assertEquals(170, container.prefWidth(-1), 1e-100);
 164     }
 165 
 166     @Test public void testPanePrefHeightIncludesPadding() {
 167         Pane container = new Pane();
 168         container.setPadding(new Insets(10,20,30,40));
 169         Rectangle r = new Rectangle(-10,-20,100,200);
 170         container.getChildren().add(r);
 171         MockResizable tr = new MockResizable(100,200);
 172         container.getChildren().add(tr);
 173         assertEquals(260, container.prefHeight(-1), 1e-100);
 174     }
 175 
 176     // test relocate, resize, resizeRelocate
 177 
 178     @Test public void testRelocateNonResizable() {
 179         Rectangle r = new Rectangle(10, 20, 100, 200);
 180         r.relocate(0,0);
 181 
 182         assertEquals(-10, r.getLayoutX(), 1e-100);
 183         assertEquals(-20, r.getLayoutY(), 1e-100);
 184     }
 185 
 186     @Test public void testRelocateResizable() {
 187         MockResizable resizable = new MockResizable(100,200);
 188         resizable.relocate(50,50);
 189 
 190         assertEquals(50, resizable.getLayoutX(), 1e-100);
 191         assertEquals(50, resizable.getLayoutY(), 1e-100);
 192     }
 193 
 194     @Test public void testResizeNonResizableIsNoOp() {
 195         Rectangle r = new Rectangle(10, 20, 100, 200);
 196         r.resize(400,400);
 197 
 198         assertEquals(100, r.getLayoutBounds().getWidth(), 1e-100);
 199         assertEquals(200, r.getLayoutBounds().getHeight(), 1e-100);
 200     }
 201 
 202     @Test public void testResizeResizable() {
 203         MockResizable resizable = new MockResizable(100,200);
 204         resizable.resize(30,40);
 205 
 206         assertEquals(30, resizable.getLayoutBounds().getWidth(), 1e-100);
 207         assertEquals(40, resizable.getLayoutBounds().getHeight(), 1e-100);
 208     }
 209 
 210     @Test public void testAutosize() {
 211         MockResizable resizable = new MockResizable(100,200);
 212         resizable.resize(30,40);
 213 
 214         assertEquals(30, resizable.getLayoutBounds().getWidth(), 1e-100);
 215         assertEquals(40, resizable.getLayoutBounds().getHeight(), 1e-100);
 216 
 217         resizable.autosize(); //back to preferred
 218         assertEquals(100, resizable.getLayoutBounds().getWidth(), 1e-100);
 219         assertEquals(200, resizable.getLayoutBounds().getHeight(), 1e-100);
 220     }
 221 
 222     @Test public void testResizeRelocateNonResizable() {
 223         Rectangle r = new Rectangle(10, 20, 100, 200);
 224         r.resizeRelocate(0, 0, 400,400);
 225 
 226         assertEquals(-10, r.getLayoutX(), 1e-100);
 227         assertEquals(-20, r.getLayoutY(), 1e-100);
 228         assertEquals(100, r.getLayoutBounds().getWidth(), 1e-100);
 229         assertEquals(200, r.getLayoutBounds().getHeight(), 1e-100);
 230     }
 231 
 232     @Test public void testResizeRelocateResizable() {
 233         MockResizable resizable = new MockResizable(100,200);
 234         resizable.resizeRelocate(50,50,30,40);
 235 
 236         assertEquals(50, resizable.getLayoutX(), 1e-100);
 237         assertEquals(50, resizable.getLayoutY(), 1e-100);
 238         assertEquals(30, resizable.getLayoutBounds().getWidth(), 1e-100);
 239         assertEquals(40, resizable.getLayoutBounds().getHeight(), 1e-100);
 240     }
 241 
 242 
 243 }