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