Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions jme3-core/src/test/java/com/jme3/input/KeyNamesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2009-2024 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.input;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
* Automated tests for the {@link KeyNames} class.
*
* Verifies that key codes defined in {@link KeyInput} correctly map
* to their human-readable names, and that unmapped codes return null.
*/
class KeyNamesTest {

/**
* Verify that common key codes return the expected human-readable names,
* covering letters, digits, special keys, modifiers, and arrow keys.
* Also verifies that an unmapped code returns null and that KEY_UNKNOWN
* maps to the string "Unknown".
*/
@Test
public void testKeyNamesMapping() {
// Letter keys
assertEquals("A", KeyNames.getName(KeyInput.KEY_A));
assertEquals("Z", KeyNames.getName(KeyInput.KEY_Z));

// Digit keys
assertEquals("0", KeyNames.getName(KeyInput.KEY_0));
assertEquals("9", KeyNames.getName(KeyInput.KEY_9));

// Special keys
assertEquals("Space", KeyNames.getName(KeyInput.KEY_SPACE));
assertEquals("Enter", KeyNames.getName(KeyInput.KEY_RETURN));
assertEquals("Backspace", KeyNames.getName(KeyInput.KEY_BACK));
assertEquals("Esc", KeyNames.getName(KeyInput.KEY_ESCAPE));

// Arrow keys
assertEquals("Up", KeyNames.getName(KeyInput.KEY_UP));
assertEquals("Down", KeyNames.getName(KeyInput.KEY_DOWN));
assertEquals("Left", KeyNames.getName(KeyInput.KEY_LEFT));
assertEquals("Right", KeyNames.getName(KeyInput.KEY_RIGHT));

// Modifier keys
assertEquals("Left Shift", KeyNames.getName(KeyInput.KEY_LSHIFT));
assertEquals("Right Shift", KeyNames.getName(KeyInput.KEY_RSHIFT));
assertEquals("Left Ctrl", KeyNames.getName(KeyInput.KEY_LCONTROL));

// KEY_UNKNOWN should map to the string "Unknown"
assertEquals("Unknown", KeyNames.getName(KeyInput.KEY_UNKNOWN));

// An unmapped key code should return null
assertNull(KeyNames.getName(0x5C));
}
Comment on lines +46 to +84
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test method testKeyNamesMapping is quite large and covers many different categories of keys. Splitting it into smaller, focused test methods improves readability and failure isolation. Additionally, the current test is missing several modifier keys (Right Ctrl, Alt, Meta/Option) and boundary checks.

Note: The KeyNames class Javadoc claims support for keyId up to 255, but the implementation uses an array of size 0xFF (255), which causes getName(255) to throw an ArrayIndexOutOfBoundsException. This should be addressed in the KeyNames class, and a boundary test should be added here once fixed.

    @Test
    void testLetterAndDigitKeys() {
        assertEquals("A", KeyNames.getName(KeyInput.KEY_A));
        assertEquals("Z", KeyNames.getName(KeyInput.KEY_Z));

        assertEquals("0", KeyNames.getName(KeyInput.KEY_0));
        assertEquals("9", KeyNames.getName(KeyInput.KEY_9));
    }

    @Test
    void testSpecialAndArrowKeys() {
        assertEquals("Space",     KeyNames.getName(KeyInput.KEY_SPACE));
        assertEquals("Enter",     KeyNames.getName(KeyInput.KEY_RETURN));
        assertEquals("Backspace", KeyNames.getName(KeyInput.KEY_BACK));
        assertEquals("Esc",       KeyNames.getName(KeyInput.KEY_ESCAPE));

        assertEquals("Up",    KeyNames.getName(KeyInput.KEY_UP));
        assertEquals("Down",  KeyNames.getName(KeyInput.KEY_DOWN));
        assertEquals("Left",  KeyNames.getName(KeyInput.KEY_LEFT));
        assertEquals("Right", KeyNames.getName(KeyInput.KEY_RIGHT));
    }

    @Test
    void testModifierKeys() {
        assertEquals("Left Shift",  KeyNames.getName(KeyInput.KEY_LSHIFT));
        assertEquals("Right Shift", KeyNames.getName(KeyInput.KEY_RSHIFT));
        assertEquals("Left Ctrl",   KeyNames.getName(KeyInput.KEY_LCONTROL));
        assertEquals("Right Ctrl",  KeyNames.getName(KeyInput.KEY_RCONTROL));
        assertEquals("Left Alt",    KeyNames.getName(KeyInput.KEY_LMENU));
        assertEquals("Right Alt",   KeyNames.getName(KeyInput.KEY_RMENU));
        assertEquals("Left Option", KeyNames.getName(KeyInput.KEY_LMETA));
        assertEquals("Right Option",KeyNames.getName(KeyInput.KEY_RMETA));
    }

    @Test
    void testUnmappedKeys() {
        assertEquals("Unknown", KeyNames.getName(KeyInput.KEY_UNKNOWN));
        assertNull(KeyNames.getName(0x5C));
    }
References
  1. Issues found in test code should be reported with a reduced priority, at most medium.

}
Loading