1 7 8 package org.jdesktop.swing.actions; 9 10 import java.awt.event.ActionEvent ; 11 import java.awt.event.ItemEvent ; 12 import java.beans.Statement ; 13 import java.util.Iterator ; 14 15 import javax.swing.Action ; 16 import javax.swing.JButton ; 17 18 import junit.framework.TestCase; 19 20 import org.jdesktop.swing.Application; 21 22 27 public class ActionManagerTest extends TestCase { 28 29 private ActionManager manager; 30 31 protected void setUp() { 33 manager = Application.getInstance().getActionManager(); 34 35 manager.addAction(createBoundAction("simple-command", "Simple", "S")); 37 manager.addAction(createBoundAction("simple2-command", "Simple 2", "2")); 38 manager.addAction(createBoundAction("simple3-command", "Simple 3", "3")); 39 40 manager.addAction(createBoundAction("toggle-command", "Toggle", 42 "T", true)); 43 manager.addAction(createBoundAction("left-command", "Left", "L", true, 45 "position-group")); 46 manager.addAction(createBoundAction("center-command", "Center", "C", true, 47 "position-group")); 48 manager.addAction(createBoundAction("right-command", "Right", "R", true, 49 "position-group")); 50 51 CompositeAction action = ActionFactory.createCompositeAction("composite-command", 53 "Composite", "C"); 54 action.addAction("simple-command"); 55 action.addAction("simple2-command"); 56 manager.addAction(action); 57 58 ServerAction saction = ActionFactory.createServerAction("namefinder-command", 60 "NameFinder", "N"); 61 saction.setURL("http://namefinder.sfbay/NameFinder"); 62 saction.addParam("nfquery", "Mark Davidson"); 63 manager.addAction(saction); 64 65 saction = ActionFactory.createServerAction("server-command", "Google", "G"); 67 saction.setURL("http://www.google.com/search"); 68 saction.addParam("q", "Zaphod+Beeblebrox"); 69 manager.addAction(saction); 70 } 71 72 public BoundAction createBoundAction(String id, String name, 73 String mnemonic) { 74 return createBoundAction(id, name, mnemonic, false); 75 } 76 77 public BoundAction createBoundAction(String id, String name, 78 String mnemonic, boolean toggle) { 79 return createBoundAction(id, name, mnemonic, toggle, null); 80 } 81 82 public BoundAction createBoundAction(String id, String name, 83 String mnemonic, boolean toggle, 84 String group) { 85 return ActionFactory.createBoundAction(id, name, mnemonic, toggle, group); 86 } 87 90 public void testActionTypes() { 91 92 assertTrue(manager.isBoundAction("simple-command")); 93 assertTrue(manager.isBoundAction("simple2-command")); 94 assertTrue(manager.isBoundAction("simple3-command")); 95 96 assertTrue(manager.isBoundAction("toggle-command")); 97 assertTrue(manager.isBoundAction("left-command")); 98 assertTrue(manager.isBoundAction("right-command")); 99 assertTrue(manager.isBoundAction("center-command")); 100 101 assertTrue(manager.isCompositeAction("composite-command")); 102 103 assertTrue(manager.isServerAction("namefinder-command")); 104 assertTrue(manager.isServerAction("server-command")); 105 106 108 assertTrue(!manager.isStateAction("simple-command")); 109 assertTrue(!manager.isStateAction("simple2-command")); 110 assertTrue(!manager.isStateAction("simple3-command")); 111 112 assertTrue(manager.isStateAction("toggle-command")); 113 assertTrue(manager.isStateAction("left-command")); 114 assertTrue(manager.isStateAction("right-command")); 115 assertTrue(manager.isStateAction("center-command")); 116 117 assertTrue(!manager.isStateAction("composite-command")); 118 119 assertTrue(!manager.isStateAction("namefinder-command")); 120 assertTrue(!manager.isStateAction("server-command")); 121 } 122 123 124 128 public void testRegisterMethod() { 129 Controller controller = new Controller(); 130 131 Iterator iter = manager.getActionIDs().iterator(); 133 while (iter.hasNext()) { 134 manager.registerCallback(iter.next(), controller, "action"); 135 } 136 137 Action action; 139 140 java.awt.ItemSelectable dummy = new JButton ("Dummy"); 142 143 iter = manager.getActionIDs().iterator(); 144 while (iter.hasNext()) { 145 controller.reset(); 146 147 Object id = iter.next(); 148 149 action = manager.getAction(id); 150 if (manager.isBoundAction(id)) { 151 if (manager.isStateAction(id)) { 152 ItemEvent evt = new ItemEvent (dummy, 666, "test", 154 ItemEvent.SELECTED); 155 Statement statement = new Statement (action, 156 "itemStateChanged", 157 new Object [] { evt }); 158 try { 159 statement.execute(); 160 } catch (Exception ex) { 161 ex.printStackTrace(); 162 } 163 assertTrue(controller.isInvoked()); 164 } else { 165 action.actionPerformed(new ActionEvent (action, 666, "test")); 167 assertTrue("ERROR: " + manager.getBoundAction(id).toString(), 168 controller.isInvoked()); 169 } 170 171 172 } 173 } 174 } 175 176 180 public void testCompositeAction() { 181 Controller controller = new Controller(); 182 183 manager.registerCallback("simple-command", controller, "doNew"); 184 manager.registerCallback("simple2-command", controller, "doSave"); 185 186 Action action = manager.getAction("composite-command"); 187 action.actionPerformed(new ActionEvent (action, 666, "test")); 188 189 assertTrue("ERROR: Controller was not invoked", controller.isInvoked()); 190 assertTrue("ERROR: Controller should have been invoked twice", 191 controller.getNumInvoked() == 2); 192 } 193 194 216 217 public void testEnabled() { 218 boolean[] values = new boolean[] { true, false, true, true, false, false }; 219 220 Iterator iter; 221 for (int i = 0; i < values.length; i++) { 222 223 iter = manager.getActionIDs().iterator(); 225 while (iter.hasNext()) { 226 manager.setEnabled(iter.next(), values[i]); 227 } 228 229 iter = manager.getActionIDs().iterator(); 230 while (iter.hasNext()) { 231 assertTrue(manager.isEnabled(iter.next()) == values[i]); 232 } 233 } 234 } 235 236 237 public void testSelected() { 238 boolean[] values = new boolean[] { true, false, true, true, false, false }; 239 240 Iterator iter; 241 for (int i = 0; i < values.length; i++) { 242 243 iter = manager.getActionIDs().iterator(); 245 while (iter.hasNext()) { 246 manager.setSelected(iter.next(), values[i]); 247 } 248 249 iter = manager.getActionIDs().iterator(); 250 while (iter.hasNext()) { 251 Object a = iter.next(); 252 if (manager.isStateAction(a)) { 253 assertTrue("Action: " + a + " selected state not " + values[i], 254 manager.isSelected(a) == values[i]); 255 } else { 256 assertFalse(manager.isSelected(a)); 258 } 259 } 260 } 261 } 262 263 264 267 public class Controller { 268 private boolean invoked = false; 269 private int numInvoked = 0; 270 271 public void action() { 272 invoked = true; 273 numInvoked++; 274 } 275 276 public void doNew() { 277 action(); 278 } 279 280 public void doSave() { 281 action(); 282 } 283 284 public void action(boolean state) { 285 action(); 286 } 287 288 public void reset() { 289 invoked = false; 290 numInvoked = 0; 291 } 292 293 public int getNumInvoked() { 294 return numInvoked; 295 } 296 297 public boolean isInvoked() { 298 return invoked; 299 } 300 } 301 } 302 | Popular Tags |