gpt4 book ai didi

swing - FEST:断言 JButton 显示某个图标

转载 作者:行者123 更新时间:2023-12-04 06:24:39 26 4
gpt4 key购买 nike

在我的 FEST 测试中,我尝试断言 JButton 具有某个 ImageIcon。我在 org.fest.swing.fixture.JButtonFixture 上没有找到对应的方法

最佳答案

您可以编写一个自己的 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() );
}

}
}
}

关于swing - FEST:断言 JButton 显示某个图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6177930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com