summaryrefslogtreecommitdiffstats
path: root/test/jrummikub/control/GameControlTest.java
blob: 68dda26b71137393a8a278a9c6bdd73a74e74736 (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
172
173
174
175
176
177
178
179
180
181
package jrummikub.control;

import static jrummikub.model.StoneColor.*;
import static org.junit.Assert.*;

import java.awt.Color;

import jrummikub.model.GameSettings;
import jrummikub.model.IHand;
import jrummikub.model.PlayerSettings;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.util.IListener;
import jrummikub.util.Pair;
import jrummikub.view.IView.BottomPanelType;
import jrummikub.view.MockView;

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

/**
 * Tests for the game control
 */
public class GameControlTest {
	private GameControl gameControl;
	private GameSettings gameSettings;
	private MockView view;
	protected boolean gameEnded;

	/** */
	@Before
	public void setUp() {
		view = new MockView();
		gameSettings = new GameSettings();
		gameSettings.getPlayerList().add(
				new PlayerSettings("Foo", new Color(1.0f, 0, 0)));
		gameSettings.getPlayerList().add(
				new PlayerSettings("Bar", new Color(0, 1.0f, 0)));
		gameControl = new GameControl(gameSettings, new SaveControl(view), view);
	}

	/** */
	@Test
	public void testStartFirstRound() {
		gameControl.startGame();
		assertSame(BottomPanelType.START_TURN_PANEL, view.bottomPanelType);
	}

	/** */
	@Test
	public void testEndOfRound() {
		gameControl.startGame();

		// Manipulate first player's hand, to allow player1 to win
		IHand playerHand = gameControl.roundControl.roundState.getActivePlayer().getHand();
		for (Pair<Stone, Position> entry : playerHand.clone()) {
			playerHand.pickUp(entry.getFirst());
		}
		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));
		// Done setting up first players hand

		view.startTurnEvent.emit();

		view.playerPanel.endTurnEvent.emit();
		view.startTurnEvent.emit();
		view.playerPanel.endTurnEvent.emit();
		view.startTurnEvent.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();
		
		assertSame(BottomPanelType.WIN_PANEL, view.bottomPanelType);

		view.newRoundEvent.emit();
		assertSame(BottomPanelType.START_TURN_PANEL, view.bottomPanelType);
	}
	
	@Test
	public void testNewGame() {
		gameControl.getEndOfGameEvent().add(new IListener() {
			@Override
			public void handle() {
				gameEnded = true;
			}
		});
		
		gameEnded = false;
		
		gameControl.startGame();
		// Manipulate first player's hand, to allow player1 to win
		IHand playerHand = gameControl.roundControl.roundState.getActivePlayer().getHand();
		for (Pair<Stone, Position> entry : playerHand.clone()) {
			playerHand.pickUp(entry.getFirst());
		}
		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));
		// Done setting up first players hand

		view.startTurnEvent.emit();

		view.playerPanel.endTurnEvent.emit();
		view.startTurnEvent.emit();
		view.playerPanel.endTurnEvent.emit();
		view.startTurnEvent.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();
		
		assertSame(BottomPanelType.WIN_PANEL, view.bottomPanelType);

		
		assertFalse(gameEnded);
		view.newGameEvent.emit();
		assertTrue(gameEnded);
	}
	
	@Test
	public void testAborting() {
		gameControl.startGame();
	
		// Manipulate first player's hand, to allow player1 to win
		IHand playerHand = gameControl.roundControl.roundState.getActivePlayer().getHand();
		for (Pair<Stone, Position> entry : playerHand.clone()) {
			playerHand.pickUp(entry.getFirst());
		}
		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));
		// Done setting up first players hand

		view.startTurnEvent.emit();

		view.playerPanel.endTurnEvent.emit();
		view.startTurnEvent.emit();
		view.playerPanel.endTurnEvent.emit();
		view.startTurnEvent.emit();

		view.handPanel.stoneClickEvent.emit(stone1, false);
		view.handPanel.stoneClickEvent.emit(stone2, true);
		gameControl.abortGame();
		assertTrue(view.handPanel.stoneClickEvent.listeners.isEmpty());
		assertTrue(view.playerPanel.endTurnEvent.listeners.isEmpty());
	}

	/** */
	@Test
	public void testRedealing() {
		gameControl.startGame();
		view.startTurnEvent.emit();
		PlayerSettings firstPlayer = gameControl.roundControl.roundState
				.getActivePlayer().getPlayerSettings();
		view.playerPanel.endTurnEvent.emit();
		view.startTurnEvent.emit();
		view.playerPanel.redealEvent.emit();
		assertSame(BottomPanelType.START_REDEAL_TURN_PANEL, view.bottomPanelType);
		view.startTurnEvent.emit();
		assertSame(firstPlayer, gameControl.roundControl.roundState
				.getActivePlayer().getPlayerSettings());
		
	}
}