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(); + } + +}