1 /*
   2  * Copyright (c) 2010, 2016, 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.SceneHelper;
  29 import static test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertPseudoClassDoesNotExist;
  30 import static test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertPseudoClassExists;
  31 import static test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertStyleClassContains;
  32 import static org.junit.Assert.assertEquals;
  33 import static org.junit.Assert.assertFalse;
  34 import static org.junit.Assert.assertNull;
  35 import static org.junit.Assert.assertSame;
  36 import static org.junit.Assert.assertTrue;
  37 import javafx.beans.property.BooleanProperty;
  38 import javafx.beans.property.ObjectProperty;
  39 import javafx.beans.property.SimpleBooleanProperty;
  40 import javafx.beans.property.SimpleObjectProperty;
  41 import javafx.css.CssMetaData;
  42 import javafx.css.StyleableProperty;
  43 import javafx.geometry.Pos;
  44 import javafx.scene.Node;
  45 import javafx.scene.Scene;
  46 import javafx.scene.input.KeyCode;
  47 import javafx.scene.input.MouseEvent;
  48 import javafx.scene.layout.StackPane;
  49 import javafx.scene.shape.Rectangle;
  50 import javafx.stage.Stage;
  51 
  52 import org.junit.Before;
  53 import org.junit.Test;
  54 
  55 import test.com.sun.javafx.pgstub.StubToolkit;
  56 import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
  57 import test.com.sun.javafx.scene.control.infrastructure.MouseEventGenerator;
  58 import javafx.scene.control.skin.TitledPaneSkin;
  59 import com.sun.javafx.tk.Toolkit;
  60 import javafx.scene.control.Button;
  61 import javafx.scene.control.TitledPane;
  62 
  63 /**
  64  *
  65  * @author srikalyc
  66  */
  67 public class TitledPaneTest {
  68     private TitledPane titledPane;//Empty string
  69     private TitledPane titledPaneWithTitleAndNode;//With title And Node
  70     private Node node;
  71     private Toolkit tk;
  72     private Scene scene;
  73     private Stage stage;
  74     private StackPane root;
  75 
  76     @Before public void setup() {
  77         node = new Rectangle();
  78         tk = (StubToolkit)Toolkit.getToolkit();//This step is not needed (Just to make sure StubToolkit is loaded into VM)
  79         titledPane = new TitledPane();
  80         titledPane.setSkin(new TitledPaneSkin(titledPane));
  81         titledPaneWithTitleAndNode = new TitledPane("title", node);
  82         root = new StackPane();
  83         scene = new Scene(root);
  84         stage = new Stage();
  85         stage.setScene(scene);
  86     }
  87 
  88     /*********************************************************************
  89      * Helper methods                                                    *
  90      ********************************************************************/
  91     private void show() {
  92         stage.show();
  93         stage.requestFocus();
  94     }
  95 
  96     /*********************************************************************
  97      * Tests for default values                                         *
  98      ********************************************************************/
  99 
 100     @Test public void defaultConstructorShouldSetStyleClassTo_titledpane() {
 101         assertStyleClassContains(titledPane, "titled-pane");
 102     }
 103 
 104     @Test public void defaultConstructorShouldEmptyTitleAndNullContent() {
 105         assertEquals(titledPane.textProperty().get(), "");
 106         assertNull(titledPane.contentProperty().get());
 107     }
 108 
 109     @Test public void twoArgConstructorShouldSetStyleClassTo_titledpane() {
 110         assertStyleClassContains(titledPane, "titled-pane");
 111     }
 112 
 113     @Test public void twoArgConstructorShouldEmptyTitleAndNullContent() {
 114         assertEquals(titledPaneWithTitleAndNode.textProperty().get(), "title");
 115         assertSame(titledPaneWithTitleAndNode.contentProperty().getValue(), node);
 116     }
 117 
 118     @Test public void defaultExpandedIsTrue() {
 119         assertTrue(titledPane.isExpanded());
 120     }
 121 
 122     @Test public void defaultAnimated() {
 123         assertTrue(titledPane.isAnimated());
 124     }
 125 
 126     @Test public void defaultCollapsible() {
 127         assertTrue(titledPane.isCollapsible());
 128     }
 129 
 130 
 131     /*********************************************************************
 132      * Tests for property binding                                        *
 133      ********************************************************************/
 134 
 135     @Test public void checkContentPropertyBind() {
 136         ObjectProperty objPr = new SimpleObjectProperty<Node>(null);
 137         titledPane.contentProperty().bind(objPr);
 138         assertEquals("ContentProperty cannot be bound", titledPane.contentProperty().getValue(), null);
 139         Node nde = new Rectangle();
 140         objPr.setValue(nde);
 141         assertEquals("ContentProperty cannot be bound", titledPane.contentProperty().getValue(), nde);
 142     }
 143 
 144     @Test public void checkExpandedPropertyBind() {
 145         BooleanProperty objPr = new SimpleBooleanProperty(true);
 146         titledPane.expandedProperty().bind(objPr);
 147         assertEquals("Expanded cannot be bound", titledPane.expandedProperty().getValue(), true);
 148         objPr.setValue(false);
 149         assertEquals("Expanded cannot be bound", titledPane.expandedProperty().getValue(), false);
 150     }
 151 
 152     @Test public void checkAnimatedPropertyBind() {
 153         BooleanProperty objPr = new SimpleBooleanProperty(true);
 154         titledPane.animatedProperty().bind(objPr);
 155         assertEquals("Animated cannot be bound", titledPane.animatedProperty().getValue(), true);
 156         objPr.setValue(false);
 157         assertEquals("Animated cannot be bound", titledPane.animatedProperty().getValue(), false);
 158     }
 159 
 160     @Test public void checkCollapsiblePropertyBind() {
 161         BooleanProperty objPr = new SimpleBooleanProperty(true);
 162         titledPane.collapsibleProperty().bind(objPr);
 163         assertEquals("Collapsible cannot be bound", titledPane.collapsibleProperty().getValue(), true);
 164         objPr.setValue(false);
 165         assertEquals("Collapsible cannot be bound", titledPane.collapsibleProperty().getValue(), false);
 166     }
 167 
 168 
 169     @Test public void contentPropertyHasBeanReference() {
 170         assertSame(titledPane, titledPane.contentProperty().getBean());
 171     }
 172 
 173     @Test public void contenPropertyHasName() {
 174         assertEquals("content", titledPane.contentProperty().getName());
 175     }
 176 
 177     @Test public void animatedPropertyHasBeanReference() {
 178         assertSame(titledPane, titledPane.animatedProperty().getBean());
 179     }
 180 
 181     @Test public void animatedPropertyHasName() {
 182         assertEquals("animated", titledPane.animatedProperty().getName());
 183     }
 184 
 185     @Test public void collapsiblePropertyHasBeanReference() {
 186         assertSame(titledPane, titledPane.collapsibleProperty().getBean());
 187     }
 188 
 189     @Test public void collapsiblePropertyHasName() {
 190         assertEquals("collapsible", titledPane.collapsibleProperty().getName());
 191     }
 192 
 193 
 194     /*********************************************************************
 195      * Check for Pseudo classes                                          *
 196      ********************************************************************/
 197     @Test public void settingExpandedTrueSetsPseudoClass() {
 198         titledPane.setExpanded(true);
 199         assertPseudoClassExists(titledPane, "expanded");
 200         assertPseudoClassDoesNotExist(titledPane, "collapsed");
 201     }
 202 
 203     @Test public void clearingExpandedClearsPseudoClass() {
 204         titledPane.setExpanded(true);
 205         titledPane.setExpanded(false);
 206         assertPseudoClassDoesNotExist(titledPane, "expanded");
 207         assertPseudoClassExists(titledPane, "collapsed");
 208     }
 209 
 210 
 211     /*********************************************************************
 212      * CSS related Tests                                                 *
 213      ********************************************************************/
 214     @Test public void whenAnimatedIsBound_impl_cssSettable_ReturnsFalse() {
 215         CssMetaData styleable = ((StyleableProperty)titledPane.animatedProperty()).getCssMetaData();
 216         assertTrue(styleable.isSettable(titledPane));
 217         BooleanProperty other = new SimpleBooleanProperty();
 218         titledPane.animatedProperty().bind(other);
 219         assertFalse(styleable.isSettable(titledPane));
 220     }
 221 
 222     @Test public void whenAnimatedIsSpecifiedViaCSSAndIsNotBound_impl_cssSettable_ReturnsTrue() {
 223         CssMetaData styleable = ((StyleableProperty)titledPane.animatedProperty()).getCssMetaData();
 224         assertTrue(styleable.isSettable(titledPane));
 225     }
 226 
 227     @Test public void whenCollapsibleIsBound_impl_cssSettable_ReturnsFalse() {
 228         CssMetaData styleable = ((StyleableProperty)titledPane.collapsibleProperty()).getCssMetaData();
 229         assertTrue(styleable.isSettable(titledPane));
 230         BooleanProperty other = new SimpleBooleanProperty();
 231         titledPane.collapsibleProperty().bind(other);
 232         assertFalse(styleable.isSettable(titledPane));
 233     }
 234 
 235     @Test public void whenCollapsibleIsSpecifiedViaCSSAndIsNotBound_impl_cssSettable_ReturnsTrue() {
 236         CssMetaData styleable = ((StyleableProperty)titledPane.collapsibleProperty()).getCssMetaData();
 237         ((StyleableProperty)titledPane.collapsibleProperty()).applyStyle(null, Boolean.FALSE);
 238         assertTrue(styleable.isSettable(titledPane));
 239     }
 240 
 241 
 242 
 243     /*********************************************************************
 244      * Miscellaneous Tests                                               *
 245      ********************************************************************/
 246     @Test public void setContentAndSeeValueIsReflectedInModel() {
 247         Node nde = new Rectangle();
 248         titledPane.setContent(nde);
 249         assertSame(titledPane.contentProperty().getValue(), nde);
 250     }
 251 
 252     @Test public void setContentAndSeeValue() {
 253         Node nde = new Rectangle();
 254         titledPane.setContent(nde);
 255         assertSame(titledPane.getContent(), nde);
 256     }
 257 
 258     @Test public void setExpandedAndSeeValueIsReflectedInModel() {
 259         titledPane.setExpanded(true);
 260         assertTrue(titledPane.expandedProperty().getValue());
 261     }
 262 
 263     @Test public void setExpandedAndSeeValue() {
 264         titledPane.setExpanded(false);
 265         assertFalse(titledPane.isExpanded());
 266     }
 267 
 268     @Test public void setAnimatedAndSeeValueIsReflectedInModel() {
 269         titledPane.setAnimated(true);
 270         assertTrue(titledPane.animatedProperty().getValue());
 271     }
 272 
 273     @Test public void setAnimatedAndSeeValue() {
 274         titledPane.setAnimated(false);
 275         assertFalse(titledPane.isAnimated());
 276     }
 277 
 278     @Test public void setCollapsibleAndSeeValueIsReflectedInModel() {
 279         titledPane.setCollapsible(true);
 280         assertTrue(titledPane.collapsibleProperty().getValue());
 281     }
 282 
 283     @Test public void setCollapsibleAndSeeValue() {
 284         titledPane.setCollapsible(false);
 285         assertFalse(titledPane.isCollapsible());
 286     }
 287 
 288     @Test public void setAlignment_RT20069() {
 289         titledPane.setExpanded(false);
 290         titledPane.setAnimated(false);
 291         titledPane.setStyle("-fx-alignment: BOTTOM_RIGHT;");
 292 
 293         root.getChildren().add(titledPane);
 294         show();
 295 
 296         assertEquals(Pos.BOTTOM_RIGHT, titledPane.getAlignment());
 297     }
 298 
 299     @Test public void keyboardFocusOnNonCollapsibleTitledPane_RT19660() {
 300         Button button = new Button("Button");
 301 
 302         titledPane.setCollapsible(false);
 303         titledPane.setExpanded(true);
 304         titledPane.setAnimated(false);
 305         titledPane.setContent(button);
 306 
 307         root.getChildren().add(titledPane);
 308         show();
 309 
 310         tk.firePulse();
 311         assertTrue(titledPane.isFocused());
 312 
 313         KeyEventFirer keyboard = new KeyEventFirer(titledPane);
 314         keyboard.doKeyPress(KeyCode.ENTER);
 315 
 316         tk.firePulse();
 317         assertTrue(titledPane.isExpanded());
 318         assertTrue(titledPane.isFocused());
 319 
 320         keyboard.doKeyPress(KeyCode.TAB);
 321         tk.firePulse();
 322         assertFalse(titledPane.isFocused());
 323         assertTrue(button.isFocused());
 324     }
 325 
 326     @Test public void mouseFocusOnNonCollapsibleTitledPane_RT19660() {
 327         Button button = new Button("Button");
 328 
 329         titledPane.setCollapsible(false);
 330         titledPane.setExpanded(true);
 331         titledPane.setAnimated(false);
 332         titledPane.setContent(button);
 333 
 334         root.getChildren().add(titledPane);
 335         show();
 336 
 337         tk.firePulse();
 338         assertTrue(titledPane.isFocused());
 339 
 340         double xval = (titledPane.localToScene(titledPane.getLayoutBounds())).getMinX();
 341         double yval = (titledPane.localToScene(titledPane.getLayoutBounds())).getMinY();
 342 
 343         final MouseEventGenerator generator = new MouseEventGenerator();
 344         SceneHelper.processMouseEvent(scene,
 345             generator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval+20, yval+20));
 346         SceneHelper.processMouseEvent(scene,
 347             generator.generateMouseEvent(MouseEvent.MOUSE_RELEASED, xval+20, yval+20));
 348 
 349         tk.firePulse();
 350         assertTrue(titledPane.isExpanded());
 351         assertTrue(titledPane.isFocused());
 352 
 353         KeyEventFirer keyboard = new KeyEventFirer(titledPane);
 354         keyboard.doKeyPress(KeyCode.TAB);
 355         tk.firePulse();
 356         assertFalse(titledPane.isFocused());
 357         assertTrue(button.isFocused());
 358     }
 359 }