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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
package jrummikub.control.network;
import static org.junit.Assert.*;
import java.awt.Color;
import java.util.UUID;
import jrummikub.control.SaveControl;
import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.GameData;
import jrummikub.util.IListener;
import jrummikub.util.LoginData;
import jrummikub.view.ISettingsPanel.SettingsMode;
import jrummikub.view.IView.BottomPanelType;
import jrummikub.view.LoginError;
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();
boolean backToLoginFired;
/** */
@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, new SaveControl(view), view);
networkControl.startNetwork();
backToLoginFired = false;
}
/** */
@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);
assertNotNull(mockConnection.offeredGame);
}
/** */
@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 joinGameNackTest() {
mockConnection.connectedEvent.emit();
GameData data = offerTestGame(id1, "Berta");
view.gameListPanel.joinEvent.emit(data);
assertSame(data, mockConnection.joinedGame);
mockConnection.gameJoinAckEvent.emit(false);
assertFalse(view.isSettingsPanelVisible);
assertTrue(view.isGameListPanelVisible);
}
/** */
@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 disconnectedGameTest() {
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.participantLeftEvent.emit("Berta");
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;
}
/** */
@Test
public void testStartGame() {
mockConnection.connectedEvent.emit();
view.gameListPanel.openNewGameEvent.emit();
assertTrue(view.isSettingsPanelVisible);
assertEquals(SettingsMode.NETWORK_SETUP,
view.settingsPanel.settingsMode);
view.settingsPanel.offerGameEvent.emit();
mockConnection.gameJoinEvent.emit("Blubb");
view.settingsPanel.startGameEvent.emit();
assertTrue(mockConnection.startRoundCalled);
assertTrue(networkControl.isGameRunning());
}
/** */
@Test
public void connectionLostTest() {
mockConnection.connectedEvent.emit();
GameData data = offerTestGame(id1, "Berta");
view.gameListPanel.joinEvent.emit(data);
assertSame(data, mockConnection.joinedGame);
mockConnection.gameJoinAckEvent.emit(true);
mockConnection.connectionLostEvent.emit();
assertSame(BottomPanelType.NETWORK_SERVER_CONNECTION_LOST_PANEL, view.bottomPanelType);
}
/** */
@Test
public void connectionFailedTest() {
mockConnection.connectionFailedEvent.emit(LoginError.UNKNOWN_ERROR);
assertTrue(view.isConnectPanelVisible);
assertSame(LoginError.UNKNOWN_ERROR, view.connectPanel.errorMode);
}
/** */
@Test
public void connectionCancelTest() {
networkControl.getBackToLoginEvent().add(new IListener() {
@Override
public void handle() {
backToLoginFired = true;
}
});
view.connectPanel.cancelEvent.emit();
assertTrue(backToLoginFired);
}
/** */
@Test
public void backToGameListTest() {
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);
mockConnection.gameStartEvent.emit();
view.newGameEvent.emit();
assertTrue(view.isGameListPanelVisible);
}
}
|