1   /*
2    * Copyright 2002-2007 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.richclient.command.config;
17  
18  import java.awt.Color;
19  
20  import javax.swing.Icon;
21  import javax.swing.JButton;
22  import javax.swing.JMenu;
23  import javax.swing.JMenuItem;
24  
25  import junit.framework.TestCase;
26  
27  import org.springframework.richclient.test.TestIcon;
28  
29  /**
30   * Testcase for CommandButtonIconInfo
31   * 
32   * @author Peter De Bruycker
33   */
34  public class CommandButtonIconInfoTests extends TestCase {
35  
36      private Icon icon;
37  
38      private Icon selectedIcon;
39  
40      private Icon rolloverIcon;
41  
42      private Icon disabledIcon;
43  
44      private Icon pressedIcon;
45  
46      private CommandButtonIconInfo completeInfo;
47  
48      public void testConstructor() {
49          CommandButtonIconInfo info = new CommandButtonIconInfo(icon);
50          assertEquals(icon, info.getIcon());
51          assertNull(info.getSelectedIcon());
52          assertNull(info.getRolloverIcon());
53          assertNull(info.getDisabledIcon());
54          assertNull(info.getPressedIcon());
55      }
56  
57      public void testConstructor2() {
58          CommandButtonIconInfo info = new CommandButtonIconInfo(icon, selectedIcon);
59          assertEquals(icon, info.getIcon());
60          assertEquals(selectedIcon, info.getSelectedIcon());
61          assertNull(info.getRolloverIcon());
62          assertNull(info.getDisabledIcon());
63          assertNull(info.getPressedIcon());
64      }
65  
66      public void testConstructor3() {
67          CommandButtonIconInfo info = new CommandButtonIconInfo(icon, selectedIcon, rolloverIcon);
68          assertEquals(icon, info.getIcon());
69          assertEquals(selectedIcon, info.getSelectedIcon());
70          assertEquals(rolloverIcon, info.getRolloverIcon());
71          assertNull(info.getDisabledIcon());
72          assertNull(info.getPressedIcon());
73      }
74  
75      public void testConstructor4() {
76          CommandButtonIconInfo info = new CommandButtonIconInfo(icon, selectedIcon, rolloverIcon, disabledIcon,
77                  pressedIcon);
78          assertEquals(icon, info.getIcon());
79          assertEquals(selectedIcon, info.getSelectedIcon());
80          assertEquals(rolloverIcon, info.getRolloverIcon());
81          assertEquals(disabledIcon, info.getDisabledIcon());
82          assertEquals(pressedIcon, info.getPressedIcon());
83      }
84  
85      public void testConfigureWithNullButton() {
86          CommandButtonIconInfo info = new CommandButtonIconInfo(icon);
87          try {
88              info.configure(null);
89              fail("Should throw IllegalArgumentException");
90          }
91          catch (IllegalArgumentException e) {
92              pass();
93          }
94      }
95  
96      public void testConfigureWithJButton() {
97          JButton button = new JButton("Test");
98          JButton result = (JButton)completeInfo.configure(button);
99          assertSame(button, result);
100 
101         assertEquals(icon, button.getIcon());
102         assertEquals(selectedIcon, button.getSelectedIcon());
103         assertEquals(rolloverIcon, button.getRolloverIcon());
104         assertEquals(disabledIcon, button.getDisabledIcon());
105         assertEquals(pressedIcon, button.getPressedIcon());
106     }
107 
108     public void testConfigureWithJMenuItem() {
109         JMenuItem button = new JMenuItem("Test");
110         JMenuItem result = (JMenuItem)completeInfo.configure(button);
111         assertSame(button, result);
112 
113         assertEquals(icon, button.getIcon());
114         assertEquals(selectedIcon, button.getSelectedIcon());
115         assertEquals(rolloverIcon, button.getRolloverIcon());
116         assertEquals(disabledIcon, button.getDisabledIcon());
117         assertEquals(pressedIcon, button.getPressedIcon());
118     }
119 
120     public void testConfigureWithJMenu() {
121         JMenu button = new JMenu("Test");
122         button.setIcon(icon);
123         button.setSelectedIcon(selectedIcon);
124         button.setRolloverIcon(rolloverIcon);
125         button.setDisabledIcon(disabledIcon);
126         button.setPressedIcon(pressedIcon);
127 
128         JMenuItem result = (JMenuItem)completeInfo.configure(button);
129         assertSame(button, result);
130 
131         assertEquals(icon, button.getIcon());
132         assertEquals(selectedIcon, button.getSelectedIcon());
133         assertEquals(rolloverIcon, button.getRolloverIcon());
134         assertEquals(disabledIcon, button.getDisabledIcon());
135         assertEquals(pressedIcon, button.getPressedIcon());
136     }
137 
138     private static void pass() {
139         // test passes
140     }
141 
142     protected void setUp() throws Exception {
143         icon = new TestIcon(Color.BLUE);
144         selectedIcon = new TestIcon(Color.BLACK);
145         rolloverIcon = new TestIcon(Color.GREEN);
146         disabledIcon = new TestIcon(Color.GRAY);
147         pressedIcon = new TestIcon(Color.WHITE);
148 
149         completeInfo = new CommandButtonIconInfo(icon, selectedIcon, rolloverIcon, disabledIcon, pressedIcon);
150     }
151 }