1 /*
   2  * Copyright (c) 2011, 2017, 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.control.skin;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 
  30 import javafx.beans.value.ObservableValue;
  31 import javafx.geometry.Insets;
  32 import javafx.scene.control.CheckBox;
  33 import javafx.scene.control.skin.CheckBoxSkin;
  34 import com.sun.javafx.tk.Toolkit;
  35 import javafx.scene.Group;
  36 import javafx.scene.Scene;
  37 import javafx.stage.Stage;
  38 
  39 import org.junit.BeforeClass;
  40 import org.junit.Before;
  41 import org.junit.Test;
  42 
  43 /**
  44  */
  45 public class CheckBoxSkinTest {
  46     private CheckBox checkbox;
  47     private CheckBoxSkinMock skin;
  48     private static Toolkit tk;
  49     private Scene scene;
  50     private Stage stage;
  51 
  52     @BeforeClass public static void initToolKit() {
  53         tk = Toolkit.getToolkit();
  54     }
  55 
  56     @Before public void setup() {
  57         checkbox = new CheckBox("Test");
  58         skin = new CheckBoxSkinMock(checkbox);
  59         // Set some padding so that any places where padding was being
  60         // computed but wasn't expected will be caught.
  61         checkbox.setPadding(new Insets(10, 10, 10, 10));
  62         checkbox.setSkin(skin);
  63 
  64         scene = new Scene(new Group(checkbox));
  65         stage = new Stage();
  66         stage.setScene(scene);
  67         stage.show();
  68         tk.firePulse();
  69     }
  70 
  71     @Test public void maxWidthTracksPreferred() {
  72         checkbox.setPrefWidth(500);
  73         assertEquals(500, checkbox.maxWidth(-1), 0);
  74     }
  75 
  76     @Test public void maxHeightTracksPreferred() {
  77         checkbox.setPrefHeight(500);
  78         assertEquals(500, checkbox.maxHeight(-1), 0);
  79     }
  80 
  81     @Test public void testPadding() {
  82         checkbox.setPadding(new Insets(10, 20, 30, 40));
  83 
  84         tk.firePulse();
  85 
  86         double expectedArea = checkbox.getHeight() * checkbox.getWidth();
  87         double actualArea = checkbox.getSkin().getNode().computeAreaInScreen();
  88 
  89         assertEquals(expectedArea, actualArea, 0.001);
  90     }
  91 
  92     public static final class CheckBoxSkinMock extends CheckBoxSkin {
  93         boolean propertyChanged = false;
  94         int propertyChangeCount = 0;
  95         public CheckBoxSkinMock(CheckBox checkbox) {
  96             super(checkbox);
  97         }
  98 
  99         public void addWatchedProperty(ObservableValue<?> p) {
 100             p.addListener(o -> {
 101                 propertyChanged = true;
 102                 propertyChangeCount++;
 103             });
 104         }
 105     }
 106 }