package eu.ydp.empiria.player.client.controller.multiview;


import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

import com.google.gwt.dev.util.collect.HashMap;
import com.google.gwt.junit.GWTMockUtilities;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import eu.ydp.empiria.player.client.AbstractTestBaseWithoutAutoInjectorInit;
import eu.ydp.gwtutil.client.collections.KeyValue;
import eu.ydp.gwtutil.junit.runners.ExMockRunner;
import eu.ydp.gwtutil.junit.runners.PrepareForTest;

@RunWith(ExMockRunner.class)
@PrepareForTest({FlowPanel.class, Widget.class})
public class PagePlaceHolderPanelCreatorJUnitTest extends AbstractTestBaseWithoutAutoInjectorInit {

	private PagePlaceHolderPanelCreator placeHolderPanelCreator;
	private PanelCache panelsCache;
	private FlowPanel mainFlowPanel;
	

	@BeforeClass
	public static void disarm() {
		GWTMockUtilities.disarm();
	}

	@AfterClass
	public static void rearm() {
		GWTMockUtilities.restore();
	}
	
	@Before
	public void prepareTests(){
		panelsCache = mock(PanelCache.class);
		mainFlowPanel = mock(FlowPanel.class);
		placeHolderPanelCreator = new PagePlaceHolderPanelCreator(panelsCache, mainFlowPanel);
	}
	
	@After
	public void tearDown() throws Exception {
		Mockito.verifyNoMoreInteractions(panelsCache, mainFlowPanel);
	}

	@Test
	public void testCreatePanelsUntilIndex_cacheIsEmpty() throws Exception {
		int pageIndex = 1;
		
		when(panelsCache.isEmpty())
			.thenReturn(true);
		
		FlowPanel zeroPanelPlaceHolderKey = mock(FlowPanel.class);
		FlowPanel zeroPanelPlaceHolderValue = mock(FlowPanel.class);
		KeyValue<FlowPanel, FlowPanel> zeroPanelPlaceHolderPair = new KeyValue<FlowPanel, FlowPanel>(zeroPanelPlaceHolderKey, zeroPanelPlaceHolderValue);
		when(panelsCache.getOrCreateAndPut(0))
			.thenReturn(zeroPanelPlaceHolderPair);
		
		FlowPanel firstPanelPlaceHolderKey = mock(FlowPanel.class);
		FlowPanel firstPanelPlaceHolderValue = mock(FlowPanel.class);
		KeyValue<FlowPanel, FlowPanel> firstPanelPlaceHolderPair = new KeyValue<FlowPanel, FlowPanel>(firstPanelPlaceHolderKey, firstPanelPlaceHolderValue);
		when(panelsCache.getOrCreateAndPut(1))
			.thenReturn(firstPanelPlaceHolderPair);
		
		placeHolderPanelCreator.createPanelsUntilIndex(pageIndex);
		
		
		verify(panelsCache).isEmpty();
		verify(panelsCache).getOrCreateAndPut(0);
		verify(mainFlowPanel).add(zeroPanelPlaceHolderKey);
		
		verify(panelsCache).getOrCreateAndPut(1);
		verify(mainFlowPanel).add(firstPanelPlaceHolderKey);
	}
	
	@Test
	public void testCreatePanelsUntilIndex_cacheIsNotEmpty() throws Exception {
		int pageIndex = 1;
		
		when(panelsCache.isEmpty())
			.thenReturn(false);
		
		Map<Integer, KeyValue<FlowPanel, FlowPanel>> panelsCacheMap = preparePanelsCacheMap(0);
		when(panelsCache.getCache())
			.thenReturn(panelsCacheMap);
		
		FlowPanel firstPanelPlaceHolderKey = mock(FlowPanel.class);
		FlowPanel firstPanelPlaceHolderValue = mock(FlowPanel.class);
		KeyValue<FlowPanel, FlowPanel> firstPanelPlaceHolderPair = new KeyValue<FlowPanel, FlowPanel>(firstPanelPlaceHolderKey, firstPanelPlaceHolderValue);
		when(panelsCache.getOrCreateAndPut(1))
			.thenReturn(firstPanelPlaceHolderPair);
		
		placeHolderPanelCreator.createPanelsUntilIndex(pageIndex);
		
		
		verify(panelsCache).isEmpty();
		verify(panelsCache).getCache();
		verify(panelsCache).getOrCreateAndPut(1);
		verify(mainFlowPanel).add(firstPanelPlaceHolderKey);
	}

	private Map<Integer, KeyValue<FlowPanel, FlowPanel>> preparePanelsCacheMap(int ... indexes) {
		Map<Integer, KeyValue<FlowPanel, FlowPanel>> panelsCacheMap = new HashMap<Integer, KeyValue<FlowPanel,FlowPanel>>();
		for(int index : indexes){
			panelsCacheMap.put(index, new KeyValue<FlowPanel, FlowPanel>());
		}
		return panelsCacheMap;
	}
}

@@ -2982,33 +2982,33 @@
 int pageIndex = 
-1
+2
 ;%0D%0A%09%09%0D%0A%09%09when(pa
@@ -3144,16 +3144,19 @@
 cheMap(0
+, 1
 );%0D%0A%09%09wh
@@ -3224,37 +3224,38 @@
 %09%09%0D%0A%09%09FlowPanel 
-first
+second
 PanelPlaceHolder
@@ -3288,37 +3288,38 @@
 );%0D%0A%09%09FlowPanel 
-first
+second
 PanelPlaceHolder
@@ -3375,37 +3375,38 @@
 nel, FlowPanel%3E 
-first
+second
 PanelPlaceHolder
@@ -3439,37 +3439,38 @@
 nel, FlowPanel%3E(
-first
+second
 PanelPlaceHolder
@@ -3466,37 +3466,38 @@
 PlaceHolderKey, 
-first
+second
 PanelPlaceHolder
@@ -3534,33 +3534,33 @@
 tOrCreateAndPut(
-1
+2
 ))%0D%0A%09%09%09.thenRetu
@@ -3554,37 +3554,38 @@
 %0A%09%09%09.thenReturn(
-first
+second
 PanelPlaceHolder
@@ -3767,33 +3767,33 @@
 tOrCreateAndPut(
-1
+2
 );%0D%0A%09%09verify(mai
@@ -3800,37 +3800,38 @@
 nFlowPanel).add(
-first
+second
 PanelPlaceHolder

Test extended to cover correctly maxValue finding function