Commit 508aedc6 authored by for migration's avatar for migration
Browse files

added SWTBot client to listen for interactions with the GUI

parent 595ae1de
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SWTBotListener</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: SWTBotListener
Bundle-SymbolicName: SWTBotListener;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: Daniel
Require-Bundle: javax.inject,
org.eclipse.osgi,
org.eclipse.jface,
org.eclipse.e4.ui.model.workbench,
org.eclipse.e4.ui.di,
org.eclipse.e4.ui.services,
org.eclipse.ui
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Automatic-Module-Name: SWTBotListener
source.. = src/
output.. = bin/
bin.includes = plugin.xml,\
META-INF/,\
.,\
icons/,\
fragment.e4xmi
<?xml version="1.0" encoding="ASCII"?>
<fragment:ModelFragments xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:basic="http://www.eclipse.org/ui/2010/UIModel/application/descriptor/basic" xmlns:fragment="http://www.eclipse.org/ui/2010/UIModel/fragment" xmi:id="_BxaXACerEeWxCPrV0pAZQQ">
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_EI7oACerEeWxCPrV0pAZQQ" featurename="descriptors" parentElementId="xpath:/">
<elements xsi:type="basic:PartDescriptor" xmi:id="_KMklcCerEeWxCPrV0pAZQQ" elementId="test.partDescFragment.ASampleE4View" label="Sample View" iconURI="platform:/plugin/SWTBotListener/icons/Sample.png" tooltip="" category="Sample Category" contributionURI="bundleclass://SWTBotListener/swtbotlistener.parts.SampleView">
<tags>View</tags>
<tags>categoryTag:Sample Category</tags>
</elements>
</fragments>
</fragment:ModelFragments>
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
id="swtbotlistener.fragment"
point="org.eclipse.e4.workbench.model">
<fragment
apply="notexists"
uri="fragment.e4xmi">
</fragment>
</extension>
<extension
point="org.eclipse.ui.startup">
<startup class="swtbotlistener.startup.StartSWTBotListener"/>
</extension>
</plugin>
package swtbotlistener.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.Socket;
import java.time.Instant;
public class ClientConnection extends Thread{
private int port;
EventHandler handler;
public ClientConnection(int port, EventHandler handler) {
this.port = port;
this.handler = handler;
}
@Override
public void run() {
Socket socket = null;
String host = "localhost";
while (socket == null) {
try {
socket = new Socket(host, port);
} catch (ConnectException e) {
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
}
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = in.readLine()) != null && !interrupted()) {
String timestamp = String.format("%d: ", Instant.now().toEpochMilli());
handler.handleEvent(timestamp + line);
}
socket.close();
} catch (IOException e) {
// Client was shut down through closeSocket() and readLine threw
// an exception.
return;
}
}
}
package swtbotlistener.client;
public interface EventHandler {
public void handleEvent(String line);
}
package swtbotlistener.client;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterEventHandler implements EventHandler {
private static final String LINE_SEPERATOR = System.getProperty("line.separator");
private File file;
private FileWriter fileWriter;
public FileWriterEventHandler(File file) {
this.file = file;
}
public void init() throws IOException {
if (file.exists()) {
// do not do anything------
}
if(file.exists()==false)
{
file.createNewFile();
}
this.fileWriter = new FileWriter(file,true);
}
@Override
public void handleEvent(String line) {
try {
fileWriter.write(line);
fileWriter.write(LINE_SEPERATOR);
fileWriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package swtbotlistener.parts;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
public class SampleView {
private Label myLabelInView;
@PostConstruct
public void createPartControl(Composite parent) {
System.out.println("Enter in SampleE4View postConstruct");
myLabelInView = new Label(parent, SWT.BORDER);
myLabelInView.setText("This is a sample E4 view");
}
@Focus
public void setFocus() {
myLabelInView.setFocus();
}
/**
* This method is kept for E3 compatiblity. You can remove it if you do not
* mix E3 and E4 code. <br/>
* With E4 code you will set directly the selection in ESelectionService and
* you do not receive a ISelection
*
* @param s
* the selection received from JFace (E3 mode)
*/
@Inject
public void setSelection(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection s) {
if (s==null || s.isEmpty())
return;
if (s instanceof IStructuredSelection) {
IStructuredSelection iss = (IStructuredSelection) s;
if (iss.size() == 1)
setSelection(iss.getFirstElement());
else
setSelection(iss.toArray());
}
}
/**
* This method manages the selection of your current object. In this example
* we listen to a single Object (even the ISelection already captured in E3
* mode). <br/>
* You should change the parameter type of your received Object to manage
* your specific selection
*
* @param o
* : the current object received
*/
@Inject
public void setSelection(@Named(IServiceConstants.ACTIVE_SELECTION) Object o) {
// Remove the 2 following lines in pure E4 mode, keep them in mixed mode
if (o instanceof ISelection) // Already captured
return;
// Test if label exists (inject methods are called before PostConstruct)
if (myLabelInView != null)
myLabelInView.setText("Current single selection class is : " + o.getClass());
}
/**
* This method manages the multiple selection of your current objects. <br/>
* You should change the parameter type of your array of Objects to manage
* your specific selection
*
* @param o
* : the current array of objects received in case of multiple selection
*/
@Inject
public void setSelection(@Named(IServiceConstants.ACTIVE_SELECTION) Object[] selectedObjects) {
// Test if label exists (inject methods are called before PostConstruct)
if (myLabelInView != null)
myLabelInView.setText("This is a multiple selection of " + selectedObjects.length + " objects");
}
}
package swtbotlistener.startup;
import java.io.File;
import java.io.IOException;
import org.eclipse.ui.IStartup;
import swtbotlistener.client.ClientConnection;
import swtbotlistener.client.FileWriterEventHandler;
public class StartSWTBotListener implements IStartup{
private static final String TEST_FILE = "./interaction_data.txt";
FileWriterEventHandler handler;
@Override
public void earlyStartup() {
int port = Integer.parseInt(System.getProperty("org.eclipse.swtbot.generator.server.enable"));
handler = new FileWriterEventHandler(new File(TEST_FILE));
try {
handler.init();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ClientConnection connection = new ClientConnection(port, handler);
System.out.println("[SWTBot] Starting Up");
connection.start();
}
}
......@@ -53,5 +53,10 @@ Require-Bundle: org.eclipse.ui.ide.application,
org.fortiss.tooling.kernel.ui,
org.fortiss.af3.specification;bundle-version="2.13.0",
org.fortiss.af3.specification.ui;bundle-version="2.13.0",
org.fortiss.af3.testing.ui;bundle-version="2.13.0"
org.fortiss.af3.testing.ui;bundle-version="2.13.0",
org.eclipse.swtbot.generator;bundle-version="2.7.0",
org.eclipse.swtbot.generator.jdt;bundle-version="2.7.0",
org.eclipse.swtbot.generator.rules.workbench;bundle-version="2.7.0",
org.eclipse.swtbot.generator.ui;bundle-version="2.7.0",
SWTBotListener;bundle-version="1.0.0"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment