ingenias.testing.fest.JGraphFixture Maven / Gradle / Ivy
package ingenias.testing.fest;
import static org.testng.AssertJUnit.assertTrue;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import ingenias.editor.entities.Entity;
import ingenias.exception.NotFound;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import org.fest.swing.core.GenericTypeMatcher;
import org.fest.swing.core.KeyPressInfo;
import org.fest.swing.core.Robot;
import org.fest.swing.fixture.ComponentFixture;
import org.fest.swing.fixture.ContainerFixture;
import org.fest.swing.fixture.JPanelFixture;
import org.fest.swing.fixture.JPopupMenuFixture;
import org.fest.swing.timing.Pause;
import org.jgraph.JGraph;
import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.GraphConstants;
import org.swixml.converters.KeyEvent;
import org.testng.Assert;
public class JGraphFixture extends ComponentFixture {
private DefaultGraphCell lastSelectedCell=null;
private DefaultGraphCell originCell=null;
private DefaultGraphCell targetCell=null;
public JGraphFixture(Robot robot, Class extends JGraph> type) {
super(robot, type);
// TODO Auto-generated constructor stub
}
public JGraphFixture(Robot robot, JGraph target) {
super(robot, target);
// TODO Auto-generated constructor stub
}
public JGraphFixture(Robot robot, String name, Class extends JGraph> type) {
super(robot, name, type);
}
public JGraphFixture withDGC(String name) {
Object[] roots = target.getRoots();
boolean found=false;
StringBuffer currentEntities=new StringBuffer();
for (int k=0;kbounds.getMaxX()){
centerPoint.x=(int) bounds.getMinX();
centerPoint.y=centerPoint.y+5;
if (centerPoint.y>bounds.getMaxY())
nextCell=null;
}
}
} while(nextCell!=null && nextCell!=cell);
return centerPoint;
}
private Rectangle2D getBounds(DefaultGraphCell cell) {
AttributeMap atts = target.getModel().getAttributes(cell);
if (atts==null)
return null;
Rectangle2D bounds = GraphConstants.getBounds(atts);
return bounds;
}
public JGraphFixture deleteSelectedDGC() {
assertTrue("Before the call, a cell must have been selected first with withDGC and then with selectDGC",
lastSelectedCell!=null && target.getSelectionModel().isCellSelected(lastSelectedCell));
robot.pressAndReleaseKey(KeyEvent.VK_DELETE);
return this;
}
public JGraphFixture moveAway() {
assertTrue("There must be a selected cell with withDGC", lastSelectedCell!=null);
Point startingPoint=getCentralCoordinates(lastSelectedCell);
startingPoint.x=startingPoint.x-20; // to avoid central square
startingPoint.y=startingPoint.y-20; // to avoid central square
Point destination=null;
int allowedDistance=100;
float distance=0;
Rectangle2D entitySize = getBoundsCoordinates(lastSelectedCell);
Dimension targetSize= target.getSize();
boolean possibleToFitIn=
entitySize.getWidth()0) or reduces (if variation<0) the size of the selected entity
* @return
*/
public JGraphFixture changeWidthBy(int variation) {
assertTrue("There must be a selected cell with withDGC", lastSelectedCell!=null);
Rectangle2D currentBounds = getBoundsCoordinates(lastSelectedCell);
float startingPointX=(float) (currentBounds.getMinX()+currentBounds.getWidth());
float startingPointY=(float) (currentBounds.getMinY()+currentBounds.getHeight()/2);
float destinationPointX=(float) (currentBounds.getMinX()+currentBounds.getWidth()+variation);
float destinationPointY=(float) (currentBounds.getMinY()+currentBounds.getHeight()/2);
Point starting=new Point((int)startingPointX,(int)startingPointY);
Point destination=new Point((int)destinationPointX,(int)destinationPointX);
robot.pressMouse(target, starting);
robot.moveMouse(target, destination);
robot.releaseMouseButtons();
return this;
}
/**
* Expand (if variation>0) or reduces (if variation<0) the size of the selected entity
* @return
*/
public JGraphFixture changeHeightBy(int variation) {
assertTrue("There must be a selected cell with withDGC", lastSelectedCell!=null);
Rectangle2D currentBounds = getBoundsCoordinates(lastSelectedCell);
float startingPointX=(float) (currentBounds.getMinX()+currentBounds.getWidth()/2);
float startingPointY=(float) (currentBounds.getMinY()+currentBounds.getHeight());
float destinationPointX=(float) (currentBounds.getMinX()+currentBounds.getWidth()/2);
float destinationPointY=(float) (currentBounds.getMinY()+currentBounds.getHeight()+variation);
Point starting=new Point((int)startingPointX,(int)startingPointY);
Point destination=new Point((int)destinationPointX,(int)destinationPointY);
robot.pressMouse(target, starting);
robot.moveMouse(target, destination);
robot.releaseMouseButtons();
return this;
}
private float mindistanceToAll(Point destination) {
Object[] roots = target.getRoots();
boolean found=false;
float minDistance=Float.MAX_VALUE;
for (int k=0;k