Added
Link Here
|
1 |
/* |
2 |
* Copyright (c) 2006 Borland Software Corporation |
3 |
* |
4 |
* All rights reserved. This program and the accompanying materials |
5 |
* are made available under the terms of the Eclipse Public License v1.0 |
6 |
* which accompanies this distribution, and is available at |
7 |
* http://www.eclipse.org/legal/epl-v10.html |
8 |
* |
9 |
* Contributors: |
10 |
* Michael Golubev (Borland) - initial API and implementation |
11 |
*/ |
12 |
|
13 |
package org.eclipse.gmf.tests.gen; |
14 |
|
15 |
import java.util.HashMap; |
16 |
import java.util.Iterator; |
17 |
import java.util.LinkedList; |
18 |
import java.util.List; |
19 |
|
20 |
import junit.framework.Assert; |
21 |
|
22 |
import org.eclipse.draw2d.IFigure; |
23 |
import org.eclipse.draw2d.Label; |
24 |
import org.eclipse.draw2d.Polyline; |
25 |
import org.eclipse.draw2d.geometry.PointList; |
26 |
import org.eclipse.emf.ecore.EClass; |
27 |
import org.eclipse.emf.ecore.EObject; |
28 |
import org.eclipse.emf.ecore.EStructuralFeature; |
29 |
import org.eclipse.emf.ecore.ETypedElement; |
30 |
import org.eclipse.gmf.gmfgraph.BasicFont; |
31 |
import org.eclipse.gmf.gmfgraph.Color; |
32 |
import org.eclipse.gmf.gmfgraph.ColorConstants; |
33 |
import org.eclipse.gmf.gmfgraph.ConstantColor; |
34 |
import org.eclipse.gmf.gmfgraph.Dimension; |
35 |
import org.eclipse.gmf.gmfgraph.Figure; |
36 |
import org.eclipse.gmf.gmfgraph.Font; |
37 |
import org.eclipse.gmf.gmfgraph.FontStyle; |
38 |
import org.eclipse.gmf.gmfgraph.GMFGraphPackage; |
39 |
import org.eclipse.gmf.gmfgraph.Point; |
40 |
import org.eclipse.gmf.gmfgraph.RGBColor; |
41 |
import org.eclipse.swt.SWT; |
42 |
import org.eclipse.swt.graphics.FontData; |
43 |
import org.eclipse.swt.graphics.RGB; |
44 |
|
45 |
public class ReflectiveFigureCheck extends FigureCodegenTestBase.FigureCheck { |
46 |
private final Figure myGMFRootFigure; |
47 |
private static final FeatureCheckRegistry ourRegistry = new FeatureCheckRegistry(); |
48 |
|
49 |
public ReflectiveFigureCheck(Figure eFigure){ |
50 |
myGMFRootFigure = eFigure; |
51 |
} |
52 |
|
53 |
public void checkFigure(IFigure figure) { |
54 |
assertNotNull(figure); |
55 |
checkFigure(myGMFRootFigure, figure); |
56 |
} |
57 |
|
58 |
private void checkFigure(Figure gmfFigure, IFigure d2dFigure){ |
59 |
checkFigureItself(gmfFigure, d2dFigure); |
60 |
checkFigureChildren(gmfFigure, d2dFigure); |
61 |
} |
62 |
|
63 |
private void checkFigureChildren(Figure gmfFigure, IFigure d2dFigure){ |
64 |
List gmfChildren = gmfFigure.getChildren(); |
65 |
List d2dChildren = d2dFigure.getChildren(); |
66 |
assertNotNull(gmfChildren); |
67 |
assertNotNull(d2dChildren); |
68 |
assertEquals(gmfChildren.size(), d2dChildren.size()); |
69 |
|
70 |
Iterator gmfIter = gmfChildren.iterator(); |
71 |
Iterator d2dIter = d2dChildren.iterator(); |
72 |
|
73 |
while (gmfIter.hasNext() && d2dIter.hasNext()){ |
74 |
Figure nextGMF = (Figure) gmfIter.next(); |
75 |
IFigure nextD2D = (IFigure) d2dIter.next(); |
76 |
checkFigure(nextGMF, nextD2D); |
77 |
} |
78 |
} |
79 |
|
80 |
private void checkFigureItself(Figure gmfFigure, IFigure d2dFigure){ |
81 |
EClass figureClass = gmfFigure.eClass(); |
82 |
for (Iterator features = figureClass.getEAllStructuralFeatures().iterator(); features.hasNext();){ |
83 |
EStructuralFeature next = (EStructuralFeature)features.next(); |
84 |
FeatureChecker checker = getFeatureCheckRegistry().getFeatureChecker(next); |
85 |
if (checker != null){ |
86 |
checker.check(gmfFigure, d2dFigure); |
87 |
} |
88 |
} |
89 |
} |
90 |
|
91 |
private FeatureCheckRegistry getFeatureCheckRegistry(){ |
92 |
return ourRegistry; |
93 |
} |
94 |
|
95 |
private static abstract class FeatureChecker extends Assert { |
96 |
private final EStructuralFeature myFeature; |
97 |
|
98 |
public FeatureChecker(EStructuralFeature feature){ |
99 |
this(feature, false); |
100 |
} |
101 |
|
102 |
protected FeatureChecker(EStructuralFeature feature, boolean expectedMulti){ |
103 |
myFeature = feature; |
104 |
if (expectedMulti){ |
105 |
int upperBound = feature.getUpperBound(); |
106 |
assertTrue(upperBound > 1 || upperBound == ETypedElement.UNBOUNDED_MULTIPLICITY); |
107 |
} else { |
108 |
assertEquals(1, feature.getUpperBound()); |
109 |
} |
110 |
} |
111 |
|
112 |
protected abstract Object getExpected(EObject eObject); |
113 |
protected abstract Object getActual(IFigure figure); |
114 |
|
115 |
protected boolean shouldCheck(EObject eObject){ |
116 |
return eObject.eIsSet(myFeature); |
117 |
} |
118 |
|
119 |
protected final Object eGet(EObject eObject){ |
120 |
Object result = eObject.eGet(myFeature); |
121 |
assertNotNull(result); |
122 |
return result; |
123 |
} |
124 |
|
125 |
public final EStructuralFeature getFeature(){ |
126 |
return myFeature; |
127 |
} |
128 |
|
129 |
public void check(EObject eObject, IFigure figure){ |
130 |
if (shouldCheck(eObject)){ |
131 |
Object expected = getExpected(eObject); |
132 |
Object actual = getActual(figure); |
133 |
assertEquals(expected, actual); |
134 |
} |
135 |
} |
136 |
} |
137 |
|
138 |
private static abstract class GMFDimensionChecker extends FeatureChecker { |
139 |
public GMFDimensionChecker(EStructuralFeature feature){ |
140 |
super(feature); |
141 |
assertEquals(GMFGraphPackage.eINSTANCE.getDimension(), feature.getEType()); |
142 |
} |
143 |
|
144 |
protected Object getExpected(EObject eObject) { |
145 |
Dimension eDimension = (Dimension)eGet(eObject); |
146 |
return new org.eclipse.draw2d.geometry.Dimension(eDimension.getDx(), eDimension.getDy()); |
147 |
} |
148 |
}; |
149 |
|
150 |
private static abstract class GMFPointChecker extends FeatureChecker { |
151 |
public GMFPointChecker(EStructuralFeature feature){ |
152 |
super(feature); |
153 |
assertEquals(GMFGraphPackage.eINSTANCE.getPoint(), feature.getEType()); |
154 |
} |
155 |
|
156 |
protected Object getExpected(EObject eObject) { |
157 |
Point ePoint = (Point)eGet(eObject); |
158 |
return new org.eclipse.draw2d.geometry.Dimension(ePoint.getX(), ePoint.getY()); |
159 |
} |
160 |
}; |
161 |
|
162 |
private static class GMFFontChecker extends FeatureChecker { |
163 |
public GMFFontChecker(){ |
164 |
super(GMFGraphPackage.eINSTANCE.getFigure_Font()); |
165 |
assertEquals(GMFGraphPackage.eINSTANCE.getFont(), getFeature().getEType()); |
166 |
} |
167 |
|
168 |
protected org.eclipse.swt.graphics.Font getActualFont(IFigure figure){ |
169 |
return figure.getFont(); |
170 |
} |
171 |
|
172 |
protected final Object getActual(IFigure figure) { |
173 |
throw new UnsupportedOperationException(); |
174 |
} |
175 |
|
176 |
protected final Object getExpected(EObject eObject) { |
177 |
throw new UnsupportedOperationException(); |
178 |
} |
179 |
|
180 |
public final void check(EObject eObject, IFigure figure) { |
181 |
if (shouldCheck(eObject)){ |
182 |
Font eFont = (Font)eGet(eObject); |
183 |
checkFont(eFont, getActualFont(figure)); |
184 |
} |
185 |
} |
186 |
|
187 |
public void checkFont(Font gmfFont, org.eclipse.swt.graphics.Font actual){ |
188 |
assertNotNull(actual); |
189 |
if (gmfFont instanceof BasicFont && actual.getFontData().length == 1){ |
190 |
BasicFont expected = (BasicFont)gmfFont; |
191 |
FontData theOnly = actual.getFontData()[0]; |
192 |
assertEquals(expected.getFaceName(), theOnly.getName()); |
193 |
assertEquals(expected.getHeight(), theOnly.getHeight()); |
194 |
|
195 |
int expectedStyle = gmfStyle2swtStyle(expected.getStyle()); |
196 |
assertEquals(expectedStyle, theOnly.getStyle()); |
197 |
} |
198 |
} |
199 |
|
200 |
private int gmfStyle2swtStyle(FontStyle gmfStyle){ |
201 |
switch (gmfStyle.getValue()){ |
202 |
case FontStyle.BOLD : return SWT.BOLD; |
203 |
case FontStyle.ITALIC : return SWT.ITALIC; |
204 |
case FontStyle.NORMAL : return SWT.NORMAL; |
205 |
default: throw new IllegalStateException("Unknown font style: " + gmfStyle); |
206 |
} |
207 |
} |
208 |
} |
209 |
|
210 |
private abstract static class GMFColorChecker extends FeatureChecker { |
211 |
public GMFColorChecker(EStructuralFeature feature){ |
212 |
super(feature); |
213 |
assertEquals(GMFGraphPackage.eINSTANCE.getColor(), feature.getEType()); |
214 |
} |
215 |
|
216 |
protected abstract org.eclipse.swt.graphics.Color getActualColor(IFigure figure); |
217 |
|
218 |
protected Object getExpected(EObject eObject) { |
219 |
Color color = (Color)eGet(eObject); |
220 |
if (color instanceof ConstantColor){ |
221 |
return transformConstantColor((ConstantColor)color).getRGB(); |
222 |
} |
223 |
if (color instanceof RGBColor){ |
224 |
return transformRGBColor((RGBColor)color); |
225 |
} |
226 |
throw new IllegalStateException("Unknown color: " + color); |
227 |
} |
228 |
|
229 |
protected Object getActual(IFigure figure) { |
230 |
return getActualColor(figure).getRGB(); |
231 |
} |
232 |
|
233 |
private org.eclipse.swt.graphics.Color transformConstantColor(ConstantColor color) { |
234 |
switch(color.getValue().getValue()){ |
235 |
case ColorConstants.BLACK : return org.eclipse.draw2d.ColorConstants.black; |
236 |
case ColorConstants.BLUE : return org.eclipse.draw2d.ColorConstants.blue; |
237 |
case ColorConstants.CYAN : return org.eclipse.draw2d.ColorConstants.cyan; |
238 |
case ColorConstants.DARK_BLUE : return org.eclipse.draw2d.ColorConstants.darkBlue; |
239 |
case ColorConstants.DARK_GRAY: return org.eclipse.draw2d.ColorConstants.darkGray; |
240 |
case ColorConstants.DARK_GREEN: return org.eclipse.draw2d.ColorConstants.darkGreen; |
241 |
case ColorConstants.GRAY: return org.eclipse.draw2d.ColorConstants.gray; |
242 |
case ColorConstants.GREEN: return org.eclipse.draw2d.ColorConstants.green; |
243 |
case ColorConstants.LIGHT_BLUE: return org.eclipse.draw2d.ColorConstants.lightBlue; |
244 |
case ColorConstants.LIGHT_GRAY: return org.eclipse.draw2d.ColorConstants.lightGray; |
245 |
case ColorConstants.LIGHT_GREEN: return org.eclipse.draw2d.ColorConstants.lightGreen; |
246 |
case ColorConstants.ORANGE: return org.eclipse.draw2d.ColorConstants.orange; |
247 |
case ColorConstants.RED: return org.eclipse.draw2d.ColorConstants.red; |
248 |
case ColorConstants.WHITE: return org.eclipse.draw2d.ColorConstants.white; |
249 |
case ColorConstants.YELLOW: return org.eclipse.draw2d.ColorConstants.yellow; |
250 |
default: throw new IllegalArgumentException("Unknown Color: " + color); |
251 |
} |
252 |
} |
253 |
|
254 |
private RGB transformRGBColor(RGBColor color) { |
255 |
return new RGB(color.getRed(), color.getGreen(), color.getBlue()); |
256 |
} |
257 |
} |
258 |
|
259 |
private static abstract class PointListChecker extends FeatureChecker { |
260 |
public PointListChecker(EStructuralFeature feature){ |
261 |
super(feature, true); |
262 |
assertEquals(GMFGraphPackage.eINSTANCE.getPoint(), feature.getEType()); |
263 |
} |
264 |
|
265 |
protected abstract PointList getPoints(IFigure figure); |
266 |
|
267 |
protected Object getActual(IFigure figure) { |
268 |
PointList points = getPoints(figure); |
269 |
assertNotNull(points); |
270 |
LinkedList result = new LinkedList(); |
271 |
for (int i = 0; i < points.size(); i++){ |
272 |
org.eclipse.draw2d.geometry.Point next = points.getPoint(i); |
273 |
result.add(next); |
274 |
} |
275 |
return result; |
276 |
} |
277 |
|
278 |
protected Object getExpected(EObject eObject) { |
279 |
List points = (List) eGet(eObject); |
280 |
assertNotNull(points); |
281 |
LinkedList result = new LinkedList(); |
282 |
for (Iterator it = points.iterator(); it.hasNext();){ |
283 |
Point next = (Point) it.next(); |
284 |
result.add(new org.eclipse.draw2d.geometry.Point(next.getX(), next.getY())); |
285 |
} |
286 |
return result; |
287 |
} |
288 |
} |
289 |
|
290 |
private static class FeatureCheckRegistry { |
291 |
private final HashMap myFeature2Checker = new HashMap(); |
292 |
private static final FeatureChecker SIZE = new GMFPointChecker(GMFGraphPackage.eINSTANCE.getFigure_Size()){ |
293 |
protected Object getActual(IFigure figure) { |
294 |
return figure.getSize(); |
295 |
} |
296 |
}; |
297 |
|
298 |
private static final FeatureChecker PREFERRED_SIZE = new GMFDimensionChecker(GMFGraphPackage.eINSTANCE.getFigure_PreferredSize()){ |
299 |
protected Object getActual(IFigure figure) { |
300 |
return figure.getPreferredSize(); |
301 |
} |
302 |
}; |
303 |
|
304 |
private static final FeatureChecker FONT = new GMFFontChecker(); |
305 |
|
306 |
private static final FeatureChecker FOREGROUND = new GMFColorChecker(GMFGraphPackage.eINSTANCE.getFigure_ForegroundColor()){ |
307 |
protected org.eclipse.swt.graphics.Color getActualColor(IFigure figure) { |
308 |
return figure.getForegroundColor(); |
309 |
} |
310 |
}; |
311 |
|
312 |
private static final FeatureChecker BACKGROUND = new GMFColorChecker(GMFGraphPackage.eINSTANCE.getFigure_BackgroundColor()){ |
313 |
protected org.eclipse.swt.graphics.Color getActualColor(IFigure figure) { |
314 |
return figure.getBackgroundColor(); |
315 |
} |
316 |
}; |
317 |
|
318 |
private static final FeatureChecker LABEL_TEXT = new FeatureChecker(GMFGraphPackage.eINSTANCE.getLabel_Text()){ |
319 |
protected Object getActual(IFigure figure) { |
320 |
assertTrue(figure instanceof Label); |
321 |
return ((Label)figure).getText(); |
322 |
} |
323 |
|
324 |
protected Object getExpected(EObject eObject) { |
325 |
return eGet(eObject); |
326 |
} |
327 |
}; |
328 |
|
329 |
private static final FeatureChecker POLYLINE_POINTS = new PointListChecker(GMFGraphPackage.eINSTANCE.getPolyline_Template()){ |
330 |
protected PointList getPoints(IFigure figure) { |
331 |
assertTrue(figure instanceof Polyline); |
332 |
return ((Polyline)figure).getPoints(); |
333 |
} |
334 |
}; |
335 |
|
336 |
public FeatureCheckRegistry(){ |
337 |
registerCheck(SIZE); |
338 |
registerCheck(PREFERRED_SIZE); |
339 |
registerCheck(FONT); |
340 |
registerCheck(FOREGROUND); |
341 |
registerCheck(BACKGROUND); |
342 |
registerCheck(LABEL_TEXT); |
343 |
registerCheck(POLYLINE_POINTS); |
344 |
} |
345 |
|
346 |
private void registerCheck(FeatureChecker checker){ |
347 |
myFeature2Checker.put(checker.getFeature(), checker); |
348 |
} |
349 |
|
350 |
public FeatureChecker getFeatureChecker(EStructuralFeature feature){ |
351 |
return (FeatureChecker) myFeature2Checker.get(feature); |
352 |
} |
353 |
} |
354 |
|
355 |
} |