-
Notifications
You must be signed in to change notification settings - Fork 48
/
GraphViewerTests.java
109 lines (98 loc) · 3.81 KB
/
GraphViewerTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
* 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/>
* Contributors: Fabian Steeg - initial tests
*******************************************************************************/
package org.eclipse.zest.tests;
import java.util.ArrayList;
import java.util.List;
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;
import org.eclipse.zest.core.widgets.GraphItem;
import org.eclipse.zest.core.widgets.GraphNode;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Tests for the {@link GraphViewer} class.
*
* @author Fabian Steeg (fsteeg)
*
*/
public class GraphViewerTests extends Assert {
private GraphViewer viewer;
private Shell shell;
/**
* Set up the shell and viewer to use in the tests.
*/
@Before
public void setUp() {
shell = new Shell();
viewer = new GraphViewer(shell, SWT.NONE);
}
/**
* Create a drop target on a viewer's control and check disposal (see
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=200732)
*/
@Test
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()); //$NON-NLS-1$
}
/**
* Create a drag source on a viewer and check disposal (see
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=334009)
*/
@Test
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()); //$NON-NLS-1$
}
/**
* Assert that no invalid selections with null data are produced by the viewer
* (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=356449)
*/
@Test
public void testValidSelection() {
Graph graph = new Graph(shell, SWT.NONE);
GraphNode n1 = new GraphNode(graph, SWT.NONE);
GraphNode n2 = new GraphNode(graph, SWT.NONE);
GraphConnection c = new GraphConnection(graph, SWT.NONE, n1, n2);
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, //$NON-NLS-1$
((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$
}
/**
* Assert that listeners for post selection events are properly notified by the
* viewer (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=366916)
*/
@Test
public void testPostSelectionListener() {
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()); //$NON-NLS-1$
}
}