package jrummikub.control; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.util.Iterator; import jrummikub.model.MockGameState; import jrummikub.model.MockTable; import jrummikub.model.Position; import jrummikub.model.Stone; import jrummikub.model.StoneSet; import jrummikub.util.Pair; import jrummikub.view.MockView; import org.junit.Before; import org.junit.Test; public class RoundControlTest { private MockView view; private MockGameState testGameState; private RoundControl testRound; private MockTable newTable; @Before public void setup() { view = new MockView(); testGameState = new MockGameState(); testRound = new RoundControl(testGameState, view); Stone stone = testGameState.getGameHeap().drawStone(); testGameState.table.drop(new StoneSet(stone), new Position(5, 0)); newTable = new MockTable(); newTable.sets.add(testGameState.table.sets.get(0)); testGameState.table.clonedTable = newTable; } private void checkCorrectlyDealed() { assertEquals(106 - testGameState.getPlayerCount() * 14 - testGameState.table.getSize(), testGameState.getGameHeap().getSize()); for (int i = 0; i < testGameState.getPlayerCount(); i++) { assertEquals(14, testGameState.getPlayer(i).getHand().getSize()); } } private void checkTurnStartSetUp() { assertNotNull(view.currentPlayerName); assertNotNull(view.getTablePanel().leftPlayerName); assertNotNull(view.getTablePanel().topPlayerName); assertNotNull(view.getTablePanel().rightPlayerName); assertTrue(view.displayStartTurnPanel); assertFalse(view.startTurnEvent.listeners.isEmpty()); checkTableDisplay(); } private void checkTableDisplay() { Iterator> stoneSetsView = view.tablePanel.stoneSets .iterator(); Iterator> stoneSetsModel = testGameState.table.sets .iterator(); while (stoneSetsView.hasNext()) { assertTrue(stoneSetsModel.hasNext()); assertSame(stoneSetsView.next(), stoneSetsModel.next()); } assertFalse(stoneSetsModel.hasNext()); } private void resetTurnStart() { view.currentPlayerName = null; view.getTablePanel().leftPlayerName = null; view.getTablePanel().topPlayerName = null; view.getTablePanel().rightPlayerName = null; view.displayStartTurnPanel = false; } @Test public void testDeal() { testRound.deal(); checkCorrectlyDealed(); } @Test public void testStartRound() { testRound.startRound(); checkCorrectlyDealed(); checkTurnStartSetUp(); } @Test public void testTableDisplay() { testRound.startRound(); checkCorrectlyDealed(); view.startTurnEvent.emit(); checkTableDisplay(); view.getPlayerPanel().endTurnEvent.emit(); } @Test public void testTableValidHandChanged() { testRound.startRound(); MockTable oldTable = testGameState.table; newTable.valid = true; oldTable.clonedTable = newTable; view.startTurnEvent.emit(); assertFalse(view.displayStartTurnPanel); testGameState.players.get(0).hand.stones.remove(0); resetTurnStart(); view.getPlayerPanel().endTurnEvent.emit(); assertNotSame(oldTable, testGameState.table); assertSame(newTable, testGameState.table); assertEquals(1, testGameState.activePlayer); checkTurnStartSetUp(); } @Test public void testTableInvalidHandChanged() { testRound.startRound(); MockTable oldTable = testGameState.table; newTable.valid = false; oldTable.clonedTable = newTable; view.startTurnEvent.emit(); assertFalse(view.displayStartTurnPanel); Stone stone = testGameState.players.get(0).hand.stones.remove(0); newTable.drop(new StoneSet(stone), new Position(0, 0)); resetTurnStart(); view.getPlayerPanel().endTurnEvent.emit(); assertSame(oldTable, testGameState.table); assertNotSame(newTable, testGameState.table); assertEquals(1, testGameState.activePlayer); assertEquals(14 + 3, testGameState.players.get(0).hand.getSize()); checkTurnStartSetUp(); } @Test public void testTableValidHandUnchanged() { testRound.startRound(); MockTable oldTable = testGameState.table; newTable.valid = true; oldTable.clonedTable = newTable; view.startTurnEvent.emit(); assertFalse(view.displayStartTurnPanel); resetTurnStart(); view.getPlayerPanel().endTurnEvent.emit(); assertEquals(14 + 1, testGameState.players.get(0).hand.getSize()); assertEquals(1, testGameState.activePlayer); assertSame(newTable, testGameState.table); assertNotSame(oldTable, testGameState.table); checkTurnStartSetUp(); } @Test public void testTableInvalidHandUnchanged() { testRound.startRound(); MockTable oldTable = testGameState.table; newTable.valid = false; oldTable.clonedTable = newTable; view.startTurnEvent.emit(); assertFalse(view.displayStartTurnPanel); resetTurnStart(); view.getPlayerPanel().endTurnEvent.emit(); assertEquals(14 + 1, testGameState.players.get(0).hand.getSize()); assertEquals(1, testGameState.activePlayer); assertNotSame(newTable, testGameState.table); assertSame(oldTable, testGameState.table); checkTurnStartSetUp(); } @Test public void testWinning() { testRound.startRound(); MockTable oldTable = testGameState.table; newTable.valid = true; oldTable.clonedTable = newTable; view.startTurnEvent.emit(); assertFalse(view.displayStartTurnPanel); testGameState.players.get(0).hand.stones.clear(); resetTurnStart(); view.getPlayerPanel().endTurnEvent.emit(); assertTrue(view.displayWinPanel); } }