Browse Source

tictactoe: added test case to check field clicked state in gui

tictactoe
Malte Schellhardt 3 years ago
committed by Lorenz Hohmann
parent
commit
5aa5d1a1fd
  1. 16
      src/main/java/de/tims/tictactoe/GameLogic.java
  2. 12
      src/test/java/de/tims/tictactoe/GameLogicTest.java

16
src/main/java/de/tims/tictactoe/GameLogic.java

@ -1,12 +1,13 @@
package de.tims.tictactoe; package de.tims.tictactoe;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JPanel; import javax.swing.JPanel;
public class GameLogic {
public class GameLogic implements ActionListener {
private static final char PLAYER_1 = 'x'; private static final char PLAYER_1 = 'x';
private static final char PLAYER_2 = 'o'; private static final char PLAYER_2 = 'o';
@ -110,9 +111,20 @@ public class GameLogic {
for (int i = 0; i < this.fields.length; i++) { for (int i = 0; i < this.fields.length; i++) {
this.fields[i] = new JButton(); this.fields[i] = new JButton();
this.fields[i].addActionListener(this);
this.contentPanel.add(this.fields[i]); this.contentPanel.add(this.fields[i]);
} }
return this.contentPanel; return this.contentPanel;
} }
public JButton getGUIField(int number) {
// TODO Auto-generated method stub
return this.fields[number];
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
} }

12
src/test/java/de/tims/tictactoe/GameLogicTest.java

@ -81,6 +81,18 @@ class GameLogicTest {
assertEquals(expectedResult, realResult); assertEquals(expectedResult, realResult);
} }
@Test
void buttonStateTest() {
boolean expectedResult = true;
game.generateGUI();
JButton currentField = game.getGUIField(0);
((JButton) currentField).doClick();
boolean realResult = currentField.isEnabled();
assertEquals(expectedResult, realResult);
}
@ParameterizedTest(name = "[{index}] {0} -> {2} fields") @ParameterizedTest(name = "[{index}] {0} -> {2} fields")
@MethodSource("testCasesForCountPlayfields") @MethodSource("testCasesForCountPlayfields")
void fieldCountTest(String testName, int size, int expectedResult) { void fieldCountTest(String testName, int size, int expectedResult) {

Loading…
Cancel
Save