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