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.LoginData; import jrummikub.view.ISettingsPanel.SettingsMode; 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(); /** */ @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(); } /** */ @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 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); } }