package org.uberfire.client.workbench.panels.impl;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import org.junit.Test;
import org.mockito.Mock;
import org.uberfire.client.workbench.PanelManager;
import org.uberfire.client.workbench.panels.WorkbenchPanelPresenter;
import org.uberfire.workbench.model.CompassPosition;
import org.uberfire.workbench.model.Position;

import com.google.common.collect.ImmutableMap;

/**
 * Tests behaviours required by all subclasses of AbstractDockingWorkbenchPanelPresenter.
 */
public abstract class AbstractDockingWorkbenchPanelPresenterTest extends AbstractWorkbenchPanelPresenterTest {

    @Mock protected PanelManager panelManager;

    /**
     * The individual test classes that extend this base class implement this method by returning the implementation of
     * AbstractDockingWorkbenchPanelPresenter that they want to test. The protected mock objects set up by this base class
     * should be given to the presenter's constructor.
     */
    @Override
    abstract AbstractDockingWorkbenchPanelPresenter<?> getPresenterToTest();

    @Test
    public void removingLastPartFromPanelShouldRemovePanelToo() throws Exception {
        WorkbenchPanelPresenter westChildPanelPresenter = mock( SimpleWorkbenchPanelPresenter.class );
        WorkbenchPanelPresenter parentPanelPresenter = mock( SimpleWorkbenchPanelPresenter.class );

        AbstractDockingWorkbenchPanelPresenter<?> panelPresenter = getPresenterToTest();
        panelPresenter.setDefinition( panelPresenterPanelDefinition );

        panelPresenter.setParent( parentPanelPresenter );
        panelPresenter.addPanel( westChildPanelPresenter, CompassPosition.WEST );
        panelPresenter.addPart( mockPartPresenter );

        panelPresenter.removePart( mockPartPresenter.getDefinition() );

        // the now-empty panel should have removed itself
        verify( panelManager ).removeWorkbenchPanel( panelPresenterPanelDefinition );
    }

    @Test
    public void childrenOfRemovedPanelsShouldBeRescued() throws Exception {
        WorkbenchPanelPresenter westChildPanelPresenter = mock( SimpleWorkbenchPanelPresenter.class );
        WorkbenchPanelPresenter westChildChild = mock( SimpleWorkbenchPanelPresenter.class );

        when( westChildPanelPresenter.getPanels() ).thenReturn( ImmutableMap.of( (Position) CompassPosition.WEST, westChildChild) );

        AbstractDockingWorkbenchPanelPresenter<?> panelPresenter = getPresenterToTest();
        panelPresenter.addPanel( westChildPanelPresenter, CompassPosition.WEST );
        panelPresenter.removePanel( westChildPanelPresenter );

        // the child of the removed child should have been placed into our WEST child slot
        assertEquals( CompassPosition.WEST, panelPresenter.positionOf( westChildChild ) );
    }

}

@@ -56,72 +56,47 @@
 ort 
-static org.junit.Assert.*;%0Aimport static org.mockito.Mockito.*;%0A
+com.google.common.collect.ImmutableMap;
 %0Aimp
@@ -140,16 +140,57 @@
 o.Mock;%0A
+import org.uberfire.client.util.Layouts;%0A
 import o
@@ -395,63 +395,202 @@
 el.P
-os
+anelDefin
 ition;%0A
-%0A
 import 
-com.google.common.collect.ImmutableMap
+org.uberfire.workbench.model.Position;%0Aimport org.uberfire.workbench.model.impl.PanelDefinitionImpl;%0A%0Aimport static org.junit.Assert.*;%0Aimport static org.mockito.Mockito.*
 ;%0A%0A/
@@ -807,16 +807,20 @@
    @Mock
+%0A   
  protect
@@ -2544,16 +2544,17 @@
 ildChild
+ 
 ) );%0A%0A  
@@ -2975,10 +2975,2013 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void addingMultiplePanelsInTheSamePosition() throws Exception %7B%0A        AbstractDockingWorkbenchPanelPresenter%3C?%3E panelPresenter = getPresenterToTest();%0A        panelPresenter.setDefinition( panelPresenterPanelDefinition );%0A%0A        WorkbenchPanelPresenter southRootChild1 = mock( SimpleWorkbenchPanelPresenter.class );%0A        WorkbenchPanelPresenter southRootChild2 = mock( SimpleWorkbenchPanelPresenter.class );%0A%0A        final PanelDefinition southRootChild1PanelDefinition = new PanelDefinitionImpl();%0A        final PanelDefinition southRootChild2PanelDefinition = new PanelDefinitionImpl();%0A        when( southRootChild1.getDefinition() ).thenReturn( southRootChild1PanelDefinition );%0A        when( southRootChild2.getDefinition() ).thenReturn( southRootChild2PanelDefinition );%0A%0A        panelPresenter.addPanel( southRootChild1, CompassPosition.SOUTH );%0A        panelPresenter.addPanel( southRootChild2, CompassPosition.SOUTH );%0A%0A        verify( panelPresenter.getPanelView() ).addPanel( southRootChild1PanelDefinition,%0A                                                          southRootChild1.getPanelView(),%0A                                                          CompassPosition.SOUTH );%0A        verify( panelPresenter.getPanelView() ).addPanel( southRootChild2PanelDefinition,%0A                                                          southRootChild2.getPanelView(),%0A                                                          CompassPosition.SOUTH );%0A%0A        //Root contains 1 child, which is the last Panel to be added to Root%0A        assertTrue( panelPresenter.getDefinition().getChildren().contains( southRootChild2.getDefinition() ) );%0A%0A        //Root's View contains 2 Panels in the SOUTH position therefore the SOUTH position should be DEFAULT_CHILD_SIZE * 2 in size%0A        verify( panelPresenter.getPanelView() ).setChildSize( southRootChild1.getPanelView(),%0A                                                              Layouts.DEFAULT_CHILD_SIZE * 2 );%0A    %7D%0A%0A
 %7D%0A

Test to support change in Pull Request#86