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