package de.bitbrain.braingdx.graphics.pipeline;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.mockito.Mockito;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Batch;

@RunWith(Parameterized.class)
public class RenderPipelineTest {

    @Parameter
    public RenderPipelineFactory pipelineFactory;

    private RenderPipeline pipeline;
    
    @Parameters
    public static Collection<RenderPipelineFactory[]> getParams() {
	List<RenderPipelineFactory[]> params = new ArrayList<RenderPipelineFactory[]>();
	Gdx.app = mock(Application.class);
	when(Gdx.app.getType()).thenReturn(ApplicationType.Desktop);
	params.add(new RenderPipelineFactory[] { new LayeredRenderPipelineFactory() });
	return params;
    }

    @Before
    public void beforeTest() {
	pipeline = pipelineFactory.create();
    }

    @Test
    public void testAddLayer() {
	final String id = "my-id";
	pipeline.add(id, mock(RenderLayer.class));
	assertThat(pipeline.getPipe(id)).isNotNull();
    }

    @Test
    public void testRender() {
	RenderLayer layer = mock(RenderLayer.class);
	Batch batch = mock(Batch.class);
	pipeline.add("my-id", layer);
	pipeline.resize(0, 0);
	pipeline.render(batch, 0f);
	Mockito.inOrder(layer).verify(layer, Mockito.calls(1)).render(batch, 0f);
    }

    @Test
    public void testGetPipeIds() {
	pipeline.add("a", mock(RenderLayer.class));
	pipeline.add("b", mock(RenderLayer.class));
	pipeline.add("c", mock(RenderLayer.class));
	assertThat(pipeline.getPipeIds()).hasSize(3);
    }

    @Test
    public void testGetPipeIdsDuplicates() {
	pipeline.add("a", mock(RenderLayer.class));
	pipeline.add("b", mock(RenderLayer.class));
	pipeline.add("b", mock(RenderLayer.class));
	pipeline.add("c", mock(RenderLayer.class));
	pipeline.add("c", mock(RenderLayer.class));
	assertThat(pipeline.getPipeIds()).hasSize(3);

    }
}

@@ -484,16 +484,44 @@
 meters;%0A
+import org.mockito.InOrder;%0A
 import o
@@ -1562,16 +1562,111 @@
 er layer
+A = mock(RenderLayer.class);%0A%09RenderLayer layerB = mock(RenderLayer.class);%0A%09RenderLayer layerC
  = mock(
@@ -1743,16 +1743,18 @@
 y-id
+-a
 %22, layer
 );%0A%09
@@ -1749,16 +1749,85 @@
 %22, layer
+A);%0A%09pipeline.add(%22my-id-b%22, layerB);%0A%09pipeline.add(%22my-id-c%22, layerC
 );%0A%09pipe
@@ -1875,16 +1875,32 @@
 , 0f);%0A%09
+InOrder order = 
 Mockito.
@@ -1916,22 +1916,166 @@
 ayer
-).verify(layer
+A, layerB, layerC);%0A%09order.verify(layerA, Mockito.calls(1)).render(batch, 0f);%0A%09order.verify(layerB, Mockito.calls(1)).render(batch, 0f);%0A%09order.verify(layerC
 , Mo

Verify order of calls in RenderPipelineTest