package me.teaisaweso.client;

import java.util.Collection;

import junit.framework.Assert;
import junit.framework.TestCase;
import me.teaisaweso.client.graphmanagers.GraphManager2d;
import me.teaisaweso.client.graphmanagers.GraphManager2dFactory;
import me.teaisaweso.shared.Graph;
import me.teaisaweso.shared.Vertex;

/**
 * NOTE: this class relies on the GraphManager2d factory working, if it isn't
 * you should probably assume that this test is totally broken
 */
public class GraphManager2dTest extends TestCase {
	private GraphManager2d mManager;

	/**
	 * runs test setup, creates a graphmanager instance
	 */
	public void setUp() {
		mManager = GraphManager2dFactory.getInstance().makeDefaultGraphManager();
	}

	public void tearDown() {
		mManager = null;
	}

	/**
	 * tests that adding vertices works
	 */
	public void testAddVertex_single() {
		mManager.addVertex(new Vertex("hi"), 0, -3, 10);
		Graph underlying = mManager.getUnderlyingGraph();
		Assert.assertEquals(true, underlying.getVertices().contains(new Vertex("hi")));
		Collection<VertexDrawable> drawables = mManager.getVertexDrawables();
		Assert.assertEquals(1, drawables.size());
		VertexDrawable[] vds = drawables.toArray(new VertexDrawable[] { new VertexDrawable(0, 0, 0,
				0, "") });
		Assert.assertEquals(0, vds[0].getCenterX());
		Assert.assertEquals(-5, vds[0].getLeft());
		Assert.assertEquals(-3, vds[0].getCenterY());
		Assert.assertEquals(-8, vds[0].getTop());
		Assert.assertEquals(10, vds[0].getWidth());
		Assert.assertEquals(10, vds[0].getHeight());
		Assert.assertEquals("hi", vds[0].getLabel());
	}

	public void testAddVertex_multiple() {
		mManager.addVertex(new Vertex("bees"), 100, 17, 2);
		mManager.addVertex(new Vertex("cheese"), 0, 0, 2);

		Graph underlying = mManager.getUnderlyingGraph();
		Assert.assertEquals(2, underlying.getVertices().size());
		Assert.assertEquals(true, underlying.getVertices().contains(new Vertex("bees")));
		Assert.assertEquals(true, underlying.getVertices().contains(new Vertex("cheese")));

		VertexDrawable[] vds = mManager.getVertexDrawables().toArray(new VertexDrawable[] {});
		Assert.assertEquals(2, vds.length);
	}

}

@@ -311,16 +311,61 @@
 .Vertex;
+%0Aimport me.teaisaweso.shared.VertexDirection;
 %0A%0A/**%0A *
@@ -591,16 +591,237 @@
 Manager;
+%0A%09private boolean mCalled = false;%0A%09private Runnable mCheckCalled = new Runnable() %7B%0A        %0A        @Override%0A        public void run() %7B%0A            GraphManager2dTest.this.mCalled = true;%0A            %0A        %7D%0A    %7D;
 %0A%0A%09/**%0A%09
@@ -976,16 +976,35 @@
 ager();%0A
+%09%09mCalled = false;%0A
 %09%7D%0A%0A%09pub
@@ -1021,24 +1021,24 @@
 earDown() %7B%0A
-
 %09%09mManager =
@@ -1044,16 +1044,35 @@
 = null;%0A
+%09%09mCalled = false;%0A
 %09%7D%0A%0A%09/**
@@ -2450,12 +2450,226 @@
 gth);%0A%09%7D
+%0A%09%0A%09public void testInvalidate_addEdge() %7B%0A%09    mManager.addRedrawCallback(mCheckCalled);%0A%09    mManager.addEdge(new Vertex(%22a%22), new Vertex(%22b%22), VertexDirection.fromTo);%0A%09    Assert.assertEquals(true, mCalled);%0A%09%7D
 %0A%0A%7D%0A

add some tests for invalidate callbacks