KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > NbKeymapTest


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.core;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.util.*;
26 import javax.swing.AbstractAction JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import javax.swing.KeyStroke JavaDoc;
29 import javax.swing.text.Keymap JavaDoc;
30 import org.netbeans.junit.*;
31 import junit.textui.TestRunner;
32 import org.openide.cookies.InstanceCookie;
33 import org.openide.filesystems.*;
34 import org.openide.loaders.DataFolder;
35 import org.openide.loaders.DataObject;
36 import org.openide.loaders.InstanceDataObject;
37
38 /** Test NbKeymap.
39  * @author Jesse Glick
40  * @see "#30455" */

41 public class NbKeymapTest extends NbTestCase {
42     public NbKeymapTest(String JavaDoc name) {
43         super(name);
44     }
45     
46     protected boolean runInEQ () {
47         return true;
48     }
49     
50     public void testBasicFunctionality() throws Exception JavaDoc {
51         Keymap JavaDoc km = new NbKeymap();
52         Action JavaDoc a1 = new DummyAction("a1");
53         Action JavaDoc a2 = new DummyAction("a2");
54         Action JavaDoc d = new DummyAction("d");
55         KeyStroke JavaDoc k1 = KeyStroke.getKeyStroke("X");
56         KeyStroke JavaDoc k2 = KeyStroke.getKeyStroke("Y");
57         assertFalse(k1.equals(k2));
58         assertNull(km.getAction(k1));
59         assertNull(km.getAction(k2));
60         assertEquals(Collections.EMPTY_LIST, Arrays.asList(km.getBoundActions()));
61         assertEquals(Collections.EMPTY_LIST, Arrays.asList(km.getBoundKeyStrokes()));
62         assertNull(km.getDefaultAction());
63         km.setDefaultAction(d);
64         assertEquals(d, km.getDefaultAction());
65         km.addActionForKeyStroke(k1, a1);
66         assertEquals(a1, km.getAction(k1));
67         assertTrue(km.isLocallyDefined(k1));
68         assertEquals(null, km.getAction(k2));
69         assertEquals(Collections.singletonList(a1), Arrays.asList(km.getBoundActions()));
70         assertEquals(Collections.singletonList(k1), Arrays.asList(km.getBoundKeyStrokes()));
71         km.addActionForKeyStroke(k2, a2);
72         assertEquals(a1, km.getAction(k1));
73         assertEquals(a2, km.getAction(k2));
74         assertEquals(2, km.getBoundActions().length);
75         assertEquals(2, km.getBoundKeyStrokes().length);
76         km.addActionForKeyStroke(k1, d);
77         assertEquals(d, km.getAction(k1));
78         assertEquals(a2, km.getAction(k2));
79         assertEquals(2, km.getBoundActions().length);
80         assertEquals(2, km.getBoundKeyStrokes().length);
81         assertEquals(Collections.EMPTY_LIST, Arrays.asList(km.getKeyStrokesForAction(a1)));
82         assertEquals(Collections.singletonList(k2), Arrays.asList(km.getKeyStrokesForAction(a2)));
83         assertEquals(Collections.singletonList(k1), Arrays.asList(km.getKeyStrokesForAction(d)));
84         km.removeKeyStrokeBinding(k2);
85         assertEquals(d, km.getAction(k1));
86         assertNull(km.getAction(k2));
87         assertEquals(Collections.singletonList(d), Arrays.asList(km.getBoundActions()));
88         assertEquals(Collections.singletonList(k1), Arrays.asList(km.getBoundKeyStrokes()));
89         km.removeBindings();
90         assertNull(km.getAction(k1));
91         assertNull(km.getAction(k2));
92         assertEquals(Collections.EMPTY_LIST, Arrays.asList(km.getBoundActions()));
93         assertEquals(Collections.EMPTY_LIST, Arrays.asList(km.getBoundKeyStrokes()));
94     }
95     
96     public void testObservability() throws Exception JavaDoc {
97         NbKeymap km = new NbKeymap();
98         O o = new O();
99         km.addObserver(o);
100         assertFalse(o.changed);
101         Action JavaDoc a1 = new DummyAction("a1");
102         Action JavaDoc a2 = new DummyAction("a2");
103         KeyStroke JavaDoc k1 = KeyStroke.getKeyStroke("X");
104         km.addActionForKeyStroke(k1, a1);
105         assertTrue(o.changed);
106         o.changed = false;
107         km.addActionForKeyStroke(k1, a2);
108         assertTrue(o.changed);
109         o.changed = false;
110         km.removeKeyStrokeBinding(k1);
111         assertTrue(o.changed);
112     }
113     
114     public void testAcceleratorMapping() throws Exception JavaDoc {
115         Keymap JavaDoc km = new NbKeymap();
116         Action JavaDoc a1 = new DummyAction("a1");
117         Action JavaDoc a2 = new DummyAction("a2");
118         KeyStroke JavaDoc k1 = KeyStroke.getKeyStroke("X");
119         KeyStroke JavaDoc k2 = KeyStroke.getKeyStroke("Y");
120         assertNull(a1.getValue(Action.ACCELERATOR_KEY));
121         assertNull(a2.getValue(Action.ACCELERATOR_KEY));
122         AccL l = new AccL();
123         a1.addPropertyChangeListener(l);
124         assertFalse(l.changed);
125         km.addActionForKeyStroke(k1, a1);
126         assertEquals(k1, a1.getValue(Action.ACCELERATOR_KEY));
127         assertTrue(l.changed);
128         l.changed = false;
129         km.addActionForKeyStroke(k2, a2);
130         assertEquals(k2, a2.getValue(Action.ACCELERATOR_KEY));
131         km.addActionForKeyStroke(k2, a1);
132         Object JavaDoc acc = a1.getValue(Action.ACCELERATOR_KEY);
133         assertTrue(acc == k1 || acc == k2);
134         assertNull(a2.getValue(Action.ACCELERATOR_KEY));
135         km.removeKeyStrokeBinding(k1);
136         assertEquals(k2, a1.getValue(Action.ACCELERATOR_KEY));
137         km.removeKeyStrokeBinding(k2);
138         assertNull(a1.getValue(Action.ACCELERATOR_KEY));
139         assertTrue(l.changed);
140     }
141     
142     public void testAddActionForKeyStrokeMap() throws Exception JavaDoc {
143         NbKeymap km = new NbKeymap();
144         O o = new O();
145         km.addObserver(o);
146         Action JavaDoc a1 = new DummyAction("a1");
147         Action JavaDoc a2 = new DummyAction("a2");
148         Action JavaDoc a3 = new DummyAction("a3");
149         KeyStroke JavaDoc k1 = KeyStroke.getKeyStroke("X");
150         KeyStroke JavaDoc k2 = KeyStroke.getKeyStroke("Y");
151         Map m = new HashMap();
152         m.put(k1, a1);
153         m.put(k2, a2);
154         km.addActionForKeyStrokeMap(m);
155         assertTrue(o.changed);
156         assertEquals(a1, km.getAction(k1));
157         assertEquals(a2, km.getAction(k2));
158         assertEquals(k1, a1.getValue(Action.ACCELERATOR_KEY));
159         assertEquals(k2, a2.getValue(Action.ACCELERATOR_KEY));
160         assertEquals(2, km.getBoundActions().length);
161         assertEquals(2, km.getBoundKeyStrokes().length);
162         km.removeBindings();
163         km.addActionForKeyStroke(k1, a3);
164         km.addActionForKeyStrokeMap(m);
165         assertEquals(a1, km.getAction(k1));
166         assertEquals(a2, km.getAction(k2));
167         assertEquals(k1, a1.getValue(Action.ACCELERATOR_KEY));
168         assertEquals(k2, a2.getValue(Action.ACCELERATOR_KEY));
169         assertNull(a3.getValue(Action.ACCELERATOR_KEY));
170         assertEquals(2, km.getBoundActions().length);
171         assertEquals(2, km.getBoundKeyStrokes().length);
172     }
173     
174     private static final class DummyAction extends AbstractAction JavaDoc {
175         private final String JavaDoc name;
176         public DummyAction(String JavaDoc name) {
177             this.name = name;
178         }
179         public void actionPerformed(ActionEvent JavaDoc e) {}
180         public String JavaDoc toString() {
181             return "DummyAction[" + name + "]";
182         }
183     }
184     
185     private static final class O implements Observer {
186         public boolean changed = false;
187         public void update(Observable o, Object JavaDoc arg) {
188             changed = true;
189         }
190     }
191     
192     private static final class AccL implements PropertyChangeListener JavaDoc {
193         public boolean changed = false;
194         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
195             if (Action.ACCELERATOR_KEY.equals(evt.getPropertyName())) {
196                 changed = true;
197             }
198         }
199     }
200     
201 }
202
Popular Tags