使用Abbot给Java Swing写单元测试,遇到这样一个问题:如果用到了showDialog(...)方法,由于是ModelDialog,系统执行到这里就被block了,无法通过Abbot写单元测试。

举个简单的例子来说:Frame中有个button,点击后会显示JColorChooser Dialog,选取颜色后点击OK或者Cancel按钮,Dialog消失,同时返回Color对象,然后就可以在frame中修改Label的颜色。

所以想象中的单元测试应该是这样:

    @Testpublic void testSelectColorRedAndClickOKButton() throws Exception {FrameDemo demo = new FrameDemo();demo.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);demo.pack();demo.setVisible(true);JButton colorButton = (JButton) getFinder().find(demo, new ClassMatcher(JButton.class))new JMenuItemTester().actionClick(colorButton);//1. Here will block after JColorChooser shows// 2. Some action to select color red: JColorChooserTester().actionSelectColor(Color.red)JLabel label = (JLabel) getFinder().find(demo, new ClassMatcher(JButton.class))assertEquals(Color.red, label.getForeground());}

类FrameDemo很简单:

      public class FrameDemo extends JFrame{public void FrameDemo(){JLabel label = new JLabel("Text color will change after you select new color");label.setForeground(Color.black);JButton colorButton = new JButton("Choose Color...");JPanel panel = new JPanel();panel.add(label);panel.add(colorButton);this.setContentPane(panel);}}

但是有两个问题:

1.注释1所在地方,当JColorChooser Dialog显示之后线程就被block了;

2.Abbot中没有类JColorChooserTester帮助选择颜色。

根据以上考虑,决定自己写一个JColorChooserTester,完成以下功能:

1.根据输入选择颜色,并点击OK按钮关闭对话框;

2.可以不选取颜色直接点击Cancel按钮关闭对话框;

3.应该在显示对话框之前就调用该方法。

根据这些分析,对上面的测试做了修改:

    @Testpublic void testSelectColorRedAndClickOKButton() throws Exception {FrameDemo demo = new FrameDemo();demo.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);demo.pack();demo.setVisible(true);JButton colorButton = (JButton) getFinder().find(demo, new ClassMatcher(JButton.class));new JColorChooserTester().actionSelectColor(demo, Color.red);// We want dialog to return rednew JMenuItemTester().actionClick(colorButton);JLabel label = (JLabel) getFinder().find(demo, new ClassMatcher(JButton.class));assertEquals(Color.red, label.getForeground());}

测试写完了,接下来就写JColorChooserTester类,上面的测试能够通过,并经过简单的重构后:

package com.tw.ui.tester;import abbot.finder.BasicFinder;
import abbot.finder.Matcher;
import abbot.finder.ComponentNotFoundException;
import abbot.finder.MultipleComponentsFoundException;
import abbot.finder.matchers.ClassMatcher;
import abbot.tester.JComponentTester;
import abbot.tester.JMenuItemTester;import javax.swing.*;
import java.awt.*;import org.apache.log4j.Logger;public class JColorChooserTester extends JComponentTester {private static final long DEFAULT_TIME_OUT_IN_MILLISECONDS = 5000;private long waitInMilliSeconds;private Logger logger = Logger.getLogger(JColorChooserTester.class);public JColorChooserTester() {this(DEFAULT_TIME_OUT_IN_MILLISECONDS);}public JColorChooserTester(long waitInMilliSeconds) {this.waitInMilliSeconds = waitInMilliSeconds;}public void actionClickCancel(final Container container) {final JColorChooserFinder chooserFinder = new JColorChooserFinder(container);Thread thread = new Thread(){public void run(){new JMenuItemTester().actionClick(chooserFinder.getCancelButton());}};thread.start();}public void actionSelectColor(Container container, final Color returnedColor) {final JColorChooserFinder chooserFinder = new JColorChooserFinder(container);Thread thread = new Thread(){public void run(){chooserFinder.getColorChooser().setColor(returnedColor);new JMenuItemTester().actionClick(chooserFinder.getOKButton());}};thread.start();}class JColorChooserFinder {JColorChooser colorChooser;JButton cancelButton;JButton okButton;private boolean shouldWaitForDialog = true;long start = System.currentTimeMillis();public JColorChooserFinder(Container container) {FindDialog(container);}private void FindDialog(final Container container) {SwingUtilities.invokeLater(new Runnable() {public void run() {try {JDialog dialog = waitAndFindDialog(container);if (dialog != null) {logger.debug("Find JColorChooser Dialog.");cancelButton = findCancelButton(dialog);okButton = findOKButton(dialog);colorChooser = findColorChooser(dialog);}} catch (Exception e) {}finally {shouldWaitForDialog = false;}}});}private JDialog waitAndFindDialog(final Container container) {JDialog dialog = null;long waited = 0;while (dialog == null && shouldWaitMore(waited)) {try {dialog = (JDialog) new BasicFinder().find(container, new ClassMatcher(JDialog.class));} catch (ComponentNotFoundException e) {} catch (MultipleComponentsFoundException e) {logger.debug("Found multiple JDialogs");break;}waited += waitMore(500);}return dialog;}public JButton getCancelButton() {long waited = 0;while (shouldWaitForDialog && shouldWaitMore(waited)) {waited += waitMore(500);}return cancelButton;}private long waitMore(long millis) {try {Thread.sleep(millis);} catch (InterruptedException e) {}return millis;}public JButton getOKButton() {long waited = 0;while (shouldWaitForDialog && shouldWaitMore(waited)) {waited += waitMore(500);}return okButton;}public JColorChooser getColorChooser() {long waited = 0;while (shouldWaitForDialog && shouldWaitMore(waited)) {waited += waitMore(500);}return colorChooser;}private JButton findCancelButton(JDialog dialog) {return findButtonByText(dialog, UIManager.getString("ColorChooser.cancelText"));}private JButton findOKButton(JDialog dialog) {return findButtonByText(dialog, UIManager.getString("ColorChooser.okText"));}private JColorChooser findColorChooser(JDialog dialog) {try {return (JColorChooser) new BasicFinder().find(dialog, new ClassMatcher(JColorChooser.class));} catch (Exception e) {return null;}}private JButton findButtonByText(JDialog dialog, final String cancelString) {try {return (JButton) new BasicFinder().find(dialog, new Matcher() {public boolean matches(Component component) {return component.getClass().equals(JButton.class) && ((JButton) component).getText().equals(cancelString);}});} catch (Exception e) {return null;}}private boolean shouldWaitMore(long waited) {return waited < waitInMilliSeconds;}        }
}

JColorChooserTester的源码和测试代码请见附件。

Swing中JColorChooser的Abbot单元测试相关推荐

  1. 在maven项目中使用Junit进行单元测试

    在maven项目中使用Junit进行单元测试(一) 在maven项目中使用Junit进行单元测试一 创建maven项目 编写测试用代码 小结 这是第一篇博文,所以我决定先从比较简单的内容写起,同时熟悉 ...

  2. 在Eclipse中使用JUnit4进行单元测试

    在Eclipse中使用JUnit4进行单元测试 http://www.sina.com.cn  2010年01月18日 14:08  IT168.com [IT168 技术文档]我们在编写大型程序的时 ...

  3. Swing中事件的三种处理方法

    2019独角兽企业重金招聘Python工程师标准>>>  Swing是目前Java中不可缺少的窗口工具组,是用户建立图形化用户界面(GUI)程序的强大工具.Java Swing组件自 ...

  4. 【Java】在Eclipse中使用JUnit4进行单元测试(初级篇)

    本文绝大部分内容引自这篇文章: http://www.devx.com/Java/Article/31983/0/page/1 我们在编写大型程序的时候,需要写成千上万个方法或函数,这些函数的功能可能 ...

  5. java集合刷新面板_java Swing 中 面板刷新的问题。。求指教 。 高手在哪里啊。。。...

    javaswing中repaint()刷新面板的问题 求指教...代码如下:运行后点击按钮1,面板没刷新,需要缩放面板才能看到新的面板.我用计时器或则线程来调用repaint(),好像都没... ja ...

  6. java swing 控件拖动_java swing中实现拖拽功能示例

    java实现拖拽示例 Swing中实现拖拽功能,代码很简单,都有注释,自己看,运行效果如下图: package com; import java.awt.*;import java.awt.datat ...

  7. 在Eclipse中使用JUnit4进行单元测试(初级篇)

    转载自   在Eclipse中使用JUnit4进行单元测试(初级篇) 本文绝大部分内容引自这篇文章: http://www.devx.com/Java/Article/31983/0/page/1 我 ...

  8. swingworker_使用SwingWorker的Java Swing中的多线程

    swingworker 如果要使用Swing用J​​ava编写桌面或Java Web Start程序,您可能会觉得需要通过创建自己的线程在后台运行某些程序. 没有什么可以阻止您在Swing中使用标准的 ...

  9. swing 聊天气泡背景_Java Swing中的聊天气泡

    swing 聊天气泡背景 本文将向您解释"如何在Java swing应用程序中绘制聊天气泡?" 聊天气泡与呼出或提示气泡相同. 今天,大多数聊天应用程序都以这种格式显示转换,因此本 ...

  10. 使用SwingWorker的Java Swing中的多线程

    如果要使用Swing用J​​ava编写桌面或Java Web Start程序,您可能会觉得需要通过创建自己的线程在后台运行某些东西. 没有什么可以阻止您在Swing中使用标准的多线程技术,并且需要遵循 ...

最新文章

  1. Springboot总结,核心功能,优缺点
  2. 什么是RNA-Seq (RNA Sequencing)
  3. mongodb 安装、开启服务 和 php添加mongodb扩展
  4. 设计模式C++实现——组合模式
  5. SpringMVC-方法四种类型返回值总结,你用过几种?
  6. Docker 面临的安全隐患,我们该如何应对
  7. 《Head First设计模式》第五章笔记-单件模式
  8. BCrypt加密怎么存入数据库_Spring Boot 中密码加密的两种姿势
  9. 图片或文字或box垂直居中
  10. mysql-connector-java-5.1.22下载及安装
  11. 耐驰测试仪上的软件,Proteus
  12. 零基础入门机器学习——声音识别——打卡Task1
  13. IDEA新手使用教程(详解)
  14. Dynamsoft Barcode Reader Crack,强大而快速的条码解码
  15. 机器学习中的概率分布
  16. 第一篇博客-Sql排名函数DENSE_RANK
  17. 装了svn桌面右键没有_右键菜单没有svn选项怎么办|win7 svn没有右键菜单怎么解决|svn添加到右键菜单方法...
  18. 7-2 你今天刷快手了吗
  19. 软件测试面试刁难人?花重金购买的资料1套全给你解决
  20. 深度推荐模型之NFM模型

热门文章

  1. Android入门,android基础开发
  2. 三个优秀的Android图表开源控件
  3. 华为交换机Hybird 与 单臂路由
  4. 组态软件及其应用方式
  5. oracle财务系统表,Oracle ERP 财务模块表结构.ppt
  6. 智慧城市的投资运营与评估
  7. EAX寄存器(关键跳,关键CALL)
  8. 朋友,你A9了吗?(重新定义A8、A9)
  9. Windows下同步工具FastCopy
  10. yum源的三种配置方式