package jrummikub.control.network; import static org.junit.Assert.*; import java.awt.Color; import java.util.UUID; import jrummikub.control.SaveControl; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; import jrummikub.util.GameData; import jrummikub.util.IListener; import jrummikub.util.LoginData; import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.IView.BottomPanelType; import jrummikub.view.LoginError; import jrummikub.view.MockView; import org.junit.Before; import org.junit.Test; /** * Test class for network control */ public class NetworkControlTest { LoginData loginData; MockConnectionControl mockConnection; NetworkControl networkControl; MockView view; UUID id1 = UUID.randomUUID(); UUID id2 = UUID.randomUUID(); UUID id3 = UUID.randomUUID(); boolean backToLoginFired; /** */ @Before public void setup() { mockConnection = new MockConnectionControl(); mockConnection.nickname = "Karl"; view = new MockView(); loginData = new LoginData("Karl", "server", "password", "channel"); networkControl = new NetworkControl(loginData, mockConnection, new SaveControl(view), view); networkControl.startNetwork(); backToLoginFired = false; } /** */ @Test public void connectedTest() { mockConnection.connectedEvent.emit(); assertTrue(view.isGameListPanelVisible); assertEquals("channel", view.gameListPanel.channelName); } /** */ @Test public void offeredGameTest() { mockConnection.connectedEvent.emit(); offerTestGame(id1, "Berta"); assertEquals(1, view.gameListPanel.gameList.size()); assertEquals(2, view.gameListPanel.gameList.get(0).getGameSettings() .getPlayerList().size()); assertEquals(id1, view.gameListPanel.gameList.get(0).getGameID()); assertEquals("Berta", view.gameListPanel.gameList.get(0).getHost()); } /** */ @Test public void offerGameTest() { mockConnection.connectedEvent.emit(); view.gameListPanel.openNewGameEvent.emit(); assertTrue(view.isSettingsPanelVisible); assertEquals(SettingsMode.NETWORK_SETUP, view.settingsPanel.settingsMode); view.settingsPanel.addPlayerEvent.emit(); view.settingsPanel.offerGameEvent.emit(); assertTrue(view.isSettingsPanelVisible); assertEquals(SettingsMode.NETWORK_OFFER, view.settingsPanel.settingsMode); assertNotNull(mockConnection.offeredGame); } /** */ @Test public void joinGameTest() { mockConnection.connectedEvent.emit(); GameData data = offerTestGame(id1, "Berta"); view.gameListPanel.joinEvent.emit(data); assertSame(data, mockConnection.joinedGame); mockConnection.gameJoinAckEvent.emit(true); assertTrue(view.isSettingsPanelVisible); assertEquals(SettingsMode.NETWORK_JOIN, view.settingsPanel.settingsMode); } /** */ @Test public void joinGameNackTest() { mockConnection.connectedEvent.emit(); GameData data = offerTestGame(id1, "Berta"); view.gameListPanel.joinEvent.emit(data); assertSame(data, mockConnection.joinedGame); mockConnection.gameJoinAckEvent.emit(false); assertFalse(view.isSettingsPanelVisible); assertTrue(view.isGameListPanelVisible); } /** */ @Test public void withdrawnGameTest() { mockConnection.connectedEvent.emit(); offerTestGame(id1, "Berta"); offerTestGame(id2, "Horst"); assertEquals(2, view.gameListPanel.gameList.size()); assertEquals(2, view.gameListPanel.gameList.get(0).getGameSettings() .getPlayerList().size()); assertEquals(id1, view.gameListPanel.gameList.get(0).getGameID()); assertEquals("Berta", view.gameListPanel.gameList.get(0).getHost()); assertEquals(2, view.gameListPanel.gameList.get(1).getGameSettings() .getPlayerList().size()); assertEquals(id2, view.gameListPanel.gameList.get(1).getGameID()); assertEquals("Horst", view.gameListPanel.gameList.get(1).getHost()); mockConnection.gameWithdrawalEvent.emit(id1); assertEquals(1, view.gameListPanel.gameList.size()); assertEquals(id2, view.gameListPanel.gameList.get(0).getGameID()); assertEquals("Horst", view.gameListPanel.gameList.get(0).getHost()); } /** */ @Test public void disconnectedGameTest() { mockConnection.connectedEvent.emit(); offerTestGame(id1, "Berta"); offerTestGame(id2, "Horst"); assertEquals(2, view.gameListPanel.gameList.size()); assertEquals(2, view.gameListPanel.gameList.get(0).getGameSettings() .getPlayerList().size()); assertEquals(id1, view.gameListPanel.gameList.get(0).getGameID()); assertEquals("Berta", view.gameListPanel.gameList.get(0).getHost()); assertEquals(2, view.gameListPanel.gameList.get(1).getGameSettings() .getPlayerList().size()); assertEquals(id2, view.gameListPanel.gameList.get(1).getGameID()); assertEquals("Horst", view.gameListPanel.gameList.get(1).getHost()); mockConnection.participantLeftEvent.emit("Berta"); assertEquals(1, view.gameListPanel.gameList.size()); assertEquals(id2, view.gameListPanel.gameList.get(0).getGameID()); assertEquals("Horst", view.gameListPanel.gameList.get(0).getHost()); } /** */ @Test public void testCancel() { mockConnection.connectedEvent.emit(); view.gameListPanel.cancelEvent.emit(); assertFalse(view.isGameListPanelVisible); assertFalse(mockConnection.connected); } private GameData offerTestGame(UUID id, String host) { GameSettings gsettings = new GameSettings(); PlayerSettings psettings = new PlayerSettings(host, Color.BLACK); psettings.setType(Type.HUMAN); gsettings.getPlayerList().add(psettings); PlayerSettings psettings2 = new PlayerSettings("Offen", Color.RED); psettings.setType(Type.VACANT); gsettings.getPlayerList().add(psettings2); GameData gameData = new GameData(id, gsettings, host); mockConnection.gameOfferEvent.emit(gameData); return gameData; } /** */ @Test public void testStartGame() { mockConnection.connectedEvent.emit(); view.gameListPanel.openNewGameEvent.emit(); assertTrue(view.isSettingsPanelVisible); assertEquals(SettingsMode.NETWORK_SETUP, view.settingsPanel.settingsMode); view.settingsPanel.offerGameEvent.emit(); mockConnection.gameJoinEvent.emit("Blubb"); view.settingsPanel.startGameEvent.emit(); assertTrue(mockConnection.startRoundCalled); assertTrue(networkControl.isGameRunning()); } /** */ @Test public void connectionLostTest() { mockConnection.connectedEvent.emit(); GameData data = offerTestGame(id1, "Berta"); view.gameListPanel.joinEvent.emit(data); assertSame(data, mockConnection.joinedGame); mockConnection.gameJoinAckEvent.emit(true); mockConnection.connectionLostEvent.emit(); assertSame(BottomPanelType.NETWORK_SERVER_CONNECTION_LOST_PANEL, view.bottomPanelType); } /** */ @Test public void connectionFailedTest() { mockConnection.connectionFailedEvent.emit(LoginError.UNKNOWN_ERROR); assertTrue(view.isConnectPanelVisible); assertSame(LoginError.UNKNOWN_ERROR, view.connectPanel.errorMode); } /** */ @Test public void connectionCancelTest() { networkControl.getBackToLoginEvent().add(new IListener() { @Override public void handle() { backToLoginFired = true; } }); view.connectPanel.cancelEvent.emit(); assertTrue(backToLoginFired); } /** */ @Test public void backToGameListTest() { mockConnection.connectedEvent.emit(); GameData data = offerTestGame(id1, "Berta"); view.gameListPanel.joinEvent.emit(data); assertSame(data, mockConnection.joinedGame); mockConnection.gameJoinAckEvent.emit(true); assertTrue(view.isSettingsPanelVisible); assertEquals(SettingsMode.NETWORK_JOIN, view.settingsPanel.settingsMode); mockConnection.gameStartEvent.emit(); view.newGameEvent.emit(); assertTrue(view.isGameListPanelVisible); } }