1 /*
   2  * Copyright (c) 2011, 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 com.sun.javafx.scene.control.skin;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 import javafx.geometry.Bounds;
  30 import javafx.geometry.HPos;
  31 import javafx.geometry.Insets;
  32 import javafx.geometry.Orientation;
  33 import javafx.geometry.VPos;
  34 import javafx.scene.control.Separator;
  35 import javafx.scene.control.SkinBaseAccessor;
  36 import javafx.scene.layout.Region;
  37 
  38 import org.junit.Before;
  39 import org.junit.Test;
  40 
  41 /**
  42  */
  43 public class SeparatorSkinLayoutTest {
  44     private Separator separator;
  45     private SeparatorSkin skin;
  46     private Region line;
  47 
  48     @Before public void setup() {
  49         separator = new Separator();
  50         skin = new SeparatorSkin(separator);
  51         // Set some padding so that any places where padding was being
  52         // computed but wasn't expected will be caught.
  53         separator.setPadding(new Insets(10, 8, 6, 4));
  54         separator.setSkin(skin);
  55         // It so happens that SeparatorSkin has exactly one child: line
  56         line = (Region) SkinBaseAccessor.getChildren(skin).get(0);
  57         line.setPadding((new Insets(4, 3, 2, 1)));
  58         separator.resize(100, 100);
  59         separator.layout();
  60     }
  61 
  62     private void assertLineWidthMatchesSkinWidth() {
  63         Bounds lineBounds = line.getBoundsInParent();
  64         assertEquals(separator.getWidth() - separator.getInsets().getRight(), lineBounds.getMaxX(), .1);
  65         assertEquals(separator.getInsets().getLeft(), lineBounds.getMinX(), .1);
  66     }
  67 
  68     private void assertLineHeightMatchesSkinHeight() {
  69         Bounds lineBounds = line.getBoundsInParent();
  70         assertEquals(separator.getInsets().getTop(), lineBounds.getMinY(), .1);
  71         assertEquals(separator.getHeight() - separator.getInsets().getBottom(), lineBounds.getMaxY(), .1);
  72     }
  73 
  74     // ----------------------- Horizontal Tests
  75 
  76     /**
  77      * The line should be as wide as the skin, and as tall as its prefHeight,
  78      * and be positioned within the padding of the skin
  79      */
  80     @Test public void separatorWith_TOP_PositionsLineAtTopOfArea() {
  81         separator.setValignment(VPos.TOP);
  82         separator.layout();
  83         Bounds lineBounds = line.getBoundsInParent();
  84         assertEquals(separator.getInsets().getTop(), lineBounds.getMinY(), .1);
  85         assertEquals(line.prefHeight(-1), lineBounds.getHeight(), .1);
  86         assertLineWidthMatchesSkinWidth();
  87     }
  88 
  89     /**
  90      * The line should be as wide as the skin, and as tall as its prefHeight,
  91      * and be positioned centered vertically within the padding of the skin
  92      */
  93     @Test public void separatorWith_CENTER_PositionsLineAtCenterOfArea() {
  94         separator.setValignment(VPos.CENTER);
  95         separator.layout();
  96         Bounds lineBounds = line.getBoundsInParent();
  97         final double ch = separator.getHeight() - (separator.getInsets().getTop() + separator.getInsets().getBottom());
  98         final double centerLine = separator.getInsets().getTop() + (ch / 2.0);
  99         assertEquals(centerLine - (lineBounds.getHeight() / 2.0), lineBounds.getMinY(), .1);
 100         assertEquals(line.prefHeight(-1), lineBounds.getHeight(), .1);
 101         assertLineWidthMatchesSkinWidth();
 102     }
 103 
 104     /**
 105      * The line should be as wide as the skin, and as tall as its prefHeight,
 106      * and be positioned within the bottom padding of the skin
 107      */
 108     @Test public void separatorWith_BOTTOM_PositionsLineAtBottomOfArea() {
 109         separator.setValignment(VPos.BOTTOM);
 110         separator.layout();
 111         Bounds lineBounds = line.getBoundsInParent();
 112         final double y = separator.getHeight() - (separator.getInsets().getBottom() + line.prefHeight(-1));
 113         assertEquals(y, lineBounds.getMinY(), .1);
 114         assertEquals(line.prefHeight(-1), lineBounds.getHeight(), .1);
 115         assertLineWidthMatchesSkinWidth();
 116     }
 117 
 118     // ----------------------- Vertical Tests
 119 
 120     /**
 121      * The line should be as tall as the skin, and as wide as its prefWidth,
 122      * and be positioned within the left padding of the skin
 123      */
 124     @Test public void separatorWith_LEFT_PositionsLineAtLeftOfArea_vertical() {
 125         separator.setHalignment(HPos.LEFT);
 126         separator.setOrientation(Orientation.VERTICAL);
 127         separator.layout();
 128         Bounds lineBounds = line.getBoundsInParent();
 129         assertEquals(separator.getInsets().getLeft(), lineBounds.getMinX(), .1);
 130         assertEquals(line.prefWidth(-1), lineBounds.getWidth(), .1);
 131         assertLineHeightMatchesSkinHeight();
 132     }
 133 
 134     /**
 135      * The line should be as tall as the skin, and as wide as its prefWidth,
 136      * and be positioned centered within the padding of the skin
 137      */
 138     @Test public void separatorWith_CENTER_PositionsLineAtCenterOfArea_vertical() {
 139         separator.setHalignment(HPos.CENTER);
 140         separator.setOrientation(Orientation.VERTICAL);
 141         separator.layout();
 142         Bounds lineBounds = line.getBoundsInParent();
 143         final double cw = separator.getWidth() - (separator.getInsets().getLeft() + separator.getInsets().getRight());
 144         final double centerLine = separator.getInsets().getLeft() + (cw / 2.0);
 145         assertEquals(centerLine - (lineBounds.getWidth() / 2.0), lineBounds.getMinX(), .1);
 146         assertEquals(line.prefWidth(-1), lineBounds.getWidth(), .1);
 147         assertLineHeightMatchesSkinHeight();
 148     }
 149 
 150     /**
 151      * The line should be as tall as the skin, and as wide as its prefWidth,
 152      * and be positioned within the right padding of the skin
 153      */
 154     @Test public void separatorWith_RIGHT_PositionsLineAtRightOfArea_vertical() {
 155         separator.setHalignment(HPos.RIGHT);
 156         separator.setOrientation(Orientation.VERTICAL);
 157         separator.layout();
 158         Bounds lineBounds = line.getBoundsInParent();
 159         final double x = separator.getWidth() - (separator.getInsets().getRight() + line.prefWidth(-1));
 160         assertEquals(x, lineBounds.getMinX(), .1);
 161         assertEquals(line.prefWidth(-1), lineBounds.getWidth(), .1);
 162         assertLineHeightMatchesSkinHeight();
 163     }
 164 
 165 }