KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > config > KeyStrokeOptionTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.config;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37 import edu.rice.cs.util.newjvm.ExecJVM;
38
39 import javax.swing.*;
40 import java.awt.event.InputEvent JavaDoc;
41 import java.awt.event.KeyEvent JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.util.Locale JavaDoc;
44
45 /** Class according to the JUnit protocol. Tests the proper functionality of the class KeyStrokeOption.
46  * @version $Id: KeyStrokeOptionTest.java 4026 2006-10-31 15:50:16Z rcartwright $
47  */

48 public final class KeyStrokeOptionTest extends DrJavaTestCase {
49   /** @param name The name of this test case. */
50   public KeyStrokeOptionTest(String JavaDoc name) { super(name); }
51
52   public void testGetName() {
53     KeyStrokeOption io1 = new KeyStrokeOption("indent_size",null);
54     KeyStrokeOption io2 = new KeyStrokeOption("max_files",null);
55
56     assertEquals("indent_size", io1.getName());
57     assertEquals("max_files", io2.getName());
58   }
59
60   public void testParse() {
61     KeyStrokeOption io = new KeyStrokeOption("max_files",null);
62     assertEquals(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_MASK),
63                  io.parse("ctrl ENTER"));
64     assertEquals(KeyStrokeOption.NULL_KEYSTROKE,
65                  io.parse("<none>"));
66     assertEquals(KeyStroke.getKeyStroke(KeyEvent.VK_NUM_LOCK, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true),
67                  io.parse("alt shift released NUM_LOCK"));
68     assertEquals(KeyStroke.getKeyStroke(KeyEvent.VK_COMMA, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, false),
69                  io.parse("alt shift COMMA"));
70     assertEquals(KeyStroke.getKeyStroke('%'),
71                  io.parse("typed %"));
72     assertEquals(KeyStroke.getKeyStroke(new Character JavaDoc('%'), InputEvent.ALT_MASK | InputEvent.CTRL_MASK),
73                  io.parse("ctrl alt typed %"));
74
75     try { io.parse("true"); fail(); }
76     catch (IllegalArgumentException JavaDoc e) { }
77
78     try { io.parse(".33"); fail(); }
79     catch (IllegalArgumentException JavaDoc e) { }
80
81     try { io.parse("Alt Z"); fail(); }
82     catch (IllegalArgumentException JavaDoc e) { }
83
84     try { io.parse("ctrl alt shift typed F1"); fail(); }
85     catch (IllegalArgumentException JavaDoc e) { }
86   }
87
88   /** Test the format method by comparing a KeyStroke object to itself after it has been formatted to a string and
89    * parsed back into a KeyStroke object. We cannot compare strings because format always puts the modifiers in the
90    * same order which could be a different order than the user specifies.
91    */

92   public void testFormat() {
93     KeyStrokeOption io = new KeyStrokeOption("max_files",null);
94     KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.ALT_MASK | InputEvent.META_MASK);
95     assertEquals(ks, io.parse(io.format(ks)));
96     ks = KeyStroke.getKeyStroke(KeyEvent.VK_NUMBER_SIGN, InputEvent.ALT_MASK | InputEvent.CTRL_MASK);
97     assertEquals(ks, io.parse(io.format(ks)));
98    
99     ks = KeyStroke.getKeyStroke(new Character JavaDoc('!'), InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK);
100     assertEquals(ks, io.parse(io.format(ks)));
101     ks = KeyStroke.getKeyStroke('!');
102     assertEquals(ks, io.parse(io.format(ks)));
103     ks = KeyStroke.getKeyStroke(KeyEvent.VK_F10, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
104     assertEquals(ks, io.parse(io.format(ks)));
105   }
106
107   /** Tests that key strokes are output in a parseable format even in foreign locales. The test must be run in a
108     * separate JVM, because once the locale is set, it cannot be set back. (If someone can figure out how
109     * to effectively set it back, feel free to remove this hack!)
110     */

111   public void testLocaleSpecificFormat() throws IOException JavaDoc, InterruptedException JavaDoc {
112     String JavaDoc className = "edu.rice.cs.drjava.config.KeyStrokeOptionTest";
113     String JavaDoc[] args = new String JavaDoc[0];
114
115     Process JavaDoc process = ExecJVM.runJVMPropagateClassPath(className, args, FileOption.NULL_FILE);
116     int status = process.waitFor();
117     assertEquals("Local specific keystroke test failed!", 0, status);
118   }
119
120   /** Main method to be called by testLocalSpecificFormat. Runs in a new JVM so as not to affect the locale of other
121    * tests.
122    */

123   public static void main(String JavaDoc[] args) {
124     // Set to German, which has different words for ctrl and shift
125
Locale.setDefault(Locale.GERMAN);
126
127     KeyStrokeOption io = new KeyStrokeOption("test",null);
128     KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.ALT_MASK | InputEvent.CTRL_MASK |
129                                           InputEvent.SHIFT_MASK | InputEvent.META_MASK);
130     String JavaDoc s = io.format(ks);
131     // Test alt/option
132
if ((s.indexOf("alt") == -1) && (s.indexOf("option") == -1)) System.exit(1);
133
134     // Test ctrl
135
if (s.indexOf("ctrl") == -1) System.exit(2);
136     
137     // Test shift
138
if (s.indexOf("shift") == -1) System.exit(3);
139
140     // Test meta
141
if ((s.indexOf("meta") == -1) && (s.indexOf("command") == -1)) System.exit(4);
142
143     // Ok, so exit cleanly
144
System.exit(0);
145   }
146 }
147
Popular Tags