source: trunk/quest-misc-test/src/test/java/de/ugoe/cs/tasktree/keyboardmaps/KeyboardMapTest.java @ 725

Last change on this file since 725 was 725, checked in by pharms, 12 years ago
  • use console for logging instead of logger
File size: 14.7 KB
Line 
1package de.ugoe.cs.tasktree.keyboardmaps;
2
3import static org.junit.Assert.*;
4
5import java.util.Locale;
6import java.util.logging.Level;
7
8import org.junit.Before;
9import org.junit.Test;
10
11import de.ugoe.cs.util.console.TextConsole;
12
13/**
14 * TODO comment
15 *
16 * @version $Revision: $ $Date: 11.07.2012$
17 * @author 2012, last modified by $Author: patrick$
18 */
19public class KeyboardMapTest {
20
21    /**
22     *
23     */
24    @Before
25    public void setUp() {
26        new TextConsole(Level.FINEST);
27    }
28
29    /**
30     * TODO: comment
31     *
32     */
33    @Test
34    public void testInitialization() {
35        KeyboardMap map = KeyboardMapFactory.createKeyboardMap(Locale.ENGLISH);
36        assertNotNull(map);
37
38        map = KeyboardMapFactory.createKeyboardMap(Locale.FRENCH);
39        assertNotNull(map);
40
41        map = KeyboardMapFactory.createKeyboardMap(Locale.GERMAN);
42        assertNotNull(map);
43
44        map = KeyboardMapFactory.createKeyboardMap(Locale.ITALIAN);
45        assertNotNull(map);
46
47        map = KeyboardMapFactory.createKeyboardMap(Locale.JAPANESE);
48        assertNotNull(map);
49
50        map = KeyboardMapFactory.createKeyboardMap(Locale.KOREAN);
51        assertNotNull(map);
52
53        try {
54            map = KeyboardMapFactory.createKeyboardMap(Locale.CHINESE);
55            fail("expected exception did not occur");
56        }
57        catch (IllegalArgumentException e) {
58            // was expected and is ignored
59        }
60
61        try {
62            map = KeyboardMapFactory.createKeyboardMap(Locale.SIMPLIFIED_CHINESE);
63            fail("expected exception did not occur");
64        }
65        catch (IllegalArgumentException e) {
66            // was expected and is ignored
67        }
68
69        try {
70            map = KeyboardMapFactory.createKeyboardMap(Locale.TRADITIONAL_CHINESE);
71            fail("expected exception did not occur");
72        }
73        catch (IllegalArgumentException e) {
74            // was expected and is ignored
75        }
76
77        map = KeyboardMapFactory.createKeyboardMap(Locale.FRANCE);
78        assertNotNull(map);
79
80        map = KeyboardMapFactory.createKeyboardMap(Locale.GERMANY);
81        assertNotNull(map);
82
83        map = KeyboardMapFactory.createKeyboardMap(Locale.ITALY);
84        assertNotNull(map);
85
86        map = KeyboardMapFactory.createKeyboardMap(Locale.JAPAN);
87        assertNotNull(map);
88
89        map = KeyboardMapFactory.createKeyboardMap(Locale.KOREA);
90        assertNotNull(map);
91
92        try {
93            map = KeyboardMapFactory.createKeyboardMap(Locale.CHINA);
94            fail("expected exception did not occur");
95        }
96        catch (IllegalArgumentException e) {
97            // was expected and is ignored
98        }
99
100        try {
101            map = KeyboardMapFactory.createKeyboardMap(Locale.PRC);
102            fail("expected exception did not occur");
103        }
104        catch (IllegalArgumentException e) {
105            // was expected and is ignored
106        }
107
108        try {
109            map = KeyboardMapFactory.createKeyboardMap(Locale.TAIWAN);
110            fail("expected exception did not occur");
111        }
112        catch (IllegalArgumentException e) {
113            // was expected and is ignored
114        }
115
116        map = KeyboardMapFactory.createKeyboardMap(Locale.UK);
117        assertNotNull(map);
118
119        map = KeyboardMapFactory.createKeyboardMap(Locale.US);
120        assertNotNull(map);
121
122        map = KeyboardMapFactory.createKeyboardMap(Locale.CANADA);
123        assertNotNull(map);
124
125        map = KeyboardMapFactory.createKeyboardMap(Locale.CANADA_FRENCH);
126        assertNotNull(map);
127
128        map = KeyboardMapFactory.createKeyboardMap(Locale.GERMAN);
129        assertNotNull(map);
130
131    }
132
133    /**
134     * TODO: comment
135     *
136     */
137    @Test
138    public void testGermanKeyboardMap() {
139        KeyboardMap map = KeyboardMapFactory.createKeyboardMap(Locale.GERMAN);
140        assertNotNull(map);
141
142        assertCombinations(map, VirtualKey.DIGIT_1, '1', '!', '¹', '¡', true);
143        assertCombinations(map, VirtualKey.EXCLAMATION_MARK, '1', '!', '¹', '¡', true);
144        assertCombinations(map, VirtualKey.INVERTED_EXCLAMATION_MARK, '1', '!', '¹', '¡', true);
145
146        assertCombinations(map, VirtualKey.DIGIT_2, '2', '"', '²', '⅛', true);
147        assertCombinations(map, VirtualKey.QUOTEDBL, '2', '"', '²', '⅛', true);
148
149        assertCombinations(map, VirtualKey.DIGIT_3, '3', '§', '³', '£', true);
150
151        assertCombinations(map, VirtualKey.DIGIT_4, '4', '$', '¼', '¤', true);
152        assertCombinations(map, VirtualKey.DOLLAR, '4', '$', '¼', '¤', true);
153
154        assertCombinations(map, VirtualKey.DIGIT_5, '5', '%', '½', '⅜', true);
155
156        assertCombinations(map, VirtualKey.DIGIT_6, '6', '&', '¾', '⅝', true);
157        assertCombinations(map, VirtualKey.AMPERSAND, '6', '&', '¾', '⅝', true);
158
159        assertCombinations(map, VirtualKey.DIGIT_7, '7', '/', '{', '⅞', true);
160        assertCombinations(map, VirtualKey.SLASH, '7', '/', '{', '⅞', true);
161        assertCombinations(map, VirtualKey.BRACELEFT, '7', '/', '{', '⅞', true);
162
163        assertCombinations(map, VirtualKey.DIGIT_8, '8', '(', '[', '™', true);
164        assertCombinations(map, VirtualKey.LEFT_PARENTHESIS, '8', '(', '[', '™', true);
165        assertCombinations(map, VirtualKey.OPEN_BRACKET, '8', '(', '[', '™', true);
166
167        assertCombinations(map, VirtualKey.DIGIT_9, '9', ')', ']', '±', true);
168        assertCombinations(map, VirtualKey.RIGHT_PARENTHESIS, '9', ')', ']', '±', true);
169        assertCombinations(map, VirtualKey.CLOSE_BRACKET, '9', ')', ']', '±', true);
170
171        assertCombinations(map, VirtualKey.DIGIT_0, '0', '=', '}', 0, true);
172        assertCombinations(map, VirtualKey.EQUALS, '0', '=', '}', 0, true);
173        assertCombinations(map, VirtualKey.BRACERIGHT, '0', '=', '}', 0, true);
174
175        assertCombinations(map, VirtualKey.BACK_SLASH, 'ß', '?', '\\', '¿', true);
176
177        assertCombinations(map, VirtualKey.LETTER_Q, 'q', 'Q', '@', 'Ω', true);
178        assertCombinations(map, VirtualKey.AT, 'q', 'Q', '@', 'Ω', true);
179
180        assertCombinations(map, VirtualKey.LETTER_W, 'w', 'W', 0, 0, true);
181
182        assertCombinations(map, VirtualKey.LETTER_E, 'e', 'E', '€', 0, true);
183        assertCombinations(map, VirtualKey.EURO_SIGN, 'e', 'E', '€', 0, true);
184
185        assertCombinations(map, VirtualKey.LETTER_R, 'r', 'R', 0, '®', true);
186
187        assertCombinations(map, VirtualKey.LETTER_T, 't', 'T', 'ŧ', 'Ŧ', true);
188
189        assertCombinations(map, VirtualKey.LETTER_Z, 'z', 'Z', '←', '¥', true);
190
191        assertCombinations(map, VirtualKey.LETTER_U, 'u', 'U', '↓', '↑', true);
192
193        assertCombinations(map, VirtualKey.LETTER_I, 'i', 'I', '→', 'ı', true);
194
195        assertCombinations(map, VirtualKey.LETTER_O, 'o', 'O', 'ø', 'Ø', true);
196
197        assertCombinations(map, VirtualKey.LETTER_P, 'p', 'P', 'þ', 'Þ', true);
198
199        assertCombinations(map, VirtualKey.LETTER_A, 'a', 'A', 'æ', 'Æ', true);
200
201        assertCombinations(map, VirtualKey.LETTER_S, 's', 'S', 0, 0, true);
202
203        assertCombinations(map, VirtualKey.LETTER_D, 'd', 'D', 'ð', 'Ð', true);
204
205        assertCombinations(map, VirtualKey.LETTER_F, 'f', 'F', 'đ', 'ª', true);
206
207        assertCombinations(map, VirtualKey.LETTER_G, 'g', 'G', 'ŋ', 'Ŋ', true);
208
209        assertCombinations(map, VirtualKey.LETTER_H, 'h', 'H', 'ħ', 'Ħ', true);
210
211        assertCombinations(map, VirtualKey.LETTER_J, 'j', 'J', 0, 0, true);
212
213        assertCombinations(map, VirtualKey.LETTER_K, 'k', 'K', 'ĸ', 0, true);
214
215        assertCombinations(map, VirtualKey.LETTER_L, 'l', 'L', 0, 0, true);
216
217        assertCombinations(map, VirtualKey.LETTER_Y, 'y', 'Y', '»', 0, true);
218
219        assertCombinations(map, VirtualKey.LETTER_X, 'x', 'X', '«', 0, true);
220
221        assertCombinations(map, VirtualKey.LETTER_C, 'c', 'C', '¢', '©', true);
222
223        assertCombinations(map, VirtualKey.LETTER_V, 'v', 'V', '„', 0, true);
224
225        assertCombinations(map, VirtualKey.LETTER_B, 'b', 'B', '“', 0, true);
226
227        assertCombinations(map, VirtualKey.LETTER_N, 'n', 'N', 0, 0, true);
228
229        assertCombinations(map, VirtualKey.LETTER_M, 'm', 'M', 'µ', 'º', true);
230
231        assertCombinations(map, VirtualKey.NUMPAD_0, '0', 0, 0, 0, false);
232
233        assertCombinations(map, VirtualKey.NUMPAD_1, '1', 0, 0, 0, false);
234
235        assertCombinations(map, VirtualKey.NUMPAD_2, '2', 0, 0, 0, false);
236
237        assertCombinations(map, VirtualKey.NUMPAD_3, '3', 0, 0, 0, false);
238
239        assertCombinations(map, VirtualKey.NUMPAD_4, '4', 0, 0, 0, false);
240
241        assertCombinations(map, VirtualKey.NUMPAD_5, '5', 0, 0, 0, false);
242
243        assertCombinations(map, VirtualKey.NUMPAD_6, '6', 0, 0, 0, false);
244
245        assertCombinations(map, VirtualKey.NUMPAD_7, '7', 0, 0, 0, false);
246
247        assertCombinations(map, VirtualKey.NUMPAD_8, '8', 0, 0, 0, false);
248
249        assertCombinations(map, VirtualKey.NUMPAD_9, '9', 0, 0, 0, false);
250
251        assertCombinations(map, VirtualKey.SEPARATOR, ',', 0, 0, 0, false);
252        assertCombinations(map, VirtualKey.DECIMAL, ',', 0, 0, 0, false);
253
254        assertCombinations(map, VirtualKey.CIRCUMFLEX, '^', '°', '¬', 0, true);
255
256        assertCombinations(map, VirtualKey.TAB, '\t', 0, '\t', 0, true);
257
258        assertCombinations(map, VirtualKey.SPACE, ' ', ' ', ' ', ' ', true);
259
260        assertCombinations(map, VirtualKey.COMMA, ',', ';', '·', '×', true);
261        assertCombinations(map, VirtualKey.SEMICOLON, ',', ';', '·', '×', true);
262        assertCombinations(map, VirtualKey.MULTIPLY, ',', ';', '·', '×', true);
263
264        assertCombinations(map, VirtualKey.MINUS, '-', '_', 0, 0, true);
265        assertCombinations(map, VirtualKey.UNDERSCORE, '-', '_', 0, 0, true);
266
267        assertCombinations(map, VirtualKey.PERIOD, '.', ':', '…', '÷', true);
268        assertCombinations(map, VirtualKey.COLON, '.', ':', '…', '÷', true);
269        assertCombinations(map, VirtualKey.DIVIDE, '.', ':', '…', '÷', true);
270
271        assertCombinations(map, VirtualKey.PLUS, '+', '*', '~', 0, true);
272        assertCombinations(map, VirtualKey.ASTERISK, '+', '*', '~', 0, true);
273        assertCombinations(map, VirtualKey.DEAD_TILDE, '+', '*', '~', 0, true);
274
275        assertCombinations(map, VirtualKey.LESS, '<', '>', '|', '¦', true);
276        assertCombinations(map, VirtualKey.GREATER, '<', '>', '|', '¦', true);
277
278        assertCombinations(map, VirtualKey.NUMBER_SIGN, '#', '\'', 0, 0, true);
279
280        /*
281         * DEAD_GRAVE(KeyEvent.VK_DEAD_GRAVE), DEAD_ACUTE(KeyEvent.VK_DEAD_ACUTE),
282         * DEAD_CIRCUMFLEX(KeyEvent.VK_DEAD_CIRCUMFLEX), DEAD_TILDE(KeyEvent.VK_DEAD_TILDE),
283         * DEAD_MACRON(KeyEvent.VK_DEAD_MACRON), DEAD_BREVE(KeyEvent.VK_DEAD_BREVE),
284         * DEAD_ABOVEDOT(KeyEvent.VK_DEAD_ABOVEDOT), DEAD_DIAERESIS(KeyEvent.VK_DEAD_DIAERESIS),
285         * DEAD_ABOVERING(KeyEvent.VK_DEAD_ABOVERING),
286         * DEAD_DOUBLEACUTE(KeyEvent.VK_DEAD_DOUBLEACUTE), DEAD_CARON(KeyEvent.VK_DEAD_CARON),
287         * DEAD_CEDILLA(KeyEvent.VK_DEAD_CEDILLA), DEAD_OGONEK(KeyEvent.VK_DEAD_OGONEK),
288         * DEAD_IOTA(KeyEvent.VK_DEAD_IOTA), DEAD_VOICED_SOUND(KeyEvent.VK_DEAD_VOICED_SOUND),
289         * DEAD_SEMIVOICED_SOUND(KeyEvent.VK_DEAD_SEMIVOICED_SOUND),
290         */
291    }
292
293    /**
294   *
295   */
296    private void assertCombinations(KeyboardMap map,
297                                    VirtualKey  key,
298                                    int         normal,
299                                    int         withShift,
300                                    int         withAltGr,
301                                    int         withShiftAndAltGr,
302                                    boolean     numLockIndependent)
303    {
304        if (numLockIndependent) {
305            if (normal != 0) {
306                assertCombination("normal", normal, map, key, false, false, false);
307            }
308
309            if (withShift != 0) {
310                assertCombination("shift", withShift, map, key, false, true, false);
311            }
312
313            if (withAltGr != 0) {
314                assertCombination("altgr", withAltGr, map, key, false, false, true);
315            }
316
317            if (withShiftAndAltGr != 0) {
318                assertCombination("shift and altgr", withShiftAndAltGr, map, key, false, true, true);
319            }
320        }
321        else {
322            assertInvalidCombination("normal", normal, map, key, false, false, false);
323            assertInvalidCombination("shift", withShift, map, key, false, true, false);
324            assertInvalidCombination("altgr", withAltGr, map, key, false, false, true);
325            assertInvalidCombination
326              ("shift and altgr", withShiftAndAltGr, map, key, false, true, true);
327        }
328
329        if (normal != 0) {
330            assertCombination("numlock", normal, map, key, true, false, false);
331        }
332
333        if (withShift != 0) {
334            assertCombination("numlock and shift", withShift, map, key, true, true, false);
335        }
336
337        if (withAltGr != 0) {
338            assertCombination("numlock and altgr", withAltGr, map, key, true, false, true);
339        }
340
341        if (withShiftAndAltGr != 0) {
342            assertCombination
343              ("numlock, shift and altgr", withShiftAndAltGr, map, key, true, true, true);
344        }
345    }
346
347    /**
348     * TODO: comment
349     *
350     * @param string
351     * @param normal
352     * @param characterFor
353     */
354    private void assertCombination(String      type,
355                                   int         expectedChar,
356                                   KeyboardMap map,
357                                   VirtualKey  key,
358                                   boolean     numlock,
359                                   boolean     shift,
360                                   boolean     altgr)
361    {
362        String message = "checked for " + type + ": expected '" + ((char) expectedChar) + "'";
363
364        char retrievedChar;
365        try {
366            retrievedChar = map.getCharacterFor(key, numlock, shift, altgr, false);
367        }
368        catch (IllegalArgumentException e) {
369            fail("no character found. " + message);
370            return;
371        }
372
373        message += " but got '" + retrievedChar + "'";
374
375        assertEquals(message, expectedChar, retrievedChar);
376    }
377
378    /**
379     * TODO: comment
380     *
381     * @param string
382     * @param normal
383     * @param characterFor
384     */
385    private void assertInvalidCombination(String      type,
386                                          int         expectedChar,
387                                          KeyboardMap map,
388                                          VirtualKey  key,
389                                          boolean     numlock,
390                                          boolean     shift,
391                                          boolean     altgr)
392    {
393        char retrievedChar;
394        try {
395            retrievedChar = map.getCharacterFor(key, numlock, shift, altgr, false);
396            assertEquals(Character.UNASSIGNED, retrievedChar);
397        }
398        catch (IllegalArgumentException e) {
399            // this is ok and checked for
400        }
401
402    }
403
404}
Note: See TracBrowser for help on using the repository browser.