1 /*
   2  * Copyright (c) 2010, 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 test.javafx.scene.input;
  27 
  28 import com.sun.javafx.scene.input.KeyCodeMap;
  29 import static org.junit.Assert.assertEquals;
  30 import static org.junit.Assert.assertFalse;
  31 import static org.junit.Assert.assertNotNull;
  32 import static org.junit.Assert.assertSame;
  33 import static org.junit.Assert.assertTrue;
  34 import javafx.scene.Node;
  35 import javafx.event.EventTarget;
  36 import javafx.scene.input.KeyCode;
  37 import javafx.scene.input.KeyEvent;
  38 
  39 import org.junit.Test;
  40 
  41 public class KeyEventTest {
  42 
  43     private final Node node1 = new TestNode();
  44     private final Node node2 = new TestNode();
  45     
  46     public KeyEvent testKeyEvent(EventTarget target, String character, KeyCode code, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown) {
  47         return new KeyEvent(null, target, KeyEvent.KEY_PRESSED, character, null, code, shiftDown, controlDown, altDown, metaDown);
  48     }
  49 
  50     @Test
  51     public void shouldCreateKeyTypedEvent() {
  52         KeyEvent event = new KeyEvent(null, node1, KeyEvent.KEY_TYPED, "A", "A", KeyCodeMap.valueOf(0x41), true,
  53                 false, true, false);
  54 
  55         assertSame(node1, event.getTarget());
  56         assertEquals("A", event.getCharacter());
  57         assertTrue(event.getText().isEmpty());
  58         assertSame(KeyCode.UNDEFINED, event.getCode());
  59         assertTrue(event.isShiftDown());
  60         assertFalse(event.isControlDown());
  61         assertTrue(event.isAltDown());
  62         assertFalse(event.isMetaDown());
  63         assertSame(KeyEvent.KEY_TYPED, event.getEventType());
  64     }
  65 
  66     @Test
  67     public void shouldCreateKeyReleasedEvent() {
  68         KeyEvent event = new KeyEvent(null, node1, KeyEvent.KEY_RELEASED, "A", "A", KeyCodeMap.valueOf(0x41), false,
  69                 true, false, true);
  70 
  71         assertSame(node1, event.getTarget());
  72         assertEquals(KeyEvent.CHAR_UNDEFINED, event.getCharacter());
  73         assertEquals("A", event.getText());
  74         assertSame(KeyCode.A, event.getCode());
  75         assertFalse(event.isShiftDown());
  76         assertTrue(event.isControlDown());
  77         assertFalse(event.isAltDown());
  78         assertTrue(event.isMetaDown());
  79         assertSame(KeyEvent.KEY_RELEASED, event.getEventType());
  80     }
  81 
  82     @Test
  83     public void shouldCopyKeyTypedEvent() {
  84         KeyEvent original = new KeyEvent(null, node1, KeyEvent.KEY_TYPED, "A", "A", KeyCodeMap.valueOf(0x41), true,
  85                 false, false, false);
  86         KeyEvent event = original.copyFor(null, node2);
  87 
  88         assertSame(node2, event.getTarget());
  89         assertEquals("A", event.getCharacter());
  90         assertTrue(event.getText().isEmpty());
  91         assertSame(KeyCode.UNDEFINED, event.getCode());
  92         assertTrue(event.isShiftDown());
  93         assertFalse(event.isControlDown());
  94         assertFalse(event.isAltDown());
  95         assertFalse(event.isMetaDown());
  96         assertSame(KeyEvent.KEY_TYPED, event.getEventType());
  97     }
  98 
  99     @Test
 100     public void shouldGetNonEmptyDescription() {
 101         KeyEvent event1 = testKeyEvent(node1, "A", KeyCode.A,
 102             false, false, false, false);
 103         KeyEvent event2 = testKeyEvent(node1, "", KeyCode.UNDEFINED,
 104             true, true, true, true);
 105 
 106         String s1 = event1.toString();
 107         String s2 = event2.toString();
 108         assertNotNull(s1);
 109         assertNotNull(s2);
 110         assertFalse(s1.isEmpty());
 111         assertFalse(s2.isEmpty());
 112     }
 113 }