1 /*
   2  * Copyright (c) 2011, 2014, 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 com.sun.javafx.application.PlatformImpl;
  29 import com.sun.javafx.pgstub.StubToolkit;
  30 import com.sun.javafx.scene.control.infrastructure.ControlTestUtils;
  31 import com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
  32 import com.sun.javafx.tk.Toolkit;
  33 import javafx.scene.Scene;
  34 import javafx.scene.layout.StackPane;
  35 import javafx.stage.Stage;
  36 import javafx.beans.property.SimpleObjectProperty;
  37 import javafx.beans.value.ObservableValue;
  38 import javafx.scene.input.KeyCode;
  39 
  40 import org.junit.Before;
  41 import org.junit.Test;
  42 
  43 import static org.junit.Assert.*;
  44 
  45 /**
  46  */
  47 public class AccordionTest {    
  48     private Accordion accordion;
  49     private Toolkit tk;
  50     private Scene scene;
  51     private Stage stage;
  52     private StackPane root;    
  53     
  54     @Before public void setup() {
  55         tk = (StubToolkit)Toolkit.getToolkit();//This step is not needed (Just to make sure StubToolkit is loaded into VM)
  56         accordion = new Accordion();
  57         root = new StackPane();
  58         scene = new Scene(root);
  59         stage = new Stage();
  60         stage.setScene(scene);
  61     }
  62 
  63     /*********************************************************************
  64      * Helper methods                                                    *
  65      ********************************************************************/
  66     private void show() {
  67         stage.show();
  68     }
  69 
  70     /*********************************************************************
  71      * Tests for the constructors                                        *
  72      ********************************************************************/
  73 
  74     @Test public void defaultConstructorShouldDefaultToStyleClass_accordion() {
  75         ControlTestUtils.assertStyleClassContains(accordion, "accordion");
  76     }
  77 
  78     /*********************************************************************
  79      * Tests for the properties                                          *
  80      ********************************************************************/
  81 
  82     @Test public void expandedShouldBeNullByDefaultWithNoPanes() {
  83         assertNull(accordion.getExpandedPane());
  84     }
  85 
  86     @Test public void expandedShouldBeNullByDefaultEvenWithPanes() {
  87         accordion.getPanes().add(new TitledPane());
  88         assertNull(accordion.getExpandedPane());
  89     }
  90 
  91     @Test public void settingTheExpandedPaneToAPaneInPanesShouldWork() {
  92         TitledPane a = new TitledPane();
  93         TitledPane b = new TitledPane();
  94         TitledPane c = new TitledPane();
  95         accordion.getPanes().addAll(a, b, c);
  96         accordion.setExpandedPane(b);
  97         assertSame(b, accordion.getExpandedPane());
  98         accordion.setExpandedPane(a);
  99         assertSame(a, accordion.getExpandedPane());
 100         accordion.setExpandedPane(c);
 101         assertSame(c, accordion.getExpandedPane());
 102     }
 103 
 104     @Test public void settingTheExpandedPaneToNullWhenItWasNotNullShouldWork() {
 105         TitledPane a = new TitledPane();
 106         TitledPane b = new TitledPane();
 107         TitledPane c = new TitledPane();
 108         accordion.getPanes().addAll(a, b, c);
 109         accordion.setExpandedPane(b);
 110         accordion.setExpandedPane(null);
 111         assertNull(accordion.getExpandedPane());
 112     }
 113 
 114     @Test public void settingTheExpandedPaneToAPaneNotInPanesShouldStillChange() {
 115         TitledPane a = new TitledPane();
 116         TitledPane b = new TitledPane();
 117         TitledPane c = new TitledPane();
 118         TitledPane d = new TitledPane();
 119         accordion.getPanes().addAll(a, b, c);
 120         accordion.setExpandedPane(b);
 121         assertSame(b, accordion.getExpandedPane());
 122         accordion.setExpandedPane(d);
 123         assertSame(d, accordion.getExpandedPane());
 124     }
 125 
 126     @Test public void removingAPaneThatWasExpandedPaneShouldResultInNull() {
 127         TitledPane a = new TitledPane();
 128         TitledPane b = new TitledPane();
 129         TitledPane c = new TitledPane();
 130         accordion.getPanes().addAll(a, b, c);
 131         accordion.setExpandedPane(b);
 132         accordion.getPanes().removeAll(b, c);
 133         assertNull(accordion.getExpandedPane());
 134     }
 135 
 136     @Test public void removingAPaneThatWasNotExpandedShouldHaveNoChangeTo_expandedPane() {
 137         TitledPane a = new TitledPane();
 138         TitledPane b = new TitledPane();
 139         TitledPane c = new TitledPane();
 140         accordion.getPanes().addAll(a, b, c);
 141         accordion.setExpandedPane(b);
 142         accordion.getPanes().removeAll(a, c);
 143         assertSame(b, accordion.getExpandedPane());
 144     }
 145 
 146     @Test public void removingAPaneThatWasExpandedPaneButIsBoundResultsInNoChange() {
 147         TitledPane a = new TitledPane();
 148         TitledPane b = new TitledPane();
 149         TitledPane c = new TitledPane();
 150         ObservableValue<TitledPane> value = new SimpleObjectProperty<TitledPane>(b);
 151         accordion.getPanes().addAll(a, b, c);
 152         accordion.expandedPaneProperty().bind(value);
 153         accordion.getPanes().removeAll(b, c);
 154         assertSame(b, accordion.getExpandedPane());
 155     }
 156 
 157     @Test public void checkComputedHeight_RT19025() {
 158         TitledPane a = new TitledPane("A", new javafx.scene.shape.Rectangle(50, 100));
 159         TitledPane b = new TitledPane("B", new javafx.scene.shape.Rectangle(50, 100));
 160         TitledPane c = new TitledPane("C", new javafx.scene.shape.Rectangle(50, 100));
 161 
 162         a.setAnimated(false);
 163         b.setAnimated(false);
 164         c.setAnimated(false);
 165         
 166         accordion.getPanes().addAll(a, b, c);
 167         root.setPrefSize(100, 300);
 168         root.getChildren().add(accordion);
 169         show();
 170         root.applyCss();
 171         root.autosize();
 172         root.layout();
 173         
 174         final double expectedPrefWidth = PlatformImpl.isCaspian() ? 54 : 
 175                                          PlatformImpl.isModena()  ? 52 :
 176                                          0;
 177         
 178         assertEquals(expectedPrefWidth, accordion.prefWidth(-1), 1e-100);
 179         assertEquals(60, accordion.prefHeight(-1), 1e-100);
 180 
 181         accordion.setExpandedPane(b);
 182         root.applyCss();
 183         root.autosize();
 184         root.layout();
 185 
 186         assertEquals(expectedPrefWidth, accordion.prefWidth(-1), 1e-100);
 187         
 188         final double expectedPrefHeight = PlatformImpl.isCaspian() ? 170 : 
 189                                           PlatformImpl.isModena()  ? 161 :
 190                                           0;
 191         assertEquals(expectedPrefHeight, accordion.prefHeight(-1), 1e-100);
 192     }
 193     
 194     @Test public void aiobeWhenFocusIsOnAControlInsideTheAccordion_RT22027() {
 195         Button b1 = new Button("A");
 196         Button b2 = new Button("B");
 197         
 198         TitledPane a = new TitledPane("A", b1);
 199         TitledPane b = new TitledPane("B", b2);
 200         
 201         accordion.getPanes().addAll(a, b);
 202         accordion.setExpandedPane(a);
 203         accordion.setLayoutX(200);
 204         
 205         root.setPrefSize(800, 800);
 206         root.getChildren().add(accordion);
 207         b1.requestFocus();
 208         show();
 209         
 210         root.applyCss();
 211         root.autosize();
 212         root.layout();
 213                 
 214         KeyEventFirer keyboard = new KeyEventFirer(b1);                
 215 
 216         try {        
 217             keyboard.doKeyPress(KeyCode.HOME);
 218             tk.firePulse(); 
 219         } catch (Exception e) {
 220             fail();
 221         }
 222     }
 223 }