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

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