<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Writing EDT-safe Swing UI tests</title>
	<atom:link href="http://alexruiz.developerblogs.com/?feed=rss2&#038;p=160" rel="self" type="application/rss+xml" />
	<link>http://alexruiz.developerblogs.com/?p=160</link>
	<description>Having Fun with Java!</description>
	<lastBuildDate>Thu, 19 Aug 2010 17:21:54 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Alex Ruiz</title>
		<link>http://alexruiz.developerblogs.com/?p=160&#038;cpage=1#comment-11673</link>
		<dc:creator>Alex Ruiz</dc:creator>
		<pubDate>Mon, 05 Jul 2010 03:35:26 +0000</pubDate>
		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=160#comment-11673</guid>
		<description>I have found that &quot;invokeAndWait&quot; can create deadlocks in test code. I haven&#039;t looked into in detail since FEST offers similar behavior without that limitation.

The trick is to execute line #2 in the EDT and wait till all the events in the EDT has been consumed before executing the  code in #3. 

How to do it will take probably more than one post to explain. I&#039;d suggest you to fork the code in FEST, especifically the org.fest.swing.edt package. Don&#039;t worry about license, it is Apache 2.0, which is business friendly :)

Cheers,</description>
		<content:encoded><![CDATA[<p>I have found that &#8220;invokeAndWait&#8221; can create deadlocks in test code. I haven&#8217;t looked into in detail since FEST offers similar behavior without that limitation.</p>
<p>The trick is to execute line #2 in the EDT and wait till all the events in the EDT has been consumed before executing the  code in #3. </p>
<p>How to do it will take probably more than one post to explain. I&#8217;d suggest you to fork the code in FEST, especifically the org.fest.swing.edt package. Don&#8217;t worry about license, it is Apache 2.0, which is business friendly :)</p>
<p>Cheers,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jimmy</title>
		<link>http://alexruiz.developerblogs.com/?p=160&#038;cpage=1#comment-11654</link>
		<dc:creator>Jimmy</dc:creator>
		<pubDate>Sun, 04 Jul 2010 05:01:04 +0000</pubDate>
		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=160#comment-11654</guid>
		<description>[Sorry for the duplicate comment, I misspelled my email address in earlier one]

Hi Alex,

I was so glad to find an article which talks about writing EDT safe swing tests. This is exactly the challenge that I am facing currently. Unfortunately, I am not using FEST, so the methods described here does not solve my problem. I would appreciate if you could help me in understanding what I am doing wrong in my test.

Here is what I have in my JUnit test:

@Test
public void testSomething()
{
// Line 1: do something
// Line 2: Execute code which generates mouseDragged event
// Line 3: Verification that mouseDragged event is generated
}

What I notice is that my Line 3 gets executed before the mouseDragged method (which does get executed but little late). I tried adding my verification code on to EDT by wrapping it in InvokeAndWait but it still does not help.

In general I am looking for some basic guidelines on what is the best way to avoid such situation. What exactly should be wrapped in InvokeAndWait and what should not in testing?

Any help is much appreciated.

- Jimmy</description>
		<content:encoded><![CDATA[<p>[Sorry for the duplicate comment, I misspelled my email address in earlier one]</p>
<p>Hi Alex,</p>
<p>I was so glad to find an article which talks about writing EDT safe swing tests. This is exactly the challenge that I am facing currently. Unfortunately, I am not using FEST, so the methods described here does not solve my problem. I would appreciate if you could help me in understanding what I am doing wrong in my test.</p>
<p>Here is what I have in my JUnit test:</p>
<p>@Test<br />
public void testSomething()<br />
{<br />
// Line 1: do something<br />
// Line 2: Execute code which generates mouseDragged event<br />
// Line 3: Verification that mouseDragged event is generated<br />
}</p>
<p>What I notice is that my Line 3 gets executed before the mouseDragged method (which does get executed but little late). I tried adding my verification code on to EDT by wrapping it in InvokeAndWait but it still does not help.</p>
<p>In general I am looking for some basic guidelines on what is the best way to avoid such situation. What exactly should be wrapped in InvokeAndWait and what should not in testing?</p>
<p>Any help is much appreciated.</p>
<p>- Jimmy</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Ruiz</title>
		<link>http://alexruiz.developerblogs.com/?p=160&#038;cpage=1#comment-9348</link>
		<dc:creator>Alex Ruiz</dc:creator>
		<pubDate>Fri, 05 Feb 2010 03:11:18 +0000</pubDate>
		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=160#comment-9348</guid>
		<description>I apologize for the confusion. I&#039;m a TestNG user and it doesn&#039;t require the @BeforeClass method to be static. I should have specified that.

Thanks!</description>
		<content:encoded><![CDATA[<p>I apologize for the confusion. I&#8217;m a TestNG user and it doesn&#8217;t require the @BeforeClass method to be static. I should have specified that.</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Ruiz</title>
		<link>http://alexruiz.developerblogs.com/?p=160&#038;cpage=1#comment-9347</link>
		<dc:creator>Alex Ruiz</dc:creator>
		<pubDate>Fri, 05 Feb 2010 03:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=160#comment-9347</guid>
		<description>@RunsInEDT is simply there for documentation purposes. While working on FEST, I found it pretty difficult to know which method is being ran in the EDT and which on the current thread. I use the annotation just as a reminder of which methods/classes are EDT-safe.</description>
		<content:encoded><![CDATA[<p>@RunsInEDT is simply there for documentation purposes. While working on FEST, I found it pretty difficult to know which method is being ran in the EDT and which on the current thread. I use the annotation just as a reminder of which methods/classes are EDT-safe.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luke Nezda</title>
		<link>http://alexruiz.developerblogs.com/?p=160&#038;cpage=1#comment-9346</link>
		<dc:creator>Luke Nezda</dc:creator>
		<pubDate>Fri, 05 Feb 2010 03:06:37 +0000</pubDate>
		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=160#comment-9346</guid>
		<description>@BeforeClass requires decorated method be static: sample code here and copy+pasted to http://www.ibm.com/developerworks/java/library/j-swingtest/ should be:

@BeforeClass public static void setUpOnce() {
   FailOnThreadViolationRepaintManager.install();
}</description>
		<content:encoded><![CDATA[<p>@BeforeClass requires decorated method be static: sample code here and copy+pasted to <a href="http://www.ibm.com/developerworks/java/library/j-swingtest/" rel="nofollow">http://www.ibm.com/developerworks/java/library/j-swingtest/</a> should be:</p>
<p>@BeforeClass public static void setUpOnce() {<br />
   FailOnThreadViolationRepaintManager.install();<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luke Nezda</title>
		<link>http://alexruiz.developerblogs.com/?p=160&#038;cpage=1#comment-9345</link>
		<dc:creator>Luke Nezda</dc:creator>
		<pubDate>Fri, 05 Feb 2010 03:02:33 +0000</pubDate>
		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=160#comment-9345</guid>
		<description>Does combo of JUnit 4 @Test and @RunsInEDT do this?</description>
		<content:encoded><![CDATA[<p>Does combo of JUnit 4 @Test and @RunsInEDT do this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Ruiz</title>
		<link>http://alexruiz.developerblogs.com/?p=160&#038;cpage=1#comment-9047</link>
		<dc:creator>Alex Ruiz</dc:creator>
		<pubDate>Fri, 15 Jan 2010 18:37:49 +0000</pubDate>
		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=160#comment-9047</guid>
		<description>Hi Bruce,

We currently don&#039;t support such feature, mainly because there haven&#039;t been any requests for it.

I can see this feature is useful, as long as you don&#039;t use the AWT Robot (or FEST&#039;s Robot,) because they cannot run in the EDT.

Can you please file a ticket at http://jira.codehaus.org/browse/FEST ? I&#039;d like to keep this conversation there :)

Thanks!</description>
		<content:encoded><![CDATA[<p>Hi Bruce,</p>
<p>We currently don&#8217;t support such feature, mainly because there haven&#8217;t been any requests for it.</p>
<p>I can see this feature is useful, as long as you don&#8217;t use the AWT Robot (or FEST&#8217;s Robot,) because they cannot run in the EDT.</p>
<p>Can you please file a ticket at <a href="http://jira.codehaus.org/browse/FEST" rel="nofollow">http://jira.codehaus.org/browse/FEST</a> ? I&#8217;d like to keep this conversation there :)</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bruce Alspaugh</title>
		<link>http://alexruiz.developerblogs.com/?p=160&#038;cpage=1#comment-9046</link>
		<dc:creator>Bruce Alspaugh</dc:creator>
		<pubDate>Fri, 15 Jan 2010 18:28:34 +0000</pubDate>
		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=160#comment-9046</guid>
		<description>Is it possible to somehow tell the test runner (either JUnit or TestNG) that you would like to run a particular test method on the EDT?  Maybe specify in the annotation that the test is supposed to be submitted to a java.util.concurrent.Executor that executes its tasks on the EDT.  In other words say something similar to:

@Test(executor=SwingExecutor.class)
public void testMySwingComponentOnEDT() throws Exception {
// all this test code gets run on the Swing EDT
}

The test runner will wait for the test method to finish to see if it throws an exception.  If the test method throws an exception, it will be re-thrown back onto the original test runner thread. I think it would make the test code cleaner if we could avoid all the nested inner classes.</description>
		<content:encoded><![CDATA[<p>Is it possible to somehow tell the test runner (either JUnit or TestNG) that you would like to run a particular test method on the EDT?  Maybe specify in the annotation that the test is supposed to be submitted to a java.util.concurrent.Executor that executes its tasks on the EDT.  In other words say something similar to:</p>
<p>@Test(executor=SwingExecutor.class)<br />
public void testMySwingComponentOnEDT() throws Exception {<br />
// all this test code gets run on the Swing EDT<br />
}</p>
<p>The test runner will wait for the test method to finish to see if it throws an exception.  If the test method throws an exception, it will be re-thrown back onto the original test runner thread. I think it would make the test code cleaner if we could avoid all the nested inner classes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: a</title>
		<link>http://alexruiz.developerblogs.com/?p=160&#038;cpage=1#comment-8237</link>
		<dc:creator>a</dc:creator>
		<pubDate>Thu, 03 Dec 2009 12:30:11 +0000</pubDate>
		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=160#comment-8237</guid>
		<description>nice post but 
execute(new GuiTask() {
  public JLabel executeInEDT() {
    myCheckBox.setSelected(true);
  }
});
should be
final myCheckBox = ....;
execute(new GuiTask() {
  public voidexecuteInEDT() {
    myCheckBox.setSelected(true);
  }
});</description>
		<content:encoded><![CDATA[<p>nice post but<br />
execute(new GuiTask() {<br />
  public JLabel executeInEDT() {<br />
    myCheckBox.setSelected(true);<br />
  }<br />
});<br />
should be<br />
final myCheckBox = &#8230;.;<br />
execute(new GuiTask() {<br />
  public voidexecuteInEDT() {<br />
    myCheckBox.setSelected(true);<br />
  }<br />
});</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Ruiz</title>
		<link>http://alexruiz.developerblogs.com/?p=160&#038;cpage=1#comment-7700</link>
		<dc:creator>Alex Ruiz</dc:creator>
		<pubDate>Thu, 19 Nov 2009 13:04:37 +0000</pubDate>
		<guid isPermaLink="false">http://alexruiz.developerblogs.com/?p=160#comment-7700</guid>
		<description>Another good catch! :)

In the case of &#039;focus&#039;, that code was written before GuiActionRunner was introduced to FEST. Currently it waits till the component has focus, and fails if the component didn&#039;t get it.

You are right, it should call &#039;waitForIdle&#039; :)

Now that I revisit this method, I&#039;m not sure &#039;waitForIdle&#039; is good enough, since requesting focus is not a guaranteed action (per Swing Javadocs.) &#039;waitForIdle&#039; will wait till the EDT has no actions to process, but the component might never get the focus. In the case of testing, this is a big issue, since tests would expect a component to have focus.

In short, I need to call &#039;waitForIdle&#039; *and* check if the component has focus :)

(Can you please file a ticket?)

Thanks Per!</description>
		<content:encoded><![CDATA[<p>Another good catch! :)</p>
<p>In the case of &#8216;focus&#8217;, that code was written before GuiActionRunner was introduced to FEST. Currently it waits till the component has focus, and fails if the component didn&#8217;t get it.</p>
<p>You are right, it should call &#8216;waitForIdle&#8217; :)</p>
<p>Now that I revisit this method, I&#8217;m not sure &#8216;waitForIdle&#8217; is good enough, since requesting focus is not a guaranteed action (per Swing Javadocs.) &#8216;waitForIdle&#8217; will wait till the EDT has no actions to process, but the component might never get the focus. In the case of testing, this is a big issue, since tests would expect a component to have focus.</p>
<p>In short, I need to call &#8216;waitForIdle&#8217; *and* check if the component has focus :)</p>
<p>(Can you please file a ticket?)</p>
<p>Thanks Per!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
