Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » SWTBot » How to test non-eclipse code
How to test non-eclipse code [message #485871] Tue, 15 September 2009 10:54 Go to next message
Eclipse UserFriend
Originally posted by: michael.danielsson.tieto.com

I have searched for a simple example where some non-eclipse GUI code is
tested, but I haven't found any.

Can someone please create a some tests where the following class is tested


package com.md.glosy.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class QGroup {
private Group group;
private Text text;


public QGroup(Composite composite, int style) {
group = createQuestionGroup(composite, style);
}

private Group createQuestionGroup(Composite composite, int style) {
Group newGroup = new Group(composite, style);
newGroup.setText("QGroup");
newGroup.setLayout(new GridLayout());

Composite c = new Composite(newGroup, style);
c.setLayout(new GridLayout());

Label l = new Label(c, SWT.WRAP);
l.setText("Översätt följande:");

text = new Text(c, SWT.SINGLE | SWT.BORDER);
newGroup.pack();

return newGroup;
}


public void clearQuestion() {
text.setText("");
}

public void setQuestion(String question) {
text.setText(question);
}

public String getQuestion() {
return text.getText();
}

public void setTitle(String title) {
group.setText(title);
}

}


Regards, Michael
Re: How to test non-eclipse code [message #485878 is a reply to message #485871] Tue, 15 September 2009 12:07 Go to previous messageGo to next message
Ketan Padegaonkar is currently offline Ketan PadegaonkarFriend
Messages: 873
Registered: July 2009
Senior Member
On 15/09/09 4:24 PM, Michael Danielsson wrote:
> I have searched for a simple example where some non-eclipse GUI code is
> tested, but I haven't found any.
>
> Can someone please create a some tests where the following class is tested

The class you mention does nothing interesting worth testing -- it just
delegates to the correct text boxes for doing stuff. There's no logic in
the class worth testing.

If you're really interested in testing if that delegation is actually
done, there's other ways to do it instead of using swtbot -- mocking.

class QGroup {

QGroup(IText text){
this.text = text;
}

setQuestion(String question){
text.setText(question);
}

}

The implementation of the IText interface is something that delegates to
swt Text:

class MyText implements IText {

MyText(Composite parent, int style){
this.text = new Text(parent, style);
}

void setText(String text){
text.setText(text);
}

void getText(){
text.getText();
}
}

The way I'd test this using mockito (mockito.org) is:

IText t = mock(IText.class);
QGroup g = new QGroup(t);
g.setText("foo");

verify(t).setText("foo");

If however you'd like to use swtbot to test it -- just create a shell
with the QGroup and write swtbot tests the way a real human being would
test it.

--
Ketan
http://studios.thoughtworks.com/twist | http://twitter.com/ketanpkr
Re: How to test non-eclipse code [message #487565 is a reply to message #485878] Wed, 23 September 2009 15:28 Go to previous messageGo to next message
Michael Danielsson is currently offline Michael DanielssonFriend
Messages: 2
Registered: September 2009
Junior Member
I know this is a very simple example, but I have searched for a simple example where some non-eclipse GUI code is
tested with SwtBot and haven't found any. So please if you have the time give me the code that tests this simple class.

Regards,
Michael Danielsson
Re: How to test non-eclipse code [message #487638 is a reply to message #487565] Wed, 23 September 2009 20:35 Go to previous messageGo to next message
Pascal G is currently offline Pascal GFriend
Messages: 157
Registered: July 2009
Senior Member
Michael Danielsson wrote:
> I know this is a very simple example, but I have searched for a simple
> example where some non-eclipse GUI code is
> tested with SwtBot and haven't found any. So please if you have the time
> give me the code that tests this simple class.
>
> Regards,
> Michael Danielsson
>

SWTBot is made to test SWT widget only. I don't really understand what
you are trying to achieve here: the bot can only find things it knows
about, hence it can only find org.eclipse.swt stuff... you could use the
approach described by Ketan to test your own widgets (which all they
really do is delegation...).

Other then that, you could create your own mini-framework which mimics
your classes (exactly like what SWTBot does):

class QGroupHelper {
private SWTBotText text;
private AbstractSWTBot<Group> group;

public QGroupHelper(SWTBotText text, AbstractSWTBot<Group> group) {
this.text = text;
this.group = group;
}

public void clearQuestion() {
text.setText("");
}

public void setQuestion(String question) {
text.setText(question);
}

public String getQuestion() {
return text.getText();
}

public void setTitle(String title) {
// Group is not currently supported inside of SWTBot
}
}

class QBot
{
private bot = new SWTBot();

public QGroupHelper findQGroup(String question)
{
return new QGroupHelper(bot.text(question),
bot.widget(widgetOfType(Group.class)));
// Warning: the search here might not work. It might not
associate the good text and group. A special search for siblings and/or
parent could be provided, but I don't know how.
}
}

That's the base idea, you'll have to work on the details on your own
though...

Hope this helps
--
Pascal Gélinas | Software Developer
*Nu Echo Inc.*
http://www.nuecho.com/ | http://blog.nuecho.com/

*Because performance matters.*
Re: How to test non-eclipse code [message #487677 is a reply to message #487638] Thu, 24 September 2009 06:54 Go to previous message
Michael Danielsson is currently offline Michael DanielssonFriend
Messages: 2
Registered: September 2009
Junior Member
I have tried to separete the gui from the logic and therefore I have classes which like in this case only creates a composite with some content. Since I have separated the gui from the logic I can test a lot of my code with JUnit, but I also would like to test the gui.
In the qui tests I would like to check that some controls exist in the composite and I also would like to check that the getters and setters work. In many cases I also want to check that an event is triggered from the gui.

So what I am looking for is some code which, I suppose, adds a composite to a shell, checks that all controlls are there, enters some text, presses some buttons and check that the correct events are fired.
As I have said before I have looked for a simple example with code that I can use but I have not found any.

Is it possible to do this with SwtBot?


Regards,
Michael Danielsson
Previous Topic:buttons not found -- what can be done?
Next Topic:How to test RCP product?
Goto Forum:
  


Current Time: Sun Dec 21 14:37:42 GMT 2014

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

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