Skip to content

Commit

Permalink
Creation of a moveSingleFB method for SWTBOT UI Tests #604
Browse files Browse the repository at this point in the history
Added a method for moving a single FB in class SWTBotFB for better
readability and reusability of FB moving related code in SWTBot UI
tests.

Addresses #604
  • Loading branch information
Andrearium authored and azoitl committed Oct 21, 2024
1 parent 1d58ba3 commit 6665064
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import static org.eclipse.swtbot.swt.finder.waits.Conditions.treeItemHasNode;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.fordiac.ide.application.editparts.FBEditPart;
import org.eclipse.fordiac.ide.test.ui.swtbot.SWT4diacGefBot;
Expand Down Expand Up @@ -171,4 +173,26 @@ public Rectangle getBoundsOfFB(final SWTBotGefEditor editor, final String fBname
return fbBounds;
}

/**
* Moves a Function Block to the given position
*
* @param editor
* @param fBname The FB that should be moved
* @param toPoint The new position of the FB
*/
public void moveSingleFB(SWTBotGefEditor editor, final String fBname, final Point toPoint) {
assertNotNull(editor);
assertNotNull(editor.getEditPart(fBname));
editor = selectFBWithFBNameInEditor((SWTBot4diacGefEditor) editor, fBname);
final SWTBotGefEditPart fbEditPart = editor.getEditPart(fBname).parent();
assertNotNull(fbEditPart);

// move FB, get bounds of FB and expand bounds to have a tolerance for the grid
// alignment when checking the new position
editor.drag(fbEditPart, toPoint.x, toPoint.y);
final Rectangle toPosition = getBoundsOfFB(editor, fBname);
final Rectangle expandedBounds = toPosition.expand(new Insets(2));
assertTrue(expandedBounds.contains(toPoint.x, toPoint.y));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Andrea Zoitl
* Copyright (c) 2023, 2024 Andrea Zoitl
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -12,8 +12,8 @@
*******************************************************************************/
package org.eclipse.fordiac.ide.test.ui.networkediting.basicfb;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -25,10 +25,10 @@
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.fordiac.ide.test.ui.Abstract4diacUITests;
import org.eclipse.fordiac.ide.test.ui.helpers.UITestPinHelper;
import org.eclipse.fordiac.ide.test.ui.helpers.SWTBotConnection;
import org.eclipse.fordiac.ide.test.ui.helpers.SWTBotFB;
import org.eclipse.fordiac.ide.test.ui.helpers.UITestNamesHelper;
import org.eclipse.fordiac.ide.test.ui.helpers.UITestPinHelper;
import org.eclipse.fordiac.ide.test.ui.swtbot.SWTBot4diacGefEditor;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.swt.graphics.Point;
Expand All @@ -48,34 +48,34 @@ public class Basic2FBNetworkEditingTests extends Abstract4diacUITests {
* Then a second FB (E_SWITCH) is dragged onto the canvas and the check is the
* same as above.
*/
@SuppressWarnings({ "static-method", "static-access" })
@SuppressWarnings("static-method")
@Test
public void dragAndDrop2FB() {
// select FB E_CYCLE
// drag&drop FB E_CYCLE and check if position is the given one
final SWTBot4diacGefEditor editor = (SWTBot4diacGefEditor) bot.gefEditor(UITestNamesHelper.PROJECT_NAME);
final SWTBotFB fbBot = new SWTBotFB(bot);
final Point point1 = new Point(100, 100);
fbBot.dragAndDropEventsFB(UITestNamesHelper.E_CYCLE_TREE_ITEM, point1);
final Point posECycle = new Point(100, 100);
fbBot.dragAndDropEventsFB(UITestNamesHelper.E_CYCLE_TREE_ITEM, posECycle);
assertNotNull(editor.getEditPart(UITestNamesHelper.E_CYCLE_FB));
final Rectangle absPos1 = fbBot.getBoundsOfFB(editor, UITestNamesHelper.E_CYCLE_FB);
assertEquals(point1.x, absPos1.x);
assertEquals(point1.y, absPos1.y);
final Rectangle posFB = fbBot.getBoundsOfFB(editor, UITestNamesHelper.E_CYCLE_FB);
assertEquals(posECycle.x, posFB.x);
assertEquals(posECycle.y, posFB.y);

// select FB E_SWITCH
final Point point2 = new Point(300, 150);
fbBot.dragAndDropEventsFB(UITestNamesHelper.E_SWITCH_TREE_ITEM, point2);
final Point posESwitch = new Point(300, 150);
fbBot.dragAndDropEventsFB(UITestNamesHelper.E_SWITCH_TREE_ITEM, posESwitch);
assertNotNull(editor.getEditPart(UITestNamesHelper.E_SWITCH_FB));
final Rectangle absPos2 = fbBot.getBoundsOfFB(editor, UITestNamesHelper.E_SWITCH_FB);
assertEquals(point2.x, absPos2.x);
assertEquals(point2.y, absPos2.y);
final Rectangle absFB2 = fbBot.getBoundsOfFB(editor, UITestNamesHelper.E_SWITCH_FB);
assertEquals(posESwitch.x, absFB2.x);
assertEquals(posESwitch.y, absFB2.y);
}

/**
* The Method drag and drops 2 FBs onto the editing area. Afterwards one FB is
* deleted and it is checked if the FB is no longer on the canvas after
* deletion.
*/
@SuppressWarnings({ "static-method", "static-access" })
@SuppressWarnings("static-method")
@Test
public void delete1FB() {
final SWTBotFB fbBot = new SWTBotFB(bot);
Expand Down Expand Up @@ -104,7 +104,7 @@ public void delete1FB() {
* selected as expected. Then a rectangle is drawn over the FBs to check whether
* the FBs are selected.
*/
@SuppressWarnings({ "static-method", "static-access" })
@SuppressWarnings("static-method")
@Test
public void select2FBsViaMouseLeftClickOnFB() {
final SWTBotFB fbBot = new SWTBotFB(bot);
Expand Down Expand Up @@ -139,7 +139,7 @@ public void select2FBsViaMouseLeftClickOnFB() {
* which is not expected. To achieve this it is necessary to create a
* draw2d.geometry Point with the same coordinates of the swt.graphics Point.
*/
@SuppressWarnings({ "static-method", "static-access" })
@SuppressWarnings("static-method")
@Test
public void move1FB() {
final Point pos1 = new Point(100, 100);
Expand All @@ -148,28 +148,13 @@ public void move1FB() {
final Point pos2 = new Point(350, 100);
fbBot.dragAndDropEventsFB(UITestNamesHelper.E_N_TABLE_TREE_ITEM, pos2);

// select E_CYCLE and check position of E_CYCLE
// select FB E_CYCLE and move it to new position of x=125, y=185
final SWTBotGefEditor editor = bot.gefEditor(UITestNamesHelper.PROJECT_NAME);
assertNotNull(editor);
assertNotNull(editor.getEditPart(UITestNamesHelper.E_CYCLE_FB));
editor.click(UITestNamesHelper.E_CYCLE_FB);
final SWTBotGefEditPart fb1 = editor.getEditPart(UITestNamesHelper.E_CYCLE_FB).parent();
assertNotNull(fb1);
final Rectangle fb1Bounds1 = fbBot.getBoundsOfFB(editor, UITestNamesHelper.E_CYCLE_FB);
assertTrue(fb1Bounds1.contains(pos1.x, pos1.y));

// check position of E_N_TABLE
final Rectangle fb2Bounds1 = fbBot.getBoundsOfFB(editor, UITestNamesHelper.E_N_TABLE_FB);
assertTrue(fb2Bounds1.contains(pos2.x, pos2.y));

// move E_CYCLE and check new position
final Point pos3 = new Point(125, 185);
editor.drag(fb1, pos3.x, pos3.y);
final Rectangle fb1Bounds2 = fbBot.getBoundsOfFB(editor, UITestNamesHelper.E_CYCLE_FB);
assertTrue(fb1Bounds2.contains(pos3.x, pos3.y));
fbBot.moveSingleFB(editor, UITestNamesHelper.E_CYCLE_FB, new Point(125, 185));

// check if E_N_TABLE is still on same position
assertTrue(fb2Bounds1.contains(pos2.x, pos2.y));
// check position of E_N_TABLE to ensure that it unchanged
final Rectangle boundsENTable = fbBot.getBoundsOfFB(editor, UITestNamesHelper.E_N_TABLE_FB);
assertTrue(boundsENTable.contains(pos2.x, pos2.y));
}

/**
Expand All @@ -181,7 +166,7 @@ public void move1FB() {
* this position is also checked. To achieve this it is necessary to create a
* draw2d.geometry Point with the same coordinates of the swt.graphics Point.
*/
@SuppressWarnings({ "static-method", "static-access" })
@SuppressWarnings("static-method")
@Test
public void moveBothFBOneAfterAnother() {
final Point pos1 = new Point(200, 200);
Expand All @@ -190,30 +175,10 @@ public void moveBothFBOneAfterAnother() {
final Point pos2 = new Point(400, 200);
fbBot.dragAndDropEventsFB(UITestNamesHelper.E_SWITCH_TREE_ITEM, pos2);

// select and move E_CYCLE
// select and move E_CYCLE and E_SWITCH
final SWTBot4diacGefEditor editor = (SWTBot4diacGefEditor) bot.gefEditor(UITestNamesHelper.PROJECT_NAME);
assertNotNull(editor);
assertNotNull(editor.getEditPart(UITestNamesHelper.E_CYCLE_FB));
editor.click(UITestNamesHelper.E_CYCLE_FB);
final SWTBotGefEditPart fb1 = editor.getEditPart(UITestNamesHelper.E_CYCLE_FB).parent();
assertNotNull(fb1);
final Rectangle fb1Bounds1 = fbBot.getBoundsOfFB(editor, UITestNamesHelper.E_CYCLE_FB);
assertTrue(fb1Bounds1.contains(pos1.x, pos1.y));
final Point pos3 = new Point(85, 85);
editor.drag(fb1, pos3.x, pos3.y);
final Rectangle fb1Bounds2 = fbBot.getBoundsOfFB(editor, UITestNamesHelper.E_CYCLE_FB);
assertTrue(fb1Bounds2.contains(pos3.x, pos3.y));

// select and move E_SWITCH
editor.click(UITestNamesHelper.E_SWITCH_FB);
final SWTBotGefEditPart fb2 = editor.getEditPart(UITestNamesHelper.E_SWITCH_FB).parent();
assertNotNull(fb2);
final Rectangle fb2Bounds1 = fbBot.getBoundsOfFB(editor, UITestNamesHelper.E_SWITCH_FB);
assertTrue(fb2Bounds1.contains(pos2.x, pos2.y));
final Point pos4 = new Point(285, 85);
editor.drag(fb2, pos4.x, pos4.y);
final Rectangle fb2Bounds2 = fbBot.getBoundsOfFB(editor, UITestNamesHelper.E_SWITCH_FB);
assertTrue(fb2Bounds2.contains(pos4.x, pos4.y));
fbBot.moveSingleFB(editor, UITestNamesHelper.E_CYCLE_FB, new Point(85, 85));
fbBot.moveSingleFB(editor, UITestNamesHelper.E_SWITCH_FB, new Point(285, 85));
}

/**
Expand All @@ -224,7 +189,7 @@ public void moveBothFBOneAfterAnother() {
* selected as expected. Then a rectangle is drawn over the FBs to check whether
* the FBs are selected.
*/
@SuppressWarnings({ "static-method", "static-access" })
@SuppressWarnings("static-method")
@Test
public void moveBothFBTogether() {
final Point absPos1Fb1 = new Point(100, 100);
Expand Down Expand Up @@ -291,7 +256,7 @@ public void moveBothFBTogether() {
* Then the E_CYCLE is moved and it is checked whether the start point of the
* connection has also moved and whether the end point has remained unchanged.
*/
@SuppressWarnings({ "static-method", "static-access" })
@SuppressWarnings("static-method")
@Test
public void checkIfConnectionRemainsAfterMoving1FB() {
final Point pos1 = new Point(100, 50);
Expand Down Expand Up @@ -351,7 +316,7 @@ public void checkIfConnectionRemainsAfterMoving1FB() {
* Afterwards, the E_SWITCH is also moved and checked whether the start point
* and end point of the connection match the new positions of the FBs.
*/
@SuppressWarnings({ "static-method", "static-access" })
@SuppressWarnings("static-method")
@Test
public void checkIfConnectionRemainsAfterMovingBothFBsOneAfterAnother() {
final Point pos1 = new Point(375, 75);
Expand Down Expand Up @@ -430,7 +395,7 @@ public void checkIfConnectionRemainsAfterMovingBothFBsOneAfterAnother() {
* FB and moved to a new position. The translation is calculated and compared
* with the new values of the connection start and end point.
*/
@SuppressWarnings({ "static-method", "static-access" })
@SuppressWarnings("static-method")
@Test
public void checkIfConnectionRemainsAfterMovingBothFBsTogether() {
final Point pos1 = new Point(200, 100);
Expand Down

0 comments on commit 6665064

Please sign in to comment.