/**
 * 
 */
package org.richfaces.renderer;

import java.util.Iterator;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputText;

import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
import org.richfaces.component.html.HtmlRichMessage;

import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

/**
 * @author Anton Belevich
 *
 */
public class RichMessageRendererTest extends AbstractAjax4JsfTestCase {
	
	HtmlRichMessage message = null;
	HtmlOutputText text = null;
	HtmlInputText input = null;
		
	public RichMessageRendererTest(String name) {
		super(name);
	}
	
	public void setUp() throws Exception {
		
		super.setUp();
		
		input = (HtmlInputText)application.createComponent("javax.faces.HtmlInputText");
		input.setId("select");
		
		text = (HtmlOutputText)application.createComponent("javax.faces.HtmlOutputText");
		text.setValue("Error");
		
		message = (HtmlRichMessage)application.createComponent("org.richfaces.component.RichMessage");
		message.setId("message");
		message.setFor("select");
		message.setErrorClass("errorClass");
		message.setErrorLabelClass("errorLabelClass");
		message.setErrorMarkerClass("errorMarkerClass");
		message.getFacets().put("errorFacet", text);
				
		FacesMessage facesMessage = new FacesMessage();
	
		facesMessage.setDetail("TEST detail");
		facesMessage.setSummary("TEST summary");
		facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
		facesContext.addMessage("select",facesMessage);
		
		facesContext.getViewRoot().getChildren().add(input);
		facesContext.getViewRoot().getChildren().add(message);
	
	}
	
	public void testRenderStyle() throws Exception {
        HtmlPage page = renderView();
        assertNotNull(page);
        List links = page.getDocumentElement().getHtmlElementsByTagName("link");
        assertNotNull(links);
        HtmlElement link = (HtmlElement)links.get(0);
        assertTrue(link.getAttributeValue("href").contains("org/richfaces/renderkit/html/css/msg.css"));
    }
	
	public void testRendererMessage() throws Exception{
		
		HtmlPage page = renderView();
		assertNotNull(page);
		
		HtmlElement span = page.getHtmlElementById(message.getClientId(facesContext));
		
		assertNotNull(span);
		assertEquals("span", span.getNodeName().toLowerCase());
		
		String classAttr = span.getAttributeValue("class");
	    assertTrue(classAttr.contains("rich-message"));
	    assertTrue(classAttr.contains("errorClass"));
	    
	    Iterator childIter= span.getChildElementsIterator();
   
	    for (; childIter.hasNext();) {
	    	HtmlElement name = (HtmlElement) childIter.next();
	    	assertNotNull(name);
	    	assertEquals(name.getNodeName().toLowerCase(), "span");
	    	
	    	classAttr = name.getAttributeValue("class");
	    	assertTrue(classAttr.contains("rich-message-label")|| classAttr.contains("rich-message-marker") );
	    	assertTrue(classAttr.contains("errorLabelClass")||classAttr.contains("errorMarkerClass") );
	    }
	
	}
	
	public void tearDown() throws Exception {
		super.tearDown();
		message = null;
	} 

}
