Thursday, December 3, 2009

Create eclipse RCP application with tree view and write SWTBot test

We are going to develop our RCP application with a tree view and a write test package using SWTBot for this application.

INPUT POSTS(Create simple eclipse RCP)

Requirement:Eclipse 3.5 Galileo,JDK Version >= 1.5
This ation pplication will display a tree view with root element and two subelement of this root element. These subelement will further have child elements.

Steps:
- Follow all steps given in 'Create simple eclipse RCP' post.
- Create following packages
com.sam.rcp.client.example.model
com.sam.rcp.client.example.provider
com.sam.rcp.client.example.view

- Create these class in respective packages

Person.java
package com.sam.rcp.client.example.model;

public class Person {

String name;
Person parent;
Person[] children = new Person[0];

// constructors
public Person(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Person getParent() {
return parent;
}

public void setParent(Person parent) {
this.parent = parent;
}

public Person[] getChildren() {
return children;
}

public void setChildren(Person[] children) {
this.children = children;
}

public Person(String name, Person[] children) {
this.name = name;
this.children = children;
for (Person child : children) {
child.setParent(this);
}
}

}


DataProvider.java
package com.sam.rcp.client.example.provider;

import com.sam.rcp.client.example.model.Person;

public class DataProvider {

public static Person[] getInputData() {
return new Person[] { new Person("Root", new Person[] {
new Person("Parent one", new Person[] {
new Person("Child one-one"),
new Person("Child one-two") }),
new Person("Parent two", new Person[] {
new Person("Child two-one"),
new Person("Child two-two") }) }) };
}
}


MyTreeContentProvider.java
package com.sam.rcp.client.example.provider;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;

import com.sam.rcp.client.example.model.Person;

public class MyTreeContentProvider extends ArrayContentProvider implements
ITreeContentProvider {

public Object[] getChildren(Object parent) {
Person person = (Person) parent;

return person.getChildren();
}

public Object getParent(Object element) {
Person person = (Person) element;
return person.getParent();
}

public boolean hasChildren(Object element) {
Person person = (Person) element;
return person.getChildren().length > 0;
}
}


MyTreeLabelProvider.java
package com.sam.rcp.client.example.provider;

import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import com.sam.rcp.client.example.model.Person;

public class MyTreeLabelProvider extends LabelProvider {
public Image getImage(Object element) {
Person person = (Person) element;
if (person.getChildren() != null && person.getChildren().length > 0) {
return PlatformUI.getWorkbench().getSharedImages().getImage(
ISharedImages.IMG_OBJ_FOLDER);
}
return PlatformUI.getWorkbench().getSharedImages().getImage(
ISharedImages.IMG_OBJ_ELEMENT);
}

public String getText(Object element) {
Person person = (Person) element;
return person.getName();
}
}


TreeView.java
package com.sam.rcp.client.example.view;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.sam.rcp.client.example.provider.DataProvider;
import com.sam.rcp.client.example.provider.MyTreeContentProvider;
import com.sam.rcp.client.example.provider.MyTreeLabelProvider;

public class TreeView extends ViewPart {
private TreeViewer viewer;

public void createPartControl(Composite parent) {
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setContentProvider(new MyTreeContentProvider());
viewer.setLabelProvider(new MyTreeLabelProvider());
viewer.setInput(DataProvider.getInputData());
}

public void setFocus() {
viewer.getControl().setFocus();
}
}


- In plugin.xml add following extentions

plugin.xml




- When you run this example the output will be


Create SWTBot test for above application: This test will launch an eclipse and perform all operations defined in test class. This will fetch all element of tree in a sequence including leaf child. Then these values will be compared agains given values

TreeViewTest.java:
package com.sam.rcp.client.example;

import junit.framework.TestCase;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;

public class TreeViewTest extends TestCase {
public SWTWorkbenchBot bot;

public TreeViewTest() {
bot = new SWTWorkbenchBot();
}

public void testChangeView() throws Exception {
bot.viewByTitle("Welcome").close();
bot.menu("Window").menu("Show View").menu("Other...").click();
SWTBotShell openViewShell = bot.shell("Show View");
openViewShell.activate();
SWTBotTree tree = bot.tree();
for (SWTBotTreeItem item : tree.getAllItems()) {
if ("Tree View Category".equals(item.getText())) {

item.expand();
for (SWTBotTreeItem element : item.getItems()) {
if ("Tree View".equals(element.getText())) {

element.select();
}
}
}
}
bot.button("OK").click();
String[] expectation = { "Root","Parent one", "Child one-one","Child one-two","Parent two","Child two-one","Child two-two"};
int index = 0;
SWTBotTree viewTree = bot.tree();
for (SWTBotTreeItem item : viewTree.getAllItems()) {
assertEquals(expectation[index++], item.getText());
item.expand();
for (SWTBotTreeItem element : item.getItems()) {
assertEquals(expectation[index++], element.getText());
element.expand();
for (SWTBotTreeItem childElement : element.getItems()) {
assertEquals(expectation[index++], childElement.getText());
}
}
}
}
}

- Run test case

Note: All images can be viewed in new tab/window in proper view.

SWTBot test for RCP application, through command line

We are going to execute same test application (SWTBot test package for RCP application in Eclipse,Create simple eclipse RCP) through command line.

INPUT POSTS(Create simple eclipse RCP,SWTBot test package for RCP application in Eclipse)

Requirement:: headless version of SWTBot for Eclipse 3.5 Galileo
for the installation of SWTBot you can refer (http://www.eclipse.org/swtbot/downloads.php ->http://www.eclipse.org/downloads/download.php?file=/technology/swtbot/galileo/dev-build/org.eclipse.swtbot.eclipse.test-2.0.0.433-dev-e35.zip).

Steps:
Select File -> Export -> Deployable plug-ins and fragment and click next.

Browse directory and set it to your eclipse plug in directory for example C:\Data\SampleExamples\setup\eclipse\plugins and click Finish.


This will create a deployable jar in eclipse plug in directory like com.sam.rcp.client.example_1.0.0.xxxxxxx.jar.


Install Ant: (http://ant.apache.org/bindownload.cgi)


Create ant build file for exported project (com.sam.rcp.client.example)


Note: Right click and open in new tab/window


Open a command window and run test case. Go to directory where build file is for example

(example:C:\Data\SampleExamples\setup\build\SWTBotBuild) and run ant build.


This will launch a eclipse RCP and perform all operations similar as running from eclipse.


Note: Right click and open in new tab/window


Please follow posts for more advance example.



SWTBot test for RCP application in Eclipse

We are going to develop SWTBot test package to test eclipse RCP application. You can refer previous post for RCP application for which We are going to write test package.


INPUT POSTS(Create simple eclipse RCP)


Requirement:: SWTBot for Eclipse 3.5 Galileo

for the installation of SWTBot as eclipse plugin you can refer (http://www.eclipse.org/swtbot/downloads.php -> http://download.eclipse.org/technology/swtbot/galileo/dev-build/update-site)


Create test package:


Add source folder src/test/java to project and create package ‘com.sam.rcp.client.example’ in this folder. Create class ViewTest in this package.


Double click on plugìn.xml and click on dependencies tab. Add following required plugins


org.hamcrest;bundle-version="1.1.0",

org.eclipse.swtbot.eclipse.finder;bundle-version="2.0.0",

org.eclipse.swtbot.junit4_x;bundle-version="2.0.0",

org.eclipse.swtbot.swt.finder;bundle-version="2.0.0",

org.junit4;bundle-version="4.5.0"

Java Source Code:

ViewTest.java

package com.sam.rcp.client.example;

import junit.framework.TestCase;

import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;

import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;

import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;

import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;

public class ViewTest extends TestCase {

public SWTWorkbenchBot bot;

public ViewTest() {

bot = new SWTWorkbenchBot();

}

public void testChangeView() throws Exception {

if (bot.viewByTitle("Welcome") != null) {

bot.viewByTitle("Welcome").close();

}

bot.menu("Window").menu("Show View").menu("Other...").click();

SWTBotTree tree = bot.tree();

for (SWTBotTreeItem item : tree.getAllItems()) {

if ("Other".equalsIgnoreCase(item.getText())) {

item.expand();

for (SWTBotTreeItem element : item.getItems()) {

if ("View".equals(element.getText())) {

element.select();

}

}

}

}

bot.button("OK").click();

SWTBotTable table = bot.table();

//System.out.println(table);

Thread.sleep(2000);

int rowCount = table.rowCount();

String[] expectation = { "One", "Two", "Three" };

int currentRow = 0;

for (currentRow = 0; currentRow <>

assertEquals(expectation[currentRow], table

.getTableItem(currentRow).getText());

}

}

}

Above test method will launch an eclipse and perform all operation defined in java code. It will open required view and get the value of elements. These values will be compared against given values.

Run RCP test case:

Right click on ViewTest.java -> Run As -> SWTBot Test. This will launch a new eclipse RCP and
perform all operations. After completion window will close automatically. You can see the test
result in JUnit widow


You can analyze all operation visually, this will give you great feel.

Create simple eclipse RCP

We are going to develop our first RCP application and will develop test package using SWTBot to test this application (in next post).

Requirement:Eclipse 3.5 Galileo,JDK Version >= 1.5

Steps:
In Eclipse select File-> New Project. From the list select "Plug-In Project" and click.

Give your plug-in project name "com.sam.rcp.client.example".

Click "Next" and make the following settings. Select "Yes" at the question "Would you like to create a rich client application".


Click next and select the template "RCP application with a view" then
Click Finish.

Following project structure will be created.

Start your RCP application:
Select run -> run configuration -> right click on Eclipse Application -> new.
specify configuration name “sam.rcp.client.example” and click run


Select Window->Show View->Others…->Expand Other->Select View and click Ok.


Ouput: