modules/controls/src/test/java/javafx/scene/control/skin/SeparatorSkinTest.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization


   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() {


 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 }


   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() {


 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 }