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

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.UUID;

import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.GameData;
import jrummikub.util.LoginData;
import jrummikub.view.ISettingsPanel.SettingsMode;
import jrummikub.view.MockView;

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

/**
 * Test class for network control
 */
public class NetworkControlTest {
	LoginData loginData;
	MockConnectionControl mockConnection;
	NetworkControl networkControl;
	MockView view;
	UUID id1 = UUID.randomUUID();
	UUID id2 = UUID.randomUUID();
	UUID id3 = UUID.randomUUID();

	/** */
	@Before
	public void setup() {
		mockConnection = new MockConnectionControl();
		mockConnection.nickname = "Karl";
		view = new MockView();
		loginData = new LoginData("Karl", "server", "password", "channel");
		networkControl = new NetworkControl(loginData, mockConnection, view);
		networkControl.startNetwork();
	}

	/** */
	@Test
	public void connectedTest() {
		mockConnection.connectedEvent.emit();
		assertTrue(view.isGameListPanelVisible);
		assertEquals("channel", view.gameListPanel.channelName);
	}

	/** */
	@Test
	public void offeredGameTest() {
		mockConnection.connectedEvent.emit();
		offerTestGame(id1, "Berta");

		assertEquals(1, view.gameListPanel.gameList.size());
		assertEquals(2, view.gameListPanel.gameList.get(0).getGameSettings()
				.getPlayerList().size());
		assertEquals(id1, view.gameListPanel.gameList.get(0).getGameID());
		assertEquals("Berta", view.gameListPanel.gameList.get(0).getHost());
	}

	/** */
	@Test
	public void offerGameTest() {
		mockConnection.connectedEvent.emit();
		view.gameListPanel.openNewGameEvent.emit();

		assertTrue(view.isSettingsPanelVisible);
		assertEquals(SettingsMode.NETWORK_SETUP,
				view.settingsPanel.settingsMode);

		view.settingsPanel.addPlayerEvent.emit();

		view.settingsPanel.offerGameEvent.emit();

		assertTrue(view.isSettingsPanelVisible);
		assertEquals(SettingsMode.NETWORK_OFFER,
				view.settingsPanel.settingsMode);
	}

	/** */
	@Test
	public void joinGameTest() {
		mockConnection.connectedEvent.emit();
		GameData data = offerTestGame(id1, "Berta");
		view.gameListPanel.joinEvent.emit(data);
		assertSame(data, mockConnection.joinedGame);
		mockConnection.gameJoinAckEvent.emit(true);
		assertTrue(view.isSettingsPanelVisible);
		assertEquals(SettingsMode.NETWORK_JOIN, view.settingsPanel.settingsMode);
	}

	/** */
	@Test
	public void withdrawnGameTest() {
		mockConnection.connectedEvent.emit();
		offerTestGame(id1, "Berta");

		offerTestGame(id2, "Horst");

		assertEquals(2, view.gameListPanel.gameList.size());
		assertEquals(2, view.gameListPanel.gameList.get(0).getGameSettings()
				.getPlayerList().size());
		assertEquals(id1, view.gameListPanel.gameList.get(0).getGameID());
		assertEquals("Berta", view.gameListPanel.gameList.get(0).getHost());
		assertEquals(2, view.gameListPanel.gameList.get(1).getGameSettings()
				.getPlayerList().size());
		assertEquals(id2, view.gameListPanel.gameList.get(1).getGameID());
		assertEquals("Horst", view.gameListPanel.gameList.get(1).getHost());

		mockConnection.gameWithdrawalEvent.emit(id1);
		assertEquals(1, view.gameListPanel.gameList.size());
		assertEquals(id2, view.gameListPanel.gameList.get(0).getGameID());
		assertEquals("Horst", view.gameListPanel.gameList.get(0).getHost());
	}

	/** */
	@Test
	public void testCancel() {
		mockConnection.connectedEvent.emit();

		view.gameListPanel.cancelEvent.emit();
		assertFalse(view.isGameListPanelVisible);
		assertFalse(mockConnection.connected);
	}

	private GameData offerTestGame(UUID id, String host) {
		GameSettings gsettings = new GameSettings();
		PlayerSettings psettings = new PlayerSettings(host, Color.BLACK);
		psettings.setType(Type.HUMAN);
		gsettings.getPlayerList().add(psettings);
		PlayerSettings psettings2 = new PlayerSettings("Offen", Color.RED);
		psettings.setType(Type.VACANT);
		gsettings.getPlayerList().add(psettings2);
		GameData gameData = new GameData(id, gsettings, host);
		mockConnection.gameOfferEvent.emit(gameData);
		return gameData;
	}

}