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