From 9df549727672e8df768d46e3ef1fb0c7957f0f89 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 19 Jun 2011 23:19:01 +0200 Subject: Make NetworkRoundControl test work git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@496 72836036-5685-4462-b002-a69064685172 --- .../control/network/MockConnectionControl.java | 10 +- src/jrummikub/control/RoundControl.java | 23 +++-- .../control/network/NetworkRoundControl.java | 44 +++++++-- .../control/turn/AbstractTurnControl.java | 6 +- src/jrummikub/control/turn/HumanTurnControl.java | 1 - .../control/network/NetworkRoundControlTest.java | 110 ++++++++++----------- 6 files changed, 110 insertions(+), 84 deletions(-) diff --git a/mock/jrummikub/control/network/MockConnectionControl.java b/mock/jrummikub/control/network/MockConnectionControl.java index fbdfb04..924febb 100644 --- a/mock/jrummikub/control/network/MockConnectionControl.java +++ b/mock/jrummikub/control/network/MockConnectionControl.java @@ -53,6 +53,10 @@ public class MockConnectionControl implements IConnectionControl { public GameData joinedGame; /** */ public Color playerColor; + /** */ + public boolean turnStarted; + /** */ + public boolean turnEnded; @Override public String getNickname() { @@ -191,13 +195,11 @@ public class MockConnectionControl implements IConnectionControl { @Override public void endTurn(ITable table) { - // TODO Auto-generated method stub - + turnEnded = true; } @Override public void startTurn(IRoundState state) { - // TODO Auto-generated method stub - + turnStarted = true; } } diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java index a19a2ab..f209f3e 100644 --- a/src/jrummikub/control/RoundControl.java +++ b/src/jrummikub/control/RoundControl.java @@ -117,7 +117,20 @@ public class RoundControl { } } - private void prepareTurn() { + protected void prepareTurn() { + doPrepareTurn(); + + boolean isHuman = roundState.getActivePlayer().getPlayerSettings() + .getType() == HUMAN; + boolean oneHuman = roundState.getGameSettings().oneHuman(); + boolean isAI = (turnControl instanceof AIControl); + + if (isAI || (isHuman && oneHuman)) { + startTurn(); + } + } + + protected void doPrepareTurn() { boolean isHuman = roundState.getActivePlayer().getPlayerSettings() .getType() == HUMAN; boolean oneHuman = roundState.getGameSettings().oneHuman(); @@ -141,12 +154,6 @@ public class RoundControl { turnControl = createTurnControl(roundState.getActivePlayer() .getPlayerSettings()); - - boolean isAI = (turnControl instanceof AIControl); - - if (isAI || (isHuman && oneHuman)) { - startTurn(); - } } protected void startTurn() { @@ -217,7 +224,7 @@ public class RoundControl { || totalValue >= roundState.getGameSettings().getInitialMeldThreshold(); } - private void endOfTurn(ITable newTable) { + protected void endOfTurn(ITable newTable) { boolean wasHuman = (turnControl instanceof HumanTurnControl); boolean wasAI = (turnControl instanceof AIControl); diff --git a/src/jrummikub/control/network/NetworkRoundControl.java b/src/jrummikub/control/network/NetworkRoundControl.java index d7de9ea..a05f8ae 100644 --- a/src/jrummikub/control/network/NetworkRoundControl.java +++ b/src/jrummikub/control/network/NetworkRoundControl.java @@ -11,12 +11,22 @@ import jrummikub.view.IView; public class NetworkRoundControl extends RoundControl { private IConnectionControl connectionControl; private boolean currentlyActive; - - public NetworkRoundControl(IRoundState roundState, IView view, IConnectionControl connectionControl, boolean startActive) { + + public NetworkRoundControl(IRoundState roundState, IView view, + IConnectionControl connectionControl, boolean startActive) { super(roundState, view); - + this.connectionControl = connectionControl; currentlyActive = startActive; + + connections.add(connectionControl.getTurnStartEvent().add( + new IListener1() { + @Override + public void handle(IRoundState state) { + NetworkRoundControl.this.roundState = state; + startTurn(); + } + })); } @Override @@ -28,7 +38,7 @@ public class NetworkRoundControl extends RoundControl { } }); } - + @Override protected ITurnControl createTurnControl(PlayerSettings playerSettings) { switch (playerSettings.getType()) { @@ -39,17 +49,31 @@ public class NetworkRoundControl extends RoundControl { currentlyActive = false; break; } - + if (!currentlyActive) { return new NetworkTurnControl(connectionControl); } - + return super.createTurnControl(playerSettings); } - + @Override - protected void startTurn() { - connectionControl.startTurn(roundState); - super.startTurn(); + protected void prepareTurn() { + boolean wasActive = currentlyActive; + + doPrepareTurn(); + + if (wasActive) { + connectionControl.startTurn(roundState); + } + } + + @Override + protected void endOfTurn(ITable newTable) { + if (currentlyActive) { + connectionControl.endTurn(newTable); + } + + super.endOfTurn(newTable); } } diff --git a/src/jrummikub/control/turn/AbstractTurnControl.java b/src/jrummikub/control/turn/AbstractTurnControl.java index f129f24..b687ca0 100644 --- a/src/jrummikub/control/turn/AbstractTurnControl.java +++ b/src/jrummikub/control/turn/AbstractTurnControl.java @@ -97,7 +97,11 @@ public abstract class AbstractTurnControl implements ITurnControl { } protected void cleanUp() { - timer.stopTimer(); + if (timer != null) { + timer.stopTimer(); + } + started = true; + for (Connection c : connections) { c.remove(); } diff --git a/src/jrummikub/control/turn/HumanTurnControl.java b/src/jrummikub/control/turn/HumanTurnControl.java index 51f77e4..7ca2731 100644 --- a/src/jrummikub/control/turn/HumanTurnControl.java +++ b/src/jrummikub/control/turn/HumanTurnControl.java @@ -90,7 +90,6 @@ public class HumanTurnControl extends AbstractTurnControl { connections.add(view.getPlayerPanel().getEndTurnEvent() .add(new IListener() { - @Override public void handle() { endOfTurn(false); diff --git a/test/jrummikub/control/network/NetworkRoundControlTest.java b/test/jrummikub/control/network/NetworkRoundControlTest.java index c8eb54d..797c481 100644 --- a/test/jrummikub/control/network/NetworkRoundControlTest.java +++ b/test/jrummikub/control/network/NetworkRoundControlTest.java @@ -7,33 +7,27 @@ import java.awt.Color; import jrummikub.control.turn.AIControl; import jrummikub.model.GameSettings; import jrummikub.model.IPlayer; -import jrummikub.model.IRoundState; -import jrummikub.model.ITable; import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; import jrummikub.model.RoundState; import jrummikub.model.Stone; -import jrummikub.util.IListener1; import jrummikub.view.MockView; import org.junit.Before; import org.junit.Test; public class NetworkRoundControlTest { - private MockConnectionControl connectionControl; + private MockConnectionControl connectionControl; private MockView view; private RoundState testRoundState; private NetworkRoundControl testRound; private GameSettings gameSettings; - private boolean turnStarted; - private boolean turnEnded; - @Before public void setup() { AIControl.useBackgroundThread = false; - + gameSettings = new GameSettings(); gameSettings.getPlayerList().add(new PlayerSettings("Ida", Color.RED)); @@ -46,83 +40,79 @@ public class NetworkRoundControlTest { gameSettings.getPlayerList().get(2).setType(Type.NETWORK); gameSettings.getPlayerList().get(3).setType(Type.COMPUTER); testRoundState = new RoundState(gameSettings); - + view = new MockView(); connectionControl = new MockConnectionControl(); } - + @Test public void testHostRound() { - testRound = new NetworkRoundControl(testRoundState, view, connectionControl, true); - - connectionControl.getTurnStartEvent().add(new IListener1() { - @Override - public void handle(IRoundState roundState) { - assertSame(testRoundState, roundState); - - turnStarted = true; - } - }); - - connectionControl.getTurnEndEvent().add(new IListener1() { - @Override - public void handle(ITable table) { - turnEnded = true; - } - }); - - turnStarted = false; - turnEnded = false; - + testRound = new NetworkRoundControl(testRoundState, view, + connectionControl, true); + + connectionControl.turnStarted = false; + connectionControl.turnEnded = false; + testRound.startRound(); - assertTrue(turnStarted); - turnStarted = false; - + assertEquals(4, testRoundState.getPlayerCount()); for (int i = 0; i < 4; ++i) { IPlayer player = testRoundState.getNthPlayer(i); - assertSame(gameSettings.getPlayerList().get(i), player.getPlayerSettings()); - assertEquals(gameSettings.getNumberOfStonesDealt(), player.getHand().getSize()); + assertSame(gameSettings.getPlayerList().get(i), + player.getPlayerSettings()); + assertEquals(gameSettings.getNumberOfStonesDealt(), player.getHand() + .getSize()); } - assertSame(testRoundState.getNthPlayer(0), testRoundState.getActivePlayer()); - + for (int i = 0; i < 4; ++i) { IPlayer player = testRoundState.getNthPlayer(i); - + while (player.getHand().getSize() > 1) { Stone stone = player.getHand().iterator().next().getFirst(); player.getHand().pickUp(stone); } } - - assertFalse(turnEnded); + + assertSame(testRoundState.getNthPlayer(0), testRoundState.getActivePlayer()); + assertTrue(connectionControl.turnStarted); + connectionControl.turnStarted = false; + + connectionControl.turnStartEvent.emit(testRoundState); + + assertFalse(connectionControl.turnEnded); view.playerPanel.endTurnEvent.emit(); - assertTrue(turnEnded); - turnEnded = false; - turnStarted = false; + assertTrue(connectionControl.turnEnded); + connectionControl.turnEnded = false; + + assertSame(testRoundState.getNthPlayer(1), testRoundState.getActivePlayer()); + assertTrue(connectionControl.turnStarted); + connectionControl.turnStarted = false; + + connectionControl.turnStartEvent.emit(testRoundState); + assertTrue(connectionControl.turnEnded); + connectionControl.turnEnded = false; assertSame(testRoundState.getNthPlayer(2), testRoundState.getActivePlayer()); - + assertTrue(connectionControl.turnStarted); + connectionControl.turnStarted = false; + connectionControl.turnStartEvent.emit(testRoundState); - assertTrue(turnStarted); - turnStarted = false; - - assertFalse(turnEnded); + assertFalse(connectionControl.turnEnded); + connectionControl.turnEndEvent.emit(testRoundState.getTable()); - assertTrue(turnEnded); - turnEnded = false; - + assertSame(testRoundState.getNthPlayer(3), testRoundState.getActivePlayer()); - + assertFalse(connectionControl.turnStarted); + connectionControl.turnStartEvent.emit(testRoundState); - assertTrue(turnStarted); - turnStarted = false; - - assertFalse(turnEnded); + assertFalse(connectionControl.turnEnded); + connectionControl.turnEndEvent.emit(testRoundState.getTable()); - assertTrue(turnEnded); - turnEnded = false; - + assertSame(testRoundState.getNthPlayer(0), testRoundState.getActivePlayer()); + assertFalse(connectionControl.turnStarted); + + connectionControl.turnStartEvent.emit(testRoundState); + assertFalse(connectionControl.turnEnded); } } -- cgit v1.2.3