001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.springframework.richclient.command.config;
017
018 import java.awt.event.ActionEvent;
019 import java.beans.PropertyChangeEvent;
020 import java.beans.PropertyChangeListener;
021
022 import javax.swing.AbstractAction;
023 import javax.swing.AbstractButton;
024 import javax.swing.Action;
025 import javax.swing.Icon;
026 import javax.swing.JButton;
027
028 import junit.framework.TestCase;
029 import org.springframework.richclient.command.AbstractCommand;
030 import org.springframework.richclient.core.DescribedElement;
031 import org.springframework.richclient.image.EmptyIcon;
032
033 /**
034 * @author Peter De Bruycker
035 */
036 public class CommandFaceDescriptorTests extends TestCase {
037
038 private CommandButtonLabelInfo buttonLabelInfo;
039
040 private CommandFaceDescriptor descriptor;
041
042 private TestPropertyChangeListener propertyChangeListener;
043
044 public void testDefaultConstructor() {
045 CommandFaceDescriptor descriptor = new CommandFaceDescriptor();
046
047 assertEquals(CommandButtonLabelInfo.BLANK_BUTTON_LABEL, descriptor.getLabelInfo());
048 assertTrue(descriptor.isBlank());
049 assertEquals(CommandButtonLabelInfo.BLANK_BUTTON_LABEL.getText(), descriptor.getText());
050 assertNull(descriptor.getDescription());
051 assertEquals(CommandButtonIconInfo.BLANK_ICON_INFO, descriptor.getIconInfo());
052 assertNull(descriptor.getCaption());
053 }
054
055 public void testConstructorWithNullButtonLabelInfo() {
056 try {
057 new CommandFaceDescriptor((CommandButtonLabelInfo)null);
058 fail("Should throw IllegalArgumentException");
059 }
060 catch (IllegalArgumentException e) {
061 pass();
062 }
063 }
064
065 public void testConstructorWithEncodedLabel() {
066 CommandFaceDescriptor descriptor = new CommandFaceDescriptor("&Test@ctrl T");
067 assertEquals("Test", descriptor.getText());
068 assertFalse(CommandButtonLabelInfo.BLANK_BUTTON_LABEL.equals(descriptor.getLabelInfo()));
069 assertFalse(descriptor.isBlank());
070 assertNull(descriptor.getDescription());
071 assertNull(descriptor.getCaption());
072 assertEquals(CommandButtonIconInfo.BLANK_ICON_INFO, descriptor.getIconInfo());
073 }
074
075 public void testConstructorWithEmptyEncodedLabel() {
076 CommandFaceDescriptor descriptor = new CommandFaceDescriptor("");
077 assertEquals(CommandButtonLabelInfo.BLANK_BUTTON_LABEL, descriptor.getLabelInfo());
078 }
079
080 public void testConstructorWithEncodedLabelAndIcon() {
081 CommandFaceDescriptor descriptor = new CommandFaceDescriptor("&Test@ctrl T", EmptyIcon.SMALL, "caption");
082
083 assertEquals("Test", descriptor.getText());
084 assertFalse(CommandButtonLabelInfo.BLANK_BUTTON_LABEL.equals(descriptor.getLabelInfo()));
085 assertFalse(descriptor.isBlank());
086 assertNull(descriptor.getDescription());
087 assertEquals("caption", descriptor.getCaption());
088 assertFalse(CommandButtonIconInfo.BLANK_ICON_INFO.equals(descriptor.getIconInfo()));
089 assertEquals(EmptyIcon.SMALL, descriptor.getIconInfo().getIcon());
090 }
091
092 public void testSetDescription() {
093 CommandFaceDescriptor descriptor = new CommandFaceDescriptor("&Test@ctrl T", EmptyIcon.SMALL, "caption");
094 descriptor.setDescription("Long description");
095
096 assertEquals("Long description", descriptor.getDescription());
097 }
098
099 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 // caption not changed
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 // no change
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 // no change
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 // no change
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 // no change
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 // no change
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 // no change
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 * (non-Javadoc)
327 *
328 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
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 // test passes
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 * @see junit.framework.TestCase#setUp()
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 }