【问题标题】:FEST: Assert that JButton is showing a certain IconFEST:断言 JButton 正在显示某个图标
【发布时间】:2011-06-08 18:59:12
【问题描述】:

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

【问题讨论】:

    标签: swing jbutton fest


    【解决方案1】:

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

    【讨论】:

    • 太棒了。我认为这应该可以在没有直接询问按钮的线程问题的情况下工作?
    【解决方案2】:

    使用目标呢?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 2015-06-01
      • 2022-01-15
      相关资源
      最近更新 更多