Skip to content

Commit

Permalink
Integrate Zest-based examples into our test suite
Browse files Browse the repository at this point in the history
With this change, we instantiate and verify the correctness of the Zest
examples as part of our JUnit test suite. Special care needs to be
taken, in order to make sure that the examples can be used in the test
environment, but also still be executed as standalone applications.

This means that those snippets must not create a new Display instance,
as one is already created by the test suite and multiple displays are
forbidden. The snippet is then instantiated by reflectively calling the
main method.

Because the snippets block until the shell is closed by the user, a
little trick is needed to allow for the tests to be executed. The actual
test is scheduled as an asynchronous task before the snippet is created,
which is then executed by the call to "Display.readAndDispatch()".

For this exact reason has the FileDialog been removed from
GraphJFaceSnippet3, as this would block the UI thread prematurely, thus
making testing impossible. The example is still functional, as it will
instead load the example file from the classpath.

In almost all cases, the SpringLayoutAlgorithm of the code snippets has
been replaced by a GridLayout, in order to make the test more stable and
reproducible.

This contribution is done in preparation for the Zest 2.0 migration as a
means to increase the test coverage and thus spot any changes that are
introduced.
  • Loading branch information
ptziegler authored and azoitl committed May 26, 2024
1 parent f6dc425 commit d606e62
Show file tree
Hide file tree
Showing 43 changed files with 2,024 additions and 181 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.zest.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.zest.core.viewers,
org.eclipse.zest.core.viewers.internal;x-internal:=true,
org.eclipse.zest.core.widgets,
org.eclipse.zest.core.widgets.internal;x-internal:=true
org.eclipse.zest.core.widgets.internal;x-friends:="org.eclipse.zest.tests"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Automatic-Module-Name: org.eclipse.zest.core
1 change: 1 addition & 0 deletions org.eclipse.zest.examples/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
</accessrules>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="icons"/>
<classpathentry kind="output" path="bin"/>
</classpath>
5 changes: 5 additions & 0 deletions org.eclipse.zest.examples/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Bundle-ManifestVersion: 2
Bundle-Name: %Plugin.name
Bundle-SymbolicName: org.eclipse.zest.examples
Bundle-Version: 3.17.0.qualifier
Export-Package: org.eclipse.zest.examples.jface;x-friends:="org.eclipse.zest.tests",
org.eclipse.zest.examples.swt;x-friends:="org.eclipse.zest.tests",
org.eclipse.zest.examples.uml;x-friends:="org.eclipse.zest.tests"
Bundle-Localization: plugin
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
Expand All @@ -18,4 +21,6 @@ Require-Bundle: org.eclipse.ui,
Bundle-RequiredExecutionEnvironment: JavaSE-17
Bundle-Vendor: %Plugin.providerName
Automatic-Module-Name: org.eclipse.zest.examples
Bundle-ClassPath: icons/,
.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC, Canada.
* Copyright 2005-2007, 2024, CHISEL Group, University of Victoria, Victoria,
* BC, Canada and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -106,8 +107,8 @@ public String getText(Object element) {
* @param args
*/
public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
Shell shell = new Shell();
Display d = shell.getDisplay();
shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setSize(400, 400);
Button button = new Button(shell, SWT.PUSH);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
* Canada.
* Copyright 2005-2007, 2024, CHISEL Group, University of Victoria, Victoria,
* BC, Canada and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -109,8 +109,8 @@ public String getText(Object element) {
* @param args
*/
public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
Shell shell = new Shell();
Display d = shell.getDisplay();
shell.setText("GraphJFaceSnippet2");
shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setSize(400, 400);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
* Canada.
* Copyright 2005-2007, 2024, CHISEL Group, University of Victoria, Victoria,
* BC, Canada and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -13,17 +13,16 @@
package org.eclipse.zest.examples.jface;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.jface.viewers.LabelProvider;
Expand All @@ -46,21 +45,6 @@
*/
public class GraphJFaceSnippet3 {

public static final String GRAPH = """
a calls b
a calls c
b calld d
b calls e
c calls f
c calls g
d calls h
d calls i
e calls j
e calls k
f calls l
f calls m
"""; //$NON-NLS-1$

static class SimpleGraphContentProvider implements IGraphContentProvider {

private StringTokenizer graph;
Expand Down Expand Up @@ -106,39 +90,25 @@ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

}

static GraphViewer viewer = null;

public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
Shell shell = new Shell();
Display display = shell.getDisplay();
shell.setText("Simple Graph File Format"); //$NON-NLS-1$

FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setFilterNames(new String[] { "Simple Graph Files (*.sgf)", "All Files (*.*)" }); //$NON-NLS-1$ //$NON-NLS-2$
dialog.setFilterExtensions(new String[] { "*.sgf", "*.*" }); // Windows wild cards //$NON-NLS-1$ //$NON-NLS-2$

String directory = System.getProperty("user.dir") + "/src/org/eclipse/zest/tests/jface/SimpleGraph.sgf"; // eclipse/zest/examples/jface/"; //$NON-NLS-1$ //$NON-NLS-2$
System.out.println(directory);
dialog.setFilterPath(directory);
// dialog.setFilterPath(System.getProperty("user.dir") +
// "src/org/eclipse/zest/examples/jface/"); //Windows path

shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setSize(400, 400);
GraphViewer viewer = null;

viewer = new GraphViewer(shell, SWT.NONE);
viewer.setContentProvider(new SimpleGraphContentProvider());
viewer.setLabelProvider(new LabelProvider());
viewer.setLayoutAlgorithm(new RadialLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING));

shell.open();
String fileName = dialog.open();

if (fileName == null) {
// use the sample graph
viewer.setInput(GRAPH);
} else {
FileReader fileReader = new FileReader(new File(fileName));
BufferedReader bufferedReader = new BufferedReader(fileReader);

try (InputStream is = GraphJFaceSnippet3.class.getResourceAsStream("SimpleGraph.sgf")) { //$NON-NLS-1$
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
StringBuilder stringBuffer = new StringBuilder();
while (bufferedReader.ready()) {
stringBuffer.append(bufferedReader.readLine() + "\n"); //$NON-NLS-1$
Expand All @@ -151,6 +121,5 @@ public static void main(String[] args) throws IOException {
display.sleep();
}
}
display.dispose();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
* Canada.
* Copyright 2005-2007, 2024, CHISEL Group, University of Victoria, Victoria,
* BC, Canada and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -100,8 +100,8 @@ public String getText(Object element) {
* @param args
*/
public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
Shell shell = new Shell();
Display d = shell.getDisplay();
shell.setText("GraphJFaceSnippet2");
shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setSize(400, 400);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
* Canada.
* Copyright 2005-2007, 2024, CHISEL Group, University of Victoria, Victoria,
* BC, Canada and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -113,8 +113,8 @@ public Image getImage(Object element) {
* @param args
*/
public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
Shell shell = new Shell();
Display d = shell.getDisplay();
shell.setText("GraphJFaceSnippet2");
shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setSize(400, 400);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
* Canada.
* Copyright 2005-2007, 2024, CHISEL Group, University of Victoria, Victoria,
* BC, Canada and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -126,8 +126,8 @@ public String getText(Object element) {
* @param args
*/
public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
Shell shell = new Shell();
Display d = shell.getDisplay();
shell.setText("GraphJFaceSnippet2"); //$NON-NLS-1$
shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setSize(400, 400);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ public void dispose() {
* @param args
*/
public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
Shell shell = new Shell();
Display d = shell.getDisplay();
shell.setText("GraphJFaceSnippet2"); //$NON-NLS-1$
shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setSize(400, 400);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/*******************************************************************************
* Copyright (c) 2011 Fabian Steeg. All rights reserved. This program and
* the accompanying materials are made available under the terms of the Eclipse
* Public License v1.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
* <p/>
* Copyright (c) 2011, 2024 Fabian Steeg and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors: Fabian Steeg - initial implementation
*******************************************************************************/
package org.eclipse.zest.examples.jface;
Expand All @@ -17,6 +20,7 @@

import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.zest.core.viewers.EntityConnectionData;
import org.eclipse.zest.core.viewers.GraphViewer;
import org.eclipse.zest.core.viewers.IGraphEntityContentProvider;
import org.eclipse.zest.core.viewers.ISelfStyleProvider;
Expand Down Expand Up @@ -81,6 +85,9 @@ public Image getImage(Object element) {

@Override
public String getText(Object element) {
if (element instanceof EntityConnectionData) {
return ""; //$NON-NLS-1$
}
return element.toString();
}

Expand Down Expand Up @@ -115,8 +122,8 @@ public void selfStyleNode(Object element, GraphNode node) {
static GraphViewer viewer = null;

public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
Shell shell = new Shell();
Display d = shell.getDisplay();
shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setSize(400, 400);
viewer = new GraphViewer(shell, SWT.NONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@
*
*/
public class AnimationSnippet {
private static Graph g;

public static void main(String[] args) {

Display d = new Display();
final Shell shell = new Shell(d);
final Shell shell = new Shell();
Display d = shell.getDisplay();
shell.setText("Animation Example");
shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setSize(400, 400);

Button b = new Button(shell, SWT.PUSH);
b.setText("Animate");

final Graph g = new Graph(shell, SWT.NONE);
g = new Graph(shell, SWT.NONE);

final GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
final GraphNode n2 = new GraphNode(g, SWT.NONE, "Rock");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
*
*/
public class CustomLayout {
private static Graph g;

public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
Shell shell = new Shell();
Display d = shell.getDisplay();
shell.setText("Custom Layout Example");
shell.setLayout(new FillLayout());
shell.setSize(400, 400);

Graph g = new Graph(shell, SWT.NONE);
g = new Graph(shell, SWT.NONE);

GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
GraphNode n2 = new GraphNode(g, SWT.NONE, "Rock");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
* Canada.
* Copyright 2005-2007, 2024, CHISEL Group, University of Victoria, Victoria,
* BC, Canada and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -38,15 +38,16 @@
*
*/
public class GraphSnippet1 {
private static Graph g;

public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
Shell shell = new Shell();
Display d = shell.getDisplay();
shell.setText("GraphSnippet1");
shell.setLayout(new FillLayout());
shell.setSize(400, 400);

Graph g = new Graph(shell, SWT.NONE);
g = new Graph(shell, SWT.NONE);

GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
GraphNode n2 = new GraphNode(g, SWT.NONE, "Rock");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
* Canada.
* Copyright 2005-2007, 2024, CHISEL Group, University of Victoria, Victoria,
* BC, Canada and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand Down Expand Up @@ -35,15 +35,16 @@
*
*/
public class GraphSnippet10 {
private static Graph g;

public static void main(String[] args) {
Display d = new Display();
Shell shell = new Shell(d);
Shell shell = new Shell();
Display d = shell.getDisplay();
shell.setText("GraphSnippet1");
shell.setLayout(new FillLayout());
shell.setSize(400, 400);

final Graph g = new Graph(shell, SWT.NONE);
g = new Graph(shell, SWT.NONE);

GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
n.setBorderColor(org.eclipse.draw2d.ColorConstants.yellow);
Expand Down
Loading

0 comments on commit d606e62

Please sign in to comment.