Swing's Event Dispatch Thread (EDT)

来源:互联网 发布:动艺网络 编辑:程序博客网 时间:2024/04/29 23:57

 

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.

原创粉丝点击