001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package javax.microedition.lcdui;
028:
029: import com.sun.midp.i3test.*;
030: import com.sun.midp.lcdui.EventConstants;
031:
032: /**
033: * Tests for the KeyConverter utility class.
034: */
035: public class TestKeyConverter extends TestCase {
036:
037: /**
038: * Checks one game action. Ensures that a keycode exists for every
039: * game action defined by the specification. Also checks that the keycode
040: * maps back to the same game action.
041: */
042: void checkGameAction(int ga) {
043: int kc = KeyConverter.getKeyCode(ga);
044: assertTrue("keycode is 0", kc != 0);
045:
046: int ga2 = KeyConverter.getGameAction(kc);
047: assertTrue("game action is 0", ga2 != 0);
048: assertTrue("game action is -1", ga2 != -1);
049:
050: assertEquals("keycode and gameaction not symmetric", ga, ga2);
051: }
052:
053: /**
054: * Tests existence and symmetry of every game action. For every game
055: * action, ensures that it is mapped to a keycode, and then ensures that
056: * that keycode maps back to the original game action. This test is
057: * device-independent.
058: */
059: void testAllGameActions() {
060: checkGameAction(Canvas.UP);
061: checkGameAction(Canvas.DOWN);
062: checkGameAction(Canvas.LEFT);
063: checkGameAction(Canvas.RIGHT);
064: checkGameAction(Canvas.FIRE);
065: checkGameAction(Canvas.GAME_A);
066: checkGameAction(Canvas.GAME_B);
067: checkGameAction(Canvas.GAME_C);
068: checkGameAction(Canvas.GAME_D);
069: }
070:
071: /**
072: * Tests getSystemKey().
073: *
074: * NOTE: this test includes device-dependent keycode values.
075: */
076: void testGetSystemKey() {
077: assertEquals("mismatch: SYSTEM_KEY_POWER",
078: EventConstants.SYSTEM_KEY_POWER, KeyConverter
079: .getSystemKey(-12));
080: assertEquals("mismatch: SYSTEM_KEY_SEND",
081: EventConstants.SYSTEM_KEY_SEND, KeyConverter
082: .getSystemKey(-10));
083: assertEquals("mismatch: SYSTEM_KEY_END",
084: EventConstants.SYSTEM_KEY_END, KeyConverter
085: .getSystemKey(-11));
086: assertEquals("mismatch: SYSTEM_KEY_CLEAR",
087: EventConstants.SYSTEM_KEY_CLEAR, KeyConverter
088: .getSystemKey(-8));
089: }
090:
091: /**
092: * Tests that none of the standard keys KEY_NUM0..KEY_NUM9, KEY_STAR,
093: * KEY_POUND, and none of the game actions, is a system key, by
094: * checking that getSystemKey returns 0 in each case. This test is
095: * probably device-independent.
096: */
097: void testNonSystemKey() {
098: assertEquals("KEY_NUM0", 0, KeyConverter
099: .getSystemKey(Canvas.KEY_NUM0));
100: assertEquals("KEY_NUM1", 0, KeyConverter
101: .getSystemKey(Canvas.KEY_NUM1));
102: assertEquals("KEY_NUM2", 0, KeyConverter
103: .getSystemKey(Canvas.KEY_NUM2));
104: assertEquals("KEY_NUM3", 0, KeyConverter
105: .getSystemKey(Canvas.KEY_NUM3));
106: assertEquals("KEY_NUM4", 0, KeyConverter
107: .getSystemKey(Canvas.KEY_NUM4));
108: assertEquals("KEY_NUM5", 0, KeyConverter
109: .getSystemKey(Canvas.KEY_NUM5));
110: assertEquals("KEY_NUM6", 0, KeyConverter
111: .getSystemKey(Canvas.KEY_NUM6));
112: assertEquals("KEY_NUM7", 0, KeyConverter
113: .getSystemKey(Canvas.KEY_NUM7));
114: assertEquals("KEY_NUM8", 0, KeyConverter
115: .getSystemKey(Canvas.KEY_NUM8));
116: assertEquals("KEY_NUM9", 0, KeyConverter
117: .getSystemKey(Canvas.KEY_NUM9));
118: assertEquals("KEY_STAR", 0, KeyConverter
119: .getSystemKey(Canvas.KEY_STAR));
120: assertEquals("KEY_POUND", 0, KeyConverter
121: .getSystemKey(Canvas.KEY_POUND));
122:
123: assertEquals("UP", 0, KeyConverter.getSystemKey(KeyConverter
124: .getKeyCode(Canvas.UP)));
125: assertEquals("DOWN", 0, KeyConverter.getSystemKey(KeyConverter
126: .getKeyCode(Canvas.DOWN)));
127: assertEquals("LEFT", 0, KeyConverter.getSystemKey(KeyConverter
128: .getKeyCode(Canvas.LEFT)));
129: assertEquals("RIGHT", 0, KeyConverter.getSystemKey(KeyConverter
130: .getKeyCode(Canvas.RIGHT)));
131: assertEquals("FIRE", 0, KeyConverter.getSystemKey(KeyConverter
132: .getKeyCode(Canvas.FIRE)));
133: assertEquals("GAME_A", 0, KeyConverter
134: .getSystemKey(KeyConverter.getKeyCode(Canvas.GAME_A)));
135: assertEquals("GAME_B", 0, KeyConverter
136: .getSystemKey(KeyConverter.getKeyCode(Canvas.GAME_B)));
137: assertEquals("GAME_C", 0, KeyConverter
138: .getSystemKey(KeyConverter.getKeyCode(Canvas.GAME_C)));
139: assertEquals("GAME_D", 0, KeyConverter
140: .getSystemKey(KeyConverter.getKeyCode(Canvas.GAME_D)));
141: }
142:
143: /**
144: * Tests getKeyName() for the set of numeric keys. The expected names for
145: * these keys are probably device-independent.
146: */
147: void testGetKeyName1() {
148: assertEquals("KEY_NUM0", "0", KeyConverter
149: .getKeyName(Canvas.KEY_NUM0));
150: assertEquals("KEY_NUM1", "1", KeyConverter
151: .getKeyName(Canvas.KEY_NUM1));
152: assertEquals("KEY_NUM2", "2", KeyConverter
153: .getKeyName(Canvas.KEY_NUM2));
154: assertEquals("KEY_NUM3", "3", KeyConverter
155: .getKeyName(Canvas.KEY_NUM3));
156: assertEquals("KEY_NUM4", "4", KeyConverter
157: .getKeyName(Canvas.KEY_NUM4));
158: assertEquals("KEY_NUM5", "5", KeyConverter
159: .getKeyName(Canvas.KEY_NUM5));
160: assertEquals("KEY_NUM6", "6", KeyConverter
161: .getKeyName(Canvas.KEY_NUM6));
162: assertEquals("KEY_NUM7", "7", KeyConverter
163: .getKeyName(Canvas.KEY_NUM7));
164: assertEquals("KEY_NUM8", "8", KeyConverter
165: .getKeyName(Canvas.KEY_NUM8));
166: assertEquals("KEY_NUM9", "9", KeyConverter
167: .getKeyName(Canvas.KEY_NUM9));
168: assertEquals("KEY_STAR", "*", KeyConverter
169: .getKeyName(Canvas.KEY_STAR));
170: assertEquals("KEY_POUND", "#", KeyConverter
171: .getKeyName(Canvas.KEY_POUND));
172: }
173:
174: /**
175: * Tests getKeyName() for the set of keys mapped to game actions.
176: *
177: * NOTE: the expected key names are device- and language-specific.
178: */
179: void testGetKeyName2() {
180: assertEquals("UP", "Up", KeyConverter.getKeyName(KeyConverter
181: .getKeyCode(Canvas.UP)));
182: assertEquals("DOWN", "Down", KeyConverter
183: .getKeyName(KeyConverter.getKeyCode(Canvas.DOWN)));
184: assertEquals("LEFT", "Left", KeyConverter
185: .getKeyName(KeyConverter.getKeyCode(Canvas.LEFT)));
186: assertEquals("RIGHT", "Right", KeyConverter
187: .getKeyName(KeyConverter.getKeyCode(Canvas.RIGHT)));
188: assertEquals("FIRE", "Select", KeyConverter
189: .getKeyName(KeyConverter.getKeyCode(Canvas.FIRE)));
190: assertEquals("GAME_A", "Calendar", KeyConverter
191: .getKeyName(KeyConverter.getKeyCode(Canvas.GAME_A)));
192: assertEquals("GAME_B", "Addressbook", KeyConverter
193: .getKeyName(KeyConverter.getKeyCode(Canvas.GAME_B)));
194: assertEquals("GAME_C", "Menu", KeyConverter
195: .getKeyName(KeyConverter.getKeyCode(Canvas.GAME_C)));
196: assertEquals("GAME_D", "Mail", KeyConverter
197: .getKeyName(KeyConverter.getKeyCode(Canvas.GAME_D)));
198: }
199:
200: /**
201: * Runs all tests.
202: */
203: public void runTests() {
204: declare("testAllGameActions");
205: testAllGameActions();
206: declare("testGetSystemKey");
207: testGetSystemKey();
208: declare("testNonSystemKey");
209: testNonSystemKey();
210: declare("testGetKeyName1");
211: testGetKeyName1();
212: declare("testGetKeyName2");
213: testGetKeyName2();
214: }
215:
216: }
|