Game implements IGame { @Override public void addListener(final IMinesGameListener listener) { itsListeners.add(listener); } Game(final int dimX, final int dimY, final int bombs, final IBoardMiner miner); private  Game(final GameState state, final IBoardMiner miner); @Override void addListener(final IMinesGameListener listener); @Override IBoard getBoard(); @Override int getBombCount(); @Override void onRequestFlag(final IField fieldInterface); @Override void onRequestOpen(final IField fieldInterface); @Override boolean isStarted(); @Override boolean isRunning(); @Override void removeListener(final IMinesGameListener listener); @Override String toString(); static Game createFromState(final GameState state, final IBoardMiner miner); GameState getState(); @Override IStopWatch getWatch(); @Override void tickWatch(); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test public void testExplodeNotification() { openAt(0, 0, itsGame2by1); TrackingMinesListener listener = new TrackingMinesListener() { @Override public void onBusted() { assertEquals(1, getChange()); super.onBusted(); } }; itsGame2by1.addListener(listener); openAt(1, 0, itsGame2by1); assertEquals(1, listener.getBusted()); assertEquals(1, listener.getChange()); assertEquals(0, listener.getStart()); assertEquals(0, listener.getDisarmed()); }
@Test public void testFlaggedNotification() { Game game = new Game(2, 2, 2, new TestBoardMiner(new boolean[][]{{false, false}, {true, true}})); openAt(0, 0, game); TrackingMinesListener listener = new TrackingMinesListener() { @Override public void onChange(final int flags, final int mines) { assertEquals(1, flags); assertEquals(2, mines); super.onChange(flags, mines); } }; game.addListener(listener); flagAt(1, 1, game); assertEquals(0, listener.getStart()); assertEquals(1, listener.getChange()); assertEquals(0, listener.getDisarmed()); assertEquals(0, listener.getBusted()); }
@Test public void testDisarmedNotification() { openAt(0, 0, itsGame2by1); TrackingMinesListener listener = new TrackingMinesListener() { @Override public void onDisarmed() { assertEquals(1, getChange()); super.onDisarmed(); } }; itsGame2by1.addListener(listener); flagAt(1, 0, itsGame2by1); assertEquals(1, listener.getChange()); assertEquals(1, listener.getDisarmed()); assertEquals(0, listener.getBusted()); assertEquals(0, listener.getStart()); }
@Test public void testStartedNotification() { final boolean[] startCall = new boolean[1]; final boolean[] changeCall = new boolean[1]; itsGame2by1.addListener(new FailingMinesListener() { @Override public void onStart() { startCall[0] = true; } @Override public void onChange(final int flags, final int mines) { assertEquals(true, startCall[0]); changeCall[0] = true; } }); openAt(0, 0, itsGame2by1); assertEquals(true, changeCall[0]); }
@Test public void testOpenManyUnflaggedNeighborsOnOpen() { Game game = new Game(100, 100, 1, new IBoardMiner() { @Override public void mine(final IBoard board, final int bombs, final IField clickedField) { ((Field) board.getField(0, 0)).setBomb(true); } }); openAt(1, 0, game); openAt(1, 1, game); openAt(0, 1, game); game.addListener(new FailingMinesListener() { @Override public void onChange(final int flags, final int mines) { assertEquals(0, flags); } }); openAt(2, 0, game); }