diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/jrummikub/control/SaveControlTest.java | 68 | ||||
-rw-r--r-- | test/jrummikub/util/Event3Test.java | 153 |
2 files changed, 221 insertions, 0 deletions
diff --git a/test/jrummikub/control/SaveControlTest.java b/test/jrummikub/control/SaveControlTest.java new file mode 100644 index 0000000..b2e0299 --- /dev/null +++ b/test/jrummikub/control/SaveControlTest.java @@ -0,0 +1,68 @@ +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 { + @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); + + 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); + file.delete(); + } + +} diff --git a/test/jrummikub/util/Event3Test.java b/test/jrummikub/util/Event3Test.java new file mode 100644 index 0000000..34aa851 --- /dev/null +++ b/test/jrummikub/util/Event3Test.java @@ -0,0 +1,153 @@ +package jrummikub.util; + +import static org.junit.Assert.*; + +import org.junit.Test; + +/** + * Test class for {@link Event2} + */ +public class Event3Test { + + int fired, fired2, fired3, fired4, fired5, fired6; + + /** */ + @Test + public void singleListener() { + fired = 0; + fired2 = 0; + fired3 = 0; + Event3<Integer, Integer, Integer> testEvent = new Event3<Integer, Integer, Integer>(); + testEvent.add(new IListener3<Integer, Integer, Integer>() { + + @Override + public void handle(Integer n, Integer m, Integer o) { + fired += n; + fired2 += m; + fired3 += o; + } + }); + assertEquals(fired, 0); + assertEquals(fired2, 0); + assertEquals(fired3, 0); + testEvent.emit(10, 5, 3); + assertEquals(fired, 10); + assertEquals(fired2, 5); + assertEquals(fired3, 3); + testEvent.emit(20, 45, 42); + assertEquals(fired, 30); + assertEquals(fired2, 50); + assertEquals(fired3, 45); + } + + /** */ + @Test + public void twoListeners() { + fired = 0; + fired2 = 0; + fired3 = 0; + fired4 = 0; + fired5 = 0; + fired6 = 0; + Event3<Integer, Integer, Integer> testEvent = new Event3<Integer, Integer, Integer>(); + testEvent.add(new IListener3<Integer, Integer, Integer>() { + + @Override + public void handle(Integer n, Integer m, Integer o) { + fired += n; + fired2 += m; + fired3 += o; + } + }); + testEvent.add(new IListener3<Integer, Integer, Integer>() { + + @Override + public void handle(Integer n, Integer m, Integer o) { + fired4 -= n; + fired5 -= m; + fired6 -= o; + } + }); + assertEquals(fired, 0); + assertEquals(fired2, 0); + assertEquals(fired3, 0); + assertEquals(fired4, 0); + assertEquals(fired5, 0); + assertEquals(fired6, 0); + testEvent.emit(5, 10, 4); + assertEquals(fired, 5); + assertEquals(fired2, 10); + assertEquals(fired3, 4); + assertEquals(fired4, -5); + assertEquals(fired5, -10); + assertEquals(fired6, -4); + } + + /** */ + @Test + public void removeListener() { + fired = 0; + fired2 = 0; + fired3 = 0; + Event3<Integer, Integer, Integer> testEvent = new Event3<Integer, Integer, Integer>(); + testEvent.add(new IListener3<Integer, Integer, Integer>() { + + @Override + public void handle(Integer n, Integer m, Integer o) { + fired += n; + fired2 += m; + fired3 += o; + } + }); + IListener3<Integer, Integer, Integer> rem = new IListener3<Integer, Integer, Integer>() { + + @Override + public void handle(Integer n, Integer m, Integer o) { + fail(); + } + }; + assertEquals(fired, 0); + assertEquals(fired2, 0); + assertEquals(fired3, 0); + testEvent.add(rem); + testEvent.remove(rem); + testEvent.emit(10, 20, 30); + assertEquals(fired, 10); + assertEquals(fired2, 20); + assertEquals(fired3, 30); + } + + /** */ + @Test + public void removeListenerByConnection() { + fired = 0; + fired2 = 0; + fired3 = 0; + Event3<Integer, Integer, Integer> testEvent = new Event3<Integer, Integer, Integer>(); + testEvent.add(new IListener3<Integer, Integer, Integer>() { + + @Override + public void handle(Integer n, Integer m, Integer o) { + fired += n; + fired2 += m; + fired3 += o; + } + }); + IListener3<Integer, Integer, Integer> rem = new IListener3<Integer, Integer, Integer>() { + + @Override + public void handle(Integer n, Integer m, Integer o) { + fail(); + } + }; + assertEquals(fired, 0); + assertEquals(fired2, 0); + assertEquals(fired3, 0); + Connection connection = testEvent.add(rem); + connection.remove(); + testEvent.emit(10, 20, 30); + assertEquals(fired, 10); + assertEquals(fired2, 20); + assertEquals(fired3, 30); + } +} |