1 /*
   2  * Copyright (c) 2011, 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.skin;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertTrue;
  30 
  31 import javafx.beans.value.ObservableValue;
  32 import javafx.geometry.Insets;
  33 import javafx.scene.control.Button;
  34 
  35 import javafx.scene.Group;
  36 import javafx.scene.Scene;
  37 import javafx.scene.control.skin.ButtonSkin;
  38 import javafx.stage.Stage;
  39 import javafx.scene.input.Mnemonic;
  40 import javafx.collections.ObservableList;
  41 import javafx.scene.input.KeyCombination;
  42 
  43 import com.sun.javafx.scene.control.behavior.TextBinding.MnemonicKeyCombination;
  44 
  45 import org.junit.Before;
  46 import org.junit.Test;
  47 
  48 /**
  49  */
  50 public class ButtonSkinTest {
  51     private Button button;
  52     private ButtonSkinMock skin;
  53 
  54     @Before public void setup() {
  55         button = new Button("Test");
  56         skin = new ButtonSkinMock(button);
  57         // Set some padding so that any places where padding was being
  58         // computed but wasn't expected will be caught.
  59         button.setPadding(new Insets(10, 10, 10, 10));
  60         button.setSkin(skin);
  61 
  62     }
  63 
  64     @Test public void maxWidthTracksPreferred() {
  65         button.setPrefWidth(500);
  66         assertEquals(500, button.maxWidth(-1), 0);
  67     }
  68 
  69     @Test public void maxHeightTracksPreferred() {
  70         button.setPrefHeight(500);
  71         assertEquals(500, button.maxHeight(-1), 0);
  72     }
  73 
  74     @Test
  75     public void testMnemonicAutoParseAddition() {
  76         if(!com.sun.javafx.PlatformUtil.isMac()) {
  77             boolean nodeFound = false;
  78             Stage stage = new Stage();
  79             Scene scene = new Scene(new Group(), 500, 500);
  80             stage.setScene(scene);
  81             
  82             button.setMnemonicParsing(true);
  83             button.setText("_Mnemonic");
  84             
  85             ((Group)scene.getRoot()).getChildren().add(button);
  86         
  87             stage.show();
  88         
  89             KeyCombination mnemonicKeyCombo = new MnemonicKeyCombination("M");
  90 
  91             ObservableList<Mnemonic> mnemonicsList = scene.getMnemonics().get(mnemonicKeyCombo);
  92             if (mnemonicsList != null) {
  93                 for (int i = 0 ; i < mnemonicsList.size() ; i++) {
  94                     if (mnemonicsList.get(i).getNode() == button) {
  95                         nodeFound = true;
  96                     }
  97                 }
  98             }
  99             assertTrue(nodeFound);
 100         }
 101     }
 102 
 103    
 104     @Test
 105     public void testMnemonicAutoParseAdditionRemovalOnParentChange() {
 106         if(!com.sun.javafx.PlatformUtil.isMac()) {
 107             boolean nodeFound = false;
 108             Stage stage = new Stage();
 109             Scene scene = new Scene(new Group(), 500, 500);
 110             stage.setScene(scene);
 111             
 112             button.setMnemonicParsing(true);
 113             button.setText("_AnotherMnemonic");
 114             
 115             ((Group)scene.getRoot()).getChildren().add(button);
 116         
 117             stage.show();
 118         
 119             KeyCombination mnemonicKeyCombo = new MnemonicKeyCombination("A");
 120 
 121             ObservableList<Mnemonic> mnemonicsList = scene.getMnemonics().get(mnemonicKeyCombo);
 122             if (mnemonicsList != null) {
 123                 for (int i = 0 ; i < mnemonicsList.size() ; i++) {
 124                     if (mnemonicsList.get(i).getNode() == button) {
 125                         nodeFound = true;
 126                     }
 127                 }
 128             }
 129             assertTrue(nodeFound);
 130 
 131             nodeFound = false;
 132 
 133             ((Group)scene.getRoot()).getChildren().remove(button);
 134   
 135             mnemonicsList = scene.getMnemonics().get(mnemonicKeyCombo);
 136             if (mnemonicsList != null) {
 137                 for (int i = 0 ; i < mnemonicsList.size() ; i++) {
 138                     if (mnemonicsList.get(i).getNode() == button) {
 139                         nodeFound = true;
 140                     }
 141                 }
 142             }
 143             assertTrue(!nodeFound);
 144         }
 145     }
 146 
 147     public static final class ButtonSkinMock extends ButtonSkin {
 148         boolean propertyChanged = false;
 149         int propertyChangeCount = 0;
 150         public ButtonSkinMock(Button button) {
 151             super(button);
 152         }
 153 
 154         public void addWatchedProperty(ObservableValue<?> p) {
 155             p.addListener(o -> {
 156                 propertyChanged = true;
 157                 propertyChangeCount++;
 158             });
 159         }
 160     }
 161 }