summaryrefslogtreecommitdiffstats
path: root/test/jrummikub/control/network/NetworkTurnControlTest.java
blob: 8920848e8b0087bc8093b1b328e950a3721c653a (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
package jrummikub.control.network;

import static org.junit.Assert.*;

import java.awt.Color;

import jrummikub.control.turn.ITurnControl.TurnInfo;
import jrummikub.control.turn.TurnMode;
import jrummikub.model.GameSettings;
import jrummikub.model.GameState;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.model.Position;
import jrummikub.model.RoundState;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.model.StoneSet;
import jrummikub.model.Table;
import jrummikub.util.IListener;
import jrummikub.util.Pair;
import jrummikub.view.MockView;

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

/** */
public class NetworkTurnControlTest {
	private MockConnectionControl connectionControl;
	private MockView view;

	private GameSettings gameSettings;
	private RoundState roundState;

	private NetworkTurnControl turnControl;

	private boolean fired;

	/** */
	@Before
	public void setup() {
		gameSettings = new GameSettings();

		gameSettings.getPlayerList().add(new PlayerSettings("Ida", Color.RED));
		gameSettings.getPlayerList().get(0).setType(Type.NETWORK);
		gameSettings.getPlayerList().add(
				new PlayerSettings("Matthias", Color.YELLOW));
		gameSettings.getPlayerList().get(1).setType(Type.NETWORK);

		roundState = new RoundState(gameSettings, new GameState());

		view = new MockView();
		connectionControl = new MockConnectionControl();
		turnControl = new NetworkTurnControl(connectionControl);
	}

	/** */
	@Test
	public void testTableUpdate() {
		turnControl.setup(new TurnInfo(roundState, TurnMode.NORMAL_TURN, true),
				gameSettings, view);
		turnControl.startTurn();

		Table table = new Table(gameSettings);
		StoneSet set = new StoneSet(new Stone(StoneColor.RED));

		table.drop(set, new Position(0, 0));
		connectionControl.tableUpdateEvent.emit(table);

		int n = 0;
		for (Pair<StoneSet, Position> entry : view.tablePanel.stoneSets) {
			assertSame(entry.getFirst(), set);
			n++;
		}
		assertEquals(1, n);
	}

	/** */
	@Test
	public void testRedeal() {
		turnControl.getRedealEvent().add(new IListener() {
			@Override
			public void handle() {
				fired = true;
			}
		});

		turnControl.setup(new TurnInfo(roundState, TurnMode.MAY_REDEAL, true),
				gameSettings, view);
		turnControl.startTurn();

		connectionControl.redealEvent.emit();

		assertTrue(fired);
	}
}