package jrummikub.control.network; import static jrummikub.model.StoneColor.RED; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.awt.Color; import java.util.Collections; import jrummikub.control.RoundControl.InvalidTurnInfo; import jrummikub.control.SaveControl; import jrummikub.model.GameSettings; import jrummikub.model.GameState; import jrummikub.model.IHand; import jrummikub.model.PlayerSettings; import jrummikub.model.Position; import jrummikub.model.RoundState; import jrummikub.model.Stone; import jrummikub.model.StoneSet; import jrummikub.model.Table; import jrummikub.util.IListener; import jrummikub.view.IView.BottomPanelType; import jrummikub.view.MockView; import org.junit.Before; import org.junit.Test; /** * Tests for the network game control */ public class NetworkGameControlTest { /** */ public NetworkGameControl hostControl; /** */ public NetworkGameControl clientControl; /** */ public GameSettings settings; /** */ public MockView view; /** */ public MockConnectionControl connection; /** */ public boolean fired; /** */ public RoundState roundState; /** */ @Before public void setUp() { settings = new GameSettings(); settings.getPlayerList().add(new PlayerSettings("Foobar", Color.BLACK)); PlayerSettings player = new PlayerSettings("Fooblubb", Color.BLACK); player.setType(PlayerSettings.Type.NETWORK); settings.getPlayerList().add(player); view = new MockView(); connection = new MockConnectionControl(); hostControl = new NetworkGameControl(settings, new SaveControl(view), view, connection, true); clientControl = new NetworkGameControl(settings, new SaveControl(view), view, connection, false); roundState = new RoundState(settings, new GameState()); fired = false; } /** */ @Test public void testStartClient() { clientControl.startGame(); assertFalse(connection.startRoundCalled); connection.roundStartEvent.emit(); connection.roundStateUpdateEvent.emit(roundState); connection.turnStartEvent.emit(); assertSame(BottomPanelType.HUMAN_HAND_PANEL, view.bottomPanelType); } /** */ @Test public void testStartHost() { hostControl.startGame(); assertTrue(connection.startRoundCalled); connection.roundStartEvent.emit(); connection.roundStateUpdateEvent.emit(roundState); assertTrue(connection.turnStarted); } /** */ @Test public void testWinHost() { hostControl.startGame(); assertTrue(connection.startRoundCalled); connection.roundStartEvent.emit(); roundState.nextTurn(); roundState.nextTurn(); IHand playerHand = roundState.getActivePlayer().getHand(); Stone stone1 = new Stone(9, RED); Stone stone2 = new Stone(10, RED); Stone stone3 = new Stone(11, RED); playerHand.drop(stone1, new Position(0, 0)); playerHand.drop(stone2, new Position(0, 0)); playerHand.drop(stone3, new Position(0, 0)); connection.roundStateUpdateEvent.emit(roundState); connection.turnStartEvent.emit(); view.handPanel.stoneClickEvent.emit(stone1, false); view.handPanel.stoneClickEvent.emit(stone2, true); view.handPanel.stoneClickEvent.emit(stone3, true); view.tablePanel.clickEvent.emit(new Position(0, 0)); view.playerPanel.endTurnEvent.emit(); assertTrue(connection.nextPlayer); connection.nextPlayerEvent.emit(); connection.turnEndEvent.emit(roundState, new InvalidTurnInfo(new Table( settings), null, Collections. emptyList())); assertSame(BottomPanelType.WIN_PANEL, view.bottomPanelType); assertTrue(view.isScorePanelVisible); } /** */ @Test public void testWinClient() { clientControl.startGame(); connection.roundStartEvent.emit(); roundState.nextTurn(); roundState.nextTurn(); IHand playerHand = roundState.getActivePlayer().getHand(); Stone stone1 = new Stone(9, RED); Stone stone2 = new Stone(10, RED); Stone stone3 = new Stone(11, RED); playerHand.drop(stone1, new Position(0, 0)); playerHand.drop(stone2, new Position(0, 0)); playerHand.drop(stone3, new Position(0, 0)); connection.roundStateUpdateEvent.emit(roundState); connection.turnStartEvent.emit(); view.handPanel.stoneClickEvent.emit(stone1, false); view.handPanel.stoneClickEvent.emit(stone2, true); view.handPanel.stoneClickEvent.emit(stone3, true); view.tablePanel.clickEvent.emit(new Position(0, 0)); view.playerPanel.endTurnEvent.emit(); assertTrue(connection.nextPlayer); connection.nextPlayerEvent.emit(); connection.turnEndEvent.emit(roundState, new InvalidTurnInfo(new Table( settings), null, Collections. emptyList())); assertSame(BottomPanelType.NETWORK_WIN_PANEL, view.bottomPanelType); assertTrue(view.isScorePanelVisible); } /** */ @Test public void testParticipantLeft() { connection.participantLeftEvent.emit("Horst"); assertTrue(BottomPanelType.NETWORK_CONNECTION_LOST_PANEL != view.bottomPanelType); connection.participantLeftEvent.emit("Fooblubb"); assertEquals(BottomPanelType.NETWORK_CONNECTION_LOST_PANEL, view.bottomPanelType); hostControl.getEndOfGameEvent().add(new IListener() { @Override public void handle() { fired = true; } }); view.newGameEvent.emit(); assertTrue(fired); } }