summaryrefslogtreecommitdiffstats
path: root/test/jrummikub/control/RoundControlTest.java
blob: 7d13ff0cbe4d9a072f0abb1e5e8270f917f61102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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 jrummikub.model.MockGameState;
import jrummikub.model.MockTable;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneSet;
import jrummikub.view.MockView;

import org.junit.Before;
import org.junit.Test;

public class RoundControlTest {
	private MockView view;
	private MockGameState testGameState;
	private RoundControl testRound;

	@Before
	public void setup() {
		view = new MockView();
		testGameState = new MockGameState();
		testRound = new RoundControl(testGameState, view);
	}

	private void checkCorrectlyDealed() {
		assertEquals(106 - testGameState.getPlayerCount() * 14, 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());
	}

	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 testTableValidHandChanged() {
		testRound.startRound();
		MockTable oldTable = testGameState.table;
		MockTable newTable = new MockTable();
		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;
		MockTable newTable = new MockTable();
		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;
		MockTable newTable = new MockTable();
		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;
		MockTable newTable = new MockTable();
		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;
		MockTable newTable = new MockTable();
		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);
	}
}