Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* The Original Software is NetBeans. The Initial Developer of the Original |
41 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun |
42 |
* Microsystems, Inc. All Rights Reserved. |
43 |
* |
44 |
* If you wish your version of this file to be governed by only the CDDL |
45 |
* or only the GPL Version 2, indicate your decision by adding |
46 |
* "[Contributor] elects to include this software in this distribution |
47 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
48 |
* single choice of license, a recipient has the option to distribute |
49 |
* your version of this file under either the CDDL, the GPL Version 2 or |
50 |
* to extend the choice of license to its licensees as provided above. |
51 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
52 |
* Version 2 license, then the option applies only if the new code is |
53 |
* made subject to such option by the copyright holder. |
54 |
*/ |
55 |
package org.netbeans.modules.visual.bugs; |
56 |
|
57 |
import java.awt.Point; |
58 |
import java.util.concurrent.atomic.AtomicInteger; |
59 |
import org.netbeans.api.visual.anchor.AnchorFactory; |
60 |
import org.netbeans.api.visual.graph.GraphScene; |
61 |
import org.netbeans.api.visual.graph.layout.GraphLayoutListener; |
62 |
import org.netbeans.api.visual.graph.layout.GridGraphLayout; |
63 |
import org.netbeans.api.visual.graph.layout.UniversalGraph; |
64 |
import org.netbeans.api.visual.layout.LayoutFactory; |
65 |
import org.netbeans.api.visual.layout.SceneLayout; |
66 |
import org.netbeans.api.visual.widget.ConnectionWidget; |
67 |
import org.netbeans.api.visual.widget.LabelWidget; |
68 |
import org.netbeans.api.visual.widget.LayerWidget; |
69 |
import org.netbeans.api.visual.widget.Widget; |
70 |
import org.netbeans.modules.visual.framework.VisualTestCase; |
71 |
|
72 |
/** |
73 |
* |
74 |
* @author Ernest Lotter |
75 |
*/ |
76 |
public class GraphLayoutListenerRemoval197502Test extends VisualTestCase { |
77 |
|
78 |
public GraphLayoutListenerRemoval197502Test(String testName) { |
79 |
super(testName); |
80 |
} |
81 |
|
82 |
/** |
83 |
* Bug #197502 caused GraphLayout.remove() to not really remove listener, |
84 |
* but in fact add it again. |
85 |
* |
86 |
* This test will involve counting the number of times that the graph layout |
87 |
* listener is invoked, and failing when the count does not match the expected |
88 |
* number. |
89 |
*/ |
90 |
public void testGraphLayoutListenerRemoval() { |
91 |
// the number of times that the listener was invoked (graphLayoutStarted invocations) |
92 |
final AtomicInteger numStarts = new AtomicInteger(0); |
93 |
GraphLayoutListener<String,String> listener = new GraphLayoutListener<String, String>() { |
94 |
@Override |
95 |
public void graphLayoutStarted(UniversalGraph<String, String> graph) { |
96 |
numStarts.incrementAndGet(); |
97 |
} |
98 |
|
99 |
@Override |
100 |
public void graphLayoutFinished(UniversalGraph<String, String> graph) { |
101 |
} |
102 |
|
103 |
@Override |
104 |
public void nodeLocationChanged(UniversalGraph<String, String> graph, String node, Point previousPreferredLocation, Point newPreferredLocation) { |
105 |
} |
106 |
}; |
107 |
|
108 |
// make a basic scene |
109 |
StringGraphScene sgs = new StringGraphScene(); |
110 |
GridGraphLayout<String, String> graphLayout = new GridGraphLayout<String, String>(); |
111 |
SceneLayout sceneGraphLayout = LayoutFactory.createSceneGraphLayout(sgs, graphLayout); |
112 |
sceneGraphLayout.invokeLayout(); |
113 |
|
114 |
// add some nodes |
115 |
sgs.addNode("Node1"); |
116 |
sgs.addNode("Node2"); |
117 |
|
118 |
// (a) as listener not added yet, numStarts should be zero after first run |
119 |
graphLayout.layoutGraph(sgs); |
120 |
assertEquals(0, numStarts.get()); |
121 |
|
122 |
// add the listener |
123 |
graphLayout.addGraphLayoutListener(listener); |
124 |
|
125 |
// (b) listener added, we expect numStarts == 1 after single layout invocation |
126 |
graphLayout.layoutGraph(sgs); |
127 |
assertEquals(1, numStarts.get()); |
128 |
|
129 |
// remove the listener (#197502 would cause this to not really remove) |
130 |
graphLayout.removeGraphLayoutListener(listener); |
131 |
|
132 |
// (c) listener removed, we expect numStarts == 1 (still) after single layout invocation |
133 |
graphLayout.layoutGraph(sgs); |
134 |
assertEquals("GraphLayoutListener erroneously invoked after being removed from GraphLayout", 1, numStarts.get()); |
135 |
} |
136 |
|
137 |
/** |
138 |
* A simple string graph scene with which to perform the test |
139 |
*/ |
140 |
private class StringGraphScene extends GraphScene.StringGraph { |
141 |
private LayerWidget mainLayer; |
142 |
private LayerWidget connectionLayer; |
143 |
|
144 |
public StringGraphScene() { |
145 |
mainLayer = new LayerWidget(this); |
146 |
connectionLayer = new LayerWidget(this); |
147 |
addChild(mainLayer); |
148 |
addChild(connectionLayer); |
149 |
} |
150 |
|
151 |
protected Widget attachNodeWidget(String node) { |
152 |
Widget widget = new LabelWidget(this, node); |
153 |
mainLayer.addChild (widget); |
154 |
return widget; |
155 |
} |
156 |
|
157 |
protected Widget attachEdgeWidget(String edge) { |
158 |
ConnectionWidget connectionWidget = new ConnectionWidget(this); |
159 |
connectionLayer.addChild(connectionWidget); |
160 |
return connectionWidget; |
161 |
} |
162 |
|
163 |
protected void attachEdgeSourceAnchor(String edge, String oldSourceNode, String sourceNode) { |
164 |
((ConnectionWidget) findWidget(edge)).setSourceAnchor(AnchorFactory.createRectangularAnchor(findWidget(sourceNode))); |
165 |
} |
166 |
|
167 |
protected void attachEdgeTargetAnchor(String edge, String oldTargetNode, String targetNode) { |
168 |
((ConnectionWidget) findWidget(edge)).setTargetAnchor(AnchorFactory.createRectangularAnchor(findWidget(targetNode))); |
169 |
} |
170 |
|
171 |
public LayerWidget getMainLayer() { |
172 |
return mainLayer; |
173 |
} |
174 |
|
175 |
public LayerWidget getConnectionLayer() { |
176 |
return connectionLayer; |
177 |
} |
178 |
} |
179 |
} |