package eu.ydp.empiria.player.client.module.mathjax.interaction;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwtmockito.GwtMockitoTestRunner;
import eu.ydp.empiria.player.client.controller.body.BodyGeneratorSocket;
import eu.ydp.empiria.player.client.gin.factory.MathJaxModuleFactory;
import eu.ydp.empiria.player.client.gin.factory.PageScopeFactory;
import eu.ydp.empiria.player.client.module.ModuleSocket;
import eu.ydp.empiria.player.client.module.mathjax.common.MathJaxPresenter;
import eu.ydp.empiria.player.client.module.mathjax.common.MathJaxView;
import eu.ydp.empiria.player.client.util.events.internal.bus.EventsBus;
import eu.ydp.empiria.player.client.util.events.internal.player.PlayerEvent;
import eu.ydp.empiria.player.client.util.events.internal.player.PlayerEventTypes;
import eu.ydp.gwtutil.client.proxy.RootPanelDelegate;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;

import static org.mockito.Mockito.*;

@RunWith(GwtMockitoTestRunner.class)
public class InteractionMathJaxModuleTest {

    private InteractionMathJaxModule testObj;
    @Mock
    private MathJaxPresenter presenter;
    @Mock
    private BodyGeneratorSocket bodyGenerator;
    @Mock
    private ModuleSocket moduleSocket;
    @Mock
    private RootPanel rootPanel;
    @Mock
    private PageScopeFactory pageScopeFactory;
    @Mock
    private EventsBus eventsBus;

    @Before
    public void init() {
        MathJaxModuleFactory factory = mock(MathJaxModuleFactory.class);
        MathJaxView view = mock(MathJaxView.class);
        when(factory.getMathJaxPresenter(view)).thenReturn(presenter);

        RootPanelDelegate rootPanelDelegate = mock(RootPanelDelegate.class);
        when(rootPanelDelegate.getRootPanel()).thenReturn(rootPanel);

        testObj = new InteractionMathJaxModule(factory, view, rootPanelDelegate, eventsBus, pageScopeFactory);
    }

    @Test
    public void shouldInitPresenter_andGenerateGaps() {
        // given;
        String script = "script";

        NodeList gaps = mock(NodeList.class);
        when(gaps.getLength()).thenReturn(1);

        Node gap = mock(Node.class);
        when(gaps.item(0)).thenReturn(gap);

        Element element = mock(Element.class, RETURNS_DEEP_STUBS);
        when(element.getElementsByTagName("gap")).thenReturn(gaps);
        when(element.getChildNodes().toString()).thenReturn(script);

        // when
        testObj.initModule(element, moduleSocket, bodyGenerator);

        // then
        verify(rootPanel).add(isA(FlowPanel.class));
        verify(bodyGenerator).processNode(eq(gap), isA(FlowPanel.class));
        verify(presenter).setMmlScript(script);
    }

    @Test
    public void rerenderModule_onEvent() {
        //given
        PlayerEvent event = mock(PlayerEvent.class);
        when(event.getType()).thenReturn(PlayerEventTypes.SOURCE_LIST_CLIENTS_SET_SIZE_COMPLETED);
        testObj.markToRerender();

        //when
        testObj.onPlayerEvent(event);

        //then
        verify(presenter).rerenderMathElement(anyString());

    }
}

@@ -1109,16 +1109,44 @@
 unWith;%0A
+import org.mockito.Answers;%0A
 import o
@@ -1627,16 +1627,92 @@
 entsBus;
+%0A    @Mock(answer = Answers.RETURNS_DEEP_STUBS)%0A    private Element element;
 %0A%0A    @B
@@ -2197,32 +2197,33 @@
 actory);%0A    %7D%0A%0A
+%0A
     @Test%0A    pu
@@ -2504,75 +2504,8 @@
 );%0A%0A
-        Element element = mock(Element.class, RETURNS_DEEP_STUBS);%0A
     
@@ -3140,32 +3140,211 @@
 LETED);%0A        
+String elementId = %22id%22;%0A        when(element.getAttribute(%22id%22)).thenReturn(elementId);%0A%0A        //when%0A        testObj.initModule(element, moduleSocket, bodyGenerator);%0A        
 testObj.markToRe
@@ -3352,32 +3352,16 @@
 ender();
-%0A%0A        //when
 %0A       
@@ -3457,19 +3457,17 @@
 ent(
-anyString()
+elementId
 );%0A%0A

fix tests 2