Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » SWTBot » SWTBot for custom SWT control
SWTBot for custom SWT control [message #1000772] Tue, 15 January 2013 12:13 Go to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Hi,

I created a custom SWT control that contains of two TreeViewers, supporting moving items from left to right and vice versa. I am planning to contribute this to Nebula, but before that, I need to fix some special issues related to TreeViewers.

Said that, I wanted to add several test cases, using SWTBot. But I can not figure out how to get the test cases running with SWTBot.

Maybe I'm just missing some basic parts, but everything I find searching the web is related to Eclipse applications using the workbench. Of course I could create a small example application that consists of just a part with that composite. But shouldn't it be possible to test plain SWT? At least I read that it is possible, but with no examples.

Running the test cases as pure JUnit test out of Eclipse will cause an exception telling me the Realm can not be null. This is because I use databinding in the back.

In some example code I wrote, I solved it this way in a main method
final Display display = Display.getDefault();
final Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout());
shell.setSize(400, 300);
shell.setText("Tree chooser example");

		
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
	public void run() {
		new TreeChooserExample(true).createControl(shell);
				
		shell.open();
				
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
});


But how to solve this with SWTBot?

Any help is appreciated.

Greez,
Dirk
Re: SWTBot for custom SWT control [message #1000803 is a reply to message #1000772] Tue, 15 January 2013 12:59 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 865
Registered: July 2009
Location: Grenoble, France
Senior Member

On 01/15/2013 01:13 PM, Dirk Fauth wrote:
> Maybe I'm just missing some basic parts, but everything I find searching
> the web is related to Eclipse applications using the workbench. Of
> course I could create a small example application that consists of just
> a part with that composite. But shouldn't it be possible to test plain
> SWT? At least I read that it is possible, but with no examples.

SWTBot Finders for SWT is a simple jar and API that depends only on SWT,
so it's possible to embed it in any SWT-based application, with or
without RCP.
Just putting the org.eclipse.swtbot.swt.finder jar in your classpath and
using APIs should be fine.


> Running the test cases as pure JUnit test out of Eclipse will cause an
> exception telling me the Realm can not be null. This is because I use
> databinding in the back.

This is not related to SWTBot. Does your UI object support pure-SWT ? If
you use databinding, you have to make sure the necessary jars/bundles
are in your classpath.
I don't know whether databinding works out of RCP.


> But how to solve this with SWTBot?

SWTBot is not aware of any databinding or JFace stuff. It's a simple bot
that will allow you to click on buttons or trees (and more). It should
work for your widget as it already work for any Wizard or View. You're
not creating a new low-level widget, but instead you're doing a
higher-level UI element made of several SWT widgets. I don't think you
need anything specific from SWTBot.

HTH,
--
Mickael Istria
JBoss, by Red Hat
My blog: http://mickaelistria.wordpress.com
My Tweets: http://twitter.com/mickaelistria
Re: SWTBot for custom SWT control [message #1000828 is a reply to message #1000803] Tue, 15 January 2013 13:41 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Hi and thanks for your reply,

I have read your answer in several other posts before, but never understood what to do. But finally I think I got it. Although it seems to be strange. So please correct me if I'm wrong now.

As far as I understand it now, if I want to test plain SWT with JUnit tests, I have to start my SWT application in every JUnit test, do my test cases and then close it. This will result in some ugly testing code, as the starting and stoping will always be the same code. Unless there are lambdas in Java.

@Test
public void testInit() {
	final Display display = Display.getDefault();
	final Shell shell = new Shell(display, SWT.SHELL_TRIM);
	shell.setLayout(new FillLayout());
	shell.setSize(400, 300);
		
	Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
		public void run() {
			createMyControl(shell);
				
			shell.open();
				
			bot = new SWTBot();
				
			assertEquals(3, bot.widget(widgetOfType(MyControl.class)).getTree().getItemCount());
		}
	});

	shell.dispose();
	display.dispose();
}


Is that correct?

Greez,
Dirk
Re: SWTBot for custom SWT control [message #1000878 is a reply to message #1000828] Tue, 15 January 2013 15:30 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 865
Registered: July 2009
Location: Grenoble, France
Senior Member

On 01/15/2013 02:41 PM, Dirk Fauth wrote:

> As far as I understand it now, if I want to test plain SWT with JUnit
> tests, I have to start my SWT application in every JUnit test, do my
> test cases and then close it. This will result in some ugly testing
> code, as the starting and stoping will always be the same code. Unless
> there are lambdas in Java.

When using PDE or Tycho to run tests, there is a launcher application
that control both starting of the host application and running your
tests. Note that it's totally independent from SWTBot.
If you want to run some "standard" java tests, you don't have such a
launcher to start your SWT application. So all your tests need to start
the SWT application they want to test.

Only difference on using SWTBot or not is that when using SWTBot, you
must put SWTBot in your classpath.
--
Mickael Istria
JBoss, by Red Hat
My blog: http://mickaelistria.wordpress.com
My Tweets: http://twitter.com/mickaelistria
Re: SWTBot for custom SWT control [message #1001322 is a reply to message #1000878] Wed, 16 January 2013 13:40 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Hi and thanks a lot for your time and your answers,

Quote:
When using PDE or Tycho to run tests, there is a launcher application
that control both starting of the host application and running your
tests.


Well I don't find that for plain SWT. For Eclipse applications this works with the application and product configuration parameters of the tycho-surefire plugin.

Am I missing something on that or did you just mean the launcher for Eclipse applications?
Re: SWTBot for custom SWT control [message #1001347 is a reply to message #1001322] Wed, 16 January 2013 14:40 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 865
Registered: July 2009
Location: Grenoble, France
Senior Member

On 01/16/2013 02:40 PM, Dirk Fauth wrote:
> Hi and thanks a lot for your time and your answers,
>
> Quote:
>> When using PDE or Tycho to run tests, there is a launcher application
>> that control both starting of the host application and running your
>> tests.
>
>
> Well I don't find that for plain SWT. For Eclipse applications this
> works with the application and product configuration parameters of the
> tycho-surefire plugin.
>
> Am I missing something on that or did you just mean the launcher for
> Eclipse applications?

I mean tycho-surefire-plugin starts an Eclipse application that controls
wraps your host application + execution of your tests.

In any case, there is no such thing for non-Eclipse application.

SWTBot used to have tests that were run on non-Eclipse environment, but
there execution was annoying to maintain and did not provide much added
value since we trust SWT behaves the same inside or outside of Eclipse.
So we abandoned them.


--
Mickael Istria
JBoss, by Red Hat
My blog: http://mickaelistria.wordpress.com
My Tweets: http://twitter.com/mickaelistria
Re: SWTBot for custom SWT control [message #1001385 is a reply to message #1001347] Wed, 16 January 2013 15:52 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Hi Mickael,

thanks a lot for helping me on this. As I have found the same question several times in several places while I was researching, I created a blog post about it.

http://www.vogella.com/blog/2013/01/16/testing-plain-swt-with-swtbot/

I hope everything is correct and I'm not telling anything wrong. Maybe you want to have a look. If you find something wrong, I will correct that of course.

Greez,
Dirk
Re: SWTBot for custom SWT control [message #1001417 is a reply to message #1001385] Wed, 16 January 2013 16:54 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 865
Registered: July 2009
Location: Grenoble, France
Senior Member

On 01/16/2013 04:52 PM, Dirk Fauth wrote:
> Hi Mickael,
>
> thanks a lot for helping me on this. As I have found the same question
> several times in several places while I was researching, I created a
> blog post about it.
>
> http://www.vogella.com/blog/2013/01/16/testing-plain-swt-with-swtbot/

That's very cool.

> I hope everything is correct and I'm not telling anything wrong. Maybe
> you want to have a look. If you find something wrong, I will correct
> that of course.

I think it's correct. I would just like to highlight the fact that
SWTBot IDE can start an Eclipse application to run tests when you run
"Run as SWTBot test". In fact, that's fully taken from PDE "Run As
Plugin Test" so the magic is in re-using PDE mechanism that allows that.
Tycho provided the same as part of the tycho-surefire-plugin.

So it's not that much SWTBoot magic, it's a general Eclipse tests magic.

--
Mickael Istria
JBoss, by Red Hat
My blog: http://mickaelistria.wordpress.com
My Tweets: http://twitter.com/mickaelistria
Re: SWTBot for custom SWT control [message #1001679 is a reply to message #1001417] Thu, 17 January 2013 07:32 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Quote:

I think it's correct. I would just like to highlight the fact that
SWTBot IDE can start an Eclipse application to run tests when you run
"Run as SWTBot test". In fact, that's fully taken from PDE "Run As
Plugin Test" so the magic is in re-using PDE mechanism that allows that.
Tycho provided the same as part of the tycho-surefire-plugin.

So it's not that much SWTBoot magic, it's a general Eclipse tests magic.


That's an information of great value for understanding the "magic" of running an application automatically. Smile

I updated my blogpost for this information.
Re: SWTBot for custom SWT control [message #1005669 is a reply to message #1001417] Mon, 28 January 2013 08:15 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 01/16/2013 05:54 PM, Mickael Istria wrote:
> I think it's correct. I would just like to highlight the fact that
> SWTBot IDE can start an Eclipse application to run tests when you run
> "Run as SWTBot test". In fact, that's fully taken from PDE "Run As
> Plugin Test" so the magic is in re-using PDE mechanism that allows that.
> Tycho provided the same as part of the tycho-surefire-plugin.

Hi

if I understand correctly, the launch "Rus as SWTBot test" basically
corresponds to a launch configuration "Run as Plug-in Test" where the
checkbox "Run in UI test" is unchecked, right?

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it


Re: SWTBot for custom SWT control [message #1005775 is a reply to message #1005669] Mon, 28 January 2013 16:55 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 865
Registered: July 2009
Location: Grenoble, France
Senior Member

On 01/28/2013 09:15 AM, Lorenzo Bettini wrote:
> if I understand correctly, the launch "Rus as SWTBot test" basically
> corresponds to a launch configuration "Run as Plug-in Test" where the
> checkbox "Run in UI test" is unchecked, right?

More or less,
It starts exactly the same way as PDE laucher, but uses a different
application (org.eclipse.swtbot.eclipse.core.application) to orchestrate
tests.
But the SWTBot test application is very close the the traditional
Eclipse test application. The only added value can be the screenshot
when test fail.

Running an SWTBot as a normal Test plugin, with "Run in UI thread"
unchecked is not bad.

Here is the definition of the laucher, you'll notice there are almost no
differences with PDE:
*
http://git.eclipse.org/c/swtbot/org.eclipse.swtbot.git/tree/org.eclipse.swtbot.eclipse.ui/plugin.xml
*
http://git.eclipse.org/c/swtbot/org.eclipse.swtbot.git/tree/org.eclipse.swtbot.eclipse.ui/src/org/eclipse/swtbot/eclipse/ui/SWTBotLaunchConfigurationDelegate.java


--
Mickael Istria
JBoss, by Red Hat
My blog: http://mickaelistria.wordpress.com
My Tweets: http://twitter.com/mickaelistria
Re: SWTBot for custom SWT control [message #1005806 is a reply to message #1005775] Mon, 28 January 2013 19:46 Go to previous message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 01/28/2013 05:55 PM, Mickael Istria wrote:
> On 01/28/2013 09:15 AM, Lorenzo Bettini wrote:
>> if I understand correctly, the launch "Rus as SWTBot test" basically
>> corresponds to a launch configuration "Run as Plug-in Test" where the
>> checkbox "Run in UI test" is unchecked, right?
>
> More or less,
> It starts exactly the same way as PDE laucher, but uses a different
> application (org.eclipse.swtbot.eclipse.core.application) to orchestrate
> tests.
> But the SWTBot test application is very close the the traditional
> Eclipse test application. The only added value can be the screenshot
> when test fail.
>
> Running an SWTBot as a normal Test plugin, with "Run in UI thread"
> unchecked is not bad.

I usually use that launch in situations where swtbot is in the target
platform, but NOT installed in the IDE; furthermore, as far as I know,
it's also the standard way of running swtbot tests headless with
Buckminster.

Thanks for the links!

cheers
Lorenzo


--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it


Previous Topic:Do you want a recorder for SWTBot ?
Next Topic:How to using Enter key on a text box
Goto Forum:
  


Current Time: Thu Mar 28 23:56:20 GMT 2024

Powered by FUDForum. Page generated in 0.03500 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top