/*
 * Copyright 2002-2007 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.richclient.command.config;

import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import junit.framework.TestCase;

/**
 * Testcase for CommandButtonIconInfo
 * 
 * @author Peter De Bruycker
 */
public class CommandButtonIconInfoTests extends TestCase {

    private Icon icon;

    private Icon selectedIcon;

    private Icon rolloverIcon;

    private Icon disabledIcon;

    private Icon pressedIcon;

    private CommandButtonIconInfo completeInfo;

    public void testConstructor() {
        CommandButtonIconInfo info = new CommandButtonIconInfo(icon);
        assertEquals(icon, info.getIcon());
        assertNull(info.getSelectedIcon());
        assertNull(info.getRolloverIcon());
        assertNull(info.getDisabledIcon());
        assertNull(info.getPressedIcon());
    }

    public void testConstructor2() {
        CommandButtonIconInfo info = new CommandButtonIconInfo(icon, selectedIcon);
        assertEquals(icon, info.getIcon());
        assertEquals(selectedIcon, info.getSelectedIcon());
        assertNull(info.getRolloverIcon());
        assertNull(info.getDisabledIcon());
        assertNull(info.getPressedIcon());
    }

    public void testConstructor3() {
        CommandButtonIconInfo info = new CommandButtonIconInfo(icon, selectedIcon, rolloverIcon);
        assertEquals(icon, info.getIcon());
        assertEquals(selectedIcon, info.getSelectedIcon());
        assertEquals(rolloverIcon, info.getRolloverIcon());
        assertNull(info.getDisabledIcon());
        assertNull(info.getPressedIcon());
    }

    public void testConstructor4() {
        CommandButtonIconInfo info = new CommandButtonIconInfo(icon, selectedIcon, rolloverIcon, disabledIcon,
                pressedIcon);
        assertEquals(icon, info.getIcon());
        assertEquals(selectedIcon, info.getSelectedIcon());
        assertEquals(rolloverIcon, info.getRolloverIcon());
        assertEquals(disabledIcon, info.getDisabledIcon());
        assertEquals(pressedIcon, info.getPressedIcon());
    }

    public void testConfigureWithNullButton() {
        CommandButtonIconInfo info = new CommandButtonIconInfo(icon);
        try {
            info.configure(null);
            fail("Should throw IllegalArgumentException");
        }
        catch (IllegalArgumentException e) {
            pass();
        }
    }

    public void testConfigureWithJButton() {
        JButton button = new JButton("Test");
        JButton result = (JButton)completeInfo.configure(button);
        assertSame(button, result);

        assertEquals(icon, button.getIcon());
        assertEquals(selectedIcon, button.getSelectedIcon());
        assertEquals(rolloverIcon, button.getRolloverIcon());
        assertEquals(disabledIcon, button.getDisabledIcon());
        assertEquals(pressedIcon, button.getPressedIcon());
    }

    public void testConfigureWithJMenuItem() {
        JMenuItem button = new JMenuItem("Test");
        JMenuItem result = (JMenuItem)completeInfo.configure(button);
        assertSame(button, result);

        assertEquals(icon, button.getIcon());
        assertEquals(selectedIcon, button.getSelectedIcon());
        assertEquals(rolloverIcon, button.getRolloverIcon());
        assertEquals(disabledIcon, button.getDisabledIcon());
        assertEquals(pressedIcon, button.getPressedIcon());
    }

    public void testConfigureWithJMenu() {
        JMenu button = new JMenu("Test");
        button.setIcon(icon);
        button.setSelectedIcon(selectedIcon);
        button.setRolloverIcon(rolloverIcon);
        button.setDisabledIcon(disabledIcon);
        button.setPressedIcon(pressedIcon);

        JMenuItem result = (JMenuItem)completeInfo.configure(button);
        assertSame(button, result);

        assertEquals(icon, button.getIcon());
        assertEquals(selectedIcon, button.getSelectedIcon());
        assertEquals(rolloverIcon, button.getRolloverIcon());
        assertEquals(disabledIcon, button.getDisabledIcon());
        assertEquals(pressedIcon, button.getPressedIcon());
    }

    private static void pass() {
        // test passes
    }

    private static class TestIcon implements Icon {

        /*
         * (non-Javadoc)
         * 
         * @see javax.swing.Icon#getIconHeight()
         */
        public int getIconHeight() {
            return 16;
        }

        /*
         * (non-Javadoc)
         * 
         * @see javax.swing.Icon#getIconWidth()
         */
        public int getIconWidth() {
            return 16;
        }

        /*
         * (non-Javadoc)
         * 
         * @see javax.swing.Icon#paintIcon(java.awt.Component,
         *      java.awt.Graphics, int, int)
         */
        public void paintIcon(Component arg0, Graphics arg1, int arg2, int arg3) {
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see junit.framework.TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();

        icon = new TestIcon();
        selectedIcon = new TestIcon();
        rolloverIcon = new TestIcon();
        disabledIcon = new TestIcon();
        pressedIcon = new TestIcon();

        completeInfo = new CommandButtonIconInfo(icon, selectedIcon, rolloverIcon, disabledIcon, pressedIcon);

    }

}
@@ -695,41 +695,11 @@
 t.Co
-mponent;%0Aimport java.awt.Graphics
+lor
 ;%0A%0Ai
@@ -841,16 +841,70 @@
 tCase;%0A%0A
+import org.springframework.richclient.test.TestIcon;%0A%0A
 /**%0A * T
@@ -4873,776 +4873,8 @@
  %7D%0A%0A
-    private static class TestIcon implements Icon %7B%0A%0A        /*%0A         * (non-Javadoc)%0A         * %0A         * @see javax.swing.Icon#getIconHeight()%0A         */%0A        public int getIconHeight() %7B%0A            return 16;%0A        %7D%0A%0A        /*%0A         * (non-Javadoc)%0A         * %0A         * @see javax.swing.Icon#getIconWidth()%0A         */%0A        public int getIconWidth() %7B%0A            return 16;%0A        %7D%0A%0A        /*%0A         * (non-Javadoc)%0A         * %0A         * @see javax.swing.Icon#paintIcon(java.awt.Component,%0A         *      java.awt.Graphics, int, int)%0A         */%0A        public void paintIcon(Component arg0, Graphics arg1, int arg2, int arg3) %7B%0A        %7D%0A    %7D%0A%0A    /*%0A     * (non-Javadoc)%0A     * %0A     * @see junit.framework.TestCase#setUp()%0A     */%0A
     
@@ -4919,32 +4919,8 @@
 n %7B%0A
-        super.setUp();%0A%0A
     
@@ -4935,32 +4935,42 @@
  = new TestIcon(
+Color.BLUE
 );%0A        selec
@@ -4984,32 +4984,43 @@
  = new TestIcon(
+Color.BLACK
 );%0A        rollo
@@ -5034,32 +5034,43 @@
  = new TestIcon(
+Color.GREEN
 );%0A        disab
@@ -5092,16 +5092,26 @@
 estIcon(
+Color.GRAY
 );%0A     
@@ -5140,16 +5140,27 @@
 estIcon(
+Color.WHITE
 );%0A%0A    
@@ -5266,17 +5266,15 @@
 dIcon);%0A
-%0A
     %7D%0A
-%0A
 %7D

use ...test.TestIcon in tests