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