summaryrefslogtreecommitdiffstats
path: root/test/jrummikub/control/network/NetworkGameControlTest.java
blob: b1769e22611abb1df00d730007a19efbfba39cc1 (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
182
183
184
package jrummikub.control.network;

import static jrummikub.model.StoneColor.RED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.awt.Color;
import java.util.Collections;

import jrummikub.control.RoundControl.InvalidTurnInfo;
import jrummikub.control.SaveControl;
import jrummikub.model.GameSettings;
import jrummikub.model.GameState;
import jrummikub.model.IHand;
import jrummikub.model.MockRoundState;
import jrummikub.model.PlayerSettings;
import jrummikub.model.Position;
import jrummikub.model.RoundState;
import jrummikub.model.Stone;
import jrummikub.model.StoneSet;
import jrummikub.model.Table;
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 network game control
 */
public class NetworkGameControlTest {

	public NetworkGameControl hostControl, clientControl;
	public GameSettings settings;
	public MockView view;
	public MockConnectionControl connection;
	public boolean fired;
	public RoundState roundState;

	/** */
	@Before
	public void setUp() {
		settings = new GameSettings();
		settings.getPlayerList().add(new PlayerSettings("Foobar", Color.BLACK));
		PlayerSettings player = new PlayerSettings("Fooblubb", Color.BLACK);
		player.setType(PlayerSettings.Type.NETWORK);
		settings.getPlayerList().add(player);
		view = new MockView();
		connection = new MockConnectionControl();
		hostControl = new NetworkGameControl(settings, new SaveControl(view),
				view, connection, true);
		clientControl = new NetworkGameControl(settings, new SaveControl(view),
				view, connection, false);
		roundState = new RoundState(settings, new GameState());
		fired = false;
	}

	/** */
	@Test
	public void testStartClient() {
		clientControl.startGame();
		assertFalse(connection.startRoundCalled);
		connection.roundStartEvent.emit();
		connection.roundStateUpdateEvent.emit(roundState);

		connection.turnStartEvent.emit();
		assertSame(BottomPanelType.HUMAN_HAND_PANEL, view.bottomPanelType);
	}

	/** */
	@Test
	public void testStartHost() {
		hostControl.startGame();
		assertTrue(connection.startRoundCalled);
		connection.roundStartEvent.emit();

		connection.roundStateUpdateEvent.emit(roundState);
		assertTrue(connection.turnStarted);
	}

	/** */
	@Test
	public void testWinHost() {
		hostControl.startGame();
		assertTrue(connection.startRoundCalled);
		connection.roundStartEvent.emit();

		roundState.nextTurn();
		roundState.nextTurn();

		IHand playerHand = roundState.getActivePlayer().getHand();

		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));

		connection.roundStateUpdateEvent.emit(roundState);

		connection.turnStartEvent.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();

		assertTrue(connection.nextPlayer);
		connection.nextPlayerEvent.emit();

		connection.turnEndEvent.emit(roundState, new InvalidTurnInfo(new Table(
				settings), null, Collections.<StoneSet> emptyList()));
		assertSame(BottomPanelType.WIN_PANEL, view.bottomPanelType);
		assertTrue(view.isScorePanelVisible);
	}

	/** */
	@Test
	public void testWinClient() {
		clientControl.startGame();
		connection.roundStartEvent.emit();

		roundState.nextTurn();
		roundState.nextTurn();

		IHand playerHand = roundState.getActivePlayer().getHand();

		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));

		connection.roundStateUpdateEvent.emit(roundState);

		connection.turnStartEvent.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();

		assertTrue(connection.nextPlayer);
		connection.nextPlayerEvent.emit();

		connection.turnEndEvent.emit(roundState, new InvalidTurnInfo(new Table(
				settings), null, Collections.<StoneSet> emptyList()));
		assertSame(BottomPanelType.NETWORK_WIN_PANEL, view.bottomPanelType);
		assertTrue(view.isScorePanelVisible);
	}

	/** */
	@Test
	public void testParticipantLeft() {

		connection.participantLeftEvent.emit("Horst");
		assertTrue(BottomPanelType.NETWORK_CONNECTION_LOST_PANEL != view.bottomPanelType);
		connection.participantLeftEvent.emit("Fooblubb");
		assertEquals(BottomPanelType.NETWORK_CONNECTION_LOST_PANEL,
				view.bottomPanelType);

		hostControl.getEndOfGameEvent().add(new IListener() {
			@Override
			public void handle() {
				fired = true;
			}
		});

		view.newGameEvent.emit();
		assertTrue(fired);

	}

}