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

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