2

In my FEST-Test I try to assert, that a JButton has a certain ImageIcon. I didn't find a corresponding method on org.fest.swing.fixture.JButtonFixture

keuleJ
  • 3,418
  • 4
  • 30
  • 51

2 Answers2

3

You can write an own ButtonFixture Wrapper which provide a method for that.

IconButtonFixture iconButtonFixture = new IconButtonFixture(buttonFixture.robot, buttonFixture.target);
iconButtonFixture.requireIcon(new ImageIcon( "file:/C:/Users/admin/workspace/Project/bin/image/icon.gif" ));

The IconButtonFixture class:

import static org.fest.swing.edt.GuiActionRunner.execute;

public class IconButtonFixture extends JButtonFixture {

    private IconButtonDriver driver;

    public IconButtonFixture(Robot robot, JButton target) {
        super(robot, target);
        driver = new IconButtonDriver(robot);
      }

    public JButtonFixture requireIcon(Icon icon) {
        driver.requireIcon(target, icon);
        return this;
    }

    private class IconButtonDriver extends AbstractButtonDriver {
        public IconButtonDriver( Robot robot ) {
            super( robot );
        }
        public void requireIcon(final JButton button, Icon icon) {
            Icon buttonIcon = execute(new GuiQuery<Icon>() {
                protected Icon executeInEDT() {
                  return button.getIcon();
                }
              });
            if(!icon.toString().equals( buttonIcon.toString() )) {
                Assert.failNotEquals( "The Button has not the expected Icon.", icon, button.getIcon() );
            }

        }
    }
}
oliholz
  • 7,447
  • 2
  • 43
  • 82
  • Great. I think that should work without the threading issues of asking the button directly? – keuleJ Jun 07 '11 at 16:43
0

what about using target?

Assert.assertNotNull( jButtonFixture.target.getIcon() );
oliholz
  • 7,447
  • 2
  • 43
  • 82