package eu.ydp.empiria.player.client.module.test.reset;

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwtmockito.GwtMockitoTestRunner;
import eu.ydp.empiria.player.client.controller.extensions.internal.workmode.PlayerWorkMode;
import eu.ydp.empiria.player.client.controller.extensions.internal.workmode.PlayerWorkModeService;
import eu.ydp.empiria.player.client.controller.flow.FlowManager;
import eu.ydp.empiria.player.client.controller.flow.request.FlowRequest;
import eu.ydp.empiria.player.client.module.test.reset.view.TestResetButtonView;
import eu.ydp.gwtutil.client.event.factory.Command;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

@RunWith(GwtMockitoTestRunner.class)
public class TestResetButtonPresenterTest {

	private TestResetButtonPresenter testObj;

	@Mock
	private TestResetButtonView testResetButtonView;
	@Mock
	private FlowManager flowManager;
	@Mock
	private PlayerWorkModeService playerWorkModeService;

	@Captor
	private ArgumentCaptor<Command> commandCaptor;

	@Before
	public void setUp() {
		testObj = new TestResetButtonPresenter(testResetButtonView, flowManager, playerWorkModeService);
	}

	@Test
	public void shouldGetView() {
		// given
		Widget widget = mock(Widget.class);
		when(testResetButtonView.asWidget()).thenReturn(widget);

		// when
		Widget result = testObj.getView();

		// then
		assertThat(result).isEqualTo(widget);
	}

	@Test
	public void shouldLock() {
		// when
		testObj.lock();

		// then
		testResetButtonView.lock();
	}

	@Test
	public void shouldUnlock() {
		// when
		testObj.unlock();

		// then
		testResetButtonView.unlock();
	}

	@Test
	public void shouldNavigateToFirstItem_ifNoLockWasCalled() {
		// given
		NativeEvent event = mock(NativeEvent.class);

		verify(testResetButtonView).addHandler(commandCaptor.capture());
		Command command = commandCaptor.getValue();

		// when
		command.execute(event);

		// then
		verifyActionsOnResetClick();
	}

	@Test
	public void shouldNavigateToFirstItem_ifLockUnlockWasCalled() {
		// given
		NativeEvent event = mock(NativeEvent.class);
		testObj.lock();

		verify(testResetButtonView).addHandler(commandCaptor.capture());
		Command command = commandCaptor.getValue();

		testObj.unlock();

		// when
		command.execute(event);

		// then
		verifyActionsOnResetClick();
	}

	@Test
	public void shouldNotNavigateToFirstItem() {
		// given
		NativeEvent event = mock(NativeEvent.class);
		testObj.lock();

		verify(testResetButtonView).addHandler(commandCaptor.capture());
		Command command = commandCaptor.getValue();

		// when
		command.execute(event);

		// then
		verifyZeroInteractions(flowManager);
	}

	@Test
	public void shouldEnablePreviewMode() {
		// when
		testObj.enablePreviewMode();

		// then
		verify(testResetButtonView).lock();
		verify(testResetButtonView).enablePreviewMode();
	}

	private void verifyActionsOnResetClick() {
		verify(flowManager).invokeFlowRequest(isA(FlowRequest.NavigateFirstItem.class));
		verify(playerWorkModeService).tryToUpdateWorkMode(PlayerWorkMode.TEST);
	}
}

@@ -50,16 +50,313 @@
 reset;%0A%0A
+import static org.fest.assertions.api.Assertions.*;%0Aimport static org.mockito.Matchers.*;%0Aimport static org.mockito.Mockito.*;%0A%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.mockito.ArgumentCaptor;%0Aimport org.mockito.Captor;%0Aimport org.mockito.Mock;%0A%0A
 import c
@@ -485,24 +485,25 @@
 TestRunner;%0A
+%0A
 import eu.yd
@@ -917,311 +917,179 @@
 ydp.
-gwtutil.client.event.factory.Command;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.mockito.ArgumentCaptor;%0Aimport org.mockito.Captor;%0Aimport org.mockito.Mock;%0A%0Aimport static org.fest.assertions.api.Assertions.assertThat;%0Aimport static org.mockito.Mockito.*
+empiria.player.client.util.events.bus.EventsBus;%0Aimport eu.ydp.empiria.player.client.util.events.state.StateChangeEvent;%0Aimport eu.ydp.gwtutil.client.event.factory.Command
 ;%0A%0A@
@@ -1371,16 +1371,52 @@
 Service;
+%0A%09@Mock%0A%09private EventsBus eventBus;
 %0A%0A%09@Capt
@@ -1595,16 +1595,26 @@
 eService
+, eventBus
 );%0A%09%7D%0A%0A%09
@@ -3102,16 +3102,52 @@
 nager);%0A
+%09%09verifyZeroInteractions(eventBus);%0A
 %09%7D%0A%0A%09@Te
@@ -3536,13 +3536,72 @@
 .TEST);%0A
+%09%09verify(eventBus).fireEvent(isA(StateChangeEvent.class));%0A
 %09%7D%0A%7D%0A

test fix