Swing's Event Dispatch Thread (EDT)

 

Swing's Event Dispatch Thread (EDT)

 

Accessing GUI components in the EDT

The cardinal rule of Swing threading states that all access to all Swing components should be done in the event dispatch thread (EDT.) To avoid unexpected behavior in your GUI tests (e.g. random test failures,) this rule also applies to test code.

FEST-Swing performs all access to Swing components in the EDT. FEST-Swing also exposes a convenient mechanism to access Swing components in the EDT from your test code. This mechanism involves three classes:

  1. GuiQuery, for reading property values from GUI components
  2. GuiTask, for performing actions on GUI components
  3. GuiActionRunner, executes a GuiQuery or GuiTask in the EDT, re-throwing any exceptions thrown when executing any GUI action in the EDT.

 

Examples:

The following examples demonstrate how to access GUI components in the EDT using FEST-Swing.

1. Reading the text of a JLabel:

  final JLabel nameLabel = ... // get a reference to a JLabel
  String text = GuiActionRunner.execute(new GuiQuery<String>() {
    public String executeInEDT() {
      return nameLabel.getText();
    }
  });

2. Setting the text of a JLabel:

  final JLabel nameLabel = ... // get a reference to a JLabel
  GuiActionRunner.execute(new GuiTask() {
    public void executeInEDT() {
      nameLabel.setText("Name:");
    }
  });

 

Testing that access to GUI components is done in the EDT

FEST provides the class FailOnThreadViolationRepaintManager that fails if access to GUI components is not performed on the EDT. This RepaintManager on based on ThreadCheckingRepaintManager by Scott Delap and Alex Potochkin. For more details about how to check if GUI component is done outside the EDT, please check Alex Potochkin's excellent article "Debugging Swing, the final summary."

Installing FailOnThreadViolationRepaintManager is pretty simple. The following example shows how to install it in the class-level setup method of a TestNG test:

  @BeforeClass public void setUpOnce() {
    FailOnThreadViolationRepaintManager.install();
  }

When using Sun's JVM, a new instance of FailOnThreadViolationRepaintManager will be set as the default repaint manager in Swing once and only once, regardless of the number of times install is called. On other JVMs this optimization is not guaranteed.

Once a FailOnThreadViolationRepaintManager is installed, it will throw a EdtViolationException if access to GUI components is not performed in the EDT when executing a FEST test.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值