001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: * Created on 16.12.2004
021:
022: */package javax.swing;
023:
024: import java.awt.event.InputEvent;
025: import java.awt.event.KeyEvent;
026:
027: public class KeyStrokeTest extends SwingTestCase {
028: public static void main(final String[] args) {
029: junit.textui.TestRunner.run(KeyStrokeTest.class);
030: }
031:
032: /*
033: * Class under test for KeyStroke getKeyStroke(String)
034: */
035: public void testGetKeyStrokeString() {
036: KeyStroke keyStroke = KeyStroke.getKeyStroke("INSERT");
037: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
038: keyStroke.getKeyChar());
039: assertEquals("keyCode's correct", KeyEvent.VK_INSERT, keyStroke
040: .getKeyCode());
041: assertEquals("modifiers are correct", 0, keyStroke
042: .getModifiers());
043: assertFalse("onKeyRelease is correct", keyStroke
044: .isOnKeyRelease());
045: keyStroke = KeyStroke.getKeyStroke("control DELETE");
046: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
047: keyStroke.getKeyChar());
048: assertEquals("keyCode's correct", KeyEvent.VK_DELETE, keyStroke
049: .getKeyCode());
050: assertTrue(
051: "modifiers are correct",
052: (keyStroke.getModifiers() & InputEvent.CTRL_DOWN_MASK) != 0);
053: assertFalse("onKeyRelease is correct", keyStroke
054: .isOnKeyRelease());
055: keyStroke = KeyStroke.getKeyStroke("alt shift X");
056: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
057: keyStroke.getKeyChar());
058: assertEquals("keyCode's correct", KeyEvent.VK_X, keyStroke
059: .getKeyCode());
060: assertTrue(
061: "modifiers are correct",
062: (keyStroke.getModifiers() & InputEvent.ALT_DOWN_MASK) != 0);
063: assertTrue(
064: "modifiers are correct",
065: (keyStroke.getModifiers() & InputEvent.SHIFT_DOWN_MASK) != 0);
066: assertFalse("onKeyRelease is correct", keyStroke
067: .isOnKeyRelease());
068: keyStroke = KeyStroke.getKeyStroke("altGraph X");
069: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
070: keyStroke.getKeyChar());
071: assertEquals("keyCode's correct", KeyEvent.VK_X, keyStroke
072: .getKeyCode());
073: assertTrue(
074: "modifiers are correct",
075: (keyStroke.getModifiers() & InputEvent.ALT_GRAPH_DOWN_MASK) != 0);
076: assertFalse("onKeyRelease is correct", keyStroke
077: .isOnKeyRelease());
078: keyStroke = KeyStroke.getKeyStroke("alt shift released X");
079: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
080: keyStroke.getKeyChar());
081: assertEquals("keyCode's correct", KeyEvent.VK_X, keyStroke
082: .getKeyCode());
083: assertTrue(
084: "modifiers are correct",
085: (keyStroke.getModifiers() & InputEvent.ALT_DOWN_MASK) != 0);
086: assertTrue(
087: "modifiers are correct",
088: (keyStroke.getModifiers() & InputEvent.SHIFT_DOWN_MASK) != 0);
089: assertTrue("onKeyRelease is correct", keyStroke
090: .isOnKeyRelease());
091: keyStroke = KeyStroke.getKeyStroke("typed a");
092: assertEquals("keyChar's correct", 'a', keyStroke.getKeyChar());
093: assertEquals("keyCode's correct", 0, keyStroke.getKeyCode());
094: assertEquals("modifiers are correct", 0, keyStroke
095: .getModifiers());
096: assertFalse("onKeyRelease is correct", keyStroke
097: .isOnKeyRelease());
098: KeyStroke keyStroke1 = KeyStroke.getKeyStroke("typed a");
099: KeyStroke keyStroke2 = KeyStroke.getKeyStroke("typed a");
100: assertTrue("keyStrokes are shared properly",
101: keyStroke1 == keyStroke2);
102: }
103:
104: /*
105: * Class under test for KeyStroke getKeyStroke(Character, int)
106: */
107: public void testGetKeyStrokeCharacterint() {
108: KeyStroke keyStroke = KeyStroke.getKeyStroke(
109: new Character('A'), InputEvent.ALT_DOWN_MASK);
110: assertEquals("keyChar's correct", 'A', keyStroke.getKeyChar());
111: assertEquals("keyCode's correct", 0, keyStroke.getKeyCode());
112: assertTrue(
113: "modifiers are correct",
114: (InputEvent.ALT_DOWN_MASK & keyStroke.getModifiers()) != 0);
115: assertFalse("onKeyRelease is correct", keyStroke
116: .isOnKeyRelease());
117: keyStroke = KeyStroke.getKeyStroke(new Character('t'),
118: InputEvent.CTRL_DOWN_MASK);
119: assertEquals("keyChar's correct", 't', keyStroke.getKeyChar());
120: assertEquals("keyCode's correct", 0, keyStroke.getKeyCode());
121: assertTrue(
122: "modifiers are correct",
123: (InputEvent.CTRL_DOWN_MASK & keyStroke.getModifiers()) != 0);
124: assertFalse("onKeyRelease is correct", keyStroke
125: .isOnKeyRelease());
126: keyStroke = KeyStroke.getKeyStroke(new Character('_'),
127: InputEvent.SHIFT_DOWN_MASK);
128: assertEquals("keyChar's correct", '_', keyStroke.getKeyChar());
129: assertEquals("keyCode's correct", 0, keyStroke.getKeyCode());
130: assertTrue(
131: "modifiers are correct",
132: (InputEvent.SHIFT_DOWN_MASK & keyStroke.getModifiers()) != 0);
133: assertFalse("onKeyRelease is correct", keyStroke
134: .isOnKeyRelease());
135: KeyStroke keyStroke1 = KeyStroke.getKeyStroke(
136: new Character('_'), InputEvent.SHIFT_DOWN_MASK);
137: KeyStroke keyStroke2 = KeyStroke.getKeyStroke(
138: new Character('_'), InputEvent.SHIFT_DOWN_MASK);
139: assertTrue("keyStrokes are shared properly",
140: keyStroke1 == keyStroke2);
141: }
142:
143: public void testGetKeyStrokeForEvent() {
144: JComponent source = new JPanel();
145: KeyEvent event = new KeyEvent(source, KeyEvent.KEY_PRESSED, 0,
146: InputEvent.SHIFT_DOWN_MASK, KeyEvent.VK_B, 'B');
147: KeyStroke keyStroke = KeyStroke.getKeyStrokeForEvent(event);
148: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
149: keyStroke.getKeyChar());
150: assertEquals("keyCode's correct", KeyEvent.VK_B, keyStroke
151: .getKeyCode());
152: assertTrue(
153: "modifiers are correct",
154: (InputEvent.SHIFT_DOWN_MASK & keyStroke.getModifiers()) != 0);
155: assertFalse("onKeyRelease is correct", keyStroke
156: .isOnKeyRelease());
157: event = new KeyEvent(source, KeyEvent.KEY_RELEASED, 0,
158: InputEvent.SHIFT_DOWN_MASK, KeyEvent.VK_C, 'C');
159: keyStroke = KeyStroke.getKeyStrokeForEvent(event);
160: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
161: keyStroke.getKeyChar());
162: assertEquals("keyCode's correct", KeyEvent.VK_C, keyStroke
163: .getKeyCode());
164: assertTrue(
165: "modifiers are correct",
166: (InputEvent.SHIFT_DOWN_MASK & keyStroke.getModifiers()) != 0);
167: assertTrue("onKeyRelease is correct", keyStroke
168: .isOnKeyRelease());
169: event = new KeyEvent(source, KeyEvent.KEY_TYPED, 0,
170: InputEvent.SHIFT_DOWN_MASK, KeyEvent.VK_UNDEFINED, 'T');
171: keyStroke = KeyStroke.getKeyStrokeForEvent(event);
172: assertEquals("keyChar's correct", 'T', keyStroke.getKeyChar());
173: assertEquals("keyCode's correct", KeyEvent.VK_UNDEFINED,
174: keyStroke.getKeyCode());
175: assertTrue(
176: "modifiers are correct",
177: (InputEvent.SHIFT_DOWN_MASK & keyStroke.getModifiers()) != 0);
178: assertFalse("onKeyRelease is correct", keyStroke
179: .isOnKeyRelease());
180: KeyStroke keyStroke1 = KeyStroke.getKeyStrokeForEvent(event);
181: KeyStroke keyStroke2 = KeyStroke.getKeyStrokeForEvent(event);
182: assertTrue("keyStrokes are shared properly",
183: keyStroke1 == keyStroke2);
184: }
185:
186: /*
187: * Class under test for KeyStroke getKeyStroke(int, int, boolean)
188: */
189: public void testGetKeyStrokeintintboolean() {
190: KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_0,
191: InputEvent.ALT_DOWN_MASK, true);
192: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
193: keyStroke.getKeyChar());
194: assertEquals("keyCode's correct", KeyEvent.VK_0, keyStroke
195: .getKeyCode());
196: assertTrue(
197: "modifiers are correct",
198: (InputEvent.ALT_DOWN_MASK & keyStroke.getModifiers()) != 0);
199: assertTrue("onKeyRelease is correct", keyStroke
200: .isOnKeyRelease());
201: keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_A,
202: InputEvent.CTRL_DOWN_MASK, false);
203: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
204: keyStroke.getKeyChar());
205: assertEquals("keyCode's correct", KeyEvent.VK_A, keyStroke
206: .getKeyCode());
207: assertTrue(
208: "modifiers are correct",
209: (InputEvent.CTRL_DOWN_MASK & keyStroke.getModifiers()) != 0);
210: assertFalse("onKeyRelease is correct", keyStroke
211: .isOnKeyRelease());
212: keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_SHIFT,
213: InputEvent.SHIFT_DOWN_MASK, true);
214: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
215: keyStroke.getKeyChar());
216: assertEquals("keyCode's correct", KeyEvent.VK_SHIFT, keyStroke
217: .getKeyCode());
218: assertTrue(
219: "modifiers are correct",
220: (InputEvent.SHIFT_DOWN_MASK & keyStroke.getModifiers()) != 0);
221: assertTrue("onKeyRelease is correct", keyStroke
222: .isOnKeyRelease());
223: KeyStroke keyStroke1 = KeyStroke.getKeyStroke(
224: KeyEvent.VK_SHIFT, InputEvent.SHIFT_DOWN_MASK, true);
225: KeyStroke keyStroke2 = KeyStroke.getKeyStroke(
226: KeyEvent.VK_SHIFT, InputEvent.SHIFT_DOWN_MASK, true);
227: assertTrue("keyStrokes are shared properly",
228: keyStroke1 == keyStroke2);
229: }
230:
231: /*
232: * Class under test for KeyStroke getKeyStroke(int, int)
233: */
234: public void testGetKeyStrokeintint() {
235: KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_0,
236: InputEvent.ALT_DOWN_MASK);
237: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
238: keyStroke.getKeyChar());
239: assertEquals("keyCode's correct", KeyEvent.VK_0, keyStroke
240: .getKeyCode());
241: assertTrue(
242: "modifiers are correct",
243: (InputEvent.ALT_DOWN_MASK & keyStroke.getModifiers()) != 0);
244: assertFalse("onKeyRelease is correct", keyStroke
245: .isOnKeyRelease());
246: keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_A,
247: InputEvent.CTRL_DOWN_MASK);
248: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
249: keyStroke.getKeyChar());
250: assertEquals("keyCode's correct", KeyEvent.VK_A, keyStroke
251: .getKeyCode());
252: assertTrue(
253: "modifiers are correct",
254: (InputEvent.CTRL_DOWN_MASK & keyStroke.getModifiers()) != 0);
255: assertFalse("onKeyRelease is correct", keyStroke
256: .isOnKeyRelease());
257: keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_SHIFT,
258: InputEvent.SHIFT_DOWN_MASK);
259: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
260: keyStroke.getKeyChar());
261: assertEquals("keyCode's correct", KeyEvent.VK_SHIFT, keyStroke
262: .getKeyCode());
263: assertTrue(
264: "modifiers are correct",
265: (InputEvent.SHIFT_DOWN_MASK & keyStroke.getModifiers()) != 0);
266: assertFalse("onKeyRelease is correct", keyStroke
267: .isOnKeyRelease());
268: KeyStroke keyStroke1 = KeyStroke.getKeyStroke(
269: KeyEvent.VK_SHIFT, InputEvent.SHIFT_DOWN_MASK);
270: KeyStroke keyStroke2 = KeyStroke.getKeyStroke(
271: KeyEvent.VK_SHIFT, InputEvent.SHIFT_DOWN_MASK);
272: assertTrue("keyStrokes are shared properly",
273: keyStroke1 == keyStroke2);
274: }
275:
276: /*
277: * Class under test for KeyStroke getKeyStroke(char, boolean)
278: */
279: @SuppressWarnings("deprecation")
280: public void testGetKeyStrokecharboolean() {
281: KeyStroke keyStroke = KeyStroke.getKeyStroke(
282: KeyEvent.CHAR_UNDEFINED, false);
283: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
284: keyStroke.getKeyChar());
285: assertEquals("keyCode's correct", 0, keyStroke.getKeyCode());
286: assertEquals("modifiers are correct", 0, keyStroke
287: .getModifiers());
288: assertFalse("onKeyRelease is correct", keyStroke
289: .isOnKeyRelease());
290: keyStroke = KeyStroke.getKeyStroke('a', true);
291: assertEquals("keyChar's correct", 'a', keyStroke.getKeyChar());
292: assertEquals("keyCode's correct", 0, keyStroke.getKeyCode());
293: assertEquals("modifiers are correct", 0, keyStroke
294: .getModifiers());
295: assertTrue("onKeyRelease is correct", keyStroke
296: .isOnKeyRelease());
297: keyStroke = KeyStroke.getKeyStroke('T', true);
298: assertEquals("keyChar's correct", 'T', keyStroke.getKeyChar());
299: assertEquals("keyCode's correct", 0, keyStroke.getKeyCode());
300: assertEquals("modifiers are correct", 0, keyStroke
301: .getModifiers());
302: assertTrue("onKeyRelease is correct", keyStroke
303: .isOnKeyRelease());
304: }
305:
306: /*
307: * Class under test for KeyStroke getKeyStroke(char)
308: */
309: public void testGetKeyStrokechar() {
310: KeyStroke keyStroke = KeyStroke.getKeyStroke((char) -1);
311: assertEquals("keyChar's correct", KeyEvent.CHAR_UNDEFINED,
312: keyStroke.getKeyChar());
313: assertEquals("keyCode's correct", 0, keyStroke.getKeyCode());
314: assertEquals("modifiers are correct", 0, keyStroke
315: .getModifiers());
316: assertFalse("onKeyRelease is correct", keyStroke
317: .isOnKeyRelease());
318: keyStroke = KeyStroke.getKeyStroke('a');
319: assertEquals("keyChar's correct", 'a', keyStroke.getKeyChar());
320: assertEquals("keyCode's correct", 0, keyStroke.getKeyCode());
321: assertEquals("modifiers are correct", 0, keyStroke
322: .getModifiers());
323: assertFalse("onKeyRelease is correct", keyStroke
324: .isOnKeyRelease());
325: keyStroke = KeyStroke.getKeyStroke('T');
326: assertEquals("keyChar's correct", 'T', keyStroke.getKeyChar());
327: assertEquals("keyCode's correct", 0, keyStroke.getKeyCode());
328: assertEquals("modifiers are correct", 0, keyStroke
329: .getModifiers());
330: assertFalse("onKeyRelease is correct", keyStroke
331: .isOnKeyRelease());
332: KeyStroke keyStroke1 = KeyStroke.getKeyStroke('a');
333: KeyStroke keyStroke2 = KeyStroke.getKeyStroke('a');
334: assertTrue("keyStrokes are shared properly",
335: keyStroke1 == keyStroke2);
336: }
337: }
|