开发者

FEST: Assert that JButton is showing a certain Icon

In my FEST-Test I try to assert, that a JButton has a certain ImageIcon. I didn't find a corresponding method开发者_如何学Python on org.fest.swing.fixture.JButtonFixture


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() );
            }

        }
    }
}


what about using target?

Assert.assertNotNull( jButtonFixture.target.getIcon() );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜