/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Nokia. Portions Copyright 2004 Nokia. All Rights Reserved. */ package org.openide.awt; import java.awt.image.BufferedImage; import javax.swing.AbstractButton; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.Icon; import javax.swing.ImageIcon; import org.openide.util.actions.SystemAction; import org.netbeans.junit.*; import junit.textui.TestRunner; /** * Tests for the Actions class. * @author David Strupl */ public class ActionsTest extends NbTestCase { // colors of the testing images in this order: // testIcon.gif // testIcon_rollover.gif // testIcon_pressed.gif // testIcon_disabled.gif private static int[][] RESULT_COLORS = { {83, 155, 226 }, {206, 132, 202}, {216, 202, 130}, {221, 130, 130} }; public ActionsTest(String name) { super(name); } public static void main(String[] args) { TestRunner.run(new NbTestSuite(ActionsTest.class)); } protected void setUp() throws Exception { } /** * Test whether pressed, rollover and disabled icons * work for SystemAction. */ public void testIconsSystemAction() throws Exception { SystemAction saInstance = SystemAction.get(TestSystemAction.class); JButton jb = new JButton(); Actions.connect(jb, saInstance); Icon icon = jb.getIcon(); assertNotNull(icon); if (! isIconOk(icon, jb, RESULT_COLORS[0])) fail(); Icon rolloverIcon = jb.getRolloverIcon(); assertNotNull(rolloverIcon); if (! isIconOk(rolloverIcon, jb, RESULT_COLORS[1])) fail(); Icon pressedIcon = jb.getPressedIcon(); assertNotNull(pressedIcon); if (! isIconOk(pressedIcon, jb, RESULT_COLORS[2])) fail(); Icon disabledIcon = jb.getDisabledIcon(); assertNotNull(disabledIcon); if (! isIconOk(disabledIcon, jb, RESULT_COLORS[3])) fail(); } /** * Test whether pressed, rollover and disabled icons * work for javax.swing.Action. */ public void testIconsAction() throws Exception { JButton jb = new JButton(); Actions.connect(jb, new TestAction()); Icon icon = jb.getIcon(); assertNotNull(icon); if (! isIconOk(icon, jb, RESULT_COLORS[0])) fail(); Icon rolloverIcon = jb.getRolloverIcon(); assertNotNull(rolloverIcon); if (! isIconOk(rolloverIcon, jb, RESULT_COLORS[1])) fail(); Icon pressedIcon = jb.getPressedIcon(); assertNotNull(pressedIcon); if (! isIconOk(pressedIcon, jb, RESULT_COLORS[2])) fail(); Icon disabledIcon = jb.getDisabledIcon(); assertNotNull(disabledIcon); if (! isIconOk(disabledIcon, jb, RESULT_COLORS[3])) fail(); } /** * Checks colors on coordinates 0,0 of the icon and compares them * to expectedResult. */ private boolean isIconOk(Icon icon, java.awt.Component c, int[] expectedResult) { BufferedImage bufImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB); icon.paintIcon(c, bufImg.getGraphics(), 0, 0); int[] res = bufImg.getData().getPixel(0, 0, (int[])null); for (int i = 0; i < res.length; i++) { if (res[i] != expectedResult[i]) { return false; } } return true; } private static final class TestSystemAction extends SystemAction { public void actionPerformed(java.awt.event.ActionEvent e) { } public org.openide.util.HelpCtx getHelpCtx() { return null; } public String getName() { return "TestSystemAction"; } protected String iconResource() { return "org/openide/awt/testIcon.gif"; } } private static final class TestAction extends AbstractAction { public TestAction() { putValue("iconBase", "org/openide/awt/testIcon"); putValue(NAME, "test"); } public void actionPerformed(java.awt.event.ActionEvent e) { } } }