1 /*
   2  * Copyright (c) 2010, 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 java.util.logging.Level;
  29 import java.util.logging.Logger;
  30 
  31 import static com.sun.javafx.scene.control.infrastructure.ControlTestUtils.*;
  32 
  33 import test.com.sun.javafx.pgstub.StubToolkit;
  34 import com.sun.javafx.tk.Toolkit;
  35 import javafx.event.ActionEvent;
  36 import javafx.event.Event;
  37 import javafx.event.EventHandler;
  38 import javafx.event.EventType;
  39 import javafx.geometry.Pos;
  40 import javafx.scene.Node;
  41 import javafx.scene.shape.Rectangle;
  42 import static org.junit.Assert.*;
  43 
  44 
  45 import org.junit.Before;
  46 import org.junit.Test;
  47 
  48 /**
  49  *
  50  * @author srikalyc
  51  */
  52 public class ToggleButtonTest {
  53     private ToggleGroup toggleGroup;
  54     private ToggleButton toggle;//Empty string
  55     private ToggleButton toggleWithText;//WithText
  56     private ToggleButton toggleWithGraphic;//With Graphic
  57     private Node node;
  58     private Toolkit tk;
  59 
  60     @Before public void setup() {
  61         tk = (StubToolkit)Toolkit.getToolkit();//This step is not needed (Just to make sure StubToolkit is loaded into VM)
  62         node = new Rectangle();
  63         toggleGroup = new ToggleGroup();
  64         toggle = new ToggleButton();
  65         toggleWithText = new ToggleButton("text");
  66         toggleWithGraphic = new ToggleButton("graphic", node);
  67     }
  68     
  69    
  70    
  71     /*********************************************************************
  72      * Tests for default values                                         *
  73      ********************************************************************/
  74     
  75     @Test public void defaultConstructorShouldSetStyleClassTo_togglebutton() {
  76         assertStyleClassContains(toggle, "toggle-button");
  77     }
  78     
  79     @Test public void defaultOneArgConstructorShouldSetStyleClassTo_togglebutton() {
  80         assertStyleClassContains(toggleWithText, "toggle-button");
  81     }
  82     
  83     @Test public void defaultTwoArgConstructorShouldSetStyleClassTo_togglebutton() {
  84         assertStyleClassContains(toggleWithGraphic, "toggle-button");
  85     }
  86     
  87     @Test public void defaultConstructorTextGraphicCheck() {
  88         assertEquals(toggle.getText(), "");
  89         assertNull(toggle.getGraphic());
  90     }
  91     
  92     @Test public void defaultOneArgConstructorTextGraphicCheck() {
  93         assertEquals(toggleWithText.getText(), "text");
  94         assertNull(toggleWithText.getGraphic());
  95     }
  96     
  97     @Test public void defaultTwoArgConstructorTextGraphicCheck() {
  98         assertEquals(toggleWithGraphic.getText(), "graphic");
  99         assertSame(toggleWithGraphic.getGraphic(), node);
 100     }
 101     
 102     @Test public void defaultSelected() {
 103         assertFalse(toggle.isSelected());
 104     }
 105 
 106     @Test public void defaultAlignment() {
 107         assertSame(toggle.getAlignment(), Pos.CENTER);
 108     }
 109     
 110     @Test public void defaultMnemonicParsing() {
 111         assertTrue(toggle.isMnemonicParsing());
 112     }
 113     
 114     /*********************************************************************
 115      * Tests for property binding                                        *
 116      ********************************************************************/
 117 
 118     @Test public void selectedPropertyHasBeanReference() {
 119         assertSame(toggle, toggle.selectedProperty().getBean());
 120     }
 121 
 122     @Test public void selectedPropertyHasName() {
 123         assertEquals("selected", toggle.selectedProperty().getName());
 124     }
 125 
 126     @Test public void toggleGroupPropertyHasBeanReference() {
 127         assertSame(toggle, toggle.toggleGroupProperty().getBean());
 128     }
 129 
 130     @Test public void toggleGroupPropertyHasName() {
 131         assertEquals("toggleGroup", toggle.toggleGroupProperty().getName());
 132     }
 133 
 134     /*********************************************************************
 135      * Check for Pseudo classes                                          *
 136      ********************************************************************/
 137     @Test public void settingSelectedSetsPseudoClass() {
 138         toggle.setSelected(true);
 139         assertPseudoClassExists(toggle, "selected");
 140     }
 141 
 142     @Test public void clearingSelectedClearsPseudoClass() {
 143         toggle.setSelected(true);
 144         toggle.setSelected(false);
 145         assertPseudoClassDoesNotExist(toggle, "selected");
 146     }
 147 
 148 
 149     /*********************************************************************
 150      * Miscellaneous Tests                                         *
 151      ********************************************************************/
 152     @Test public void setSelectedAndSeeValueIsReflectedInModel() {
 153         toggle.setSelected(true);
 154         assertTrue(toggle.selectedProperty().getValue());
 155     }
 156     
 157     @Test public void setSelectedAndSeeValue() {
 158         toggle.setSelected(false);
 159         assertFalse(toggle.isSelected());
 160     }
 161     
 162     @Test public void setToggleGroupAndSeeValueIsReflectedInModel() {
 163         toggle.setToggleGroup(toggleGroup);
 164         assertSame(toggle.toggleGroupProperty().getValue(), toggleGroup);
 165     }
 166     
 167     @Test public void setToggleGroupAndSeeValue() {
 168         toggle.setToggleGroup(toggleGroup);
 169         assertSame(toggle.getToggleGroup(), toggleGroup);
 170     }
 171     
 172     @Test public void fireAndCheckSelectionToggled() {
 173         toggle.fire();
 174         assertTrue(toggle.isSelected());
 175         toggle.fire();
 176         assertFalse(toggle.isSelected());
 177     }
 178     
 179     @Test public void fireAndCheckActionEventFired() {
 180         final Boolean []flag = new Boolean[1];
 181         flag[0] = false;
 182         toggle.addEventHandler(EventType.ROOT, event -> {
 183             if (event != null && event instanceof ActionEvent) {
 184                 flag[0] = true;
 185             }
 186         });
 187         toggle.fire();
 188         try {
 189             Thread.currentThread().sleep(2000);
 190         } catch (InterruptedException ex) {
 191             Logger.getLogger(ToggleButtonTest.class.getName()).log(Level.SEVERE, null, ex);
 192         }
 193         assertTrue("fire() doesnt emit ActionEvent!", flag[0]);
 194     }
 195     
 196 }