001    /*
002     * Copyright 2002-2007 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.Color;
019    
020    import javax.swing.Icon;
021    import javax.swing.JButton;
022    import javax.swing.JMenu;
023    import javax.swing.JMenuItem;
024    
025    import junit.framework.TestCase;
026    
027    import org.springframework.richclient.test.TestIcon;
028    
029    /**
030     * Testcase for CommandButtonIconInfo
031     * 
032     * @author Peter De Bruycker
033     */
034    public class CommandButtonIconInfoTests extends TestCase {
035    
036        private Icon icon;
037    
038        private Icon selectedIcon;
039    
040        private Icon rolloverIcon;
041    
042        private Icon disabledIcon;
043    
044        private Icon pressedIcon;
045    
046        private CommandButtonIconInfo completeInfo;
047    
048        public void testConstructor() {
049            CommandButtonIconInfo info = new CommandButtonIconInfo(icon);
050            assertEquals(icon, info.getIcon());
051            assertNull(info.getSelectedIcon());
052            assertNull(info.getRolloverIcon());
053            assertNull(info.getDisabledIcon());
054            assertNull(info.getPressedIcon());
055        }
056    
057        public void testConstructor2() {
058            CommandButtonIconInfo info = new CommandButtonIconInfo(icon, selectedIcon);
059            assertEquals(icon, info.getIcon());
060            assertEquals(selectedIcon, info.getSelectedIcon());
061            assertNull(info.getRolloverIcon());
062            assertNull(info.getDisabledIcon());
063            assertNull(info.getPressedIcon());
064        }
065    
066        public void testConstructor3() {
067            CommandButtonIconInfo info = new CommandButtonIconInfo(icon, selectedIcon, rolloverIcon);
068            assertEquals(icon, info.getIcon());
069            assertEquals(selectedIcon, info.getSelectedIcon());
070            assertEquals(rolloverIcon, info.getRolloverIcon());
071            assertNull(info.getDisabledIcon());
072            assertNull(info.getPressedIcon());
073        }
074    
075        public void testConstructor4() {
076            CommandButtonIconInfo info = new CommandButtonIconInfo(icon, selectedIcon, rolloverIcon, disabledIcon,
077                    pressedIcon);
078            assertEquals(icon, info.getIcon());
079            assertEquals(selectedIcon, info.getSelectedIcon());
080            assertEquals(rolloverIcon, info.getRolloverIcon());
081            assertEquals(disabledIcon, info.getDisabledIcon());
082            assertEquals(pressedIcon, info.getPressedIcon());
083        }
084    
085        public void testConfigureWithNullButton() {
086            CommandButtonIconInfo info = new CommandButtonIconInfo(icon);
087            try {
088                info.configure(null);
089                fail("Should throw IllegalArgumentException");
090            }
091            catch (IllegalArgumentException e) {
092                pass();
093            }
094        }
095    
096        public void testConfigureWithJButton() {
097            JButton button = new JButton("Test");
098            JButton result = (JButton)completeInfo.configure(button);
099            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    }