diff options
-rw-r--r-- | mock/jrummikub/view/MockSettingsPanel.java | 87 | ||||
-rw-r--r-- | mock/jrummikub/view/MockView.java | 5 | ||||
-rw-r--r-- | src/jrummikub/control/ApplicationControl.java | 42 | ||||
-rw-r--r-- | src/jrummikub/control/SettingsControl.java | 54 | ||||
-rw-r--r-- | src/jrummikub/view/ISettingsPanel.java | 36 | ||||
-rw-r--r-- | src/jrummikub/view/impl/SettingsPanel.java | 98 | ||||
-rw-r--r-- | test/jrummikub/control/SettingsControlTest.java | 166 |
7 files changed, 431 insertions, 57 deletions
diff --git a/mock/jrummikub/view/MockSettingsPanel.java b/mock/jrummikub/view/MockSettingsPanel.java new file mode 100644 index 0000000..55090b5 --- /dev/null +++ b/mock/jrummikub/view/MockSettingsPanel.java @@ -0,0 +1,87 @@ +package jrummikub.view; + +import java.awt.Color; + +import jrummikub.model.GameSettings; +import jrummikub.util.IEvent; +import jrummikub.util.IEvent1; +import jrummikub.util.IEvent2; +import jrummikub.util.MockEvent; +import jrummikub.util.MockEvent1; +import jrummikub.util.MockEvent2; + +public class MockSettingsPanel implements ISettingsPanel { + + public MockEvent addPlayerEvent = new MockEvent(); + public MockEvent1<Integer> removePlayerEvent = new MockEvent1<Integer>(); + public MockEvent2<Integer, Color> changePlayerColorEvent = new MockEvent2<Integer, Color>(); + public MockEvent2<Integer, String> changePlayerNameEvent = new MockEvent2<Integer, String>(); + public MockEvent1<Integer> changeInitialMeldThresholdEvent = new MockEvent1<Integer>(); + public MockEvent startGameEvent = new MockEvent(); + public SettingsError error = SettingsError.NO_ERROR; + public boolean startButtonEnabled = true; + public boolean addPlayerButtonEnabled = true; + public boolean removePlayerButtonsEnabled = false; + public GameSettings gameSettings = new GameSettings(); + + @Override + public IEvent getAddPlayerEvent() { + return addPlayerEvent; + } + + @Override + public IEvent1<Integer> getRemovePlayerEvent() { + return removePlayerEvent; + } + + @Override + public IEvent2<Integer, Color> getChangePlayerColorEvent() { + return changePlayerColorEvent; + } + + @Override + public IEvent2<Integer, String> getChangePlayerNameEvent() { + return changePlayerNameEvent; + } + + @Override + public IEvent1<Integer> getChangeInitialMeldThresholdEvent() { + return changeInitialMeldThresholdEvent; + } + + @Override + public IEvent getStartGameEvent() { + return startGameEvent; + } + + @Override + public void setError(SettingsError error) { + this.error = error; + + } + + @Override + public void enableStartGameButton(boolean enable) { + startButtonEnabled = enable; + + } + + @Override + public void enableAddPlayerButton(boolean enable) { + addPlayerButtonEnabled = enable; + + } + + @Override + public void enableRemovePlayerButtons(boolean enable) { + removePlayerButtonsEnabled = enable; + + } + + @Override + public void setGameSettings(GameSettings gameSettings) { + this.gameSettings = gameSettings; + + } + +} diff --git a/mock/jrummikub/view/MockView.java b/mock/jrummikub/view/MockView.java index 5e145da..431641a 100644 --- a/mock/jrummikub/view/MockView.java +++ b/mock/jrummikub/view/MockView.java @@ -11,6 +11,8 @@ import jrummikub.util.MockEvent; */ public class MockView implements IView { /** */ + public MockSettingsPanel settingsPanel = new MockSettingsPanel(); + public MockPlayerPanel playerPanel = new MockPlayerPanel(); /** */ public MockTablePanel tablePanel = new MockTablePanel(); @@ -86,8 +88,7 @@ public class MockView implements IView { @Override public ISettingsPanel getSettingsPanel() { - // TODO Auto-generated method stub - return null; + return settingsPanel; } @Override diff --git a/src/jrummikub/control/ApplicationControl.java b/src/jrummikub/control/ApplicationControl.java index b3d432c..8e09853 100644 --- a/src/jrummikub/control/ApplicationControl.java +++ b/src/jrummikub/control/ApplicationControl.java @@ -15,7 +15,7 @@ public class ApplicationControl { * Creates a new application control * * @param view - * the view to use + * the view to use */ public ApplicationControl(IView view) { this.view = view; @@ -25,39 +25,17 @@ public class ApplicationControl { * Starts the application by showing the game settings dialog panel */ public void startApplication() { - view.getSettingsPanel().getSettingsChangeEvent() - .add(new IListener1<GameSettings>() { - @Override - public void handle(GameSettings settings) { - checkSettings(settings); - } - }); - view.getSettingsPanel().getStartGameEvent() - .add(new IListener1<GameSettings>() { - @Override - public void handle(GameSettings settings) { - startGame(settings); - } - }); + SettingsControl settingsControl = new SettingsControl(view); + settingsControl.getStartGameEvent().add(new IListener1<GameSettings>() { - view.showSettingsPanel(true); - } - - private boolean checkSettings(GameSettings settings) { - // TODO Check - // TODO Show error + @Override + public void handle(GameSettings settings) { + GameControl gameControl = new GameControl(settings, view); + gameControl.startGame(); - return true; + } + }); + settingsControl.startSettings(); } - private void startGame(GameSettings settings) { - if (!checkSettings(settings)) { - return; - } - - view.showSettingsPanel(false); - - GameControl gameControl = new GameControl(settings, view); - gameControl.startGame(); - } } diff --git a/src/jrummikub/control/SettingsControl.java b/src/jrummikub/control/SettingsControl.java new file mode 100644 index 0000000..7b96e77 --- /dev/null +++ b/src/jrummikub/control/SettingsControl.java @@ -0,0 +1,54 @@ +package jrummikub.control; + +import jrummikub.model.GameSettings; +import jrummikub.util.Event1; +import jrummikub.util.IEvent1; +import jrummikub.util.IListener1; +import jrummikub.view.IView; + +public class SettingsControl { + private IView view; + private Event1<GameSettings> startGameEvent = new Event1<GameSettings>(); + + public SettingsControl(IView view) { + this.view = view; + } + + public IEvent1<GameSettings> getStartGameEvent() { + return startGameEvent; + } + + public void startSettings() { + /* + * view.getSettingsPanel().getSettingsChangeEvent() .add(new + * IListener1<GameSettings>() { + * + * @Override public void handle(GameSettings settings) { + * checkSettings(settings); } }); + * view.getSettingsPanel().getStartGameEvent() .add(new + * IListener1<GameSettings>() { + * + * @Override public void handle(GameSettings settings) { + * startGame(settings); } }); + */ + + view.showSettingsPanel(true); + } + + private boolean checkSettings(GameSettings settings) { + // TODO Check + // TODO Show error + + return true; + } + + private void startGame(GameSettings settings) { + if (!checkSettings(settings)) { + return; + } + + view.showSettingsPanel(false); + + startGameEvent.emit(settings); + } +} diff --git a/src/jrummikub/view/ISettingsPanel.java b/src/jrummikub/view/ISettingsPanel.java index 4437f58..17830e5 100644 --- a/src/jrummikub/view/ISettingsPanel.java +++ b/src/jrummikub/view/ISettingsPanel.java @@ -1,24 +1,44 @@ package jrummikub.view; +import java.awt.Color; + import jrummikub.model.GameSettings; +import jrummikub.util.IEvent; import jrummikub.util.IEvent1; +import jrummikub.util.IEvent2; /** * The panel for the game setup */ public interface ISettingsPanel { - /** - * The settings change event is emitted whenever the user has changed a game - * settings without starting the game - * - * @return the event - */ - public IEvent1<GameSettings> getSettingsChangeEvent(); + public IEvent getAddPlayerEvent(); + + public IEvent1<Integer> getRemovePlayerEvent(); + + public IEvent2<Integer, Color> getChangePlayerColorEvent(); + + public IEvent2<Integer, String> getChangePlayerNameEvent(); + + public IEvent1<Integer> getChangeInitialMeldThresholdEvent(); /** * the start game event is emitted when the user wants to start the game * * @return the event */ - public IEvent1<GameSettings> getStartGameEvent(); + public IEvent getStartGameEvent(); + + public void setError(SettingsError error); + + public void enableStartGameButton(boolean enable); + + public void enableAddPlayerButton(boolean enable); + + public void enableRemovePlayerButtons(boolean enable); + + public void setGameSettings(GameSettings gameSettings); + + public enum SettingsError { + NO_ERROR, DUPLICATE_PLAYER_NAME, NO_PLAYER_NAME + } }
\ No newline at end of file diff --git a/src/jrummikub/view/impl/SettingsPanel.java b/src/jrummikub/view/impl/SettingsPanel.java index 7a9347c..03cfd4c 100644 --- a/src/jrummikub/view/impl/SettingsPanel.java +++ b/src/jrummikub/view/impl/SettingsPanel.java @@ -32,7 +32,9 @@ import javax.swing.event.DocumentListener; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; import jrummikub.util.Event1; +import jrummikub.util.IEvent; import jrummikub.util.IEvent1; +import jrummikub.util.IEvent2; import jrummikub.view.ISettingsPanel; @SuppressWarnings("serial") @@ -66,15 +68,13 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private Event1<GameSettings> settingsChangeEvent = new Event1<GameSettings>(); private Event1<GameSettings> startGameEvent = new Event1<GameSettings>(); - @Override - public IEvent1<GameSettings> getSettingsChangeEvent() { - return settingsChangeEvent; - } - - @Override - public IEvent1<GameSettings> getStartGameEvent() { - return startGameEvent; - } + /* + * @Override public IEvent1<GameSettings> getSettingsChangeEvent() { return + * settingsChangeEvent; } + * + * @Override public IEvent1<GameSettings> getStartGameEvent() { return + * startGameEvent; } + */ private void addPlayerSettings() { // Find unused player name @@ -104,7 +104,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { break; } - PlayerSettingsPanel panel = new PlayerSettingsPanel("Spieler " + num, color); + PlayerSettingsPanel panel = new PlayerSettingsPanel("Spieler " + num, + color); playerSettingsPanels.add(panel); playerSettingsViewport.add(panel, playerSettingsViewport.getComponentCount() - 1); @@ -130,7 +131,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { playerSettingsViewport.add(addPlayerPanel); JButton addPlayerButton = new JButton("+"); - addPlayerButton.setFont(addPlayerButton.getFont().deriveFont(Font.BOLD)); + addPlayerButton + .setFont(addPlayerButton.getFont().deriveFont(Font.BOLD)); addPlayerButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -180,8 +182,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { }); add(startButton, c); - setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder( - 10, 10, 10, 10))); + setBorder(new CompoundBorder(new LineBorder(Color.BLACK), + new EmptyBorder(10, 10, 10, 10))); } private class PlayerSettingsPanel extends JPanel { @@ -200,8 +202,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { } private void updateColor() { - colorButton - .setIcon(ImageUtil.createColorIcon(settings.getColor(), 16, 2)); + colorButton.setIcon(ImageUtil.createColorIcon(settings.getColor(), + 16, 2)); } private void setName() { @@ -333,4 +335,70 @@ class SettingsPanel extends JPanel implements ISettingsPanel { } } } + + @Override + public IEvent getAddPlayerEvent() { + // TODO Auto-generated method stub + return null; + } + + @Override + public IEvent1<Integer> getRemovePlayerEvent() { + // TODO Auto-generated method stub + return null; + } + + @Override + public IEvent2<Integer, Color> getChangePlayerColorEvent() { + // TODO Auto-generated method stub + return null; + } + + @Override + public IEvent2<Integer, String> getChangePlayerNameEvent() { + // TODO Auto-generated method stub + return null; + } + + @Override + public IEvent1<Integer> getChangeInitialMeldThresholdEvent() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setError(SettingsError error) { + // TODO Auto-generated method stub + + } + + @Override + public void enableStartGameButton(boolean enable) { + // TODO Auto-generated method stub + + } + + @Override + public void enableAddPlayerButton(boolean enable) { + // TODO Auto-generated method stub + + } + + @Override + public void enableRemovePlayerButtons(boolean enable) { + // TODO Auto-generated method stub + + } + + @Override + public IEvent getStartGameEvent() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setGameSettings(GameSettings gameSettings) { + // TODO Auto-generated method stub + + } } diff --git a/test/jrummikub/control/SettingsControlTest.java b/test/jrummikub/control/SettingsControlTest.java new file mode 100644 index 0000000..f0e4ae0 --- /dev/null +++ b/test/jrummikub/control/SettingsControlTest.java @@ -0,0 +1,166 @@ +package jrummikub.control; + +import static org.junit.Assert.*; + +import java.awt.Color; + +import jrummikub.model.GameSettings; +import jrummikub.util.IListener1; +import jrummikub.view.ISettingsPanel; +import jrummikub.view.MockView; + +import org.junit.Before; +import org.junit.Test; + +public class SettingsControlTest { + MockView view = new MockView(); + SettingsControl settingsControl = new SettingsControl(view); + GameSettings gameSettings = null; + + @Before + public void setup() { + settingsControl.getStartGameEvent().add(new IListener1<GameSettings>() { + + @Override + public void handle(GameSettings value) { + gameSettings = value; + + } + }); + } + + @Test + public void initialStateTest() { + assertSame(ISettingsPanel.SettingsError.NO_ERROR, + view.settingsPanel.error); + view.settingsPanel.startGameEvent.emit(); + assertNotNull(gameSettings); + assertEquals(2, gameSettings.getPlayerList().size()); + assertFalse(view.settingsPanel.removePlayerButtonsEnabled); + } + + @Test + public void addPlayerTest() { + view.settingsPanel.addPlayerEvent.emit(); + view.settingsPanel.startGameEvent.emit(); + assertNotNull(gameSettings); + assertEquals(3, gameSettings.getPlayerList().size()); + assertTrue(view.settingsPanel.removePlayerButtonsEnabled); + } + + @Test + public void add14PlayerTest() { + for (int i = 0; i < 14; i++) { + view.settingsPanel.addPlayerEvent.emit(); + } + view.settingsPanel.startGameEvent.emit(); + assertNotNull(gameSettings); + assertEquals(16, gameSettings.getPlayerList().size()); + assertTrue(view.settingsPanel.removePlayerButtonsEnabled); + assertFalse(view.settingsPanel.addPlayerButtonEnabled); + } + + @Test + public void add14RemovePlayerTest() { + for (int i = 0; i < 14; i++) { + view.settingsPanel.addPlayerEvent.emit(); + } + view.settingsPanel.removePlayerEvent.emit(0); + view.settingsPanel.startGameEvent.emit(); + assertNotNull(gameSettings); + assertEquals(15, gameSettings.getPlayerList().size()); + assertTrue(view.settingsPanel.removePlayerButtonsEnabled); + assertTrue(view.settingsPanel.addPlayerButtonEnabled); + } + + @Test + public void removePlayerTest() { + view.settingsPanel.addPlayerEvent.emit(); + view.settingsPanel.removePlayerEvent.emit(0); + view.settingsPanel.startGameEvent.emit(); + assertNotNull(gameSettings); + assertEquals(2, gameSettings.getPlayerList().size()); + assertFalse(view.settingsPanel.removePlayerButtonsEnabled); + } + + @Test + public void takenColorTest() { + Color color1 = Color.RED; + Color color2 = Color.BLUE; + view.settingsPanel.changePlayerColorEvent.emit(0, color1); + view.settingsPanel.changePlayerColorEvent.emit(1, color2); + view.settingsPanel.changePlayerColorEvent.emit(1, color1); + view.settingsPanel.startGameEvent.emit(); + assertNotNull(gameSettings); + assertEquals(2, gameSettings.getPlayerList().size()); + assertSame(color2, gameSettings.getPlayerList().get(0).getColor()); + assertSame(color1, gameSettings.getPlayerList().get(1).getColor()); + } + + @Test + public void unusedColorTest() { + Color color1 = Color.RED; + Color color2 = Color.BLUE; + Color color3 = Color.PINK; + view.settingsPanel.changePlayerColorEvent.emit(0, color1); + view.settingsPanel.changePlayerColorEvent.emit(1, color2); + view.settingsPanel.changePlayerColorEvent.emit(1, color3); + view.settingsPanel.startGameEvent.emit(); + assertNotNull(gameSettings); + assertEquals(2, gameSettings.getPlayerList().size()); + assertSame(color1, gameSettings.getPlayerList().get(0).getColor()); + assertSame(color3, gameSettings.getPlayerList().get(1).getColor()); + } + + @Test + public void takenNameTest() { + String name1 = "Julia"; + view.settingsPanel.changePlayerNameEvent.emit(0, name1); + view.settingsPanel.changePlayerNameEvent.emit(1, name1); + + assertFalse(view.settingsPanel.startButtonEnabled); + assertSame(ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME, + view.settingsPanel.error); + + view.settingsPanel.startGameEvent.emit(); + assertNull(gameSettings); + } + + @Test + public void fixTakenNameTest() { + String name1 = "Julia"; + view.settingsPanel.changePlayerNameEvent.emit(0, name1); + view.settingsPanel.changePlayerNameEvent.emit(1, name1); + + assertFalse(view.settingsPanel.startButtonEnabled); + assertSame(ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME, + view.settingsPanel.error); + + view.settingsPanel.startGameEvent.emit(); + assertNull(gameSettings); + + String name2 = "Karl"; + view.settingsPanel.changePlayerNameEvent.emit(1, name2); + + assertTrue(view.settingsPanel.startButtonEnabled); + assertSame(ISettingsPanel.SettingsError.NO_ERROR, + view.settingsPanel.error); + + view.settingsPanel.startGameEvent.emit(); + assertNotNull(gameSettings); + } + + @Test + public void emptyNameTest() { + String name1 = ""; + view.settingsPanel.changePlayerNameEvent.emit(0, name1); + + assertFalse(view.settingsPanel.startButtonEnabled); + assertSame(ISettingsPanel.SettingsError.NO_PLAYER_NAME, + view.settingsPanel.error); + + view.settingsPanel.startGameEvent.emit(); + assertNull(gameSettings); + } + +} |