/******************************************************************************* | |
* <copyright> | |
* | |
* Copyright (c) 2013, 2013 SAP AG. | |
* 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 | |
* | |
* Contributors: | |
* SAP AG - initial API, implementation and documentation | |
* | |
* </copyright> | |
* | |
*******************************************************************************/ | |
package org.eclipse.fmc; | |
import static org.eclipse.fmc.blockdiagram.editor.diagram.BlockDiagramFeatureProvider.CREATE_CON_MODIFY_ACCESS; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertTrue; | |
import java.io.ByteArrayInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.Iterator; | |
import org.eclipse.core.resources.IFile; | |
import org.eclipse.core.resources.IProject; | |
import org.eclipse.core.resources.IWorkspaceRoot; | |
import org.eclipse.core.resources.ResourcesPlugin; | |
import org.eclipse.core.runtime.CoreException; | |
import org.eclipse.core.runtime.IProgressMonitor; | |
import org.eclipse.core.runtime.NullProgressMonitor; | |
import org.eclipse.core.runtime.Path; | |
import org.eclipse.draw2d.RoundedRectangle; | |
import org.eclipse.emf.common.util.URI; | |
import org.eclipse.emf.edit.domain.IEditingDomainProvider; | |
import org.eclipse.emf.transaction.RecordingCommand; | |
import org.eclipse.emf.transaction.TransactionalEditingDomain; | |
import org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl; | |
import org.eclipse.fmc.blockdiagram.editor.BlockDiagramEditor; | |
import org.eclipse.fmc.blockdiagram.editor.util.DiagramFactory; | |
import org.eclipse.graphiti.features.ICreateConnectionFeature; | |
import org.eclipse.graphiti.features.ICreateFeature; | |
import org.eclipse.graphiti.features.IFeatureProvider; | |
import org.eclipse.graphiti.features.IRemoveFeature; | |
import org.eclipse.graphiti.features.context.impl.CreateConnectionContext; | |
import org.eclipse.graphiti.features.context.impl.CreateContext; | |
import org.eclipse.graphiti.features.context.impl.RemoveContext; | |
import org.eclipse.graphiti.internal.datatypes.impl.LocationImpl; | |
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm; | |
import org.eclipse.graphiti.mm.algorithms.Polygon; | |
import org.eclipse.graphiti.mm.algorithms.styles.Point; | |
import org.eclipse.graphiti.mm.pictograms.Connection; | |
import org.eclipse.graphiti.mm.pictograms.ContainerShape; | |
import org.eclipse.graphiti.mm.pictograms.Diagram; | |
import org.eclipse.graphiti.mm.pictograms.PictogramElement; | |
import org.eclipse.graphiti.mm.pictograms.Shape; | |
import org.eclipse.ui.IWorkbenchPage; | |
import org.eclipse.ui.IWorkbenchPart; | |
import org.eclipse.ui.PartInitException; | |
import org.eclipse.ui.PlatformUI; | |
import org.eclipse.ui.ide.IDE; | |
import org.eclipse.ui.internal.ViewIntroAdapterPart; | |
import org.eclipse.ui.internal.intro.IIntroConstants; | |
import org.junit.After; | |
import org.junit.Before; | |
@SuppressWarnings("restriction") | |
public abstract class FMCTestCase { | |
protected static final String BLOCKDIAG_FILENAME = "newDiag.blockdiag"; | |
private BlockDiagramEditor editor = null; | |
private FMCDiagramStateChecker stateChecker = new FMCDiagramStateChecker(); | |
protected BlockDiagramEditor getEditor() { | |
return this.editor; | |
} | |
protected void checkDiagramState() { | |
stateChecker.checkDiagramState(getDiagram()); | |
} | |
protected TransactionalEditingDomain getEditingDomain() { | |
IWorkbenchPart activePart = PlatformUI.getWorkbench() | |
.getActiveWorkbenchWindow().getActivePage().getActivePart(); | |
IEditingDomainProvider provider = (IEditingDomainProvider) activePart | |
.getAdapter(IEditingDomainProvider.class); | |
assert provider.getEditingDomain() instanceof TransactionalEditingDomain; | |
TransactionalEditingDomain editingDomain = (TransactionalEditingDomain) provider | |
.getEditingDomain(); | |
return editingDomain; | |
} | |
protected CreateContext createContext(int width, int height, int x, int y) { | |
CreateContext ctx = new CreateContext(); | |
ctx.setLocation(x, y); | |
ctx.setSize(width, height); | |
ctx.setTargetContainer(getDiagram()); | |
return ctx; | |
} | |
protected IFeatureProvider getFeatureProvider() { | |
return editor.getDiagramTypeProvider().getFeatureProvider(); | |
} | |
protected Diagram getDiagram() { | |
return editor.getDiagramTypeProvider().getDiagram(); | |
} | |
public static void assertSameAlgorithm(Shape expected, Shape actual) { | |
GraphicsAlgorithm algorithmA = expected.getGraphicsAlgorithm(); | |
GraphicsAlgorithm algorithmB = actual.getGraphicsAlgorithm(); | |
assertEquals(algorithmA.getClass(), algorithmB.getClass()); | |
} | |
public static void assertSameSize(GraphicsAlgorithm expected, | |
GraphicsAlgorithm actual) { | |
assertEquals(expected.getHeight(), actual.getHeight()); | |
assertEquals(expected.getWidth(), actual.getWidth()); | |
if (expected instanceof RoundedRectangle) { | |
RoundedRectangle rectA = (RoundedRectangle) expected; | |
RoundedRectangle rectB = (RoundedRectangle) expected; | |
assertEquals(rectA.getCornerDimensions().width, | |
rectB.getCornerDimensions().width); | |
assertEquals(rectA.getCornerDimensions().height, | |
rectB.getCornerDimensions().height); | |
} else if (expected instanceof Polygon) { | |
Polygon polyA = (Polygon) expected; | |
Polygon polyB = (Polygon) actual; | |
assertEquals(polyA.getPoints().size(), polyB.getPoints().size()); | |
Iterator<Point> iteratorB = polyB.getPoints().iterator(); | |
for (Iterator<Point> iteratorA = polyA.getPoints().iterator(); iteratorA | |
.hasNext();) { | |
Point a = iteratorA.next(); | |
Point b = iteratorB.next(); | |
assertEquals(a.getX(), b.getX()); | |
assertEquals(a.getY(), b.getY()); | |
} | |
} | |
} | |
public static void assertSameLocation(Shape expected, Shape actual) { | |
GraphicsAlgorithm algorithmA = expected.getGraphicsAlgorithm(); | |
GraphicsAlgorithm algorithmB = actual.getGraphicsAlgorithm(); | |
assertEquals(algorithmA.getX(), algorithmB.getX()); | |
assertEquals(algorithmA.getY(), algorithmB.getY()); | |
} | |
public static void assertSize(Shape shape, int width, int height) { | |
GraphicsAlgorithm algorithm = shape.getGraphicsAlgorithm(); | |
assertEquals(width, algorithm.getWidth()); | |
assertEquals(height, algorithm.getHeight()); | |
} | |
public void assertPosition(Shape shape, int x, int y) { | |
GraphicsAlgorithm algorithm = shape.getGraphicsAlgorithm(); | |
assertEquals(x, algorithm.getX()); | |
assertEquals(y, algorithm.getY()); | |
} | |
protected IProject getProject() { | |
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); | |
IProject project = root.getProject("TestProject"); | |
return project; | |
} | |
private IFile getFmcFile(String filename) throws CoreException { | |
IProgressMonitor progressMonitor = new NullProgressMonitor(); | |
IProject project = getProject(); | |
if (!project.exists()) | |
project.create(progressMonitor); | |
project.open(progressMonitor); | |
final IFile file = project.getFile(new Path(filename)); | |
/* | |
* if(file.exists()){ file.delete(true, progressMonitor); } | |
*/ | |
InputStream stream; | |
try { | |
String contents = "This is the initial file contents for *.blockdiag file that should be word-sorted in the Preview page of the multi-page editor"; | |
stream = new ByteArrayInputStream(contents.getBytes()); | |
if (!file.exists()) { | |
file.create(stream, true, progressMonitor); | |
} | |
stream.close(); | |
} catch (IOException e) { | |
} | |
createFMCFile(file); | |
return file; | |
} | |
protected void createFMCFile(final IFile file) { | |
DiagramFactory.createDefaultBlockdiagramFile(URI.createPlatformResourceURI(file | |
.getFullPath().toString(), true), 1000, 1000, 10); | |
} | |
protected void saveDiagram() { | |
editor.doSave(new NullProgressMonitor()); | |
} | |
@After | |
public void tearDown() { | |
checkDiagramState(); | |
saveDiagram(); | |
// TODO close diagram | |
// editor.getSite().getPage().close(); | |
// editor = null; | |
} | |
@Before | |
public void setUp() throws PartInitException, CoreException { | |
IWorkbenchPage activePage = PlatformUI.getWorkbench() | |
.getActiveWorkbenchWindow().getActivePage(); | |
editor = (BlockDiagramEditor) IDE.openEditor(activePage, | |
getFmcFile(BLOCKDIAG_FILENAME)); | |
ViewIntroAdapterPart viewIntro = (ViewIntroAdapterPart) activePage | |
.findView(IIntroConstants.INTRO_VIEW_ID); | |
if (viewIntro != null) | |
viewIntro.getViewSite().getPart().dispose(); | |
} | |
protected Connection createConnection(int createFeature, Shape source, | |
Shape target) { | |
int consBefore = getDiagram().getConnections().size(); | |
ICreateConnectionFeature createConnectionFeature = getFeatureProvider() | |
.getCreateConnectionFeatures()[createFeature]; | |
CreateConnectionContext ctx = new CreateConnectionContext(); | |
ctx.setSourcePictogramElement(source); | |
ctx.setTargetPictogramElement(target); | |
ctx.setTargetLocation(new LocationImpl(source.getGraphicsAlgorithm() | |
.getX() + 50, source.getGraphicsAlgorithm().getY() + 50)); | |
if (createConnectionFeature.canExecute(ctx)) { | |
createConnectionFeature.execute(ctx); | |
assertEquals( | |
createFeature == CREATE_CON_MODIFY_ACCESS ? consBefore + 3 | |
: consBefore + 1, getDiagram().getConnections() | |
.size()); | |
return getDiagram().getConnections().get(consBefore); | |
} else | |
return null; | |
} | |
protected void remove(final PictogramElement element) { | |
getEditingDomain().getCommandStack().execute( | |
new RecordingCommand(getEditingDomain()) { | |
@Override | |
protected void doExecute() { | |
RemoveContext ctx = new RemoveContext(element); | |
IRemoveFeature deleteFeature = getFeatureProvider() | |
.getRemoveFeature(ctx); | |
assertTrue(deleteFeature.canRemove(ctx)); | |
assertTrue(deleteFeature.canExecute(ctx)); | |
deleteFeature.execute(ctx); | |
} | |
}); | |
} | |
protected Shape createShapeInShape(int createFeature, final int width, | |
final int height, final int x, final int y, ContainerShape container) { | |
int childsBefore = getDiagram().getChildren().size(); | |
int childsBeforeContainer = 0; | |
ICreateFeature feature = getFeatureProvider().getCreateFeatures()[createFeature]; | |
CreateContext ctx = createContext(width, height, x, y); | |
if (container != null) { | |
ctx.setTargetContainer(container); | |
childsBeforeContainer = container.getChildren().size(); | |
} | |
assertTrue(feature.canExecute(ctx)); | |
feature.execute(ctx); | |
if (container != null) { | |
assertEquals(childsBeforeContainer + 1, container.getChildren() | |
.size()); | |
return container.getChildren().get(childsBeforeContainer); | |
} else { | |
assertEquals(childsBefore + 1, getDiagram().getChildren().size()); | |
return getDiagram().getChildren().get(childsBefore); | |
} | |
} | |
protected Shape createShape(int createFeature, final int width, | |
final int height, final int x, final int y) { | |
return createShapeInShape(createFeature, width, height, x, y, null); | |
} | |
protected boolean isWriteTransaction(TransactionalEditingDomain domain) { | |
if (domain instanceof TransactionalEditingDomainImpl) | |
return !((TransactionalEditingDomainImpl) domain) | |
.getActiveTransaction().isReadOnly(); | |
else | |
return true; | |
} | |
} |