/**
 * 
 */
package org.ajax4jsf.portlet.seam;

import javax.faces.context.ExternalContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.portlet.faces.Bridge;

import org.ajax4jsf.portlet.context.AbstractExternalContext;
import org.ajax4jsf.portlet.context.PortletExternalContextImpl;
import org.jboss.seam.contexts.Contexts;

/**
 * @author asmirnov
 *
 */
public class PhaseListenerActionPhaseTest extends AbstractSeamPortletTestCase {

	private SeamPhaseListener phaseListener;
	/**
	 * @param name
	 */
	public PhaseListenerActionPhaseTest(String name) {
		super(name);
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.portlet.seam.AbstractSeamPortletTestCase#setUp()
	 */
	public void setUp() throws Exception {
		super.setUp();
		phaseListener = new SeamPhaseListener();
		lifecycle.addPhaseListener(phaseListener);
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.portlet.seam.AbstractSeamPortletTestCase#tearDown()
	 */
	public void tearDown() throws Exception {
		super.tearDown();
		phaseListener = null;
	}

	/**
	 * Test method for {@link org.ajax4jsf.portlet.seam.SeamPhaseListener#beforePhase(javax.faces.event.PhaseEvent)}.
	 */
	public void testBeforePhase() {
		assertFalse(Contexts.isEventContextActive());
		phaseListener.beforePhase(new PhaseEvent(facesContext,PhaseId.RESTORE_VIEW,lifecycle));
		assertTrue(Contexts.isEventContextActive());
	}

	/**
	 * Test method for {@link org.ajax4jsf.portlet.seam.SeamPhaseListener#afterPhase(javax.faces.event.PhaseEvent)}.
	 */
	public void testAfterPhase() {
		assertFalse(Contexts.isEventContextActive());
		phaseListener.beforePhase(new PhaseEvent(facesContext,PhaseId.RESTORE_VIEW,lifecycle));
		phaseListener.afterPhase(new PhaseEvent(facesContext,PhaseId.RESTORE_VIEW,lifecycle));
		assertTrue(Contexts.isEventContextActive());
	}

	@Override
	protected ExternalContext setupRequest() {
		setupActionRequest();
		request.setAttribute(Bridge.PORTLET_LIFECYCLE_PHASE, Bridge.PortletPhase.ActionPhase);
		request.setAttribute(AbstractExternalContext.PORTLET_CONFIG_ATTRIBUTE,
				portletConfig);

		return new PortletExternalContextImpl(portletContext,actionRequest,actionResponse);
	}

}
