Skip to content

Commit

Permalink
Cleaned Zests (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
azoitl committed Nov 13, 2023
1 parent e181b16 commit 01deb9b
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 98 deletions.
Expand Up @@ -9,14 +9,14 @@
package org.eclipse.zest.tests;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphConnection;
import org.eclipse.zest.core.widgets.GraphNode;
Expand Down Expand Up @@ -62,14 +62,14 @@ public void testSelectAllGetSelection() {

@Test
public void testAddSelectionListenerEventIdentity() {
final List selectionEvents = new ArrayList();
final List<SelectionEvent> selectionEvents = new ArrayList<>();
graph.addSelectionListener(setupListener(selectionEvents));
graph.addSelectionListener(setupListener(selectionEvents));
Event event = new Event();
event.widget = nodes[0];
graph.notifyListeners(SWT.Selection, event);
assertEquals("Two listeners should receive one event each", 2, selectionEvents.size());
assertEquals("Two listeners should receive the same event", selectionEvents.get(0), selectionEvents.get(1));
assertEquals("Two listeners should receive one event each", 2, selectionEvents.size()); //$NON-NLS-1$
assertEquals("Two listeners should receive the same event", selectionEvents.get(0), selectionEvents.get(1)); //$NON-NLS-1$
}

/**
Expand All @@ -80,7 +80,7 @@ public void testSelectedNodeDisposal() {
graph.setSelection(nodes);
nodes[0].dispose();
graph.layout();
assertEquals("Disposing a selected node should remove it from the selection", 1, graph.getSelection().size());
assertEquals("Disposing a selected node should remove it from the selection", 1, graph.getSelection().size()); //$NON-NLS-1$
}

/**
Expand All @@ -89,18 +89,17 @@ public void testSelectedNodeDisposal() {
*/
@Test
public void testAddSelectionListenerSetSelection() {
final List selectionEvents = new ArrayList();
final List<SelectionEvent> selectionEvents = new ArrayList<>();
graph.addSelectionListener(setupListener(selectionEvents));
graph.setSelection(nodes);
assertEquals("Programmatic selection should not trigger events", 0, selectionEvents.size());
for (int i = 0; i < nodes.length; i++) {
GraphNode node = nodes[i];
assertTrue("Programmatic selection should select nodes", node.isSelected());
assertEquals("Programmatic selection should not trigger events", 0, selectionEvents.size()); //$NON-NLS-1$
for (GraphNode node : nodes) {
assertTrue("Programmatic selection should select nodes", node.isSelected()); //$NON-NLS-1$
}
graph.setSelection(new GraphNode[] { nodes[0] });
for (int i = 1; i < nodes.length; i++) {
GraphNode node = nodes[i];
assertFalse("Changing the selection should deselect the nodes selected before", node.isSelected());
assertFalse("Changing the selection should deselect the nodes selected before", node.isSelected()); //$NON-NLS-1$
}
}

Expand All @@ -110,13 +109,12 @@ public void testAddSelectionListenerSetSelection() {
*/
@Test
public void testAddSelectionListenerSelectAll() {
final List selectionEvents = new ArrayList();
final List<SelectionEvent> selectionEvents = new ArrayList<>();
graph.addSelectionListener(setupListener(selectionEvents));
graph.selectAll();
assertEquals("Programmatic selection should not trigger events", 0, selectionEvents.size());
for (Iterator iterator = graph.getNodes().iterator(); iterator.hasNext();) {
GraphNode node = (GraphNode) iterator.next();
assertTrue("Programmatic selection should set nodes selected", node.isSelected());
assertEquals("Programmatic selection should not trigger events", 0, selectionEvents.size()); //$NON-NLS-1$
for (GraphNode node : graph.getNodes()) {
assertTrue("Programmatic selection should set nodes selected", node.isSelected()); //$NON-NLS-1$
}
}

Expand All @@ -126,18 +124,18 @@ public void testAddSelectionListenerSelectAll() {
*/
@Test
public void testAddSelectionListenerNotifyListeners() {
final List selectionEvents = new ArrayList();
final List<SelectionEvent> selectionEvents = new ArrayList<>();
graph.addSelectionListener(setupListener(selectionEvents));
graph.setSelection(nodes);
Event event = new Event();
event.widget = graph;
graph.notifyListeners(SWT.Dispose, event);
assertEquals("Non-selection events should not be received", 0, selectionEvents.size());
assertEquals("Non-selection events should not be received", 0, selectionEvents.size()); //$NON-NLS-1$
graph.notifyListeners(SWT.Selection, event);
assertEquals("Selection events should be received", 1, selectionEvents.size());
assertEquals("Selection events should be received", 1, selectionEvents.size()); //$NON-NLS-1$
}

private SelectionListener setupListener(final List events) {
private static SelectionListener setupListener(final List<SelectionEvent> events) {
return new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
Expand Down
38 changes: 21 additions & 17 deletions org.eclipse.zest.tests/src/org/eclipse/zest/tests/GraphTests.java
Expand Up @@ -10,15 +10,17 @@

import java.util.List;

import org.eclipse.draw2d.Figure;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphConnection;
import org.eclipse.zest.core.widgets.GraphItem;
import org.eclipse.zest.core.widgets.GraphNode;
import org.eclipse.zest.core.widgets.internal.ZestRootLayer;

import org.eclipse.draw2d.Figure;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -51,22 +53,22 @@ public void setUp() throws Exception {

@Test
public void testGraphData() {
graph.setData("graph data");
assertEquals("graph data", graph.getData());
graph.setData("graph data"); //$NON-NLS-1$
assertEquals("graph data", graph.getData()); //$NON-NLS-1$
}

@Test
public void testNodeItemData() {
GraphItem item = (GraphNode) graph.getNodes().get(0);
item.setData("node item data");
assertEquals("node item data", item.getData());
GraphItem item = graph.getNodes().get(0);
item.setData("node item data"); //$NON-NLS-1$
assertEquals("node item data", item.getData()); //$NON-NLS-1$
}

@Test
public void testConnectionItemData() {
GraphItem item = (GraphConnection) graph.getConnections().get(0);
item.setData("connection item data");
assertEquals("connection item data", item.getData());
GraphItem item = graph.getConnections().get(0);
item.setData("connection item data"); //$NON-NLS-1$
assertEquals("connection item data", item.getData()); //$NON-NLS-1$
}

/**
Expand All @@ -78,9 +80,9 @@ public void testConnectionItemData() {
public void testDisposeGraphWithDisposedNode() {
nodes[0].dispose(); // removes the node from the graph's nodes list
((List<GraphNode>) graph.getNodes()).add(nodes[0]); // but we're malicious and add it back
assertTrue("Node should be disposed", nodes[0].isDisposed());
assertTrue("Node should be disposed", nodes[0].isDisposed()); //$NON-NLS-1$
graph.dispose();
assertTrue("Graph should be disposed", graph.isDisposed());
assertTrue("Graph should be disposed", graph.isDisposed()); //$NON-NLS-1$
}

/**
Expand All @@ -92,9 +94,9 @@ public void testDisposeGraphWithDisposedNode() {
public void testDisposeGraphWithDisposedConnection() {
connection.dispose();
((List<GraphConnection>) graph.getConnections()).add(connection);
assertTrue("Connection should be disposed", connection.isDisposed());
assertTrue("Connection should be disposed", connection.isDisposed()); //$NON-NLS-1$
graph.dispose();
assertTrue("Graph should be disposed", graph.isDisposed());
assertTrue("Graph should be disposed", graph.isDisposed()); //$NON-NLS-1$
}

/**
Expand All @@ -103,6 +105,7 @@ public void testDisposeGraphWithDisposedConnection() {
*
* @See https://bugs.eclipse.org/bugs/show_bug.cgi?id=361525
*/
@SuppressWarnings("static-method")
@Test
public void testUnHighlightNode() {
new ZestRootLayer().unHighlightNode(new Figure());
Expand All @@ -114,6 +117,7 @@ public void testUnHighlightNode() {
*
* @See https://bugs.eclipse.org/bugs/show_bug.cgi?id=361525
*/
@SuppressWarnings("static-method")
@Test
public void testUnHighlightConnection() {
new ZestRootLayer().unHighlightConnection(new Figure());
Expand All @@ -125,11 +129,11 @@ public void testUnHighlightConnection() {
*/
@Test
public void testDisposal() {
GraphNode n = (GraphNode) graph.getNodes().get(0);
GraphConnection c = (GraphConnection) graph.getConnections().get(0);
GraphNode n = graph.getNodes().get(0);
GraphConnection c = graph.getConnections().get(0);
shell.dispose();
assertTrue("The graph's nodes should be disposed", n.isDisposed());
assertTrue("The graph's edges should be disposed", c.isDisposed());
assertTrue("The graph's nodes should be disposed", n.isDisposed()); //$NON-NLS-1$
assertTrue("The graph's edges should be disposed", c.isDisposed()); //$NON-NLS-1$

}

Expand Down
Expand Up @@ -11,17 +11,17 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.util.DelegatingDragAdapter;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.jface.util.DelegatingDragAdapter;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.zest.core.viewers.GraphViewer;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphConnection;
Expand Down Expand Up @@ -60,7 +60,7 @@ public void setUp() {
public void testDisposalWithDropTarget() {
new DropTarget(viewer.getGraphControl(), DND.DROP_MOVE | DND.DROP_COPY);
shell.dispose();
Assert.assertTrue("The viewer's graph control should be disposed", viewer.getControl().isDisposed());
Assert.assertTrue("The viewer's graph control should be disposed", viewer.getControl().isDisposed()); //$NON-NLS-1$
}

/**
Expand All @@ -72,7 +72,7 @@ public void testDisposalWithDragSource() {
viewer.addDragSupport(DND.DROP_MOVE, new Transfer[] { TextTransfer.getInstance() },
new DelegatingDragAdapter());
shell.dispose();
Assert.assertTrue("The viewer's graph control should be disposed", viewer.getControl().isDisposed());
Assert.assertTrue("The viewer's graph control should be disposed", viewer.getControl().isDisposed()); //$NON-NLS-1$
}

/**
Expand All @@ -88,11 +88,11 @@ public void testValidSelection() {
graph.setSelection(new GraphItem[] { n1, n2, c });
GraphViewer viewer = new GraphViewer(shell, SWT.NONE);
viewer.setControl(graph);
assertEquals("No null data should be in the selection", 0,
assertEquals("No null data should be in the selection", 0, //$NON-NLS-1$
((StructuredSelection) viewer.getSelection()).size());
n1.setData("1");
n2.setData("2");
assertEquals("Other data should be in the selection", 2, ((StructuredSelection) viewer.getSelection()).size());
n1.setData("1"); //$NON-NLS-1$
n2.setData("2"); //$NON-NLS-1$
assertEquals("Other data should be in the selection", 2, ((StructuredSelection) viewer.getSelection()).size()); //$NON-NLS-1$
}

/**
Expand All @@ -101,14 +101,9 @@ public void testValidSelection() {
*/
@Test
public void testPostSelectionListener() {
final List selected = new ArrayList();
viewer.addPostSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
selected.add(event);
}
});
final List<SelectionChangedEvent> selected = new ArrayList<>();
viewer.addPostSelectionChangedListener(selected::add);
viewer.getControl().notifyListeners(SWT.Selection, new Event());
assertFalse("Post selection listeners should be notified", selected.isEmpty());
assertFalse("Post selection listeners should be notified", selected.isEmpty()); //$NON-NLS-1$
}
}

0 comments on commit 01deb9b

Please sign in to comment.