package io.github.data4all.view;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import io.github.data4all.model.drawing.Point;
import io.github.data4all.view.TouchView.PointMover;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

/**
 * Test cases for the TouchView class
 * 
 * @author konerman
 */
@RunWith(RobolectricTestRunner.class)
@Config(emulateSdk = 18)
public class TouchViewTest {

    private TouchView touchview;
    private List<Point> polygon;

    private Point point1;
    private Point point2;
    private Point point3;
    private Point point4;
    private Point point5;
    private Point point6;

    @Before
    public void setUp() throws Exception {
        touchview = new TouchView(Robolectric.application);

        point1 = new Point(2, 2);
        point2 = new Point(40, 40);
        point3 = new Point(43, 43);
        point4 = new Point(33, 33);
        point5 = new Point(1, 1);
        point6 = new Point(1, 1);

        Field declaredField = touchview.getClass().getDeclaredField("polygon");
        declaredField.setAccessible(true);
        polygon = new ArrayList<Point>();
        declaredField.set(touchview, polygon);

        polygon.add(point1);
        polygon.add(point2);
        polygon.add(point3);
        polygon.add(point4);
    }

    @Test
    public void deletePoint_deleteNull_nothingChanged() {
        List<Point> list = new ArrayList<Point>(polygon);
        touchview.deletePoint(null);
        for (int i = 0; i < list.size(); i++) {
            assertEquals(list.get(i), polygon.get(i));
        }
    }

    @Test
    public void deletePoint_pointNotInPolygon_nothingChanged() {
        List<Point> list = new ArrayList<Point>(polygon);
        Point point5 = new Point(333, 333);
        touchview.deletePoint(point5);

        assertFalse(polygon.contains(point5));
        for (int i = 0; i < list.size(); i++) {
            assertEquals(list.get(i), polygon.get(i));
        }
    }

    @Test
    public void deletePoint_pointInPolygon_pointRemoved() {
        assertTrue(polygon.contains(point4));
        touchview.deletePoint(point4);
        assertFalse(polygon.contains(point4));
    }

    @Test
    public void lookUp_noPointsInPolygon_nullReturned() {
        polygon.clear();
        assertNull(touchview.lookUp(5, 5, 2));
    }

    @Test
    public void lookUp_noPointAround_nullReturned() {
        assertNull(touchview.lookUp(20, 20, 2));
    }

    @Test
    public void lookUp_pointAround_rightPointReturned() {
        assertEquals(point1, touchview.lookUp(1, 1, 5));
    }

    @Test
    public void lookUp_exactLookupOnPoint_rightPointReturned() {
        assertEquals(point1, touchview.lookUp(2, 2, 0));
    }

    @Test
    public void lookUp_towPointsInRange_nearestPointReturned() {
        assertEquals(point3, touchview.lookUp(42, 42, 2));
    }

    @Test
    public void movePoint_PointIsNull_NullReturned() {
        assertThat(touchview.movePoint(null), is(nullValue()));
    }

    @Test
    public void movePoint_PointIsNotInPolygon_NullReturned() {
        assertThat(touchview.movePoint(new Point(0, 0)), is(nullValue()));
    }

    @Test
    public void movePoint_getRightPositionInPolygon() {
        int idx = polygon.indexOf(point5);
        PointMover pm = touchview.movePoint(point5);

        assertThat(polygon.get(idx), is(polygon.get(4)));
    }

    @Test
    public void moveTo_PointMoved_XYchanged() {
        PointMover pm = touchview.movePoint(point5);
        int idx = polygon.indexOf(point5);
        pm.moveTo(5.0F, 6.0F);
        assertThat(polygon.get(idx).getX(), is(5.0F));
        assertThat(polygon.get(idx).getY(), is(6.0F));
    }
}

@@ -1094,34 +1094,8 @@
 nt5;
-%0A    private Point point6;
 %0A%0A  
@@ -1387,42 +1387,8 @@
  1);
-%0A        point6 = new Point(1, 1);
 %0A%0A  
@@ -3629,24 +3629,53 @@
 Polygon() %7B%0A
+        polygon.add(point5);%0A
         int 
@@ -3712,32 +3712,16 @@
 %0A       
- PointMover pm =
  touchvi
@@ -3858,32 +3858,61 @@
 d_XYchanged() %7B%0A
+        polygon.add(point5);%0A
         PointMov

Repair test cases