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.lang.reflect.InvocationTargetException; |
16 |
import java.lang.reflect.Method; |
17 |
|
18 |
import org.eclipse.core.runtime.Platform; |
19 |
import org.eclipse.draw2d.IFigure; |
20 |
import org.eclipse.draw2d.geometry.Dimension; |
21 |
import org.eclipse.gmf.gmfgraph.Figure; |
22 |
import org.eclipse.gmf.gmfgraph.GMFGraphFactory; |
23 |
import org.eclipse.gmf.gmfgraph.Point; |
24 |
import org.eclipse.gmf.gmfgraph.Rectangle; |
25 |
import org.eclipse.gmf.graphdef.codegen.StandaloneGenerator; |
26 |
import org.eclipse.gmf.runtime.draw2d.ui.mapmode.IMapMode; |
27 |
import org.eclipse.gmf.runtime.draw2d.ui.mapmode.MapModeTypes; |
28 |
import org.osgi.framework.Bundle; |
29 |
|
30 |
public class StandaloneMapModeTest extends FigureCodegenTestBase { |
31 |
private StandaloneGenerator.Config myGeneratorConfig; |
32 |
|
33 |
public StandaloneMapModeTest(String name) { |
34 |
super(name); |
35 |
} |
36 |
|
37 |
protected void setUp() throws Exception { |
38 |
super.setUp(); |
39 |
myGeneratorConfig = null; |
40 |
} |
41 |
|
42 |
public void testStaticIdentityMapMode(){ |
43 |
myGeneratorConfig = new StandaloneGenerator.ConfigImpl(getTestPluginName(), getFigurePackageName(), false); |
44 |
performTests(createTestFigure(), new FigureSizeCheck(123, 456)); |
45 |
} |
46 |
|
47 |
public void testRuntimeIdentityMapMode(){ |
48 |
myGeneratorConfig = new StandaloneGenerator.ConfigImpl(getTestPluginName(), getFigurePackageName(), true); |
49 |
performTests(createTestFigure(), new InstantiateFigureHook(123, 456, MapModeTypes.IDENTITY_MM, myGeneratorConfig)); |
50 |
} |
51 |
|
52 |
public void testRuntimeHiMetricsMapMode(){ |
53 |
myGeneratorConfig = new StandaloneGenerator.ConfigImpl(getTestPluginName(), getFigurePackageName(), true); |
54 |
performTests(createTestFigure(), new InstantiateFigureHook(123, 456, MapModeTypes.HIMETRIC_MM, myGeneratorConfig)); |
55 |
} |
56 |
|
57 |
protected StandaloneGenerator.Config getGMFGraphGeneratorConfig(){ |
58 |
assertNotNull(myGeneratorConfig); |
59 |
return myGeneratorConfig; |
60 |
} |
61 |
|
62 |
private Figure createTestFigure(){ |
63 |
Rectangle result = GMFGraphFactory.eINSTANCE.createRectangle(); |
64 |
result.setName("TestFigure"); |
65 |
Point size = GMFGraphFactory.eINSTANCE.createPoint(); |
66 |
size.setX(123); |
67 |
size.setY(456); |
68 |
result.setSize(size); |
69 |
return result; |
70 |
} |
71 |
|
72 |
private static class FigureSizeCheck extends FigureCheck { |
73 |
private final Dimension mySize; |
74 |
|
75 |
public FigureSizeCheck(int width, int height){ |
76 |
this(new Dimension(width, height)); |
77 |
} |
78 |
|
79 |
public FigureSizeCheck(Dimension size){ |
80 |
mySize = size; |
81 |
} |
82 |
|
83 |
public void checkFigure(IFigure figure) { |
84 |
assertEquals(mySize, figure.getSize()); |
85 |
} |
86 |
} |
87 |
|
88 |
private static class InstantiateFigureHook extends FigureSizeCheck { |
89 |
private final IMapMode myMapMode; |
90 |
private final StandaloneGenerator.Config myConfig; |
91 |
|
92 |
public InstantiateFigureHook(int width, int height, IMapMode mapMode, StandaloneGenerator.Config config){ |
93 |
super(mapMode.DPtoLP(width), mapMode.DPtoLP(height)); |
94 |
myMapMode = mapMode; |
95 |
myConfig = config; |
96 |
} |
97 |
|
98 |
protected IFigure instantiateFigure(Class figureClass) { |
99 |
try { |
100 |
hookMapMode(); |
101 |
} catch (Exception e) { |
102 |
fail(e.getMessage()); |
103 |
} |
104 |
return super.instantiateFigure(figureClass); |
105 |
} |
106 |
|
107 |
private void hookMapMode() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{ |
108 |
Bundle bundle = Platform.getBundle(myConfig.getPluginID()); |
109 |
assertNotNull(bundle); |
110 |
Class activatorClass = bundle.loadClass(myConfig.getPluginActivatorPackageName() + "." + myConfig.getPluginActivatorClassName()); |
111 |
assertNotNull(activatorClass); |
112 |
Method getDefault = activatorClass.getMethod("getDefault", new Class[0]); |
113 |
assertNotNull(getDefault); |
114 |
Object activator = getDefault.invoke(null, new Object[0]); |
115 |
assertNotNull(activator); |
116 |
|
117 |
Method setMapMode = activatorClass.getMethod("setMapMode", new Class[] {IMapMode.class}); |
118 |
assertNotNull(setMapMode); |
119 |
setMapMode.invoke(activator, new Object[] {myMapMode}); |
120 |
|
121 |
Method getMapMode = activatorClass.getMethod("getMapMode", new Class[0]); |
122 |
assertNotNull(getMapMode); |
123 |
assertEquals(myMapMode, getMapMode.invoke(activator, new Object[0])); |
124 |
} |
125 |
} |
126 |
} |