Skip to content

Commit

Permalink
Added the UI tests for the main menu elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Duhi95 committed May 22, 2024
1 parent 6386e13 commit 2ad645b
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/main/java/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 0 additions & 13 deletions src/test/java/CharacterTest.java

This file was deleted.

132 changes: 132 additions & 0 deletions src/test/java/MenuPlayerCountAndRoundsUITest.java
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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();
}

}
74 changes: 74 additions & 0 deletions src/test/java/MenuStartButtonUITest.java
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit 2ad645b

Please sign in to comment.