From ee07abab85dea64ae3e38fe9c0fb68a4f643ca28 Mon Sep 17 00:00:00 2001
From: rolandandai <87148863+rolandandai@users.noreply.github.com>
Date: Wed, 15 May 2024 00:06:52 +0200
Subject: [PATCH 01/11] Add first gui tests
---
pom.xml | 6 ++
src/main/java/ElementButton.java | 26 +++++--
src/main/java/Game.java | 4 +
src/main/java/Gui.java | 44 ++++++++---
src/test/java/CharacterMovesUITest.java | 52 +++++++++++++
src/test/java/CharacterNotMovesUITest.java | 51 +++++++++++++
.../java/RepairmanNotPlacesPipeUITest.java | 73 +++++++++++++++++++
src/test/java/RepairmanPlacesPipeUITest.java | 73 +++++++++++++++++++
8 files changed, 312 insertions(+), 17 deletions(-)
create mode 100644 src/test/java/CharacterMovesUITest.java
create mode 100644 src/test/java/CharacterNotMovesUITest.java
create mode 100644 src/test/java/RepairmanNotPlacesPipeUITest.java
create mode 100644 src/test/java/RepairmanPlacesPipeUITest.java
diff --git a/pom.xml b/pom.xml
index b46c8bb..714d997 100644
--- a/pom.xml
+++ b/pom.xml
@@ -37,6 +37,12 @@
0.8.7
test
+
+ org.assertj
+ assertj-swing-junit
+ 3.9.2
+ test
+
diff --git a/src/main/java/ElementButton.java b/src/main/java/ElementButton.java
index d627a35..2e4e388 100644
--- a/src/main/java/ElementButton.java
+++ b/src/main/java/ElementButton.java
@@ -25,6 +25,7 @@
public class ElementButton extends JButton{
private ArrayList statusimages = new ArrayList();
private Element element;
+ private JDialog dialog;
public static ArrayList holdingPipes = new ArrayList();
/**
@@ -53,7 +54,8 @@ public class ElementButton extends JButton{
{
@Override
public void actionPerformed(ActionEvent e) {
- showActionButtonWindow();
+ JDialog dialog = showActionButtonWindow();
+ dialog.setVisible(true);
}
});
}
@@ -140,17 +142,18 @@ private int getElementIndexInNeighbors(Element e, boolean searchInParam)
return -1;
}
- private void showActionButtonWindow() {
+ public JDialog showActionButtonWindow() {
//Ha a karakternek nincs már több lépése, akkor ne jelenjen meg a gomb, és jelzünk a Controlnak is.
if(Game.getInstance().getCurrentCharacter().getRemainingSteps()==0){
Control.getInstance().invokeOutOfActionWarning();
- return;
+ return null;
}
-
+
/** Letrehozunk egy JDialog objektumot es beallitjuk a tulajdonsagait.*/
/** */
- JDialog dialog = new JDialog();
+ dialog = new JDialog();
+ dialog.setName(this.getName() + "Dialog");
dialog.setTitle("Valassz a muveletek kozul");
dialog.setModal(true);/** Beallitjuk modalis ablaknak, amig bezarodik, a foablak nem erheto el*/
dialog.setLocation(400, 200);
@@ -188,6 +191,7 @@ public void actionPerformed(ActionEvent e) {
ArrayList params = new ArrayList();
params.add(getElementIndexInNeighbors(place,true));
ActionButton moveButton = new ActionButton(params);
+ moveButton.setName(this.getName() + "Move");
moveButton.setActionCommand("Move");
moveButton.setText("Move");
moveButton.addActionListener(closeButtonListener);
@@ -199,6 +203,7 @@ public void actionPerformed(ActionEvent e) {
//Ha az elemen végre lehet hajtani lyukasztást, akkor hozzáadunk egy ezt végrehajtó gombot
if(element.canPerformAction("Stab")&&place==element) {
ActionButton stabButton = new ActionButton(null);
+ stabButton.setName(this.getName() + "Stab");
stabButton.setActionCommand("Stab");
stabButton.setText("Stab");
stabButton.addActionListener(closeButtonListener);
@@ -209,6 +214,7 @@ public void actionPerformed(ActionEvent e) {
//Ha az elemen végre lehet hajtani pumpa lerakást, akkor hozzáadunk egy ezt végrehajtó gombot
if(isRepairman && element.canPerformAction("PlacePump")&&place==element && ((Repairman)Game.getInstance().getCurrentCharacter()).hasHoldingPump()) {
ActionButton placePumpButton = new ActionButton(null);
+ placePumpButton.setName(this.getName() + "PlacePump");
placePumpButton.setActionCommand("PlacePump");
placePumpButton.setText("PlacePump");
placePumpButton.addActionListener(closeButtonListener);
@@ -219,6 +225,7 @@ public void actionPerformed(ActionEvent e) {
//Ha az elemen végre lehet hajtani cső lerakást, akkor hozzáadunk egy ezt végrehajtó gombot
if(isRepairman && element.canPerformAction("PlacePipe")&&place==element && ((Repairman)Game.getInstance().getCurrentCharacter()).getHoldingPipe()!=null) {
ActionButton placePipeButton = new ActionButton(null);
+ placePipeButton.setName(this.getName() + "PlacePipe");
placePipeButton.setActionCommand("PlacePipe");
placePipeButton.setText("PlacePipe");
placePipeButton.addActionListener(closeButtonListener);
@@ -229,6 +236,7 @@ public void actionPerformed(ActionEvent e) {
//Ha az elemen végre lehet hajtani pumpa felvételt, akkor hozzáadunk egy ezt végrehajtó gombot
if(isRepairman && element.canPerformAction("PickupPump")&&place==element&&hasNothing) {
ActionButton pickupPumpButton = new ActionButton(null);
+ pickupPumpButton.setName(this.getName() + "PickupPump");
pickupPumpButton.setActionCommand("PickupPump");
pickupPumpButton.setText("PickupPump");
pickupPumpButton.addActionListener(closeButtonListener);
@@ -243,6 +251,7 @@ public void actionPerformed(ActionEvent e) {
ArrayList params = new ArrayList();
params.add(getElementIndexInNeighbors(e, false));
ActionButton pickUpPipe = new ActionButton(params);
+ pickUpPipe.setName(this.getName() + "PickUpPipe");
pickUpPipe.setActionCommand("PickUpPipe");
pickUpPipe.setText("PickUpPipe "+e.getName());
pickUpPipe.addActionListener(closeButtonListener);
@@ -254,6 +263,7 @@ public void actionPerformed(ActionEvent e) {
//Ha az elemen végre lehet hajtani javítást, akkor hozzáadunk egy ezt végrehajtó gombot
if(isRepairman && element.canPerformAction("Repair")&&place==element && (element.getClass().getName().equals("Pump") && ((Pump)element).getBroken() || element.getClass().getName().equals("Pipe") && ((Pipe)element).getHoleOnPipe())) {
ActionButton repairButton = new ActionButton(null);
+ repairButton.setName(this.getName() + "Repair");
repairButton.setActionCommand("Repair");
repairButton.setText("Repair");
repairButton.addActionListener(closeButtonListener);
@@ -264,6 +274,7 @@ public void actionPerformed(ActionEvent e) {
//Ha az elemen végre lehet hajtani ragadossá tételt, akkor hozzáadunk egy ezt végrehajtó gombot
if(element.canPerformAction("Stick")&&place==element) {
ActionButton stickButton = new ActionButton(null);
+ stickButton.setName(this.getName() + "Stick");
stickButton.setActionCommand("Stick");
stickButton.setText("Stick");
stickButton.addActionListener(closeButtonListener);
@@ -274,6 +285,7 @@ public void actionPerformed(ActionEvent e) {
//Ha az elemen végre lehet hajtani csúszóssá tételt, akkor hozzáadunk egy ezt végrehajtó gombot
if(!isRepairman && element.canPerformAction("Slime")&&place==element) {
ActionButton slimeButton = new ActionButton(null);
+ slimeButton.setName(this.getName() + "Slime");
slimeButton.setActionCommand("Slime");
slimeButton.setText("Slime");
slimeButton.addActionListener(closeButtonListener);
@@ -310,6 +322,7 @@ public void actionPerformed(ActionEvent e) {
//Karakter körének véget vető gomb
ActionButton endMoveButton = new ActionButton(null);
+ endMoveButton.setName(this.getName() + "EndMove");
endMoveButton.setActionCommand("EndMove");
endMoveButton.setText("EndMove");
endMoveButton.addActionListener(closeButtonListener);
@@ -322,8 +335,7 @@ public void actionPerformed(ActionEvent e) {
/** A dialogus meretenek beallitasa a tartalomhoz*/
dialog.pack();
- /** A dialogus megjelenitese */
- dialog.setVisible(true);
+ return dialog;
}
public void update(){
diff --git a/src/main/java/Game.java b/src/main/java/Game.java
index 4cbd309..e9fa549 100644
--- a/src/main/java/Game.java
+++ b/src/main/java/Game.java
@@ -60,6 +60,10 @@ public static Game getInstance()
{
return instance;
}
+
+ public static void resetInstance() {
+ instance = new Game();
+ }
/**
* Beállítja a Game megfelelő változóit.
diff --git a/src/main/java/Gui.java b/src/main/java/Gui.java
index dac93b2..53c277b 100644
--- a/src/main/java/Gui.java
+++ b/src/main/java/Gui.java
@@ -1,17 +1,8 @@
import java.util.ArrayList;
-
-import javax.swing.JButton;
-import javax.swing.JFrame;
-import javax.swing.JLabel;
-import javax.swing.JPanel;
-import javax.swing.JTextField;
-import javax.swing.JSpinner;
-import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import javax.swing.*;
-import javax.swing.border.*;
/**
* A játék grafikus felületét megvalósító osztály.
@@ -41,7 +32,7 @@ public class Gui {
private Pipe pi6;
// Az egyetlen Gui objektum
- private static Gui instance = new Gui();;
+ private static Gui instance = new Gui();
/**
* Visszaadja a Singleton Gui objektumot
@@ -52,6 +43,11 @@ public static Gui getInstance() {
return instance;
}
+ public static Gui resetInstance() {
+ instance = new Gui();
+ return instance;
+ }
+
private Gui(){
// ---------------FRAME INIT-----------------
frame = new JFrame("codeX");
@@ -63,6 +59,7 @@ private Gui(){
// ---------------MENU PANEL-----------------
menuPanel = new JPanel();
+ menuPanel.setName("MenuPanel");
menuPanel.setSize(1280, 720);
menuPanel.setLayout(null); // Null layout beállítása a menü panelen
@@ -98,6 +95,7 @@ public void stateChanged(ChangeEvent e) {
menuPanel.add(sPlayerCount);
JButton bStart = new JButton("Start");
+ bStart.setName("StartButton");
bStart.setBounds(500, 600, 200, 50);
bStart.setFont(new Font("Arial", Font.PLAIN, 30));
bStart.setBackground(Color.GRAY);
@@ -131,6 +129,7 @@ public void paintComponent(Graphics g) {
}
};
gamePanel.setLayout(null);
+ gamePanel.setName("GamePanel");
gamePanel.setSize(1280, 720);
elementButtons = new ArrayList();
@@ -221,6 +220,7 @@ public void paintComponent(Graphics g) {
Cistern c1 = new Cistern(0);
c1.setName("Cistern1");
ElementButton cistern1 = new ElementButton(c1);
+ cistern1.setName("Cistern1EB");
cistern1.setBounds(200, 500, 90, 90);
elementButtons.add(cistern1);
Game.getInstance().addCistern(c1);
@@ -229,6 +229,7 @@ public void paintComponent(Graphics g) {
WaterSource w1 = new WaterSource();
w1.setName("WaterSource1");
ElementButton ws1 = new ElementButton(w1);
+ ws1.setName("WaterSource1EB");
ws1.setBounds(200, 100, 90, 90);
elementButtons.add(ws1);
Game.getInstance().addElement(w1);
@@ -237,6 +238,7 @@ public void paintComponent(Graphics g) {
Pump p1 = new Pump();
p1.setName("Pump1");
ElementButton pump1 = new ElementButton(p1);
+ pump1.setName("Pump1EB");
pump1.setBounds(200, 300, 90, 90);
elementButtons.add(pump1);
Game.getInstance().addPump(p1);
@@ -245,6 +247,7 @@ public void paintComponent(Graphics g) {
Cistern c2 = new Cistern(0);
c2.setName("Cistern2");
ElementButton cistern2 = new ElementButton(c2);
+ cistern2.setName("Cistern2EB");
cistern2.setBounds(600, 500, 90, 90);
elementButtons.add(cistern2);
Game.getInstance().addCistern(c2);
@@ -253,6 +256,7 @@ public void paintComponent(Graphics g) {
WaterSource w2 = new WaterSource();
w2.setName("WaterSource2");
ElementButton ws2 = new ElementButton(w2);
+ ws2.setName("WaterSource2EB");
ws2.setBounds(600, 100, 90, 90);
elementButtons.add(ws2);
Game.getInstance().addElement(w2);
@@ -261,6 +265,7 @@ public void paintComponent(Graphics g) {
Pump p2 = new Pump(false, true, 0);
p2.setName("Pump2");
ElementButton pump2 = new ElementButton(p2);
+ pump2.setName("Pump2EB");
pump2.setBounds(600, 300, 90, 90);
elementButtons.add(pump2);
Game.getInstance().addPump(p2);
@@ -274,6 +279,7 @@ public void paintComponent(Graphics g) {
pi1.addNeighbor(p1);
p1.addNeighbor(pi1);
ElementButton pie1 = new ElementButton(pi1);
+ pie1.setName("Pipe1EB");
pie1.setBounds(215, 215, 60, 60);
elementButtons.add(pie1);
Game.getInstance().addPipe(pi1);
@@ -286,6 +292,7 @@ public void paintComponent(Graphics g) {
pi2.addNeighbor(p1);
p1.addNeighbor(pi2);
ElementButton pie2 = new ElementButton(pi2);
+ pie2.setName("Pipe2EB");
pie2.setBounds(215, 415, 60, 60);
elementButtons.add(pie2);
Game.getInstance().addPipe(pi2);
@@ -298,6 +305,7 @@ public void paintComponent(Graphics g) {
pi3.addNeighbor(p2);
p2.addNeighbor(pi3);
ElementButton pie3 = new ElementButton(pi3);
+ pie2.setName("Pipe3EB");
pie3.setBounds(615, 415, 60, 60);
elementButtons.add(pie3);
Game.getInstance().addPipe(pi3);
@@ -310,6 +318,7 @@ public void paintComponent(Graphics g) {
pi4.addNeighbor(p2);
p2.addNeighbor(pi4);
ElementButton pie4 = new ElementButton(pi4);
+ pie4.setName("Pipe4EB");
pie4.setBounds(615, 215, 60, 60);
elementButtons.add(pie4);
Game.getInstance().addPipe(pi4);
@@ -322,6 +331,7 @@ public void paintComponent(Graphics g) {
pi5.addNeighbor(p2);
p2.addNeighbor(pi5);
ElementButton pie5 = new ElementButton(pi5);
+ pie5.setName("Pipe5EB");
pie5.setBounds(415, 315, 60, 60);
elementButtons.add(pie5);
Game.getInstance().addPipe(pi5);
@@ -332,6 +342,7 @@ public void paintComponent(Graphics g) {
pi6.addNeighbor(p2);
p2.addNeighbor(pi6);
ElementButton pie6 = new ElementButton(pi6);
+ pie6.setName("Pipe6EB");
pie6.setEnabled(true);
pie6.setBounds(815, 315, 60, 60);
elementButtons.add(pie6);
@@ -568,4 +579,17 @@ public ElementButton findElementButton(Element element)
}
return null;
}
+
+ public JFrame getFrame() {
+ return frame;
+ }
+
+ public ElementButton getElementButton(String name) {
+ for (ElementButton eb : elementButtons) {
+ if (eb.getName() != null && eb.getName().equals(name)) {
+ return eb;
+ }
+ }
+ return null;
+ }
}
diff --git a/src/test/java/CharacterMovesUITest.java b/src/test/java/CharacterMovesUITest.java
new file mode 100644
index 0000000..73a6f25
--- /dev/null
+++ b/src/test/java/CharacterMovesUITest.java
@@ -0,0 +1,52 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import javax.swing.JDialog;
+import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
+import org.assertj.swing.edt.GuiActionRunner;
+import org.assertj.swing.fixture.DialogFixture;
+import org.junit.BeforeClass;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class CharacterMovesTest {
+ private DialogFixture window;
+ private Gui gui;
+
+ @BeforeClass
+ public static void setUpOnce() {
+ FailOnThreadViolationRepaintManager.install();
+ }
+
+ @BeforeEach
+ public void setUp() {
+ Gui.getInstance().getFrame().setVisible(false);
+
+ Game.resetInstance();
+ gui = Gui.resetInstance();
+
+ gui.nextPanel();
+ ElementButton pipe4 = gui.getElementButton("Pipe4EB");
+
+ JDialog dialog = GuiActionRunner.execute(() -> {
+ return pipe4.showActionButtonWindow();
+ });
+ window = new DialogFixture(dialog);
+ window.show();
+ }
+
+ @Test
+ void Character_MovesToPipe_WhenItsNeighbor() {
+ Game game = Game.getInstance();
+ assertEquals("WaterSource2", game.getSaboteurGroup().get(0).getPosition().getName());
+
+ window.button("Pipe4EBMove").click();
+
+ assertEquals("Pipe4", game.getSaboteurGroup().get(0).getPosition().getName());
+ }
+
+ @AfterEach
+ public void tearDown() {
+ window.cleanUp();
+ }
+}
diff --git a/src/test/java/CharacterNotMovesUITest.java b/src/test/java/CharacterNotMovesUITest.java
new file mode 100644
index 0000000..a3a805a
--- /dev/null
+++ b/src/test/java/CharacterNotMovesUITest.java
@@ -0,0 +1,51 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import javax.swing.JDialog;
+import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
+import org.assertj.swing.edt.GuiActionRunner;
+import org.assertj.swing.exception.ComponentLookupException;
+import org.assertj.swing.fixture.DialogFixture;
+import org.junit.BeforeClass;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class CharacterNotMovesTest {
+ private DialogFixture window;
+ private Gui gui;
+
+ @BeforeClass
+ public static void setUpOnce() {
+ FailOnThreadViolationRepaintManager.install();
+ }
+
+ @BeforeEach
+ public void setUp() {
+ Gui.getInstance().getFrame().setVisible(false);
+
+ Game.resetInstance();
+ gui = Gui.resetInstance();
+
+ gui.nextPanel();
+ ElementButton cistern1 = gui.getElementButton("Cistern1EB");
+
+ JDialog dialog = GuiActionRunner.execute(() -> {
+ return cistern1.showActionButtonWindow();
+ });
+ window = new DialogFixture(dialog);
+ window.show();
+ }
+
+ @Test
+ void Character_MovesNotMovesToCistern_WhenItsNotNeighbor() {
+ Game game = Game.getInstance();
+ assertEquals("WaterSource2", game.getSaboteurGroup().get(0).getPosition().getName());
+
+ assertThrows(ComponentLookupException.class, () -> window.button("Cistern1EBMove"));
+ }
+
+ @AfterEach
+ public void tearDown() {
+ window.cleanUp();
+ }
+}
diff --git a/src/test/java/RepairmanNotPlacesPipeUITest.java b/src/test/java/RepairmanNotPlacesPipeUITest.java
new file mode 100644
index 0000000..197de13
--- /dev/null
+++ b/src/test/java/RepairmanNotPlacesPipeUITest.java
@@ -0,0 +1,73 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.ArrayList;
+
+import javax.swing.JDialog;
+import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
+import org.assertj.swing.edt.GuiActionRunner;
+import org.assertj.swing.fixture.DialogFixture;
+import org.junit.BeforeClass;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class RepairmanNotPlacesPipeTest {
+ private DialogFixture window;
+ private Gui gui;
+
+ @BeforeClass
+ public static void setUpOnce() {
+ FailOnThreadViolationRepaintManager.install();
+ }
+
+ @BeforeEach
+ public void setUp() {
+ Gui.getInstance().getFrame().setVisible(false);
+
+ Game.resetInstance();
+ gui = Gui.resetInstance();
+
+ gui.nextPanel();
+
+ ElementButton cistern2 = gui.getElementButton("Cistern2EB");
+ JDialog dialog = GuiActionRunner.execute(() -> {
+ return cistern2.showActionButtonWindow();
+ });
+ window = new DialogFixture(dialog);
+ window.show();
+ }
+
+ @Test
+ void Repairman_NotPlacesPipe_WhenHeDoesNotHaveOne() {
+ window.button("Cistern2EBEndMove").click();
+
+ final ElementButton cistern2 = gui.getElementButton("Cistern2EB");
+ cistern2.showActionButtonWindow();
+ window.show();
+
+ window.button("Cistern2EBEndMove").click();
+
+ cistern2.showActionButtonWindow();
+ window.show();
+
+ window.button("Cistern2EBEndMove").click();
+
+ window.cleanUp();
+ JDialog lastDialog = GuiActionRunner.execute(() -> cistern2.showActionButtonWindow());
+ DialogFixture lastDialogFixture = new DialogFixture(lastDialog);
+ lastDialogFixture.show();
+
+ Game game = Game.getInstance();
+ ArrayList elements = game.getGameElements();
+ Element pipe6 = elements.get(elements.size() - 1);
+ game.getRepairmanGroup().get(1).setHoldingPipe(null);
+ assertNull(game.getRepairmanGroup().get(1).getHoldingPipe());
+ assertEquals(1, pipe6.getNeighbors().size());
+
+ lastDialogFixture.button("Cistern2EBPlacePipe").click();
+
+ assertNull(game.getRepairmanGroup().get(1).getHoldingPipe());
+ assertEquals(1, pipe6.getNeighbors().size());
+
+ lastDialogFixture.cleanUp();
+ }
+}
diff --git a/src/test/java/RepairmanPlacesPipeUITest.java b/src/test/java/RepairmanPlacesPipeUITest.java
new file mode 100644
index 0000000..f1607bf
--- /dev/null
+++ b/src/test/java/RepairmanPlacesPipeUITest.java
@@ -0,0 +1,73 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.ArrayList;
+
+import javax.swing.JDialog;
+import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
+import org.assertj.swing.edt.GuiActionRunner;
+import org.assertj.swing.fixture.DialogFixture;
+import org.junit.BeforeClass;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class RepairmanPlacesPipeTest {
+ private DialogFixture window;
+ private Gui gui;
+
+ @BeforeClass
+ public static void setUpOnce() {
+ FailOnThreadViolationRepaintManager.install();
+ }
+
+ @BeforeEach
+ public void setUp() {
+ Gui.getInstance().getFrame().setVisible(false);
+
+ Game.resetInstance();
+ gui = Gui.resetInstance();
+
+ gui.nextPanel();
+
+ ElementButton cistern2 = gui.getElementButton("Cistern2EB");
+ JDialog dialog = GuiActionRunner.execute(() -> {
+ return cistern2.showActionButtonWindow();
+ });
+ window = new DialogFixture(dialog);
+ window.show();
+ }
+
+ @Test
+ void Repairman_PlacesPipe_WhenHeIsOnCistern() {
+ window.button("Cistern2EBEndMove").click();
+
+ final ElementButton cistern2 = gui.getElementButton("Cistern2EB");
+ cistern2.showActionButtonWindow();
+ window.show();
+
+ window.button("Cistern2EBEndMove").click();
+
+ cistern2.showActionButtonWindow();
+ window.show();
+
+ window.button("Cistern2EBEndMove").click();
+
+ window.cleanUp();
+ JDialog lastDialog = GuiActionRunner.execute(() -> cistern2.showActionButtonWindow());
+ DialogFixture lastDialogFixture = new DialogFixture(lastDialog);
+ lastDialogFixture.show();
+
+ Game game = Game.getInstance();
+ ArrayList elements = game.getGameElements();
+ Element pipe6 = elements.get(elements.size() - 1);
+ assertNotNull(game.getRepairmanGroup().get(1).getHoldingPipe());
+ assertEquals(1, pipe6.getNeighbors().size());
+
+ lastDialogFixture.button("Cistern2EBPlacePipe").click();
+
+ assertNull(game.getRepairmanGroup().get(1).getHoldingPipe());
+ assertEquals(2, pipe6.getNeighbors().size());
+ assertEquals("Cistern2", pipe6.getNeighbors().get(1).getName());
+
+ lastDialogFixture.cleanUp();
+ }
+}
From 7a3c8f1c04f06b9907ea4b7b048c234293fe3427 Mon Sep 17 00:00:00 2001
From: rolandandai <87148863+rolandandai@users.noreply.github.com>
Date: Wed, 15 May 2024 09:38:33 +0200
Subject: [PATCH 02/11] Add VNC for UI tests
---
.github/workflows/maven.yml | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml
index de32502..b9e3187 100644
--- a/.github/workflows/maven.yml
+++ b/.github/workflows/maven.yml
@@ -27,8 +27,10 @@ jobs:
java-version: '17'
distribution: 'temurin'
cache: maven
+ - name: Install TightVnc for UI tests
+ run: sudo apt-get install tightvncserver
- name: Build with Maven
- run: mvn -B package --file pom.xml
+ run: execute-on-vnc.sh mvn -B package --file pom.xml
- name: Cache SonarCloud packages
uses: actions/cache@v3
with:
@@ -45,4 +47,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=BME-MIT-IET_iet-hf-2024-iet-arcok
+ run: execute-on-vnc.sh mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=BME-MIT-IET_iet-hf-2024-iet-arcok
From f520f8dda570c8cb4d3c50268aedfe60b9265503 Mon Sep 17 00:00:00 2001
From: rolandandai <87148863+rolandandai@users.noreply.github.com>
Date: Wed, 15 May 2024 09:42:03 +0200
Subject: [PATCH 03/11] Add shell script for VNC
---
execute-on-vnc.sh | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
create mode 100644 execute-on-vnc.sh
diff --git a/execute-on-vnc.sh b/execute-on-vnc.sh
new file mode 100644
index 0000000..3c1aeb7
--- /dev/null
+++ b/execute-on-vnc.sh
@@ -0,0 +1,27 @@
+#/bin/bash
+NEW_DISPLAY=42
+DONE="no"
+
+while [ "$DONE" == "no" ]
+do
+ out=$(xdpyinfo -display :${NEW_DISPLAY} 2>&1)
+ if [[ "$out" == name* ]] || [[ "$out" == Invalid* ]]
+ then
+ # command succeeded; or failed with access error; display exists
+ (( NEW_DISPLAY+=1 ))
+ else
+ # display doesn't exist
+ DONE="yes"
+ fi
+done
+
+echo "Using first available display :${NEW_DISPLAY}"
+
+OLD_DISPLAY=${DISPLAY}
+vncserver ":${NEW_DISPLAY}" -localhost -geometry 1600x1200 -depth 16
+export DISPLAY=:${NEW_DISPLAY}
+
+"$@"
+
+export DISPLAY=${OLD_DISPLAY}
+vncserver -kill ":${NEW_DISPLAY}"
From 5088018ddc9ef5cc9cbc35187772c9fceeed8c0e Mon Sep 17 00:00:00 2001
From: rolandandai <87148863+rolandandai@users.noreply.github.com>
Date: Wed, 15 May 2024 09:44:19 +0200
Subject: [PATCH 04/11] Add run permission for VNC script
---
.github/workflows/maven.yml | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml
index b9e3187..4d2a30a 100644
--- a/.github/workflows/maven.yml
+++ b/.github/workflows/maven.yml
@@ -29,8 +29,10 @@ jobs:
cache: maven
- name: Install TightVnc for UI tests
run: sudo apt-get install tightvncserver
+ - name: Add permission to run vnc script
+ run: chmod +x ./execute-on-vnc.sh
- name: Build with Maven
- run: execute-on-vnc.sh mvn -B package --file pom.xml
+ run: ./execute-on-vnc.sh mvn -B package --file pom.xml
- name: Cache SonarCloud packages
uses: actions/cache@v3
with:
@@ -47,4 +49,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- run: execute-on-vnc.sh mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=BME-MIT-IET_iet-hf-2024-iet-arcok
+ run: ./execute-on-vnc.sh mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=BME-MIT-IET_iet-hf-2024-iet-arcok
From 27daf929878af3ef7919ef14d2b4dbe9f0d93200 Mon Sep 17 00:00:00 2001
From: rolandandai <87148863+rolandandai@users.noreply.github.com>
Date: Wed, 15 May 2024 09:54:01 +0200
Subject: [PATCH 05/11] Add password to the script
---
execute-on-vnc.sh | 1 +
1 file changed, 1 insertion(+)
diff --git a/execute-on-vnc.sh b/execute-on-vnc.sh
index 3c1aeb7..d80b1fa 100644
--- a/execute-on-vnc.sh
+++ b/execute-on-vnc.sh
@@ -16,6 +16,7 @@ do
done
echo "Using first available display :${NEW_DISPLAY}"
+echo "password" | vncpasswd -f > ~/.vnc/passwd
OLD_DISPLAY=${DISPLAY}
vncserver ":${NEW_DISPLAY}" -localhost -geometry 1600x1200 -depth 16
From 509107121519791708fb693d8725e195a1ab4900 Mon Sep 17 00:00:00 2001
From: rolandandai <87148863+rolandandai@users.noreply.github.com>
Date: Wed, 15 May 2024 09:58:32 +0200
Subject: [PATCH 06/11] Add .vnc directory
---
execute-on-vnc.sh | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/execute-on-vnc.sh b/execute-on-vnc.sh
index d80b1fa..5635fe0 100644
--- a/execute-on-vnc.sh
+++ b/execute-on-vnc.sh
@@ -2,6 +2,9 @@
NEW_DISPLAY=42
DONE="no"
+mkdir -p ~/.vnc
+echo "password" > ~/.vnc/passwd
+
while [ "$DONE" == "no" ]
do
out=$(xdpyinfo -display :${NEW_DISPLAY} 2>&1)
@@ -16,7 +19,6 @@ do
done
echo "Using first available display :${NEW_DISPLAY}"
-echo "password" | vncpasswd -f > ~/.vnc/passwd
OLD_DISPLAY=${DISPLAY}
vncserver ":${NEW_DISPLAY}" -localhost -geometry 1600x1200 -depth 16
From c1796780807cc327081e841e26c9c87f684087b2 Mon Sep 17 00:00:00 2001
From: rolandandai <87148863+rolandandai@users.noreply.github.com>
Date: Wed, 15 May 2024 10:01:25 +0200
Subject: [PATCH 07/11] Change how password is supplied
---
execute-on-vnc.sh | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/execute-on-vnc.sh b/execute-on-vnc.sh
index 5635fe0..6627dc6 100644
--- a/execute-on-vnc.sh
+++ b/execute-on-vnc.sh
@@ -3,7 +3,7 @@ NEW_DISPLAY=42
DONE="no"
mkdir -p ~/.vnc
-echo "password" > ~/.vnc/passwd
+PASSWORD=$(openssl rand -base64 12)
while [ "$DONE" == "no" ]
do
@@ -24,6 +24,8 @@ OLD_DISPLAY=${DISPLAY}
vncserver ":${NEW_DISPLAY}" -localhost -geometry 1600x1200 -depth 16
export DISPLAY=:${NEW_DISPLAY}
+echo "$PASSWORD" | x11vnc -storepasswd -input - | sudo tee ~/.vnc/passwd >/dev/null 2>&1
+
"$@"
export DISPLAY=${OLD_DISPLAY}
From ff4cc416e20db6fb01b3d33a8d1b3dc764d78ab9 Mon Sep 17 00:00:00 2001
From: rolandandai <87148863+rolandandai@users.noreply.github.com>
Date: Wed, 15 May 2024 10:04:59 +0200
Subject: [PATCH 08/11] Change vnc password supply
---
execute-on-vnc.sh | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/execute-on-vnc.sh b/execute-on-vnc.sh
index 6627dc6..30b6dd0 100644
--- a/execute-on-vnc.sh
+++ b/execute-on-vnc.sh
@@ -2,8 +2,10 @@
NEW_DISPLAY=42
DONE="no"
-mkdir -p ~/.vnc
-PASSWORD=$(openssl rand -base64 12)
+umask 0077
+mkdir -p "$HOME/.vnc"
+chmod go-rwx "$HOME/.vnc"
+vncpasswd -f <<<"$password" >"$HOME/.vnc/passwd"
while [ "$DONE" == "no" ]
do
@@ -24,8 +26,6 @@ OLD_DISPLAY=${DISPLAY}
vncserver ":${NEW_DISPLAY}" -localhost -geometry 1600x1200 -depth 16
export DISPLAY=:${NEW_DISPLAY}
-echo "$PASSWORD" | x11vnc -storepasswd -input - | sudo tee ~/.vnc/passwd >/dev/null 2>&1
-
"$@"
export DISPLAY=${OLD_DISPLAY}
From 6590f04f7c6e3c58790d07c4ba7e85f880630f88 Mon Sep 17 00:00:00 2001
From: rolandandai <87148863+rolandandai@users.noreply.github.com>
Date: Wed, 15 May 2024 10:20:51 +0200
Subject: [PATCH 09/11] Simplify PlacePipe UI test cases
---
.../java/RepairmanNotPlacesPipeUITest.java | 36 +++++++------------
src/test/java/RepairmanPlacesPipeUITest.java | 29 +++++----------
2 files changed, 22 insertions(+), 43 deletions(-)
diff --git a/src/test/java/RepairmanNotPlacesPipeUITest.java b/src/test/java/RepairmanNotPlacesPipeUITest.java
index 197de13..b487b5c 100644
--- a/src/test/java/RepairmanNotPlacesPipeUITest.java
+++ b/src/test/java/RepairmanNotPlacesPipeUITest.java
@@ -5,10 +5,13 @@
import javax.swing.JDialog;
import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
import org.assertj.swing.edt.GuiActionRunner;
+import org.assertj.swing.exception.ComponentLookupException;
import org.assertj.swing.fixture.DialogFixture;
import org.junit.BeforeClass;
+import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.assertj.swing.exception.ComponentLookupException;
class RepairmanNotPlacesPipeTest {
private DialogFixture window;
@@ -27,6 +30,10 @@ public void setUp() {
gui = Gui.resetInstance();
gui.nextPanel();
+
+ Game game = Game.getInstance();
+ game.setCurrentCharacter(game.getRepairmanGroup().get(1));
+ game.getRepairmanGroup().get(1).setHoldingPipe(null);
ElementButton cistern2 = gui.getElementButton("Cistern2EB");
JDialog dialog = GuiActionRunner.execute(() -> {
@@ -38,36 +45,19 @@ public void setUp() {
@Test
void Repairman_NotPlacesPipe_WhenHeDoesNotHaveOne() {
- window.button("Cistern2EBEndMove").click();
-
- final ElementButton cistern2 = gui.getElementButton("Cistern2EB");
- cistern2.showActionButtonWindow();
- window.show();
-
- window.button("Cistern2EBEndMove").click();
-
- cistern2.showActionButtonWindow();
- window.show();
-
- window.button("Cistern2EBEndMove").click();
-
- window.cleanUp();
- JDialog lastDialog = GuiActionRunner.execute(() -> cistern2.showActionButtonWindow());
- DialogFixture lastDialogFixture = new DialogFixture(lastDialog);
- lastDialogFixture.show();
-
Game game = Game.getInstance();
ArrayList elements = game.getGameElements();
Element pipe6 = elements.get(elements.size() - 1);
- game.getRepairmanGroup().get(1).setHoldingPipe(null);
assertNull(game.getRepairmanGroup().get(1).getHoldingPipe());
assertEquals(1, pipe6.getNeighbors().size());
- lastDialogFixture.button("Cistern2EBPlacePipe").click();
+ assertThrows(ComponentLookupException.class, () -> window.button("Cistern2EBPlacePipe").click());
- assertNull(game.getRepairmanGroup().get(1).getHoldingPipe());
assertEquals(1, pipe6.getNeighbors().size());
-
- lastDialogFixture.cleanUp();
+ }
+
+ @AfterEach
+ public void tearDown() {
+ window.cleanUp();
}
}
diff --git a/src/test/java/RepairmanPlacesPipeUITest.java b/src/test/java/RepairmanPlacesPipeUITest.java
index f1607bf..67a6076 100644
--- a/src/test/java/RepairmanPlacesPipeUITest.java
+++ b/src/test/java/RepairmanPlacesPipeUITest.java
@@ -7,6 +7,7 @@
import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.fixture.DialogFixture;
import org.junit.BeforeClass;
+import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -27,6 +28,9 @@ public void setUp() {
gui = Gui.resetInstance();
gui.nextPanel();
+
+ Game game = Game.getInstance();
+ game.setCurrentCharacter(game.getRepairmanGroup().get(1));
ElementButton cistern2 = gui.getElementButton("Cistern2EB");
JDialog dialog = GuiActionRunner.execute(() -> {
@@ -38,36 +42,21 @@ public void setUp() {
@Test
void Repairman_PlacesPipe_WhenHeIsOnCistern() {
- window.button("Cistern2EBEndMove").click();
-
- final ElementButton cistern2 = gui.getElementButton("Cistern2EB");
- cistern2.showActionButtonWindow();
- window.show();
-
- window.button("Cistern2EBEndMove").click();
-
- cistern2.showActionButtonWindow();
- window.show();
-
- window.button("Cistern2EBEndMove").click();
-
- window.cleanUp();
- JDialog lastDialog = GuiActionRunner.execute(() -> cistern2.showActionButtonWindow());
- DialogFixture lastDialogFixture = new DialogFixture(lastDialog);
- lastDialogFixture.show();
-
Game game = Game.getInstance();
ArrayList elements = game.getGameElements();
Element pipe6 = elements.get(elements.size() - 1);
assertNotNull(game.getRepairmanGroup().get(1).getHoldingPipe());
assertEquals(1, pipe6.getNeighbors().size());
- lastDialogFixture.button("Cistern2EBPlacePipe").click();
+ window.button("Cistern2EBPlacePipe").click();
assertNull(game.getRepairmanGroup().get(1).getHoldingPipe());
assertEquals(2, pipe6.getNeighbors().size());
assertEquals("Cistern2", pipe6.getNeighbors().get(1).getName());
+ }
- lastDialogFixture.cleanUp();
+ @AfterEach
+ public void tearDown() {
+ window.cleanUp();
}
}
From 6386e13eeefc89fb349298f32834293b97814f53 Mon Sep 17 00:00:00 2001
From: rolandandai <87148863+rolandandai@users.noreply.github.com>
Date: Wed, 15 May 2024 11:48:28 +0200
Subject: [PATCH 10/11] Add few more ui tests
---
src/main/java/Gui.java | 5 ++
src/test/java/EndMoveUITest.java | 61 ++++++++++++++
.../java/RepairmanNotPlacesPipeUITest.java | 3 +-
src/test/java/SaboteurActsOnPipeUITest.java | 80 +++++++++++++++++++
4 files changed, 147 insertions(+), 2 deletions(-)
create mode 100644 src/test/java/EndMoveUITest.java
create mode 100644 src/test/java/SaboteurActsOnPipeUITest.java
diff --git a/src/main/java/Gui.java b/src/main/java/Gui.java
index 53c277b..0dad0d1 100644
--- a/src/main/java/Gui.java
+++ b/src/main/java/Gui.java
@@ -75,6 +75,7 @@ private Gui(){
menuPanel.add(lPlayerCount);
sRoundSettings = new JSpinner(new SpinnerNumberModel(1, 1, 100, 1));
+ sRoundSettings.setName("RoundSetter");
sRoundSettings.setBounds(700, 90, 50, 50);
sRoundSettings.setFont(new Font("Arial", Font.PLAIN, 30));
menuPanel.add(sRoundSettings);
@@ -188,6 +189,7 @@ public void paintComponent(Graphics g) {
//Turn points label
lTurnPoints = new JLabel("0");
+ lTurnPoints.setName("TurnCounterLabel");
lTurnPoints.setFont(new Font("Arial", Font.PLAIN, 15));
lTurnPoints.setBounds(100, 65, 200, 20);
jPanelScoreBoard.add(lTurnPoints);
@@ -198,12 +200,14 @@ public void paintComponent(Graphics g) {
//Who`s turn label
lWhosTurn = new JLabel("...-s turn");
+ lWhosTurn.setName("WhosTurnLabel");
lWhosTurn.setFont(new Font("Arial", Font.BOLD, 25));
lWhosTurn.setBounds(980, 20, 300, 20);
gamePanel.add(lWhosTurn);
//End Move button
JButton bEndMove = new JButton("End Move");
+ bEndMove.setName("EndMoveButton");
bEndMove.setBounds(980, 550, 300, 50);
bEndMove.setFont(new Font("Arial", Font.PLAIN, 20));
bEndMove.setBackground(Color.WHITE);
@@ -212,6 +216,7 @@ public void paintComponent(Graphics g) {
gamePanel.add(bEndMove);
JButton bEnd = new JButton("End Game");
+ bEnd.setName("EndButton");
bEnd.setFont(new Font("Arial", Font.PLAIN, 20));
bEnd.setBounds(980, 600, 300, 50);
bEnd.addActionListener(e -> nextPanel());
diff --git a/src/test/java/EndMoveUITest.java b/src/test/java/EndMoveUITest.java
new file mode 100644
index 0000000..1c5517f
--- /dev/null
+++ b/src/test/java/EndMoveUITest.java
@@ -0,0 +1,61 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import javax.swing.JFrame;
+
+import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
+import org.assertj.swing.edt.GuiActionRunner;
+import org.assertj.swing.fixture.FrameFixture;
+import org.junit.BeforeClass;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class EndMoveUITest {
+ private FrameFixture window;
+ private Gui gui;
+
+ @BeforeClass
+ public static void setUpOnce() {
+ FailOnThreadViolationRepaintManager.install();
+ }
+
+ @BeforeEach
+ public void setUp() {
+ Gui.getInstance().getFrame().setVisible(false);
+
+ Game.resetInstance();
+ Game.getInstance().setTurns(2);
+ gui = Gui.resetInstance();
+
+ gui.nextPanel();
+
+ JFrame frame = GuiActionRunner.execute(() -> {
+ return gui.getFrame();
+ });
+ window = new FrameFixture(frame);
+ }
+
+ @Test
+ void Move_Ends_WhenEndingTurn() {
+ assertEquals("Saboteur1-s Turn", window.label("WhosTurnLabel").text());
+
+ window.button("EndMoveButton").click();
+ assertEquals("Saboteur2-s Turn", window.label("WhosTurnLabel").text());
+
+ window.button("EndMoveButton").click();
+ assertEquals("Repairman1-s Turn", window.label("WhosTurnLabel").text());
+
+ window.button("EndMoveButton").click();
+ assertEquals("Repairman2-s Turn", window.label("WhosTurnLabel").text());
+ assertEquals("0", window.label("TurnCounterLabel").text());
+
+ window.button("EndMoveButton").click();
+ assertEquals("Saboteur1-s Turn", window.label("WhosTurnLabel").text());
+ assertEquals("1", window.label("TurnCounterLabel").text());
+ }
+
+ @AfterEach
+ public void tearDown() {
+ window.cleanUp();
+ }
+}
diff --git a/src/test/java/RepairmanNotPlacesPipeUITest.java b/src/test/java/RepairmanNotPlacesPipeUITest.java
index b487b5c..22cafa6 100644
--- a/src/test/java/RepairmanNotPlacesPipeUITest.java
+++ b/src/test/java/RepairmanNotPlacesPipeUITest.java
@@ -11,7 +11,6 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.assertj.swing.exception.ComponentLookupException;
class RepairmanNotPlacesPipeTest {
private DialogFixture window;
@@ -51,7 +50,7 @@ void Repairman_NotPlacesPipe_WhenHeDoesNotHaveOne() {
assertNull(game.getRepairmanGroup().get(1).getHoldingPipe());
assertEquals(1, pipe6.getNeighbors().size());
- assertThrows(ComponentLookupException.class, () -> window.button("Cistern2EBPlacePipe").click());
+ assertThrows(ComponentLookupException.class, () -> window.button("Cistern2EBPlacePipe"));
assertEquals(1, pipe6.getNeighbors().size());
}
diff --git a/src/test/java/SaboteurActsOnPipeUITest.java b/src/test/java/SaboteurActsOnPipeUITest.java
new file mode 100644
index 0000000..bebcac5
--- /dev/null
+++ b/src/test/java/SaboteurActsOnPipeUITest.java
@@ -0,0 +1,80 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import javax.swing.JDialog;
+import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
+import org.assertj.swing.edt.GuiActionRunner;
+import org.assertj.swing.fixture.DialogFixture;
+import org.junit.BeforeClass;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class SaboteurActsOnPipeUITest {
+ private DialogFixture window;
+ private Gui gui;
+
+ @BeforeClass
+ public static void setUpOnce() {
+ FailOnThreadViolationRepaintManager.install();
+ }
+
+ @BeforeEach
+ public void setUp() {
+ Gui.getInstance().getFrame().setVisible(false);
+
+ Game.resetInstance();
+ gui = Gui.resetInstance();
+
+ gui.nextPanel();
+
+ Game.getInstance().getCurrentCharacter().move(0);
+ ElementButton pipe4 = gui.getElementButton("Pipe4EB");
+
+ JDialog dialog = GuiActionRunner.execute(() -> {
+ return pipe4.showActionButtonWindow();
+ });
+ window = new DialogFixture(dialog);
+ window.show();
+ }
+
+ @Test
+ void Saboteur_StabsPipe_WhenHeIsOnPipe() {
+ Game game = Game.getInstance();
+ Pipe pipe4 = (Pipe)game.getSaboteurGroup().get(0).getPosition();
+
+ assertEquals(false, pipe4.getHoleOnPipe());
+
+ window.button("Pipe4EBStab").click();
+
+ assertEquals(true, pipe4.getHoleOnPipe());
+ }
+
+ @Test
+ void Saboteur_SticksPipe_WhenHeIsOnPipe() {
+ Game game = Game.getInstance();
+ Pipe pipe4 = (Pipe)game.getSaboteurGroup().get(0).getPosition();
+
+ assertEquals(0, pipe4.getSticky());
+
+ window.button("Pipe4EBStick").click();
+
+ assertEquals(-3, pipe4.getSticky());
+ }
+
+ @Test
+ void Saboteur_SlimesPipe_WhenHeIsOnPipe() {
+ Game game = Game.getInstance();
+ Pipe pipe4 = (Pipe)game.getSaboteurGroup().get(0).getPosition();
+
+ assertEquals(0, pipe4.getSlimey());
+
+ window.button("Pipe4EBSlime").click();
+
+ assertEquals(3, pipe4.getSlimey());
+ }
+
+ @AfterEach
+ public void tearDown() {
+ window.cleanUp();
+ }
+}
From 2ad645b267496ffd38b3c00458dedbecc1ebfc49 Mon Sep 17 00:00:00 2001
From: Duhi95 <127203489+Duhi95@users.noreply.github.com>
Date: Wed, 22 May 2024 19:04:24 +0200
Subject: [PATCH 11/11] Added the UI tests for the main menu elements
---
src/main/java/Gui.java | 1 +
src/test/java/CharacterTest.java | 13 --
.../java/MenuPlayerCountAndRoundsUITest.java | 132 ++++++++++++++++++
src/test/java/MenuStartButtonUITest.java | 74 ++++++++++
4 files changed, 207 insertions(+), 13 deletions(-)
delete mode 100644 src/test/java/CharacterTest.java
create mode 100644 src/test/java/MenuPlayerCountAndRoundsUITest.java
create mode 100644 src/test/java/MenuStartButtonUITest.java
diff --git a/src/main/java/Gui.java b/src/main/java/Gui.java
index 0dad0d1..249dd4b 100644
--- a/src/main/java/Gui.java
+++ b/src/main/java/Gui.java
@@ -91,6 +91,7 @@ public void stateChanged(ChangeEvent e) {
});
sPlayerCount = new JSpinner(new SpinnerNumberModel(4, 4, 8, 2));
+ sPlayerCount.setName("PlayerCount");
sPlayerCount.setBounds(700, 290, 50, 50);
sPlayerCount.setFont(new Font("Arial", Font.PLAIN, 30));
menuPanel.add(sPlayerCount);
diff --git a/src/test/java/CharacterTest.java b/src/test/java/CharacterTest.java
deleted file mode 100644
index 3faef33..0000000
--- a/src/test/java/CharacterTest.java
+++ /dev/null
@@ -1,13 +0,0 @@
-
-import static org.junit.jupiter.api.Assertions.*;
-
-import org.junit.jupiter.api.Test;
-
-class CharacterTest {
-
- @Test
- void testGetName() {
- assertTrue(true);
- }
-
-}
diff --git a/src/test/java/MenuPlayerCountAndRoundsUITest.java b/src/test/java/MenuPlayerCountAndRoundsUITest.java
new file mode 100644
index 0000000..71cb4f6
--- /dev/null
+++ b/src/test/java/MenuPlayerCountAndRoundsUITest.java
@@ -0,0 +1,132 @@
+import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.stream.Stream;
+
+import javax.swing.JFrame;
+
+import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
+import org.assertj.swing.edt.GuiActionRunner;
+import org.assertj.swing.exception.UnexpectedException;
+import org.assertj.swing.fixture.FrameFixture;
+import org.assertj.swing.fixture.JSpinnerFixture;
+import org.junit.BeforeClass;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.TestMethodOrder;
+
+@TestMethodOrder(OrderAnnotation.class)
+class MenuPlayerCountAndRoundsUITest {
+ private FrameFixture window;
+ private Gui gui;
+ private JSpinnerFixture spinner;
+
+
+ static Stream spinnerNamesWithMinMaxBounds() {
+ return Stream.of(Arguments.of("RoundSetter","1","100"),
+ Arguments.of("PlayerCount","4","8"));
+ }
+
+ @BeforeClass
+ public static void setUpOnce(){
+ FailOnThreadViolationRepaintManager.install();
+ }
+
+ @BeforeEach
+ public void setUp(){
+ Gui.getInstance().getFrame().setVisible(false);
+
+ gui = Gui.resetInstance();
+
+ JFrame frame = GuiActionRunner.execute(() -> {
+ return gui.getFrame();
+ });
+ window = new FrameFixture(frame);
+
+
+ }
+
+
+ @ParameterizedTest(name = "{0}setEnteredValue")
+ @MethodSource(value = "spinnerNamesWithMinMaxBounds")
+ @Order(1)
+ void setEnteredValue(String name,String minVal,String maxVal){
+ spinner = window.spinner(name);
+ assertNotEquals(spinner.text(),maxVal);
+ String lastVal = spinner.text();
+
+ spinner.enterTextAndCommit(maxVal);
+
+ assertNotEquals(lastVal,spinner.text());
+ }
+
+
+
+ @ParameterizedTest(name = "{0}increasingWhenPressedTheIncrease")
+ @MethodSource(value = "spinnerNamesWithMinMaxBounds")
+ @Order(2)
+ void increasingWhenPressedTheIncrease(String name,String minVal,String maxVal){
+ spinner = window.spinner(name);
+ spinner.enterTextAndCommit(minVal);
+
+ spinner.increment();
+ assertTrue(Integer.parseInt(minVal) < Integer.parseInt(spinner.text()));
+ }
+
+
+ @ParameterizedTest(name = "{0}decreasingWhenPressedTheDecrease")
+ @MethodSource(value = "spinnerNamesWithMinMaxBounds")
+ @Order(3)
+ void decreasingWhenPressedTheDecrease(String name,String minVal,String maxVal){
+ spinner = window.spinner(name);
+ spinner.enterTextAndCommit(maxVal);
+
+ spinner.decrement();
+ assertTrue(Integer.parseInt(maxVal) > Integer.parseInt(spinner.text()));
+ }
+
+
+ @ParameterizedTest(name = "{0}minimumValueBoundCheck")
+ @MethodSource(value = "spinnerNamesWithMinMaxBounds")
+ void minimumValueBoundCheck(String name,String minVal,String maxVal){
+ spinner = window.spinner(name);
+ spinner.enterTextAndCommit(minVal);
+
+ spinner.decrement();
+ assertEquals(minVal, spinner.text());
+ spinner.increment();
+ assertNotEquals(minVal, spinner.text());
+
+ String outBoundVal = Integer.toString(Integer.parseInt(minVal) - Integer.parseInt(maxVal));
+ assertThrows(UnexpectedException.class, () -> spinner.enterTextAndCommit(outBoundVal));
+ }
+
+ @ParameterizedTest(name = "{0}maximumValueBoundCheck")
+ @MethodSource(value = "spinnerNamesWithMinMaxBounds")
+ void maximumValueBoundCheck(String name,String minVal,String maxVal){
+ spinner = window.spinner(name);
+ spinner.enterTextAndCommit(maxVal);
+
+ spinner.increment();
+ assertEquals(maxVal, spinner.text());
+ spinner.decrement();
+ assertNotEquals(maxVal, spinner.text());
+
+ String outBoundVal = Integer.toString(Integer.parseInt(maxVal) + (Integer.parseInt(maxVal) - Integer.parseInt(minVal)));
+ assertThrows(UnexpectedException.class, () -> spinner.enterTextAndCommit(outBoundVal));
+ }
+
+
+ @AfterEach
+ public void tearDown() {
+ window.cleanUp();
+ }
+
+}
diff --git a/src/test/java/MenuStartButtonUITest.java b/src/test/java/MenuStartButtonUITest.java
new file mode 100644
index 0000000..b32e272
--- /dev/null
+++ b/src/test/java/MenuStartButtonUITest.java
@@ -0,0 +1,74 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import javax.swing.JFrame;
+
+import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
+import org.assertj.swing.edt.GuiActionRunner;
+import org.assertj.swing.exception.ComponentLookupException;
+import org.assertj.swing.fixture.FrameFixture;
+import org.assertj.swing.fixture.JPanelFixture;
+import org.junit.BeforeClass;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
+import org.junit.jupiter.api.Order;
+
+
+@TestMethodOrder(OrderAnnotation.class)
+class MenuPlayerCountUITest {
+ private FrameFixture window;
+ private Gui gui;
+
+ @BeforeClass
+ public static void setUpOnce(){
+ FailOnThreadViolationRepaintManager.install();
+ }
+
+ @BeforeEach
+ public void setUp(){
+ Gui.getInstance().getFrame().setVisible(false);
+
+ Game.resetInstance();
+ gui = Gui.resetInstance();
+
+
+ JFrame frame = GuiActionRunner.execute(() -> {
+ return gui.getFrame();
+ });
+ window = new FrameFixture(frame);
+ }
+
+
+ @Test
+ @Order(1)
+ void MenuStartButtonClick(){
+
+ assertThrows(ComponentLookupException.class, () -> window.panel("GamePanel"));
+ window.button("StartButton").click();
+ assertThrows(ComponentLookupException.class, () -> window.panel("MenuPanel"));
+ assertEquals(JPanelFixture.class, window.panel("GamePanel").getClass());
+ }
+
+ @Test
+ @Order(2)
+ void GameValuesChangingAfterStartButtonClickAtMenu(){
+ String playerCount = "8";
+ String rounds = "10";
+ window.spinner("RoundSetter").enterTextAndCommit("10");
+ window.spinner("PlayerCount").enterTextAndCommit("8");
+ window.button("StartButton").click();
+ assertEquals(Integer.parseInt(rounds), Game.getInstance().getTurns());
+ String allPlayersInGame = Integer.toString(Game.getInstance().repairmanGroup.size() + Game.getInstance().saboteurGroup.size());
+ assertEquals(playerCount, allPlayersInGame);
+ }
+
+
+
+ @AfterEach
+ public void tearDown() {
+ window.cleanUp();
+ }
+
+}