Package org.gbcpainter.test

Source Code of org.gbcpainter.test.GridTest

package org.gbcpainter.test;

import org.gbcpainter.game.model.grid.Junction;
import org.gbcpainter.game.model.grid.JunctionImpl;
import org.gbcpainter.game.model.grid.Pipe;
import org.gbcpainter.game.model.grid.PipeImpl;
import org.gbcpainter.geom.ImmutableSegment2D;
import org.gbcpainter.geom.PERPENDICULAR_DIRECTION;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.HashSet;

import static org.junit.Assert.*;

/**
* @author Lorenzo Pellegrini
*/
public class GridTest {

  private JFrame frame;

  @Before
  public void initializeEnv() throws Exception {
    frame = InitializeEnv.initialize( 400, 400, "textures" );
  }

  @After
  public void deinitalize() {
    if ( frame != null ) {
      frame.dispose();
      frame = null;
    }
  }

  @Test
  public void testPipeColoring() throws Exception {
    Pipe pipe = new PipeImpl( new ImmutableSegment2D( 0, 0, 0, 10 ) );

    assertFalse( pipe.isColored() );
    pipe.setColored( new Point( 0, 1 ), true );

    assertFalse( pipe.isColored() );
    assertTrue( pipe.isColoredAt( new Point( 0, 1 ) ) );

    for (int i = 1; i < 4; i++) {
      pipe.setColored( new Point( 0, i ), true );
    }

    try {
      pipe.setColored( new Point( 88, 55 ), true );
      fail();
    } catch ( IllegalArgumentException e ) {
    }

    try {
      pipe.setColored( new Point( 0, 0 ), true );
      fail();
    } catch ( IllegalArgumentException e ) {
    }

    try {
      pipe.setColored( new Point( 0, 10 ), true );
      fail();
    } catch ( IllegalArgumentException e ) {
    }

    assertFalse( pipe.isColored() );
    assertTrue( pipe.isColoredAt( new Point( 0, 3 ) ) );
    assertFalse( pipe.isColoredAt( new Point( 0, 4 ) ) );

    pipe.setColored( new Point( 0, 1 ), false );
    assertFalse( pipe.isColoredAt( new Point( 0, 1 ) ) );
    assertTrue( pipe.isColoredAt( new Point( 0, 2 ) ) );

    pipe.reset();
    assertFalse( pipe.isColoredAt( new Point( 0, 2 ) ) );


    for (int i = 1; i < 10; i++) {
      pipe.setColored( new Point( 0, i ), true );
    }
    assertTrue( pipe.isColored() );

    pipe.reset();
    assertFalse( pipe.isColored() );

    for (int i = 1; i < 4; i++) {
      pipe.setColored( new Point( 0, i ), true );
    }

    for (int i = 9; i > 4; i--) {
      pipe.setColored( new Point( 0, i ), true );
    }

    assertFalse( pipe.isColored() );

    pipe.setColored( new Point( 0, 4 ), true );

    assertTrue( pipe.isColored() );
  }

  @Test
  public void testJunctionColoring() throws Exception {
    Junction junction = new JunctionImpl( new Point( 0, 0 ), new HashSet<>(
        Arrays.asList( PERPENDICULAR_DIRECTION.LEFT, PERPENDICULAR_DIRECTION.RIGHT, PERPENDICULAR_DIRECTION.DOWN )
    ) );

    assertFalse( junction.isColored() );

    junction.setColored( true );
    assertTrue( junction.isColored() );


    junction.setColored( false );
    assertFalse( junction.isColored() );
  }

  @Test
  public void pipeHitTest() throws Exception {
    Pipe pipe = new PipeImpl( new ImmutableSegment2D( 0, 0, 0, 10 ) );

    assertTrue( pipe.contains( new Point( 0, 1 ) ) );
    assertFalse( pipe.contains( new Point( 0, 0 ) ) );
    assertFalse( pipe.contains( new Point( 0, 10 ) ) );

    assertFalse( pipe.contains( new Point( 1, 0 ) ) );
    assertFalse( pipe.contains( new Point( - 1, 0 ) ) );

    assertEquals( pipe.getAvailableDirections(), new HashSet<>( Arrays.asList( PERPENDICULAR_DIRECTION.UP, PERPENDICULAR_DIRECTION.DOWN ) ) );
  }

  @Test
  public void junctionHitTest() throws Exception {
    Junction junction = new JunctionImpl( new Point( 0, 0 ), new HashSet<>(
        Arrays.asList( PERPENDICULAR_DIRECTION.LEFT, PERPENDICULAR_DIRECTION.RIGHT, PERPENDICULAR_DIRECTION.DOWN )
    ) );

    assertTrue( junction.contains( new Point( 0, 0 ) ) );

    assertFalse( junction.contains( new Point( 1, 0 ) ) );
    assertFalse( junction.contains( new Point( 0, 1 ) ) );

    assertEquals( junction.getAvailableDirections(), new HashSet<>( Arrays.asList( PERPENDICULAR_DIRECTION.LEFT, PERPENDICULAR_DIRECTION.RIGHT,
                                                                                   PERPENDICULAR_DIRECTION.DOWN ) ) );
  }
}
TOP

Related Classes of org.gbcpainter.test.GridTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.