KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > settings > EditorSettingsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.settings;
21
22 import java.awt.event.KeyEvent JavaDoc;
23 import java.io.IOException JavaDoc;
24 import javax.swing.KeyStroke JavaDoc;
25 import org.netbeans.api.editor.settings.MultiKeyBinding;
26 import org.netbeans.junit.NbTestCase;
27
28 /** Testing basic functionality of Editor Settings API
29  *
30  * @author Martin Roskanin
31  */

32 public class EditorSettingsTest extends NbTestCase {
33
34     public EditorSettingsTest(String JavaDoc testName) {
35         super(testName);
36     }
37
38     public void testMultiKeyBindingsEquality() throws IOException JavaDoc{
39         // simple equals test
40
MultiKeyBinding one = new MultiKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK), "actionCTRL+M");
41         MultiKeyBinding two = new MultiKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK), "actionCTRL+M");
42         testEquality(one, two, true);
43         
44         // test keys with one KeyStroke versus key
45
KeyStroke JavaDoc ks [] = new KeyStroke JavaDoc[]{KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK)};
46         one = new MultiKeyBinding(ks, "actionCTRL+M");
47         two = new MultiKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK), "actionCTRL+M");
48         testEquality(one, two, true);
49         
50         // different action bound, should fail
51
one = new MultiKeyBinding(ks, "actionCTRL+M");
52         two = new MultiKeyBinding(
53                 KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK),
54                 "actionCTRL+M_differentAction");
55         testEquality(one, two, false);
56         
57         // simple equals for multikeybings
58
KeyStroke JavaDoc ks2 [] = new KeyStroke JavaDoc[]{KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK),
59                 KeyStroke.getKeyStroke(KeyEvent.VK_F, 0),
60                 KeyStroke.getKeyStroke(KeyEvent.VK_2, 0),
61                 KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.ALT_DOWN_MASK)
62         };
63         KeyStroke JavaDoc ks3 [] = new KeyStroke JavaDoc[]{KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK),
64                 KeyStroke.getKeyStroke(KeyEvent.VK_F, 0),
65                 KeyStroke.getKeyStroke(KeyEvent.VK_2, 0),
66                 KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.ALT_DOWN_MASK)
67         };
68         
69         one = new MultiKeyBinding(ks2, "multiOne");
70         two = new MultiKeyBinding(ks3, "multiOne");
71         testEquality(one, two, true);
72         
73         // testing different sequence (swapping VK_F and VK_2), should fail
74
KeyStroke JavaDoc ks4 [] = new KeyStroke JavaDoc[]{KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK),
75                 KeyStroke.getKeyStroke(KeyEvent.VK_2, 0),
76                 KeyStroke.getKeyStroke(KeyEvent.VK_F, 0),
77                 KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.ALT_DOWN_MASK)
78         };
79         one = new MultiKeyBinding(ks3, "multiOne");
80         two = new MultiKeyBinding(ks4, "multiOne");
81         testEquality(one, two, false);
82         
83         // testing different modifier, should fail
84
KeyStroke JavaDoc ks5 [] = new KeyStroke JavaDoc[]{KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK),
85                 KeyStroke.getKeyStroke(KeyEvent.VK_2, 0),
86                 KeyStroke.getKeyStroke(KeyEvent.VK_F, 0),
87                 KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.SHIFT_DOWN_MASK)
88         };
89         one = new MultiKeyBinding(ks4, "multiOne");
90         two = new MultiKeyBinding(ks5, "multiOne");
91         testEquality(one, two, false);
92         
93         // testing different action, should fail
94
one = new MultiKeyBinding(ks5, "multiOne");
95         two = new MultiKeyBinding(ks5, "multiOne_different");
96         testEquality(one, two, false);
97         
98         // testing action null, should fail
99
boolean failed = false;
100         try {
101             one = new MultiKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK), null);
102         } catch (NullPointerException JavaDoc npe){
103             failed = true;
104         }
105         assertTrue(failed);
106         
107         // testing key null, should fail
108
failed = false;
109         try {
110             one = new MultiKeyBinding((KeyStroke JavaDoc) null, "actionName");
111         } catch (NullPointerException JavaDoc npe){
112             failed = true;
113         }
114         assertTrue(failed);
115
116         // testing keys null, should fail
117
failed = false;
118         try {
119             one = new MultiKeyBinding((KeyStroke JavaDoc[]) null, "actionName");
120         } catch (NullPointerException JavaDoc npe){
121             failed = true;
122         }
123         assertTrue(failed);
124         
125     }
126
127     private void testEquality(MultiKeyBinding m1, MultiKeyBinding m2, boolean equals){
128         assertTrue(equals == m2.equals(m1));
129         assertTrue(equals == m1.equals(m2));
130     }
131 }
132
Popular Tags