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