/**
 * License Agreement.
 *
 * Rich Faces - Natural Ajax for Java Server Faces (JSF)
 *
 * Copyright (C) 2007 Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package org.ajax4jsf.renderkit.html;

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

import javax.faces.component.UIOutput;

import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
import org.apache.commons.lang.StringUtils;

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

public class AjaxPageRendererTest extends AbstractAjax4JsfTestCase{
	private static final String SCRIPT_NAME = "org.ajax4jsf.javascript.AjaxScript";
	
	private org.ajax4jsf.component.html.HtmlPage ajaxPage = null;

	public AjaxPageRendererTest(String name) {
		super(name);
	}
	
	public void setUp() throws Exception {
		super.setUp();
				
		ajaxPage = (org.ajax4jsf.component.html.HtmlPage) application.createComponent(org.ajax4jsf.component.html.HtmlPage.COMPONENT_TYPE);
		ajaxPage.setId("page");
		
		UIOutput head = new UIOutput();
		head.setValue("HEAD");
		ajaxPage.getFacets().put("head", head);
		
		UIOutput content = new UIOutput();
		content.setValue("content");
		ajaxPage.getChildren().add(content);
		
		ajaxPage.setFormat("xhtml");
		ajaxPage.setPageTitle("title");
		
		facesContext.getViewRoot().setLocale(new Locale("be", "BY"));
		
		facesContext.getViewRoot().getChildren().add(ajaxPage);
	}
	
	public void tearDown() throws Exception {
		super.tearDown();
	}

	public void testRender() throws Exception {
		HtmlPage page = renderView();
		assertNotNull(page);
		System.out.println(page.asXml());
		
		HtmlElement html = (HtmlElement)page.getFirstChild();
		assertNotNull(html);
		assertEquals("html", html.getTagName());
		
		String lang = html.getAttributeValue("lang");
		assertNotNull(lang);
		assertEquals(lang, "be_BY");
		
		HtmlElement title = (HtmlElement) html.getHtmlElementsByTagName("title").get(0);
		assertNotNull(title);
		
		assertEquals("title", title.getFirstChild().asText());
		
		HtmlElement meta = (HtmlElement) html.getHtmlElementsByTagName("meta").get(0);
		
		assertNotNull(meta);
		String httpEquiv = meta.getAttributeValue("http-equiv");
		assertEquals(httpEquiv, "Content-Type");
		
		String content = meta.getAttributeValue("content");
		assertEquals(content, "application/xhtml+xml");
	}
	
	public void testRenderScript() throws Exception {
		HtmlPage page = renderView();
        assertNotNull(page);
        
        List scripts = page.getDocumentElement().getHtmlElementsByTagName("script");
        int found = 0;
        int count = 0;
        for (Iterator it = scripts.iterator(); it.hasNext();) {
        	count++;
        	HtmlScript item = (HtmlScript) it.next();
            String srcAttr = item.getSrcAttribute();
        	if (StringUtils.isNotBlank(srcAttr)) {
                if (srcAttr.contains(SCRIPT_NAME)) {
                	found++;
                	break;
                }
        	}
        }
        assertEquals(1, found);
        assertEquals(1, count);
	}

}
