1 /*
   2  * Copyright (c) 2011, 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;
  27 
  28 import com.sun.javafx.test.MouseEventGenerator;
  29 import javafx.collections.ObservableMap;
  30 import javafx.scene.input.KeyCode;
  31 import javafx.scene.input.KeyCombination;
  32 import javafx.scene.input.KeyEvent;
  33 import org.junit.Before;
  34 import org.junit.Test;
  35 
  36 import java.util.ConcurrentModificationException;
  37 import java.util.Iterator;
  38 import java.util.Map;
  39 import java.util.concurrent.atomic.AtomicBoolean;
  40 
  41 import static org.junit.Assert.assertEquals;
  42 import static org.junit.Assert.assertFalse;
  43 import static org.junit.Assert.assertTrue;
  44 
  45 public class AcceleratorsTest {
  46 
  47     private Scene scene;
  48     private ObservableMap<KeyCombination, Runnable> accelerators;
  49 
  50     @Before
  51     public void setUp() {
  52         scene = new Scene(new Group());
  53         accelerators = scene.getAccelerators();
  54     }
  55 
  56 
  57     @Test
  58     public void testAcceleratorExecuted() {
  59         AtomicBoolean executed = new AtomicBoolean();
  60         accelerators.put(KeyCombination.keyCombination("Alt + A"), () -> executed.set(true));
  61         scene.impl_processKeyEvent(new KeyEvent(KeyEvent.KEY_PRESSED, "A", "A", KeyCode.A, false, false, true, false));
  62         assertTrue(executed.get());
  63     }
  64 
  65     @Test
  66     public void testAcceleratorRemovedWhenExecuted() {
  67         AtomicBoolean executed = new AtomicBoolean();
  68         final KeyCombination altA = KeyCombination.keyCombination("Alt + A");
  69         final KeyCombination altB = KeyCombination.keyCombination("Alt + B");
  70         accelerators.put(altA, () -> accelerators.remove(altA));
  71         accelerators.put(altB, () -> executed.set(true));
  72         assertEquals(2, accelerators.size());
  73         scene.impl_processKeyEvent(new KeyEvent(KeyEvent.KEY_PRESSED, "A", "A", KeyCode.A, false, false, true, false));
  74         assertEquals(1, accelerators.size());
  75         assertFalse(executed.get());
  76         scene.impl_processKeyEvent(new KeyEvent(KeyEvent.KEY_PRESSED, "B", "B", KeyCode.B, false, false, true, false));
  77         assertTrue(executed.get());
  78     }
  79 
  80     @Test(expected = ConcurrentModificationException.class)
  81     public void testAcceleratorComodification() {
  82         final KeyCombination altA = KeyCombination.keyCombination("Alt + A");
  83         final KeyCombination altB = KeyCombination.keyCombination("Alt + B");
  84         accelerators.put(altA, () -> {
  85         });
  86         accelerators.put(altB, () -> {
  87         });
  88 
  89         final Iterator<Map.Entry<KeyCombination, Runnable>> iterator = accelerators.entrySet().iterator();
  90         iterator.next();
  91 
  92         final Iterator<Map.Entry<KeyCombination, Runnable>> iterator1 = accelerators.entrySet().iterator();
  93         iterator1.next();
  94         iterator1.remove();
  95 
  96         iterator.next();
  97     }
  98 }