KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > actions > ActionManagerTest


1 /*
2  * $Id: ActionManagerTest.java,v 1.3 2005/01/26 20:01:00 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.actions;
9
10 import java.awt.event.ActionEvent JavaDoc;
11 import java.awt.event.ItemEvent JavaDoc;
12 import java.beans.Statement JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 import javax.swing.Action JavaDoc;
16 import javax.swing.JButton JavaDoc;
17
18 import junit.framework.TestCase;
19
20 import org.jdesktop.swing.Application;
21
22 /**
23  * Unit test driver for the ActionManager
24  *
25  * TODO: Should test TargetableActions
26  */

27 public class ActionManagerTest extends TestCase {
28
29     private ActionManager manager;
30
31     // TODO: Add more attributes which represent actions and types.
32
protected void setUp() {
33         manager = Application.getInstance().getActionManager();
34
35         // Simple commands
36
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         // Toggle action
41
manager.addAction(createBoundAction("toggle-command", "Toggle",
42                                             "T", true));
43         // More toggle actions for a group
44
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         // Composite action
52
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         // Server action
59
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         // XXX This doesn't work since google doesn't allow this.
66
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 JavaDoc id, String JavaDoc name,
73                                          String JavaDoc mnemonic) {
74         return createBoundAction(id, name, mnemonic, false);
75     }
76
77     public BoundAction createBoundAction(String JavaDoc id, String JavaDoc name,
78                                          String JavaDoc mnemonic, boolean toggle) {
79         return createBoundAction(id, name, mnemonic, toggle, null);
80     }
81
82     public BoundAction createBoundAction(String JavaDoc id, String JavaDoc name,
83                                          String JavaDoc mnemonic, boolean toggle,
84                                          String JavaDoc group) {
85         return ActionFactory.createBoundAction(id, name, mnemonic, toggle, group);
86     }
87     /**
88      * Test to see if the types of actions that are created map correctly.
89      */

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         // state types
107

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     /**
125      * A test which registers all the actions with a controller,
126      * invokes the actions to see if the registration was correct.
127      */

128     public void testRegisterMethod() {
129         Controller controller = new Controller();
130
131         // Register the action on the controller.
132
Iterator JavaDoc iter = manager.getActionIDs().iterator();
133         while (iter.hasNext()) {
134             manager.registerCallback(iter.next(), controller, "action");
135         }
136
137         // Invoke all the actions.
138
Action JavaDoc action;
139
140         // dummy ItemSelectable used for forging ItemEvents.
141
java.awt.ItemSelectable JavaDoc dummy = new JButton JavaDoc("Dummy");
142
143         iter = manager.getActionIDs().iterator();
144         while (iter.hasNext()) {
145             controller.reset();
146
147             Object JavaDoc id = iter.next();
148
149             action = manager.getAction(id);
150             if (manager.isBoundAction(id)) {
151                 if (manager.isStateAction(id)) {
152                     // Use reflection to fake the ItemEvent.
153
ItemEvent JavaDoc evt = new ItemEvent JavaDoc(dummy, 666, "test",
154                                                   ItemEvent.SELECTED);
155                     Statement JavaDoc statement = new Statement JavaDoc(action,
156                                                         "itemStateChanged",
157                                                         new Object JavaDoc[] { evt });
158                     try {
159                         statement.execute();
160                     } catch (Exception JavaDoc ex) {
161                         ex.printStackTrace();
162                     }
163                     assertTrue(controller.isInvoked());
164                 } else {
165                     // Simple command action.
166
action.actionPerformed(new ActionEvent JavaDoc(action, 666, "test"));
167                     assertTrue("ERROR: " + manager.getBoundAction(id).toString(),
168                                controller.isInvoked());
169                 }
170
171
172             }
173         }
174     }
175
176     /**
177      * Test the composite action. Two simple commands have registered methods.
178      * these methods should be executed in the composite action invokation.
179      */

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 JavaDoc action = manager.getAction("composite-command");
187         action.actionPerformed(new ActionEvent JavaDoc(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     /**
195      * Test the server action. The server action should send an http post.
196      * with the params and not throw an exception.
197      *
198      * TODO: It's difficult to test the server action in a firewall/non-controlled
199      * network environment. Enable this test when working specifically with ServerActions.
200     public void testServerAction() {
201         ServerAction action = manager.getServerAction("namefinder-command");
202         try {
203             // Determine if we are behind a firewall. Set
204             // set the web proxy if we are
205             URL url = new URL(action.getURL());
206             URLConnection uc = url.openConnection();
207             uc.connect();
208         } catch (Exception ex) {
209             // Set proxy since we are behind the firewall.
210             System.setProperty("http.proxyHost", "scaweb1.sfbay");
211             System.setProperty("http.proxyPort", "8080");
212         }
213         action.actionPerformed(new ActionEvent(action, 666, "test"));
214     }
215     */

216
217     public void testEnabled() {
218         boolean[] values = new boolean[] { true, false, true, true, false, false };
219
220         Iterator JavaDoc iter;
221         for (int i = 0; i < values.length; i++) {
222
223             // Test for actions enabled by disabling actions.
224
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 JavaDoc iter;
241         for (int i = 0; i < values.length; i++) {
242
243             // Test for actions enabled by disabling actions.
244
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 JavaDoc 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                     // Non StateActions will always return false.
257
assertFalse(manager.isSelected(a));
258                 }
259             }
260         }
261     }
262
263
264     /**
265      * A simple controller callback for ActionManager registration test.
266      */

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