summaryrefslogtreecommitdiffstats
path: root/test/jrummikub/control/SaveControlTest.java
blob: 66a57cd7f366ed6c7a26c0563ea9009f6428a448 (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
package jrummikub.control;

import static jrummikub.model.StoneColor.*;
import static org.junit.Assert.*;

import java.awt.Color;
import java.io.File;
import java.io.IOException;

import jrummikub.model.GameSettings;
import jrummikub.model.GameState;
import jrummikub.model.IRoundState;
import jrummikub.model.PlayerSettings;
import jrummikub.model.Position;
import jrummikub.model.RoundState;
import jrummikub.model.Stone;
import jrummikub.model.StoneSet;
import jrummikub.util.IListener3;
import jrummikub.view.MockView;

import org.junit.Test;

/** */
public class SaveControlTest {
	/**
	 * @throws IOException
	 *           if input cannot be saved/loaded
	 */
	@Test
	public void testSaveLoad() throws IOException {
		GameSettings settings = new GameSettings();
		settings.getPlayerList().add(new PlayerSettings("Matze", Color.GREEN));
		settings.getPlayerList().add(new PlayerSettings("Ida", Color.PINK));

		GameState gameState = new GameState();
		RoundState roundState = new RoundState(settings, null);

		Stone stone1 = new Stone(1, RED);
		Stone stone2 = new Stone(5, RED);
		Stone stone3 = new Stone(2, RED);

		roundState.getTable().drop(new StoneSet(stone1), new Position(0, 0));
		roundState.getActivePlayer().getHand().drop(stone2, new Position(0, 0));
		roundState.getActivePlayer().getHand().drop(stone3, new Position(0, 0));

		MockView view = new MockView();
		SaveControl saveControl = new SaveControl(view);
		saveControl.setGameSettings(settings);
		saveControl.setGameState(gameState);
		saveControl.setRoundState(roundState);

		File file = File.createTempFile("testSave", ".rum");

		view.saveEvent.emit(file);

		saveControl.getLoadEvent().add(
				new IListener3<GameSettings, GameState, IRoundState>() {

					@Override
					public void handle(GameSettings settings, GameState gameState,
							IRoundState roundState) {
						assertEquals(2, settings.getPlayerList().size());
						assertEquals(1, roundState.getTable().getSize());
						assertEquals(2, roundState.getActivePlayer().getHand().getSize());
					}
				});

		view.loadEvent.emit();
		file.delete();
	}

}