Home » Eclipse Projects » SWTBot » SWTBot for custom SWT control
SWTBot for custom SWT control [message #1000772] |
Tue, 15 January 2013 12:13 |
Dirk Fauth Messages: 2903 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 |
|
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 |
Dirk Fauth Messages: 2903 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 |
|
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 #1001347 is a reply to message #1001322] |
Wed, 16 January 2013 14:40 |
|
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 #1001417 is a reply to message #1001385] |
Wed, 16 January 2013 16:54 |
|
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 #1005806 is a reply to message #1005775] |
Mon, 28 January 2013 19:46 |
|
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
HOME: http://www.lorenzobettini.it
TDD Book: https://leanpub.com/tdd-buildautomation-ci
Xtext Book: https://www.packtpub.com/application-development/implementing-domain-specific-languages-xtext-and-xtend-second-edition
|
|
|
Goto Forum:
Current Time: Sat Nov 09 02:53:42 GMT 2024
Powered by FUDForum. Page generated in 0.04791 seconds
|