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 com.sun.javafx.scene.control.skin.MenuButtonSkin;
  29 import com.sun.javafx.tk.Toolkit;
  30 import javafx.beans.property.SimpleObjectProperty;
  31 import javafx.event.ActionEvent;
  32 import javafx.event.EventHandler;
  33 import javafx.geometry.Side;
  34 import javafx.scene.Scene;
  35 import javafx.scene.layout.VBox;
  36 import javafx.scene.shape.Rectangle;
  37 import javafx.stage.Stage;
  38 
  39 import org.junit.Before;
  40 import org.junit.Test;
  41 
  42 import static org.junit.Assert.*;
  43 
  44 /**
  45  *
  46  * @author lubermud
  47  */
  48 public class MenuButtonTest {
  49     private MenuButton menuButton;
  50 
  51     @Before public void setup() {
  52         menuButton = new MenuButton();
  53     }
  54 
  55     @Test public void defaultConstructorShouldHaveNoGraphic() {
  56         assertNull(menuButton.getGraphic());
  57     }
  58 
  59     @Test public void defaultConstructorShouldHaveNullString() {
  60         assertEquals("", menuButton.getText());
  61     }
  62 
  63     @Test public void oneArgConstructorShouldHaveNoGraphic1() {
  64         MenuButton mb2 = new MenuButton(null);
  65         assertNull(mb2.getGraphic());
  66     }
  67 
  68     @Test public void oneArgConstructorShouldHaveNoGraphic2() {
  69         MenuButton mb2 = new MenuButton("");
  70         assertNull(mb2.getGraphic());
  71     }
  72 
  73     @Test public void oneArgConstructorShouldHaveNoGraphic3() {
  74         MenuButton mb2 = new MenuButton("Hello");
  75         assertNull(mb2.getGraphic());
  76     }
  77 
  78     @Test public void oneArgConstructorShouldHaveSpecifiedString1() {
  79         MenuButton mb2 = new MenuButton(null);
  80         assertEquals("", mb2.getText());
  81     }
  82 
  83     @Test public void oneArgConstructorShouldHaveSpecifiedString2() {
  84         MenuButton mb2 = new MenuButton("");
  85         assertEquals("", mb2.getText());
  86     }
  87 
  88     @Test public void oneArgConstructorShouldHaveSpecifiedString3() {
  89         MenuButton mb2 = new MenuButton("Hello");
  90         assertEquals("Hello", mb2.getText());
  91     }
  92 
  93     @Test public void twoArgConstructorShouldHaveSpecifiedGraphic1() {
  94         MenuButton mb2 = new MenuButton(null, null);
  95         assertNull(mb2.getGraphic());
  96     }
  97 
  98     @Test public void twoArgConstructorShouldHaveSpecifiedGraphic2() {
  99         Rectangle rect = new Rectangle();
 100         MenuButton mb2 = new MenuButton("Hello", rect);
 101         assertSame(rect, mb2.getGraphic());
 102     }
 103 
 104     @Test public void twoArgConstructorShouldHaveSpecifiedString1() {
 105         MenuButton mb2 = new MenuButton(null, null);
 106         assertEquals("", mb2.getText());
 107     }
 108 
 109     @Test public void twoArgConstructorShouldHaveSpecifiedString2() {
 110         Rectangle rect = new Rectangle();
 111         MenuButton mb2 = new MenuButton("Hello", rect);
 112         assertEquals("Hello", mb2.getText());
 113     }
 114 
 115     @Test public void getItemsDefaultNotNull() {
 116         assertNotNull(menuButton.getItems());
 117     }
 118 
 119     @Test public void getItemsDefaultSizeZero() {
 120         assertEquals(0, menuButton.getItems().size());
 121     }
 122 
 123     @Test public void getItemsAddable() {
 124         menuButton.getItems().add(new MenuItem());
 125         assertTrue(menuButton.getItems().size() > 0);
 126     }
 127 
 128     @Test public void getItemsClearable() {
 129         menuButton.getItems().add(new MenuItem());
 130         menuButton.getItems().clear();
 131         assertEquals(0, menuButton.getItems().size());
 132     }
 133 
 134     @Test public void defaultIsShowingFalse() {
 135         assertFalse(menuButton.isShowing());
 136     }
 137 
 138     @Test public void showIsShowingTrue() {
 139         menuButton.show();
 140         assertTrue(menuButton.isShowing());
 141     }
 142 
 143     @Test public void hideIsShowingFalse1() {
 144         menuButton.hide();
 145         assertFalse(menuButton.isShowing());
 146     }
 147 
 148     @Test public void hideIsShowingFalse2() {
 149         menuButton.show();
 150         menuButton.hide();
 151         assertFalse(menuButton.isShowing());
 152     }
 153 
 154     @Test public void getUnspecifiedShowingProperty1() {
 155         assertNotNull(menuButton.showingProperty());
 156     }
 157 
 158     @Test public void getUnspecifiedShowingProperty2() {
 159         MenuButton mb2 = new MenuButton("", null);
 160         assertNotNull(mb2.showingProperty());
 161     }
 162 
 163     @Test public void unsetShowingButNotNull() {
 164         menuButton.showingProperty();
 165         assertNotNull(menuButton.isShowing());
 166     }
 167 
 168     @Test public void menuButtonIsFiredIsNoOp() {
 169         menuButton.fire(); // should throw no exceptions, if it does, the test fails
 170     }
 171 
 172     @Test public void defaultPopupSide() {
 173         assertEquals(Side.BOTTOM, menuButton.getPopupSide());
 174         assertEquals(Side.BOTTOM, menuButton.popupSideProperty().get());
 175     }
 176 
 177     @Test public void setNullPopupSide() {
 178         menuButton.setPopupSide(null);
 179         assertNull(menuButton.getPopupSide());
 180     }
 181 
 182     @Test public void setSpecifiedPopupSide() {
 183         Side side = Side.TOP;
 184         menuButton.setPopupSide(side);
 185         assertSame(side, menuButton.getPopupSide());
 186     }
 187 
 188     @Test public void getUnspecifiedPopupSideProperty1() {
 189         assertNotNull(menuButton.popupSideProperty());
 190     }
 191 
 192     @Test public void getUnspecifiedPopupSideProperty2() {
 193         MenuButton mb2 = new MenuButton("", null);
 194         assertNotNull(mb2.popupSideProperty());
 195     }
 196 
 197     @Test public void unsetPopupSideButNotNull() {
 198         menuButton.popupSideProperty();
 199         assertNotNull(menuButton.getPopupSide());
 200     }
 201 
 202     @Test public void popupSideCanBeBound() {
 203         Side side = Side.TOP;
 204         SimpleObjectProperty<Side> other = new SimpleObjectProperty<Side>(menuButton, "popupSide", side);
 205         menuButton.popupSideProperty().bind(other);
 206         assertSame(side, menuButton.getPopupSide());
 207     }
 208 
 209     //TODO: test show()/isShowing() for disabled=true
 210     //TODO: test MenuButton.impl_getPsuedoClassState
 211     
 212     @Test
 213     public void test_RT_21894() {
 214         
 215         // Bug reproduces by setting opacity on the MenuButton
 216         // then moving focus on and off the MenuButton
 217         final MenuButton mb = new MenuButton();
 218         mb.setText("SomeText"); 
 219         
 220         MenuButtonSkin mbs = new MenuButtonSkin(mb);
 221         mb.setSkin(mbs);
 222         
 223         Button other = new Button("other");
 224         // Doesn't have to be done this way, but this more closely duplicates
 225         // the example code in the bug report.
 226         other.setOnAction(t -> {
 227             mb.setOpacity(.5);
 228         });
 229         
 230         VBox vbox = new VBox();
 231         vbox.getChildren().addAll(mb, other);
 232         Scene scene = new Scene(vbox, 300, 300); 
 233         Stage stage = new Stage();
 234         stage.setScene(scene); 
 235         stage.show();
 236         stage.requestFocus();
 237         
 238         other.requestFocus();
 239         assertFalse(mb.isFocused());
 240         
 241         // set opacity on MenuButton
 242         other.fire();
 243         
 244         // focus on MenuButton
 245         mb.requestFocus();
 246         assertTrue(mb.isFocused());
 247         
 248         // give css a chance to run
 249         Toolkit.getToolkit().firePulse();
 250         
 251         // focus off the MenuButton
 252         other.requestFocus();
 253         assertFalse(mb.isFocused());
 254         
 255         // give css a chance to run
 256         Toolkit.getToolkit().firePulse();
 257 
 258         // MenuButton should still be 50%
 259         assertEquals(.5, mb.getOpacity(), 0.00001);
 260         
 261     }    
 262 }