1 /*
   2  * Copyright (c) 2012, 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 test.com.sun.javafx.scene;
  27 
  28 import javafx.collections.ObservableList;
  29 import javafx.scene.Group;
  30 import javafx.scene.Parent;
  31 import javafx.scene.Scene;
  32 import javafx.scene.input.KeyCode;
  33 import javafx.scene.input.KeyCodeCombination;
  34 import javafx.scene.input.KeyCombination;
  35 import javafx.scene.input.Mnemonic;
  36 import javafx.scene.text.Text;
  37 import javafx.stage.Stage;
  38 
  39 import org.junit.After;
  40 import org.junit.Before;
  41 import org.junit.Test;
  42 import static org.junit.Assert.*;
  43 
  44 
  45 /*
  46 ** Test keyboard shortcuts.
  47 */
  48 public final class KeyboardShortcutsTest {
  49 
  50     private Stage stage;
  51     private Scene scene;
  52 
  53     @Before
  54     public void setUp() {
  55         stage = new Stage();
  56         scene = new Scene(new Group(), 500, 500);
  57         stage.setScene(scene);
  58 
  59         stage.show();
  60     }
  61 
  62     @After
  63     public void tearDown() {
  64         stage = null;
  65         scene = null;
  66     }
  67 
  68 
  69     /*
  70     ** Add a mnemonic
  71     ** Make sure that it's there.
  72     */
  73     @Test
  74     public void addMnemonicTest() {
  75         boolean nodeFound = false;
  76         final Text node = new Text("text");
  77 
  78         ((Group)scene.getRoot()).getChildren().add(node);
  79 
  80         KeyCodeCombination mnemonicKeyCombo =
  81             new KeyCodeCombination(KeyCode.Q,KeyCombination.ALT_DOWN);
  82 
  83         Mnemonic myMnemonic = new Mnemonic(node, mnemonicKeyCombo);
  84         scene.addMnemonic(myMnemonic);
  85 
  86         ObservableList<Mnemonic> mnemonicsList = scene.getMnemonics().get(mnemonicKeyCombo);
  87         if (mnemonicsList != null) {
  88             for (int i = 0 ; i < mnemonicsList.size() ; i++) {
  89                 if (mnemonicsList.get(i).getNode() == node) {
  90                     nodeFound = true;
  91                 }
  92             }
  93         }
  94         assertTrue(nodeFound);
  95     }
  96 
  97 
  98     /*
  99     ** Add a mnemonic, then remove it.
 100     ** Make sure that it's gone.
 101     */
 102     @Test
 103     public void addAndRemoveMnemonicTest() {
 104         boolean nodeFound = false;
 105         final Text node = new Text("text");
 106 
 107         ((Group)scene.getRoot()).getChildren().add(node);
 108 
 109         KeyCodeCombination mnemonicKeyCombo =
 110             new KeyCodeCombination(KeyCode.Q,KeyCombination.ALT_DOWN);
 111 
 112         Mnemonic myMnemonic = new Mnemonic(node, mnemonicKeyCombo);
 113         scene.addMnemonic(myMnemonic);
 114 
 115         /*
 116         ** remove it.....
 117         */
 118         scene.removeMnemonic(myMnemonic);
 119 
 120         ObservableList<Mnemonic> mnemonicsList = scene.getMnemonics().get(mnemonicKeyCombo);
 121         if (mnemonicsList != null) {
 122             for (int i = 0 ; i < mnemonicsList.size() ; i++) {
 123                 if (mnemonicsList.get(i).getNode() == node) {
 124                     nodeFound = true;
 125                 }
 126             }
 127         }
 128         assertTrue(!nodeFound);
 129     }
 130 
 131     @Test
 132     public void mnemonicRemovedWithNodeTest() {
 133         final Text node = new Text("text");
 134         ((Group)scene.getRoot()).getChildren().add(node);
 135 
 136         KeyCodeCombination mnemonicKeyCombo =
 137                 new KeyCodeCombination(KeyCode.Q,KeyCombination.ALT_DOWN);
 138 
 139         Mnemonic myMnemonic = new Mnemonic(node, mnemonicKeyCombo);
 140         scene.addMnemonic(myMnemonic);
 141 
 142         ObservableList<Mnemonic> mnemonicsList = scene.getMnemonics().get(mnemonicKeyCombo);
 143 
 144         assertTrue(mnemonicsList.contains(myMnemonic));
 145 
 146         scene.setRoot(new Group());
 147 
 148         assertFalse(mnemonicsList.contains(myMnemonic));
 149 
 150     }
 151 }