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

import static org.junit.Assert.*;

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 game join control
 */
public class GameJoinControlTest {
	LoginData loginData;
	MockConnectionControl mockConnection;
	NetworkControl networkControl;
	GameJoinControl joinControl;
	MockView view;
	UUID id1 = UUID.randomUUID();
	UUID id2 = UUID.randomUUID();
	UUID id3 = UUID.randomUUID();

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

		mockConnection.connectedEvent.emit();
	}

	/** */
	@Test
	public void joinCancelTest() {
		GameData data = offerTestGame(id1, "Anne");
		view.gameListPanel.joinEvent.emit(data);
		mockConnection.gameJoinAckEvent.emit(true);
		assertTrue(view.isSettingsPanelVisible);
		assertEquals(SettingsMode.NETWORK_JOIN, view.settingsPanel.settingsMode);
		offerJoinedGame(id1, "Anne", "Karl");
		view.settingsPanel.changePlayerColorEvent.emit(1, Color.GRAY);
		assertEquals(Type.HUMAN, mockConnection.getCurrentGame().getGameSettings()
				.getPlayerList().get(1).getType());
		assertEquals(Color.GRAY, mockConnection.playerColor);

		view.settingsPanel.backEvent.emit();
		assertTrue(view.isGameListPanelVisible);
	}
	
	/** */
	@Test
	public void gameWithdrawnTest() {
		GameData data = offerTestGame(id1, "Anne");
		view.gameListPanel.joinEvent.emit(data);
		mockConnection.gameJoinAckEvent.emit(true);
		assertTrue(view.isSettingsPanelVisible);
		assertEquals(SettingsMode.NETWORK_JOIN, view.settingsPanel.settingsMode);
		offerJoinedGame(id1, "Anne", "Karl");
		
		mockConnection.gameWithdrawalEvent.emit(id1);
		assertTrue(view.isGameListPanelVisible);
	}
	
	/** */
	@Test
	public void participantLeftTest() {
		GameData data = offerTestGame(id1, "Anne");
		view.gameListPanel.joinEvent.emit(data);
		mockConnection.gameJoinAckEvent.emit(true);
		assertTrue(view.isSettingsPanelVisible);
		assertEquals(SettingsMode.NETWORK_JOIN, view.settingsPanel.settingsMode);
		offerJoinedGame(id1, "Anne", "Karl");
		
		mockConnection.participantLeftEvent.emit("Anne");
		assertTrue(view.isGameListPanelVisible);
	}

	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);
		psettings2.setType(Type.VACANT);
		gsettings.getPlayerList().add(psettings2);
		GameData gameData = new GameData(id, gsettings, host);
		mockConnection.gameOfferEvent.emit(gameData);
		return gameData;
	}

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

}