开发者

FEST: Assert that JButton is showing a certain Icon

开发者 https://www.devze.com 2023-03-09 01:50 出处:网络
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.JButtonFixtureYou can write an own ButtonFix

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

精彩评论

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

关注公众号