1 /*
   2  * Copyright (c) 2011, 2015, 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;
  27 
  28 import com.sun.javafx.scene.NodeHelper;
  29 import javafx.css.CssMetaData;
  30 import static test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils.*;
  31 import static org.junit.Assert.assertEquals;
  32 import static org.junit.Assert.assertFalse;
  33 import static org.junit.Assert.assertNull;
  34 import static org.junit.Assert.assertSame;
  35 import static org.junit.Assert.assertTrue;
  36 import javafx.beans.property.ObjectProperty;
  37 import javafx.beans.property.SimpleObjectProperty;
  38 import javafx.css.StyleableProperty;
  39 import javafx.scene.control.Label;
  40 import javafx.scene.control.TextField;
  41 import javafx.scene.paint.Color;
  42 import javafx.scene.paint.Paint;
  43 import javafx.scene.shape.Rectangle;
  44 
  45 import org.junit.Before;
  46 import org.junit.Ignore;
  47 import org.junit.Test;
  48 
  49 public class LabelTest {
  50     private Label label;
  51 
  52     @Before public void setup() {
  53         label = new Label();
  54     }
  55 
  56     /*********************************************************************
  57      * Tests for the constructors                                        *
  58      ********************************************************************/
  59 
  60     @Test public void defaultConstructorShouldHaveNoGraphicAndEmptyString() {
  61         assertNull(label.getGraphic());
  62         assertEquals("", label.getText());
  63     }
  64 
  65     @Test public void oneArgConstructorShouldHaveNoGraphicAndSpecifiedString() {
  66         Label label2 = new Label(null);
  67         assertNull(label2.getGraphic());
  68         assertNull(label2.getText());
  69 
  70         label2 = new Label("");
  71         assertNull(label2.getGraphic());
  72         assertEquals("", label2.getText());
  73 
  74         label2 = new Label("Hello");
  75         assertNull(label2.getGraphic());
  76         assertEquals("Hello", label2.getText());
  77     }
  78 
  79     @Test public void twoArgConstructorShouldHaveSpecifiedGraphicAndSpecifiedString() {
  80         Label label2 = new Label(null, null);
  81         assertNull(label2.getGraphic());
  82         assertNull(label2.getText());
  83 
  84         Rectangle rect = new Rectangle();
  85         label2 = new Label("Hello", rect);
  86         assertSame(rect, label2.getGraphic());
  87         assertEquals("Hello", label2.getText());
  88     }
  89 
  90     @Test public void defaultConstructorShouldSetStyleClassTo_label() {
  91         assertStyleClassContains(label, "label");
  92     }
  93 
  94     @Test public void oneArgConstructorShouldSetStyleClassTo_label() {
  95         Label label2 = new Label(null);
  96         assertStyleClassContains(label2, "label");
  97     }
  98 
  99     @Test public void twoArgConstructorShouldSetStyleClassTo_label() {
 100         Label label2 = new Label(null, null);
 101         assertStyleClassContains(label2, "label");
 102     }
 103 
 104     @Test public void defaultConstructorShouldSetFocusTraversableToFalse() {
 105         assertFalse(label.isFocusTraversable());
 106     }
 107 
 108     @Test public void oneArgConstructorShouldSetFocusTraversableToFalse() {
 109         Label label2 = new Label(null);
 110         assertFalse(label2.isFocusTraversable());
 111     }
 112 
 113     @Test public void twoArgConstructorShouldSetFocusTraversableToFalse() {
 114         Label label2 = new Label(null, null);
 115         assertFalse(label2.isFocusTraversable());
 116     }
 117 
 118     /********************************************************************************
 119      *                                                                              *
 120      *                         Tests for labelFor property                          *
 121      *                                                                              *
 122      *******************************************************************************/
 123 
 124     @Test public void labelForDefaultValueIsNULL() {
 125         assertNull(label.getLabelFor());
 126         assertNull(label.labelForProperty().get());
 127     }
 128 
 129     @Test public void settingLabelForValueShouldWork() {
 130         TextField textField = new TextField();
 131         label.setLabelFor(textField);
 132         assertSame(textField, label.getLabelFor());
 133     }
 134 
 135     @Test public void settingLabelForAndThenCreatingAModelAndReadingTheValueStillWorks() {
 136         TextField textField = new TextField();
 137         label.setLabelFor(textField);
 138         assertSame(textField, label.labelForProperty().get());
 139     }
 140 
 141     @Test public void labelForCanBeBound() {
 142         TextField textField = new TextField();
 143         ObjectProperty<TextField> other = new SimpleObjectProperty<TextField>(textField);
 144         label.labelForProperty().bind(other);
 145         assertSame(textField, label.getLabelFor());
 146         other.set(null);
 147         assertNull(label.getLabelFor());
 148     }
 149 
 150     @Test public void impl_cssSettable_AlwaysReturnsFalseForLabelFor() {
 151         try {
 152             CssMetaData styleable = ((StyleableProperty)label.labelForProperty()).getCssMetaData();
 153             assertNull(styleable);
 154         } catch (ClassCastException ignored) {
 155             // pass!
 156         } catch (Exception e) {
 157             org.junit.Assert.fail(e.toString());
 158         }
 159     }
 160 
 161     @Test public void labelForBeanIsCorrect() {
 162         assertSame(label, label.labelForProperty().getBean());
 163     }
 164 
 165     @Test public void labelForNameIsCorrect() {
 166         assertEquals("labelFor", label.labelForProperty().getName());
 167     }
 168 
 169     // TODO I'm not actually sure why we base the showMnemonics of a Label
 170     // TODO on the showMnemonics of its labelFor, but since we do, I need
 171     // TODO to test it to make sure things get hooked up and unhooked
 172     @Test public void showMnemonicsHasNoListenersOnTextFieldByDefault() {
 173         // This is a sanity check test, so the following tests make sense
 174         TextField textField = new TextField();
 175         assertEquals(0, getListenerCount(NodeHelper.showMnemonicsProperty(textField)));
 176     }
 177 
 178     @Test public void settingLabelForShouldAddListenerToShowMnemonics() {
 179         TextField textField = new TextField();
 180         label.setLabelFor(textField);
 181         assertEquals(1, getListenerCount(NodeHelper.showMnemonicsProperty(textField)));
 182     }
 183 
 184     @Test public void settingLabelForShouldAddListenerToShowMnemonics_SetThroughProperty() {
 185         TextField textField = new TextField();
 186         label.labelForProperty().set(textField);
 187         assertEquals(1, getListenerCount(NodeHelper.showMnemonicsProperty(textField)));
 188     }
 189 
 190     @Test public void settingLabelForShouldAddListenerToShowMnemonics_WhenBound() {
 191         TextField textField = new TextField();
 192         ObjectProperty<TextField> other = new SimpleObjectProperty<TextField>(textField);
 193         label.labelForProperty().bind(other);
 194         assertEquals(1, getListenerCount(NodeHelper.showMnemonicsProperty(textField)));
 195     }
 196 
 197     @Test public void clearingLabelForShouldRemoveListenerFromShowMnemonics() {
 198         TextField textField = new TextField();
 199         label.setLabelFor(textField);
 200         label.setLabelFor(null);
 201         assertEquals(0, getListenerCount(NodeHelper.showMnemonicsProperty(textField)));
 202     }
 203 
 204     @Test public void clearingLabelForShouldRemoveListenerFromShowMnemonics_SetThroughProperty() {
 205         TextField textField = new TextField();
 206         label.labelForProperty().set(textField);
 207         label.labelForProperty().set(null);
 208         assertEquals(0, getListenerCount(NodeHelper.showMnemonicsProperty(textField)));
 209     }
 210 
 211     @Test public void clearingLabelForShouldRemoveListenerFromShowMnemonics_WhenBound() {
 212         TextField textField = new TextField();
 213         ObjectProperty<TextField> other = new SimpleObjectProperty<TextField>(textField);
 214         label.labelForProperty().bind(other);
 215         other.set(null);
 216         assertEquals(0, getListenerCount(NodeHelper.showMnemonicsProperty(textField)));
 217     }
 218 
 219     @Test public void swappingLabelForShouldAddAndRemoveListenerFromShowMnemonics() {
 220         TextField a = new TextField();
 221         TextField b = new TextField();
 222         label.setLabelFor(a);
 223         label.setLabelFor(b);
 224         assertEquals(0, getListenerCount(NodeHelper.showMnemonicsProperty(a)));
 225         assertEquals(1, getListenerCount(NodeHelper.showMnemonicsProperty(b)));
 226     }
 227 
 228     @Test public void swappingLabelForShouldAddAndRemoveListenerFromShowMnemonics_SetThroughProperty() {
 229         TextField a = new TextField();
 230         TextField b = new TextField();
 231         label.labelForProperty().set(a);
 232         label.labelForProperty().set(b);
 233         assertEquals(0, getListenerCount(NodeHelper.showMnemonicsProperty(a)));
 234         assertEquals(1, getListenerCount(NodeHelper.showMnemonicsProperty(b)));
 235     }
 236 
 237     @Test public void swappingLabelForShouldAddAndRemoveListenerFromShowMnemonics_WhenBound() {
 238         TextField a = new TextField();
 239         TextField b = new TextField();
 240         ObjectProperty<TextField> other = new SimpleObjectProperty<TextField>(a);
 241         label.labelForProperty().bind(other);
 242         other.set(b);
 243         assertEquals(0, getListenerCount(NodeHelper.showMnemonicsProperty(a)));
 244         assertEquals(1, getListenerCount(NodeHelper.showMnemonicsProperty(b)));
 245     }
 246 
 247     @Test public void changingShowMnemonicsOnLabelForUpdatesStateForLabel() {
 248         TextField textField = new TextField();
 249         label.setLabelFor(textField);
 250         assertFalse(NodeHelper.isShowMnemonics(textField));
 251         assertFalse(NodeHelper.isShowMnemonics(label));
 252         NodeHelper.setShowMnemonics(textField, true);
 253         assertTrue(NodeHelper.isShowMnemonics(textField));
 254         assertTrue(NodeHelper.isShowMnemonics(label));
 255     }
 256 
 257     @Test public void showMnemonicsOfLabelIsUpdatedWhenLabelForIsSet() {
 258         TextField textField = new TextField();
 259         NodeHelper.setShowMnemonics(textField, true);
 260         label.setLabelFor(textField);
 261         assertTrue(NodeHelper.isShowMnemonics(textField));
 262         assertTrue(NodeHelper.isShowMnemonics(label));
 263     }
 264 
 265     @Test public void showMnemonicsOfLabelIsSetToFalseWhenLabelForIsCleared() {
 266         TextField textField = new TextField();
 267         NodeHelper.setShowMnemonics(textField, true);
 268         label.setLabelFor(textField);
 269         label.setLabelFor(null);
 270         assertTrue(NodeHelper.isShowMnemonics(textField));
 271         assertFalse(NodeHelper.isShowMnemonics(label));
 272     }
 273 }