FEST:断言JButton正在显示某个Icon

时间:2011-05-30 15:05:55

标签: swing jbutton fest

在我的FEST-Test中,我尝试断言JButton有一定的ImageIcon。我没有在org.fest.swing.fixture.JButtonFixture上找到相应的方法

2 个答案:

答案 0 :(得分:3)

您可以编写一个自己的ButtonFixture Wrapper,为其提供一种方法。

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

IconButtonFixture类:

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

        }
    }
}

答案 1 :(得分:0)

使用目标怎么样?

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