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 javafx.scene.control.skin;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertFalse;
  30 import static org.junit.Assert.assertTrue;
  31 
  32 import javafx.beans.value.ObservableValue;
  33 import javafx.geometry.HPos;
  34 import javafx.geometry.Insets;
  35 import javafx.geometry.Orientation;
  36 import javafx.geometry.VPos;
  37 import javafx.scene.control.Separator;
  38 import javafx.scene.control.SkinBaseAccessor;
  39 import javafx.scene.layout.Region;
  40 
  41 import org.junit.Before;
  42 import org.junit.Ignore;
  43 import org.junit.Test;
  44 
  45 /**
  46  */
  47 public class SeparatorSkinTest {
  48     private Separator separator;
  49     private SeparatorSkinMock skin;
  50     private Region line;
  51 
  52     @Before public void setup() {
  53         separator = new Separator();
  54         skin = new SeparatorSkinMock(separator);
  55         // Set some padding so that any places where padding was being
  56         // computed but wasn't expected will be caught.
  57         separator.setPadding(new Insets(10, 8, 6, 4));
  58         separator.setSkin(skin);
  59         // It so happens that SeparatorSkin has exactly one child: line
  60         line = (Region) SkinBaseAccessor.getChildren(skin).get(0);
  61         line.setPadding((new Insets(4, 3, 2, 1)));
  62         separator.layout();
  63     }
  64 
  65     /****************************************************************************
  66      *                                                                          *
  67      * Tests for change notification                                            *
  68      *                                                                          *
  69      ***************************************************************************/
  70 
  71     @Test public void orientationChangesOnSeparatorShouldInvoke_handleControlPropertyChanged() {
  72         skin.addWatchedProperty(separator.orientationProperty());
  73         assertFalse(skin.propertyChanged); // sanity check
  74         separator.setOrientation(Orientation.VERTICAL);
  75         assertTrue(skin.propertyChanged);
  76         assertEquals(1, skin.propertyChangeCount); // sanity check
  77     }
  78 
  79     @Test public void halignmentChangesOnSeparatorShouldInvoke_handleControlPropertyChanged() {
  80         skin.addWatchedProperty(separator.halignmentProperty());
  81         assertFalse(skin.propertyChanged); // sanity check
  82         separator.setHalignment(HPos.RIGHT);
  83         assertTrue(skin.propertyChanged);
  84         assertEquals(1, skin.propertyChangeCount); // sanity check
  85     }
  86 
  87     @Test public void valignmentChangesOnSeparatorShouldInvoke_handleControlPropertyChanged() {
  88         skin.addWatchedProperty(separator.valignmentProperty());
  89         assertFalse(skin.propertyChanged); // sanity check
  90         separator.setValignment(VPos.BASELINE);
  91         assertTrue(skin.propertyChanged);
  92         assertEquals(1, skin.propertyChangeCount); // sanity check
  93     }
  94 
  95     /****************************************************************************
  96      *                                                                          *
  97      * Tests for invalidation. When each of various properties change, we need  *
  98      * to invalidate some state, such as via requestLayout().                   *
  99      *                                                                          *
 100      ***************************************************************************/
 101 
 102     @Test public void orientationChangeShouldInvalidateLayout() {
 103         assertFalse(separator.isNeedsLayout());
 104         separator.setOrientation(Orientation.VERTICAL);
 105         assertTrue(separator.isNeedsLayout());
 106     }
 107 
 108     @Test public void halignmentChangeShouldInvalidateLayout() {
 109         assertFalse(separator.isNeedsLayout());
 110         separator.setHalignment(HPos.RIGHT);
 111         assertTrue(separator.isNeedsLayout());
 112     }
 113 
 114     @Test public void valignmentChangeShouldInvalidateLayout() {
 115         assertFalse(separator.isNeedsLayout());
 116         separator.setValignment(VPos.BASELINE);
 117         assertTrue(separator.isNeedsLayout());
 118     }
 119 
 120     /****************************************************************************
 121      *                                                                          *
 122      * Tests for minWidth                                                       *
 123      *                                                                          *
 124      ***************************************************************************/
 125 
 126     @Test public void minWidthWhenVerticalShouldBePaddingOfLinePlusPaddingOfSeparator() {
 127         separator.setOrientation(Orientation.VERTICAL);
 128         final Insets linePadding = line.getInsets();
 129         final Insets skinPadding = separator.getInsets();
 130         assertEquals(linePadding.getLeft() + linePadding.getRight() +
 131                 skinPadding.getLeft() + skinPadding.getRight(),
 132                 separator.minWidth(-1), 0);
 133     }
 134 
 135     @Ignore
 136     @Test public void minWidthWhenHorizontalShouldBePositiveNonZeroPlusPadding() {
 137         separator.setOrientation(Orientation.HORIZONTAL);
 138         final Insets linePadding = line.getInsets();
 139         final Insets skinPadding = separator.getInsets();
 140         assertTrue(separator.minWidth(-1) > 0);
 141         assertEquals(linePadding.getLeft() + linePadding.getRight() +
 142                 skinPadding.getLeft() + skinPadding.getRight(),
 143                 separator.minWidth(-1), 0);
 144     }
 145 
 146     /****************************************************************************
 147      *                                                                          *
 148      * Tests for minHeight                                                       *
 149      *                                                                          *
 150      ***************************************************************************/
 151 
 152     @Ignore
 153     @Test public void minHeightWhenVerticalShouldBePositiveNonZeroPlusPadding() {
 154         separator.setOrientation(Orientation.VERTICAL);
 155         final Insets linePadding = line.getInsets();
 156         final Insets skinPadding = separator.getInsets();
 157         assertTrue(separator.minHeight(-1) > 0);
 158         assertEquals(linePadding.getTop() + linePadding.getBottom() +
 159                 skinPadding.getTop() + skinPadding.getBottom(),
 160                 separator.minHeight(-1), 0);
 161     }
 162 
 163     @Test public void minHeightWhenHorizontalShouldBePaddingOfLinePlusPaddingOfSeparator() {
 164         separator.setOrientation(Orientation.HORIZONTAL);
 165         final Insets linePadding = line.getInsets();
 166         final Insets skinPadding = separator.getInsets();
 167         assertEquals(linePadding.getTop() + linePadding.getBottom() +
 168                 skinPadding.getTop() + skinPadding.getBottom(),
 169                 separator.minHeight(-1), 0);
 170     }
 171 
 172     /****************************************************************************
 173      *                                                                          *
 174      * Tests for maxWidth                                                       *
 175      *                                                                          *
 176      ***************************************************************************/
 177 
 178     @Test public void maxWidthWhenVerticalShouldBePaddingOfLinePlusPaddingOfSeparator() {
 179         separator.setOrientation(Orientation.VERTICAL);
 180         final Insets linePadding = line.getInsets();
 181         final Insets skinPadding = separator.getInsets();
 182         assertEquals(linePadding.getLeft() + linePadding.getRight() +
 183                 skinPadding.getLeft() + skinPadding.getRight(),
 184                 separator.maxWidth(-1), 0);
 185     }
 186 
 187     @Test public void maxWidthWhenHorizontalShouldBeMAX_VALUE() {
 188         separator.setOrientation(Orientation.HORIZONTAL);
 189         assertEquals(Double.MAX_VALUE, separator.maxWidth(-1), 0);
 190     }
 191 
 192     /****************************************************************************
 193      *                                                                          *
 194      * Tests for maxHeight                                                       *
 195      *                                                                          *
 196      ***************************************************************************/
 197 
 198     @Test public void maxHeightWhenVerticalShouldBeMAX_VALUE() {
 199         separator.setOrientation(Orientation.VERTICAL);
 200         assertEquals(Double.MAX_VALUE, separator.maxHeight(-1), 0);
 201     }
 202 
 203     @Test public void maxHeightWhenHorizontalShouldBePaddingOfLinePlusPaddingOfSeparator() {
 204         separator.setOrientation(Orientation.HORIZONTAL);
 205         final Insets linePadding = line.getInsets();
 206         final Insets skinPadding = separator.getInsets();
 207         assertEquals(linePadding.getTop() + linePadding.getBottom() +
 208                 skinPadding.getTop() + skinPadding.getBottom(),
 209                 separator.maxHeight(-1), 0);
 210     }
 211 
 212     @Test public void onVerticalMaxWidthTracksPreferred() {
 213         separator.setOrientation(Orientation.VERTICAL);
 214         separator.setPrefWidth(100);
 215         assertEquals(100, separator.maxWidth(-1), 0);
 216     }
 217 
 218     @Test public void onHorizontalMaxHeightTracksPreferred() {
 219         separator.setOrientation(Orientation.HORIZONTAL);
 220         separator.setPrefHeight(100);
 221         assertEquals(100, separator.maxHeight(-1), 0);
 222     }
 223 
 224     public static final class SeparatorSkinMock extends SeparatorSkin {
 225         boolean propertyChanged = false;
 226         int propertyChangeCount = 0;
 227         public SeparatorSkinMock(Separator sep) {
 228             super(sep);
 229         }
 230 
 231         public void addWatchedProperty(ObservableValue<?> p) {
 232             p.addListener(o -> {
 233                 propertyChanged = true;
 234                 propertyChangeCount++;
 235             });
 236         }
 237     }
 238 
 239 
 240 }