package jrummikub.control.network; import static org.junit.Assert.*; import java.awt.Color; import java.util.Collections; import jrummikub.control.RoundControl.InvalidTurnInfo; import jrummikub.control.turn.AIControl; import jrummikub.model.GameSettings; import jrummikub.model.GameState; import jrummikub.model.IPlayer; import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; import jrummikub.model.Position; import jrummikub.model.RoundState; import jrummikub.model.Stone; import jrummikub.model.StoneColor; import jrummikub.model.StoneSet; import jrummikub.view.MockView; import org.junit.Before; import org.junit.Test; /** */ public class NetworkRoundControlTest { private MockConnectionControl connectionControl; private MockView view; private RoundState testRoundState; private NetworkRoundControl testRound; private GameSettings gameSettings; /** */ @Before public void setup() { AIControl.useBackgroundThread = false; gameSettings = new GameSettings(); gameSettings.getPlayerList().add(new PlayerSettings("Ida", Color.RED)); gameSettings.getPlayerList().add( new PlayerSettings("Matthias", Color.YELLOW)); gameSettings.getPlayerList().add(new PlayerSettings("Jannis", Color.GREEN)); gameSettings.getPlayerList().add(new PlayerSettings("Bennet", Color.BLACK)); view = new MockView(); connectionControl = new MockConnectionControl(); } /** */ @Test public void testHostRound() { gameSettings.getPlayerList().get(1).setType(Type.COMPUTER); gameSettings.getPlayerList().get(2).setType(Type.NETWORK); gameSettings.getPlayerList().get(3).setType(Type.COMPUTER); testRoundState = new RoundState(gameSettings, new GameState()); testRound = new NetworkRoundControl(testRoundState, view, connectionControl, true); connectionControl.turnStarted = false; connectionControl.turnEnded = false; testRound.startRound(); 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()); } 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); } } connectionControl.roundStateUpdateEvent.emit(testRoundState); assertSame(testRoundState.getNthPlayer(0), testRoundState.getActivePlayer()); assertTrue(connectionControl.turnStarted); connectionControl.turnStarted = false; connectionControl.turnStartEvent.emit(); assertFalse(connectionControl.turnEnded); view.playerPanel.endTurnEvent.emit(); assertTrue(connectionControl.turnEnded); connectionControl.turnEnded = false; assertTrue(connectionControl.nextPlayer); connectionControl.nextPlayer = false; connectionControl.nextPlayerEvent.emit(); assertSame(testRoundState.getNthPlayer(1), testRoundState.getActivePlayer()); assertTrue(connectionControl.turnStarted); connectionControl.turnStarted = false; connectionControl.turnStartEvent.emit(); assertTrue(connectionControl.turnEnded); connectionControl.turnEnded = false; assertTrue(connectionControl.nextPlayer); connectionControl.nextPlayer = false; connectionControl.nextPlayerEvent.emit(); assertSame(testRoundState.getNthPlayer(2), testRoundState.getActivePlayer()); assertFalse(connectionControl.turnStarted); connectionControl.turnStartEvent.emit(); assertFalse(connectionControl.turnEnded); connectionControl.turnEndEvent.emit(testRoundState, new InvalidTurnInfo( testRoundState.getTable(), null, Collections. emptyList())); assertFalse(connectionControl.nextPlayer); connectionControl.nextPlayerEvent.emit(); assertSame(testRoundState.getNthPlayer(3), testRoundState.getActivePlayer()); assertFalse(connectionControl.turnStarted); connectionControl.turnStartEvent.emit(); assertFalse(connectionControl.turnEnded); connectionControl.turnEndEvent.emit(testRoundState, new InvalidTurnInfo( testRoundState.getTable(), null, Collections. emptyList())); assertFalse(connectionControl.nextPlayer); connectionControl.nextPlayerEvent.emit(); assertSame(testRoundState.getNthPlayer(0), testRoundState.getActivePlayer()); assertTrue(connectionControl.turnStarted); connectionControl.turnStarted = false; connectionControl.turnStartEvent.emit(); assertFalse(connectionControl.turnEnded); } /** */ @Test public void testClientRound() { gameSettings.getPlayerList().get(0).setType(Type.NETWORK); gameSettings.getPlayerList().get(1).setType(Type.COMPUTER); gameSettings.getPlayerList().get(3).setType(Type.COMPUTER); testRoundState = new RoundState(gameSettings, new GameState()); for (int i = 0; i < 4; ++i) { IPlayer player = testRoundState.getNthPlayer(i); player.getHand().drop(new Stone(StoneColor.RED), new Position(0, 0)); } testRound = new NetworkRoundControl(null, view, connectionControl, false); connectionControl.turnStarted = false; connectionControl.turnEnded = false; testRound.startRound(); connectionControl.roundStateUpdateEvent.emit(testRoundState); assertSame(testRoundState.getNthPlayer(0), testRoundState.getActivePlayer()); assertFalse(connectionControl.turnStarted); connectionControl.turnStartEvent.emit(); assertFalse(connectionControl.turnEnded); connectionControl.turnEndEvent.emit(testRoundState, new InvalidTurnInfo( testRoundState.getTable(), null, Collections. emptyList())); assertFalse(connectionControl.turnEnded); assertFalse(connectionControl.nextPlayer); connectionControl.nextPlayerEvent.emit(); assertSame(testRoundState.getNthPlayer(1), testRoundState.getActivePlayer()); assertFalse(connectionControl.turnStarted); connectionControl.turnStartEvent.emit(); assertFalse(connectionControl.turnEnded); connectionControl.turnEndEvent.emit(testRoundState, new InvalidTurnInfo( testRoundState.getTable(), null, Collections. emptyList())); assertFalse(connectionControl.nextPlayer); connectionControl.nextPlayerEvent.emit(); assertSame(testRoundState.getNthPlayer(2), testRoundState.getActivePlayer()); assertTrue(connectionControl.turnStarted); connectionControl.turnStarted = false; connectionControl.turnStartEvent.emit(); assertFalse(connectionControl.turnEnded); view.playerPanel.endTurnEvent.emit(); assertTrue(connectionControl.turnEnded); connectionControl.turnEnded = false; assertTrue(connectionControl.nextPlayer); connectionControl.nextPlayer = false; connectionControl.nextPlayerEvent.emit(); assertSame(testRoundState.getNthPlayer(3), testRoundState.getActivePlayer()); assertTrue(connectionControl.turnStarted); connectionControl.turnStarted = false; connectionControl.turnStartEvent.emit(); assertTrue(connectionControl.turnEnded); connectionControl.turnEnded = false; assertTrue(connectionControl.nextPlayer); connectionControl.nextPlayer = false; connectionControl.nextPlayerEvent.emit(); assertSame(testRoundState.getNthPlayer(0), testRoundState.getActivePlayer()); assertFalse(connectionControl.turnStarted); connectionControl.turnStartEvent.emit(); assertFalse(connectionControl.turnEnded); } @Test public void testRedeal() { gameSettings.getPlayerList().get(1).setType(Type.COMPUTER); gameSettings.getPlayerList().get(2).setType(Type.NETWORK); gameSettings.getPlayerList().get(3).setType(Type.COMPUTER); testRoundState = new RoundState(gameSettings, new GameState()); for (int i = 0; i < 4; ++i) { IPlayer player = testRoundState.getNthPlayer(i); for (int j = 0; j < 6; j++) { player.getHand().drop(new Stone(1, StoneColor.RED), new Position(0, 0)); } } testRound = new NetworkRoundControl(null, view, connectionControl, true); connectionControl.turnStarted = false; connectionControl.turnEnded = false; testRound.startRound(); connectionControl.roundStateUpdateEvent.emit(testRoundState); connectionControl.turnStartEvent.emit(); view.playerPanel.redealEvent.emit(); assertTrue(connectionControl.redealCalled); } }