FileDocCategorySizeDatePackage
TestKeyConverter.javaAPI DocphoneME MR2 API (J2ME)8823Wed May 02 18:00:22 BST 2007javax.microedition.lcdui

TestKeyConverter

public class TestKeyConverter extends TestCase
Tests for the KeyConverter utility class.

Fields Summary
Constructors Summary
Methods Summary
voidcheckGameAction(int ga)
Checks one game action. Ensures that a keycode exists for every game action defined by the specification. Also checks that the keycode maps back to the same game action.

        int kc = KeyConverter.getKeyCode(ga);
        assertTrue("keycode is 0", kc != 0);

        int ga2 = KeyConverter.getGameAction(kc);
        assertTrue("game action is 0", ga2 != 0);
        assertTrue("game action is -1", ga2 != -1);

        assertEquals("keycode and gameaction not symmetric", ga, ga2);
    
public voidrunTests()
Runs all tests.

        declare("testAllGameActions");
        testAllGameActions();
        declare("testGetSystemKey");
        testGetSystemKey();
        declare("testNonSystemKey");
        testNonSystemKey();
        declare("testGetKeyName1");
        testGetKeyName1();
        declare("testGetKeyName2");
        testGetKeyName2();
    
voidtestAllGameActions()
Tests existence and symmetry of every game action. For every game action, ensures that it is mapped to a keycode, and then ensures that that keycode maps back to the original game action. This test is device-independent.

        checkGameAction(Canvas.UP);
        checkGameAction(Canvas.DOWN);
        checkGameAction(Canvas.LEFT);
        checkGameAction(Canvas.RIGHT);
        checkGameAction(Canvas.FIRE);
        checkGameAction(Canvas.GAME_A);
        checkGameAction(Canvas.GAME_B);
        checkGameAction(Canvas.GAME_C);
        checkGameAction(Canvas.GAME_D);
    
voidtestGetKeyName1()
Tests getKeyName() for the set of numeric keys. The expected names for these keys are probably device-independent.

        assertEquals("KEY_NUM0", "0",
            KeyConverter.getKeyName(Canvas.KEY_NUM0));
        assertEquals("KEY_NUM1", "1",
            KeyConverter.getKeyName(Canvas.KEY_NUM1));
        assertEquals("KEY_NUM2", "2",
            KeyConverter.getKeyName(Canvas.KEY_NUM2));
        assertEquals("KEY_NUM3", "3",
            KeyConverter.getKeyName(Canvas.KEY_NUM3));
        assertEquals("KEY_NUM4", "4",
            KeyConverter.getKeyName(Canvas.KEY_NUM4));
        assertEquals("KEY_NUM5", "5",
            KeyConverter.getKeyName(Canvas.KEY_NUM5));
        assertEquals("KEY_NUM6", "6",
            KeyConverter.getKeyName(Canvas.KEY_NUM6));
        assertEquals("KEY_NUM7", "7",
            KeyConverter.getKeyName(Canvas.KEY_NUM7));
        assertEquals("KEY_NUM8", "8",
            KeyConverter.getKeyName(Canvas.KEY_NUM8));
        assertEquals("KEY_NUM9", "9",
            KeyConverter.getKeyName(Canvas.KEY_NUM9));
        assertEquals("KEY_STAR", "*",
            KeyConverter.getKeyName(Canvas.KEY_STAR));
        assertEquals("KEY_POUND", "#",
            KeyConverter.getKeyName(Canvas.KEY_POUND));
    
voidtestGetKeyName2()
Tests getKeyName() for the set of keys mapped to game actions. NOTE: the expected key names are device- and language-specific.

        assertEquals("UP", "Up",
            KeyConverter.getKeyName(
                KeyConverter.getKeyCode(Canvas.UP)));
        assertEquals("DOWN", "Down",
            KeyConverter.getKeyName(
                KeyConverter.getKeyCode(Canvas.DOWN)));
        assertEquals("LEFT", "Left",
            KeyConverter.getKeyName(
                KeyConverter.getKeyCode(Canvas.LEFT)));
        assertEquals("RIGHT", "Right",
            KeyConverter.getKeyName(
                KeyConverter.getKeyCode(Canvas.RIGHT)));
        assertEquals("FIRE", "Select",
            KeyConverter.getKeyName(
                KeyConverter.getKeyCode(Canvas.FIRE)));
        assertEquals("GAME_A", "Calendar",
            KeyConverter.getKeyName(
                KeyConverter.getKeyCode(Canvas.GAME_A)));
        assertEquals("GAME_B", "Addressbook",
            KeyConverter.getKeyName(
                KeyConverter.getKeyCode(Canvas.GAME_B)));
        assertEquals("GAME_C", "Menu",
            KeyConverter.getKeyName(
                KeyConverter.getKeyCode(Canvas.GAME_C)));
        assertEquals("GAME_D", "Mail",
            KeyConverter.getKeyName(
                KeyConverter.getKeyCode(Canvas.GAME_D)));
    
voidtestGetSystemKey()
Tests getSystemKey(). NOTE: this test includes device-dependent keycode values.

        assertEquals("mismatch: SYSTEM_KEY_POWER",
            EventConstants.SYSTEM_KEY_POWER, KeyConverter.getSystemKey(-12));
        assertEquals("mismatch: SYSTEM_KEY_SEND",
            EventConstants.SYSTEM_KEY_SEND, KeyConverter.getSystemKey(-10));
        assertEquals("mismatch: SYSTEM_KEY_END",
            EventConstants.SYSTEM_KEY_END, KeyConverter.getSystemKey(-11));
        assertEquals("mismatch: SYSTEM_KEY_CLEAR",
            EventConstants.SYSTEM_KEY_CLEAR, KeyConverter.getSystemKey(-8));
    
voidtestNonSystemKey()
Tests that none of the standard keys KEY_NUM0..KEY_NUM9, KEY_STAR, KEY_POUND, and none of the game actions, is a system key, by checking that getSystemKey returns 0 in each case. This test is probably device-independent.

        assertEquals("KEY_NUM0", 0,
            KeyConverter.getSystemKey(Canvas.KEY_NUM0));
        assertEquals("KEY_NUM1", 0,
            KeyConverter.getSystemKey(Canvas.KEY_NUM1));
        assertEquals("KEY_NUM2", 0,
            KeyConverter.getSystemKey(Canvas.KEY_NUM2));
        assertEquals("KEY_NUM3", 0,
            KeyConverter.getSystemKey(Canvas.KEY_NUM3));
        assertEquals("KEY_NUM4", 0,
            KeyConverter.getSystemKey(Canvas.KEY_NUM4));
        assertEquals("KEY_NUM5", 0,
            KeyConverter.getSystemKey(Canvas.KEY_NUM5));
        assertEquals("KEY_NUM6", 0,
            KeyConverter.getSystemKey(Canvas.KEY_NUM6));
        assertEquals("KEY_NUM7", 0,
            KeyConverter.getSystemKey(Canvas.KEY_NUM7));
        assertEquals("KEY_NUM8", 0,
            KeyConverter.getSystemKey(Canvas.KEY_NUM8));
        assertEquals("KEY_NUM9", 0,
            KeyConverter.getSystemKey(Canvas.KEY_NUM9));
        assertEquals("KEY_STAR", 0,
            KeyConverter.getSystemKey(Canvas.KEY_STAR));
        assertEquals("KEY_POUND", 0,
            KeyConverter.getSystemKey(Canvas.KEY_POUND));

        assertEquals("UP", 0,
            KeyConverter.getSystemKey(
                KeyConverter.getKeyCode(Canvas.UP)));
        assertEquals("DOWN", 0,
            KeyConverter.getSystemKey(
                KeyConverter.getKeyCode(Canvas.DOWN)));
        assertEquals("LEFT", 0,
            KeyConverter.getSystemKey(
                KeyConverter.getKeyCode(Canvas.LEFT)));
        assertEquals("RIGHT", 0,
            KeyConverter.getSystemKey(
                KeyConverter.getKeyCode(Canvas.RIGHT)));
        assertEquals("FIRE", 0,
            KeyConverter.getSystemKey(
                KeyConverter.getKeyCode(Canvas.FIRE)));
        assertEquals("GAME_A", 0,
            KeyConverter.getSystemKey(
                KeyConverter.getKeyCode(Canvas.GAME_A)));
        assertEquals("GAME_B", 0,
            KeyConverter.getSystemKey(
                KeyConverter.getKeyCode(Canvas.GAME_B)));
        assertEquals("GAME_C", 0,
            KeyConverter.getSystemKey(
                KeyConverter.getKeyCode(Canvas.GAME_C)));
        assertEquals("GAME_D", 0,
            KeyConverter.getSystemKey(
                KeyConverter.getKeyCode(Canvas.GAME_D)));