001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core;
043:
044: import java.awt.event.ActionEvent;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.util.*;
048: import javax.swing.AbstractAction;
049: import javax.swing.Action;
050: import javax.swing.KeyStroke;
051: import javax.swing.text.Keymap;
052: import org.netbeans.junit.*;
053: import org.openide.filesystems.*;
054:
055: /** Test NbKeymap.
056: * @author Jesse Glick
057: * @see "#30455" */
058: public class NbKeymapTest extends NbTestCase {
059: public NbKeymapTest(String name) {
060: super (name);
061: }
062:
063: protected boolean runInEQ() {
064: return true;
065: }
066:
067: public void testBasicFunctionality() throws Exception {
068: Keymap km = new NbKeymap();
069: Action a1 = new DummyAction("a1");
070: Action a2 = new DummyAction("a2");
071: Action d = new DummyAction("d");
072: KeyStroke k1 = KeyStroke.getKeyStroke("X");
073: KeyStroke k2 = KeyStroke.getKeyStroke("Y");
074: assertFalse(k1.equals(k2));
075: assertNull(km.getAction(k1));
076: assertNull(km.getAction(k2));
077: assertEquals(Collections.EMPTY_LIST, Arrays.asList(km
078: .getBoundActions()));
079: assertEquals(Collections.EMPTY_LIST, Arrays.asList(km
080: .getBoundKeyStrokes()));
081: assertNull(km.getDefaultAction());
082: km.setDefaultAction(d);
083: assertEquals(d, km.getDefaultAction());
084: km.addActionForKeyStroke(k1, a1);
085: assertEquals(a1, km.getAction(k1));
086: assertTrue(km.isLocallyDefined(k1));
087: assertEquals(null, km.getAction(k2));
088: assertEquals(Collections.singletonList(a1), Arrays.asList(km
089: .getBoundActions()));
090: assertEquals(Collections.singletonList(k1), Arrays.asList(km
091: .getBoundKeyStrokes()));
092: km.addActionForKeyStroke(k2, a2);
093: assertEquals(a1, km.getAction(k1));
094: assertEquals(a2, km.getAction(k2));
095: assertEquals(2, km.getBoundActions().length);
096: assertEquals(2, km.getBoundKeyStrokes().length);
097: km.addActionForKeyStroke(k1, d);
098: assertEquals(d, km.getAction(k1));
099: assertEquals(a2, km.getAction(k2));
100: assertEquals(2, km.getBoundActions().length);
101: assertEquals(2, km.getBoundKeyStrokes().length);
102: assertEquals(Collections.EMPTY_LIST, Arrays.asList(km
103: .getKeyStrokesForAction(a1)));
104: assertEquals(Collections.singletonList(k2), Arrays.asList(km
105: .getKeyStrokesForAction(a2)));
106: assertEquals(Collections.singletonList(k1), Arrays.asList(km
107: .getKeyStrokesForAction(d)));
108: km.removeKeyStrokeBinding(k2);
109: assertEquals(d, km.getAction(k1));
110: assertNull(km.getAction(k2));
111: assertEquals(Collections.singletonList(d), Arrays.asList(km
112: .getBoundActions()));
113: assertEquals(Collections.singletonList(k1), Arrays.asList(km
114: .getBoundKeyStrokes()));
115: km.removeBindings();
116: assertNull(km.getAction(k1));
117: assertNull(km.getAction(k2));
118: assertEquals(Collections.EMPTY_LIST, Arrays.asList(km
119: .getBoundActions()));
120: assertEquals(Collections.EMPTY_LIST, Arrays.asList(km
121: .getBoundKeyStrokes()));
122: }
123:
124: public void testObservability() throws Exception {
125: NbKeymap km = new NbKeymap();
126: O o = new O();
127: km.addObserver(o);
128: assertFalse(o.changed);
129: Action a1 = new DummyAction("a1");
130: Action a2 = new DummyAction("a2");
131: KeyStroke k1 = KeyStroke.getKeyStroke("X");
132: km.addActionForKeyStroke(k1, a1);
133: assertTrue(o.changed);
134: o.changed = false;
135: km.addActionForKeyStroke(k1, a2);
136: assertTrue(o.changed);
137: o.changed = false;
138: km.removeKeyStrokeBinding(k1);
139: assertTrue(o.changed);
140: }
141:
142: public void testAcceleratorMapping() throws Exception {
143: Keymap km = new NbKeymap();
144: Action a1 = new DummyAction("a1");
145: Action a2 = new DummyAction("a2");
146: KeyStroke k1 = KeyStroke.getKeyStroke("X");
147: KeyStroke k2 = KeyStroke.getKeyStroke("Y");
148: assertNull(a1.getValue(Action.ACCELERATOR_KEY));
149: assertNull(a2.getValue(Action.ACCELERATOR_KEY));
150: AccL l = new AccL();
151: a1.addPropertyChangeListener(l);
152: assertFalse(l.changed);
153: km.addActionForKeyStroke(k1, a1);
154: assertEquals(k1, a1.getValue(Action.ACCELERATOR_KEY));
155: assertTrue(l.changed);
156: l.changed = false;
157: km.addActionForKeyStroke(k2, a2);
158: assertEquals(k2, a2.getValue(Action.ACCELERATOR_KEY));
159: km.addActionForKeyStroke(k2, a1);
160: Object acc = a1.getValue(Action.ACCELERATOR_KEY);
161: assertTrue(acc == k1 || acc == k2);
162: assertNull(a2.getValue(Action.ACCELERATOR_KEY));
163: km.removeKeyStrokeBinding(k1);
164: assertEquals(k2, a1.getValue(Action.ACCELERATOR_KEY));
165: km.removeKeyStrokeBinding(k2);
166: assertNull(a1.getValue(Action.ACCELERATOR_KEY));
167: assertTrue(l.changed);
168: }
169:
170: public void testAddActionForKeyStrokeMap() throws Exception {
171: NbKeymap km = new NbKeymap();
172: O o = new O();
173: km.addObserver(o);
174: Action a1 = new DummyAction("a1");
175: Action a2 = new DummyAction("a2");
176: Action a3 = new DummyAction("a3");
177: KeyStroke k1 = KeyStroke.getKeyStroke("X");
178: KeyStroke k2 = KeyStroke.getKeyStroke("Y");
179: Map<KeyStroke, Action> m = new HashMap<KeyStroke, Action>();
180: m.put(k1, a1);
181: m.put(k2, a2);
182: km.addActionForKeyStrokeMap(m);
183: assertTrue(o.changed);
184: assertEquals(a1, km.getAction(k1));
185: assertEquals(a2, km.getAction(k2));
186: assertEquals(k1, a1.getValue(Action.ACCELERATOR_KEY));
187: assertEquals(k2, a2.getValue(Action.ACCELERATOR_KEY));
188: assertEquals(2, km.getBoundActions().length);
189: assertEquals(2, km.getBoundKeyStrokes().length);
190: km.removeBindings();
191: km.addActionForKeyStroke(k1, a3);
192: km.addActionForKeyStrokeMap(m);
193: assertEquals(a1, km.getAction(k1));
194: assertEquals(a2, km.getAction(k2));
195: assertEquals(k1, a1.getValue(Action.ACCELERATOR_KEY));
196: assertEquals(k2, a2.getValue(Action.ACCELERATOR_KEY));
197: assertNull(a3.getValue(Action.ACCELERATOR_KEY));
198: assertEquals(2, km.getBoundActions().length);
199: assertEquals(2, km.getBoundKeyStrokes().length);
200: }
201:
202: private static final class DummyAction extends AbstractAction {
203: private final String name;
204:
205: public DummyAction(String name) {
206: this .name = name;
207: }
208:
209: public void actionPerformed(ActionEvent e) {
210: }
211:
212: public String toString() {
213: return "DummyAction[" + name + "]";
214: }
215: }
216:
217: private static final class O implements Observer {
218: public boolean changed = false;
219:
220: public void update(Observable o, Object arg) {
221: changed = true;
222: }
223: }
224:
225: private static final class AccL implements PropertyChangeListener {
226: public boolean changed = false;
227:
228: public void propertyChange(PropertyChangeEvent evt) {
229: if (Action.ACCELERATOR_KEY.equals(evt.getPropertyName())) {
230: changed = true;
231: }
232: }
233: }
234:
235: }
|