1 19 20 package org.netbeans.core; 21 22 import java.awt.event.ActionEvent ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.util.*; 26 import javax.swing.AbstractAction ; 27 import javax.swing.Action ; 28 import javax.swing.KeyStroke ; 29 import javax.swing.text.Keymap ; 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 41 public class NbKeymapTest extends NbTestCase { 42 public NbKeymapTest(String name) { 43 super(name); 44 } 45 46 protected boolean runInEQ () { 47 return true; 48 } 49 50 public void testBasicFunctionality() throws Exception { 51 Keymap km = new NbKeymap(); 52 Action a1 = new DummyAction("a1"); 53 Action a2 = new DummyAction("a2"); 54 Action d = new DummyAction("d"); 55 KeyStroke k1 = KeyStroke.getKeyStroke("X"); 56 KeyStroke 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 { 97 NbKeymap km = new NbKeymap(); 98 O o = new O(); 99 km.addObserver(o); 100 assertFalse(o.changed); 101 Action a1 = new DummyAction("a1"); 102 Action a2 = new DummyAction("a2"); 103 KeyStroke 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 { 115 Keymap km = new NbKeymap(); 116 Action a1 = new DummyAction("a1"); 117 Action a2 = new DummyAction("a2"); 118 KeyStroke k1 = KeyStroke.getKeyStroke("X"); 119 KeyStroke 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 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 { 143 NbKeymap km = new NbKeymap(); 144 O o = new O(); 145 km.addObserver(o); 146 Action a1 = new DummyAction("a1"); 147 Action a2 = new DummyAction("a2"); 148 Action a3 = new DummyAction("a3"); 149 KeyStroke k1 = KeyStroke.getKeyStroke("X"); 150 KeyStroke 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 { 175 private final String name; 176 public DummyAction(String name) { 177 this.name = name; 178 } 179 public void actionPerformed(ActionEvent e) {} 180 public String 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 arg) { 188 changed = true; 189 } 190 } 191 192 private static final class AccL implements PropertyChangeListener { 193 public boolean changed = false; 194 public void propertyChange(PropertyChangeEvent evt) { 195 if (Action.ACCELERATOR_KEY.equals(evt.getPropertyName())) { 196 changed = true; 197 } 198 } 199 } 200 201 } 202 | Popular Tags |