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