Android Open Source - SeniorDesign Game Test






From Project

Back to project page SeniorDesign.

License

The source code is released under:

GNU General Public License

If you think the Android project SeniorDesign listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package fakesetgame.seniordesign;
//from  w ww . j  av a2s  . c  o  m
import android.app.Application;
import android.test.ApplicationTestCase;

import junit.framework.Assert;

import fakesetgame.seniordesign.model.Game;
import fakesetgame.seniordesign.model.Color;
import fakesetgame.seniordesign.model.Shading;
import fakesetgame.seniordesign.model.Shape;
import fakesetgame.seniordesign.model.Tile;


/**
 * Created by Sami on 10/3/2014.
 */
public class GameTest extends ApplicationTestCase<Application> {
    public GameTest() {
        super(Application.class);
    }

    public void testInvalidAttemptingSet() throws Exception {
        Game gameState = new Game(Game.GameType.Normal, 5, Double.MIN_VALUE, Double.MAX_VALUE);
        int initialScore;
        boolean isValidFalse;

        Tile tile1 = new Tile(Shape.Oval, Shading.Filled, Color.Red, 1);
        Tile tile2 = new Tile(Shape.Oval, Shading.Filled, Color.Red, 2);
        Tile tile3 = new Tile(Shape.Oval, Shading.Filled, Color.Green, 1);

        initialScore = gameState.getFoundSetCount();
        isValidFalse = gameState.attemptSet(tile1, tile2, tile3);

        Assert.assertEquals(initialScore, gameState.getFoundSetCount());
        Assert.assertFalse(isValidFalse);
    }

    public void testValidAttemptingSet() throws Exception {
        Game gameState = new Game(Game.GameType.Normal, 5, Double.MIN_VALUE, Double.MAX_VALUE);
        int initialScore;
        boolean isValidTrue;

        Tile validTile1 = new Tile(Shape.Oval, Shading.Filled, Color.Red, 1);
        Tile validTile2 = new Tile(Shape.Oval, Shading.Filled, Color.Red, 2);
        Tile validTile3 = new Tile(Shape.Oval, Shading.Filled, Color.Red, 3);

        initialScore = gameState.getFoundSetCount();
        isValidTrue = gameState.attemptSet(validTile1, validTile2, validTile3);

        Assert.assertEquals(initialScore + 1, gameState.getFoundSetCount());
        Assert.assertTrue(isValidTrue);

        Assert.assertFalse("Sending the same elements again should not be a valid attempt.",
                gameState.attemptSet(validTile1, validTile2, validTile3)
        );
    }

    public void testGameTimer() throws Exception {
        Game game = new Game(Game.GameType.Normal, 5, Double.MIN_VALUE, Double.MAX_VALUE);

        // sleep 200 ms
        Thread.sleep(200);

        long elapsed = game.getElapsedTime();
        Assert.assertTrue(
                String.format("Timer should have elapsed about 200ms, but elapsed %dms instead.", game.getElapsedTime()),
                Math.abs(game.getElapsedTime() - 200) < 50
        );

        game.unpauseTimer();
        Assert.assertTrue(
                "Starting a timer that is already running should not cause a change in elapsed time, but it has.",
                Math.abs(elapsed - game.getElapsedTime()) < 50
        );

        game.pauseTimer();
        elapsed = game.getElapsedTime();

        Thread.sleep(200);
        Assert.assertEquals(
                "Timer should not have elapsed while paused, but it has.",
                elapsed, game.getElapsedTime());

        game.pauseTimer();
        Assert.assertEquals(
                "Pausing twice should not cause elapsed time to change, but it has.",
                elapsed, game.getElapsedTime());

        game.unpauseTimer();

        Thread.sleep(200);
        long newElapsed = game.getElapsedTime();
        Assert.assertTrue(
                String.format("After resuming, timer should have elapsed about 200ms, but old elapsed time is %dms and new elapsed time is %dms.", elapsed, newElapsed),
                newElapsed >= elapsed + 200 && newElapsed < elapsed + 400);

        game.pauseTimer();
        elapsed = game.getElapsedTime();

        Assert.assertTrue(
                String.format("Timer should have 400 ms elapsed time, but it shows %dms.", elapsed),
                Math.abs(game.getElapsedTime() - 400) < 50);

        Thread.sleep(200);
        Assert.assertEquals(
                "Timer should not have elapsed while paused (2nd pause), but it has.",
                elapsed, game.getElapsedTime());

        game.unpauseTimer();

        Thread.sleep(200);
        newElapsed = game.getElapsedTime();
        Assert.assertTrue(
                String.format("After resuming (2nd resume), timer should have elapsed about 200ms, but old elapsed time is %dms and new elapsed time is %dms.", elapsed, newElapsed),
                newElapsed >= elapsed + 200 && newElapsed < elapsed + 400);

        Assert.assertTrue(
                String.format("Timer should have 600 ms elapsed time (2nd pause), but it shows %dms.", game.getElapsedTime()),
                Math.abs(game.getElapsedTime() - 600) < 50);

        game.restartTimer();
        Thread.sleep(100);
        Assert.assertTrue(
                String.format("Timer should have 100 ms elapsed time, but it shows %dms.", game.getElapsedTime()),
                Math.abs(game.getElapsedTime() - 100) < 50);
    }
}




Java Source Code List

fakesetgame.seniordesign.BoardTest.java
fakesetgame.seniordesign.GameScreen.java
fakesetgame.seniordesign.GameTest.java
fakesetgame.seniordesign.HomeScreen.java
fakesetgame.seniordesign.OptionsScreen.java
fakesetgame.seniordesign.SettingsTest.java
fakesetgame.seniordesign.SplashScreen.java
fakesetgame.seniordesign.SummaryScreen.java
fakesetgame.seniordesign.TileSetTest.java
fakesetgame.seniordesign.data.FoundSetRecord.java
fakesetgame.seniordesign.data.GameOutcome.java
fakesetgame.seniordesign.data.GameSummaryListItemCursorAdapter.java
fakesetgame.seniordesign.data.OptionsHelper.java
fakesetgame.seniordesign.data.PlayerDataDbHelper.java
fakesetgame.seniordesign.data.Setting.java
fakesetgame.seniordesign.data.package-info.java
fakesetgame.seniordesign.model.BoardSize.java
fakesetgame.seniordesign.model.Board.java
fakesetgame.seniordesign.model.Color.java
fakesetgame.seniordesign.model.GameOverEvent.java
fakesetgame.seniordesign.model.GameOverListener.java
fakesetgame.seniordesign.model.Game.java
fakesetgame.seniordesign.model.HintProvider.java
fakesetgame.seniordesign.model.Modifier.java
fakesetgame.seniordesign.model.Shading.java
fakesetgame.seniordesign.model.Shape.java
fakesetgame.seniordesign.model.TileSet.java
fakesetgame.seniordesign.model.Tile.java
fakesetgame.seniordesign.model.package-info.java
fakesetgame.seniordesign.view.GameSummaryListItemView.java
fakesetgame.seniordesign.view.ShadedImageView.java
fakesetgame.seniordesign.view.package-info.java
fakesetgame.seniordesign.package-info.java