1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.richclient.command.config;
17
18 import java.awt.event.ActionEvent;
19 import java.beans.PropertyChangeEvent;
20 import java.beans.PropertyChangeListener;
21
22 import javax.swing.AbstractAction;
23 import javax.swing.AbstractButton;
24 import javax.swing.Action;
25 import javax.swing.Icon;
26 import javax.swing.JButton;
27
28 import junit.framework.TestCase;
29 import org.springframework.richclient.command.AbstractCommand;
30 import org.springframework.richclient.core.DescribedElement;
31 import org.springframework.richclient.image.EmptyIcon;
32
33
34
35
36 public class CommandFaceDescriptorTests extends TestCase {
37
38 private CommandButtonLabelInfo buttonLabelInfo;
39
40 private CommandFaceDescriptor descriptor;
41
42 private TestPropertyChangeListener propertyChangeListener;
43
44 public void testDefaultConstructor() {
45 CommandFaceDescriptor descriptor = new CommandFaceDescriptor();
46
47 assertEquals(CommandButtonLabelInfo.BLANK_BUTTON_LABEL, descriptor.getLabelInfo());
48 assertTrue(descriptor.isBlank());
49 assertEquals(CommandButtonLabelInfo.BLANK_BUTTON_LABEL.getText(), descriptor.getText());
50 assertNull(descriptor.getDescription());
51 assertEquals(CommandButtonIconInfo.BLANK_ICON_INFO, descriptor.getIconInfo());
52 assertNull(descriptor.getCaption());
53 }
54
55 public void testConstructorWithNullButtonLabelInfo() {
56 try {
57 new CommandFaceDescriptor((CommandButtonLabelInfo)null);
58 fail("Should throw IllegalArgumentException");
59 }
60 catch (IllegalArgumentException e) {
61 pass();
62 }
63 }
64
65 public void testConstructorWithEncodedLabel() {
66 CommandFaceDescriptor descriptor = new CommandFaceDescriptor("&Test@ctrl T");
67 assertEquals("Test", descriptor.getText());
68 assertFalse(CommandButtonLabelInfo.BLANK_BUTTON_LABEL.equals(descriptor.getLabelInfo()));
69 assertFalse(descriptor.isBlank());
70 assertNull(descriptor.getDescription());
71 assertNull(descriptor.getCaption());
72 assertEquals(CommandButtonIconInfo.BLANK_ICON_INFO, descriptor.getIconInfo());
73 }
74
75 public void testConstructorWithEmptyEncodedLabel() {
76 CommandFaceDescriptor descriptor = new CommandFaceDescriptor("");
77 assertEquals(CommandButtonLabelInfo.BLANK_BUTTON_LABEL, descriptor.getLabelInfo());
78 }
79
80 public void testConstructorWithEncodedLabelAndIcon() {
81 CommandFaceDescriptor descriptor = new CommandFaceDescriptor("&Test@ctrl T", EmptyIcon.SMALL, "caption");
82
83 assertEquals("Test", descriptor.getText());
84 assertFalse(CommandButtonLabelInfo.BLANK_BUTTON_LABEL.equals(descriptor.getLabelInfo()));
85 assertFalse(descriptor.isBlank());
86 assertNull(descriptor.getDescription());
87 assertEquals("caption", descriptor.getCaption());
88 assertFalse(CommandButtonIconInfo.BLANK_ICON_INFO.equals(descriptor.getIconInfo()));
89 assertEquals(EmptyIcon.SMALL, descriptor.getIconInfo().getIcon());
90 }
91
92 public void testSetDescription() {
93 CommandFaceDescriptor descriptor = new CommandFaceDescriptor("&Test@ctrl T", EmptyIcon.SMALL, "caption");
94 descriptor.setDescription("Long description");
95
96 assertEquals("Long description", descriptor.getDescription());
97 }
98
99 public void testSetCaption() {
100 descriptor.setCaption("new caption");
101 assertTrue(propertyChangeListener.changed);
102 assertEquals(descriptor, propertyChangeListener.source);
103 assertEquals("caption", propertyChangeListener.oldValue);
104 assertEquals("new caption", propertyChangeListener.newValue);
105 assertEquals(DescribedElement.CAPTION_PROPERTY, propertyChangeListener.propertyName);
106
107 propertyChangeListener.reset();
108
109
110 descriptor.setCaption("new caption");
111 assertFalse(propertyChangeListener.changed);
112 }
113
114 public void testSetIconNull() {
115 descriptor.setIcon(null);
116 assertNull(descriptor.getIconInfo().getIcon());
117
118 descriptor.setIconInfo(CommandButtonIconInfo.BLANK_ICON_INFO);
119 descriptor.setIcon(null);
120 assertEquals(CommandButtonIconInfo.BLANK_ICON_INFO, descriptor.getIconInfo());
121 }
122
123 public void testSetIcon() {
124 Icon oldIcon = descriptor.getIcon();
125 descriptor.setIcon(EmptyIcon.LARGE);
126 assertEquals(EmptyIcon.LARGE, descriptor.getIcon());
127
128 assertTrue(propertyChangeListener.changed);
129 assertEquals(descriptor, propertyChangeListener.source);
130 assertEquals(oldIcon, propertyChangeListener.oldValue);
131 assertEquals(descriptor.getIcon(), propertyChangeListener.newValue);
132 assertEquals(CommandFaceDescriptor.ICON_PROPERTY, propertyChangeListener.propertyName);
133
134 propertyChangeListener.reset();
135
136 descriptor.setIcon(EmptyIcon.LARGE);
137 assertFalse(propertyChangeListener.changed);
138 }
139
140 public void testSetLargeIconNull() {
141 descriptor.setLargeIcon(null);
142 assertNull(descriptor.getLargeIconInfo().getIcon());
143
144 descriptor.setLargeIconInfo(CommandButtonIconInfo.BLANK_ICON_INFO);
145 descriptor.setLargeIcon(null);
146 assertEquals(CommandButtonIconInfo.BLANK_ICON_INFO, descriptor.getLargeIconInfo());
147 }
148
149 public void testSetLargeIcon() {
150 Icon oldIcon = descriptor.getLargeIcon();
151 descriptor.setLargeIcon(EmptyIcon.LARGE);
152 assertEquals(EmptyIcon.LARGE, descriptor.getLargeIcon());
153
154 assertTrue(propertyChangeListener.changed);
155 assertEquals(descriptor, propertyChangeListener.source);
156 assertEquals(oldIcon, propertyChangeListener.oldValue);
157 assertEquals(descriptor.getLargeIcon(), propertyChangeListener.newValue);
158 assertEquals(CommandFaceDescriptor.LARGE_ICON_PROPERTY, propertyChangeListener.propertyName);
159
160 propertyChangeListener.reset();
161
162 descriptor.setLargeIcon(EmptyIcon.LARGE);
163 assertFalse(propertyChangeListener.changed);
164 }
165
166 public void testSetNullIconInfo() {
167 descriptor.setIconInfo(null);
168 assertEquals(CommandButtonIconInfo.BLANK_ICON_INFO, descriptor.getIconInfo());
169 }
170
171 public void testSetNullLabelInfo() {
172 descriptor.setLabelInfo(null);
173 assertEquals(CommandButtonLabelInfo.BLANK_BUTTON_LABEL, descriptor.getLabelInfo());
174 }
175
176 public void testConfigureWithConfigurer() {
177 JButton button = new JButton();
178 TestCommandButtonConfigurer configurer = new TestCommandButtonConfigurer();
179 descriptor.configure(button, null, configurer);
180 assertEquals(button, configurer.button);
181 assertEquals(descriptor, configurer.face);
182 }
183
184 public void testConfigureWithNullConfigurerAndNullButton() {
185 try {
186 descriptor.configure(new JButton(), null, null);
187 fail("Should throw IllegalArgumentException");
188 }
189 catch (IllegalArgumentException e) {
190 pass();
191 }
192 try {
193 descriptor.configure(null, null, new TestCommandButtonConfigurer());
194 fail("Should throw IllegalArgumentException");
195 }
196 catch (IllegalArgumentException e) {
197 pass();
198 }
199 }
200
201 private static class TestCommandButtonConfigurer extends DefaultCommandButtonConfigurer implements
202 CommandButtonConfigurer {
203 private CommandFaceDescriptor face;
204
205 private AbstractButton button;
206
207 public void configure(AbstractButton button, AbstractCommand command, CommandFaceDescriptor faceDescriptor) {
208 super.configure(button, command, faceDescriptor);
209 this.face = faceDescriptor;
210 this.button = button;
211 }
212 }
213
214 public void testSetLabelInfoAsText() {
215 CommandButtonLabelInfo oldLabelInfo = descriptor.getLabelInfo();
216 descriptor.setButtonLabelInfo("&Other Test@ctrl O");
217 CommandButtonLabelInfo newLabelInfo = descriptor.getLabelInfo();
218 assertEquals(CommandButtonLabelInfo.valueOf("&Other Test@ctrl O"), newLabelInfo);
219
220 assertTrue(propertyChangeListener.changed);
221 assertEquals(descriptor, propertyChangeListener.source);
222 assertEquals(oldLabelInfo, propertyChangeListener.oldValue);
223 assertEquals(newLabelInfo, propertyChangeListener.newValue);
224 assertEquals(CommandFaceDescriptor.LABEL_INFO_PROPERTY, propertyChangeListener.propertyName);
225
226 propertyChangeListener.reset();
227
228 descriptor.setButtonLabelInfo("&Other Test@ctrl O");
229 assertFalse(propertyChangeListener.changed);
230 }
231
232 public void testSetLabelInfo() {
233 CommandButtonLabelInfo oldLabelInfo = descriptor.getLabelInfo();
234 CommandButtonLabelInfo newLabelInfo = CommandButtonLabelInfo.valueOf("&Other Test@ctrl O");
235 descriptor.setLabelInfo(newLabelInfo);
236 assertEquals(newLabelInfo, descriptor.getLabelInfo());
237
238 assertTrue(propertyChangeListener.changed);
239 assertEquals(descriptor, propertyChangeListener.source);
240 assertEquals(oldLabelInfo, propertyChangeListener.oldValue);
241 assertEquals(newLabelInfo, propertyChangeListener.newValue);
242 assertEquals(CommandFaceDescriptor.LABEL_INFO_PROPERTY, propertyChangeListener.propertyName);
243
244 propertyChangeListener.reset();
245
246 descriptor.setButtonLabelInfo("&Other Test@ctrl O");
247 assertFalse(propertyChangeListener.changed);
248 }
249
250 public void testConfigureNullAction() {
251 try {
252 descriptor.configure(null);
253 fail("Should throw IllegalArgumentException");
254 }
255 catch (IllegalArgumentException e) {
256 pass();
257 }
258 }
259
260 public void testConfigure() {
261 Action action = new AbstractAction() {
262 public void actionPerformed(ActionEvent arg0) {
263 }
264 };
265
266 descriptor.configure(action);
267 assertEquals("name", descriptor.getLabelInfo().getText(), action.getValue(Action.NAME));
268 assertEquals("mnemonic", new Integer(descriptor.getLabelInfo().getMnemonic()), action
269 .getValue(Action.MNEMONIC_KEY));
270 assertEquals("accelerator", descriptor.getLabelInfo().getAccelerator(), action
271 .getValue(Action.ACCELERATOR_KEY));
272 assertEquals("icon", descriptor.getIconInfo().getIcon(), action.getValue(Action.SMALL_ICON));
273 assertEquals("caption", descriptor.getCaption(), action.getValue(Action.SHORT_DESCRIPTION));
274 assertEquals("description", descriptor.getDescription(), action.getValue(Action.LONG_DESCRIPTION));
275 }
276
277 public void testSetIconInfo() {
278 CommandButtonIconInfo oldIconInfo = descriptor.getIconInfo();
279 CommandButtonIconInfo newIconInfo = new CommandButtonIconInfo(EmptyIcon.LARGE);
280 descriptor.setIconInfo(newIconInfo);
281 assertEquals(newIconInfo, descriptor.getIconInfo());
282
283 assertTrue(propertyChangeListener.changed);
284 assertEquals(descriptor, propertyChangeListener.source);
285 assertEquals(oldIconInfo, propertyChangeListener.oldValue);
286 assertEquals(newIconInfo, propertyChangeListener.newValue);
287 assertEquals(CommandFaceDescriptor.ICON_INFO_PROPERTY, propertyChangeListener.propertyName);
288
289 propertyChangeListener.reset();
290
291 descriptor.setIconInfo(newIconInfo);
292 assertFalse(propertyChangeListener.changed);
293 }
294
295 public void testSetLargeIconInfo() {
296 CommandButtonIconInfo oldIconInfo = descriptor.getLargeIconInfo();
297 CommandButtonIconInfo newIconInfo = new CommandButtonIconInfo(EmptyIcon.LARGE);
298 descriptor.setLargeIconInfo(newIconInfo);
299 assertEquals(newIconInfo, descriptor.getLargeIconInfo());
300
301 assertTrue(propertyChangeListener.changed);
302 assertEquals(descriptor, propertyChangeListener.source);
303 assertEquals(oldIconInfo, propertyChangeListener.oldValue);
304 assertEquals(newIconInfo, propertyChangeListener.newValue);
305 assertEquals(CommandFaceDescriptor.LARGE_ICON_INFO_PROPERTY, propertyChangeListener.propertyName);
306
307 propertyChangeListener.reset();
308
309 descriptor.setLargeIconInfo(newIconInfo);
310 assertFalse(propertyChangeListener.changed);
311 }
312
313 private static class TestPropertyChangeListener implements PropertyChangeListener {
314
315 private boolean changed = false;
316
317 private String propertyName;
318
319 private Object newValue;
320
321 private Object oldValue;
322
323 private Object source;
324
325
326
327
328
329
330 public void propertyChange(PropertyChangeEvent e) {
331 changed = true;
332 propertyName = e.getPropertyName();
333 newValue = e.getNewValue();
334 oldValue = e.getOldValue();
335 source = e.getSource();
336 }
337
338
339
340
341 public void reset() {
342 changed = false;
343 propertyName = null;
344 newValue = null;
345 oldValue = null;
346 source = null;
347 }
348
349 }
350
351 private static void pass() {
352
353 }
354
355 public void testConstructorWithButtonLabelInfo() {
356 CommandFaceDescriptor descriptor = new CommandFaceDescriptor(buttonLabelInfo);
357
358 assertEquals(buttonLabelInfo, descriptor.getLabelInfo());
359 assertFalse(descriptor.isBlank());
360 assertEquals("Test", descriptor.getText());
361 assertNull(descriptor.getDescription());
362 assertEquals(CommandButtonIconInfo.BLANK_ICON_INFO, descriptor.getIconInfo());
363 assertNull(descriptor.getCaption());
364 }
365
366
367
368
369 protected void setUp() throws Exception {
370 buttonLabelInfo = CommandButtonLabelInfo.valueOf("&Test@ctrl T");
371 descriptor = new CommandFaceDescriptor("&Test@ctrl T", EmptyIcon.SMALL, "caption");
372 descriptor.setDescription("long description");
373 assertNotNull(descriptor.getLabelInfo().getAccelerator());
374 propertyChangeListener = new TestPropertyChangeListener();
375 descriptor.addPropertyChangeListener(propertyChangeListener);
376 }
377
378 }