/**
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * 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.richfaces.component;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.faces.component.UIComponent;
import javax.faces.component.UIForm;
import javax.faces.component.html.HtmlForm;
import javax.faces.component.html.HtmlOutputText;
import javax.servlet.http.HttpServletResponse;

import org.ajax4jsf.resource.InternetResource;
import org.ajax4jsf.resource.InternetResourceBuilder;
import org.ajax4jsf.resource.ResourceBuilderImpl;
import org.ajax4jsf.resource.image.ImageInfo;
import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
import org.apache.commons.lang.StringUtils;
import org.richfaces.component.html.HtmlMenuGroup;
import org.richfaces.renderkit.html.images.MenuNodeImage;

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

/**
 * Unit test for MenuGroup Component.
 */
public class MenuGroupComponentTest extends AbstractAjax4JsfTestCase {

    private static Set javaScripts = new HashSet();

    static {
        javaScripts.add("PrototypeScript");
        javaScripts.add("AjaxScript");
    }

    private UIMenuGroup menuGroup;

    private UIForm form;

    /**
     * Create the test case
     * 
     * @param testName
     *            name of the test case
     */
    public MenuGroupComponentTest(String testName) {
        super(testName);
    }

    public void setUp() throws Exception {
        super.setUp();

        form = new HtmlForm();
        form.setId("form");
        facesContext.getViewRoot().getChildren().add(form);

        menuGroup = (HtmlMenuGroup) application
                .createComponent(HtmlMenuGroup.COMPONENT_TYPE);
        menuGroup.setId("menuGroup");
        menuGroup.setValue("Menu Group");

        form.getChildren().add(menuGroup);
    }

    public void tearDown() throws Exception {
        super.tearDown();
        menuGroup = null;
        form = null;
    }

    /**
     * Rigourous Test :-)
     */
    public void testMenuGroupRender() throws Exception {
        HtmlPage page = renderView();
        assertNotNull(page);
        // System.out.println(page.asXml());
        HtmlElement div = page.getHtmlElementById("ref"
                + menuGroup.getClientId(facesContext));
        assertNotNull(div);
        assertEquals("div", div.getNodeName());

    }

    /**
     * MenuGroup icon test.
     */
    public void testMenuGroupIcon() throws Exception {
        menuGroup.setDisabled(false);
        menuGroup.getFacets().clear();
        menuGroup.getAttributes().put("icon", "menuGroupIcon");
        HtmlPage page = renderView();
        // System.out.println(page.asXml());
        assertNotNull(page);
        HtmlElement span = page.getHtmlElementById("ref"
                + menuGroup.getClientId(facesContext) + ":icon");
        assertNotNull(span);
        assertEquals("span", span.getNodeName());
        List images = span.getHtmlElementsByTagName("img");
        assertNotNull(images);
        assertEquals(1, images.size());
    }

    /**
     * MenuGroup icon test.
     */
    public void testMenuGroupIconDisabled() throws Exception {
        menuGroup.setDisabled(true);
        menuGroup.getAttributes().put("iconDisabled", "menuGroupIcon");
        HtmlPage page = renderView();
        // System.out.println(page.asXml());
        assertNotNull(page);
        HtmlElement span = page.getHtmlElementById("ref"
                + menuGroup.getClientId(facesContext) + ":icon");
        assertEquals("span", span.getNodeName());
        assertNotNull(span);
        List images = span.getHtmlElementsByTagName("img");
        assertNotNull(images);
        assertEquals(1, images.size());
    }

    public void testMenuGroupIconFacet() throws Exception {
        menuGroup.setDisabled(false);
        UIComponent facet = createComponent(HtmlOutputText.COMPONENT_TYPE,
                HtmlOutputText.class.getName(), null, null, null);
        menuGroup.getFacets().put("icon", facet);
        HtmlPage page = renderView();
        assertNotNull(page);
        // System.out.println(page.asXml());
        HtmlElement span = page.getHtmlElementById("ref"
                + menuGroup.getClientId(facesContext) + ":icon");
        assertEquals("span", span.getNodeName());
        assertNotNull(span);
    }

    public void testMenuGroupIconFacetDisabled() throws Exception {
        menuGroup.setDisabled(true);
        UIComponent facet = createComponent(HtmlOutputText.COMPONENT_TYPE,
                HtmlOutputText.class.getName(), null, null, null);
        menuGroup.getFacets().put("iconDisabled", facet);
        HtmlPage page = renderView();
        assertNotNull(page);
        // System.out.println(page.asXml());
        HtmlElement span = page.getHtmlElementById("ref"
                + menuGroup.getClientId(facesContext) + ":icon");
        assertEquals("span", span.getNodeName());
        assertNotNull(span);
    }

    public void testDisabledMenuItem() throws Exception {
        menuGroup.setDisabled(true);
        HtmlPage page = renderView();
        assertNotNull(page);
        // System.out.println(page.asXml());
        HtmlElement span = page.getHtmlElementById("ref"
                + menuGroup.getClientId(facesContext) + ":icon");
        assertNotNull(span);
        assertEquals("span", span.getNodeName());
        String classAttr = span.getAttributeValue("class");
        assertTrue(classAttr
                .contains("dr-menu-icon rich-menu-item-icon-disabled"));

        HtmlElement div = page.getHtmlElementById("ref"
                + menuGroup.getClientId(facesContext));
        assertNotNull(div);
        assertEquals("div", div.getNodeName());
        classAttr = div.getAttributeValue("class");
        assertTrue(classAttr
                .contains("dr-menu-item dr-menu-item-disabled rich-menu-group-disabled rich-menu-group"));

    }

    public void testEnabledMenuItem() throws Exception {
        assertEquals(false, menuGroup.isDisabled());
        menuGroup.getAttributes().put("iconClass", "iconClass");
        HtmlPage page = renderView();
        assertNotNull(page);
        // System.out.println(page.asXml());
        HtmlElement anchor = page.getHtmlElementById("ref"
                + menuGroup.getClientId(facesContext) + ":anchor");
        assertNotNull(anchor);
        assertEquals("span", anchor.getNodeName());
        String classAttr = anchor.getAttributeValue("class");
        assertTrue(classAttr.contains("rich-menu-item-label"));

        HtmlElement span = page.getHtmlElementById("ref"
                + menuGroup.getClientId(facesContext) + ":icon");
        assertNotNull(span);
        assertEquals("span", span.getNodeName());
        classAttr = span.getAttributeValue("class");
        assertTrue(classAttr.contains("dr-menu-icon rich-menu-item-icon"));
        assertTrue(classAttr.contains((String) menuGroup.getAttributes().get(
                "iconClass")));

        HtmlElement div = page.getHtmlElementById("ref"
                + menuGroup.getClientId(facesContext));
        assertNotNull(div);
        assertEquals("div", div.getNodeName());
        classAttr = div.getAttributeValue("class");
        assertTrue(classAttr
                .contains("dr-menu-item dr-menu-item-enabled rich-menu-group"));

        div = page.getHtmlElementById("ref"
                + menuGroup.getClientId(facesContext) + ":folder");
        assertNotNull(div);
        assertEquals("div", div.getNodeName());
        classAttr = div.getAttributeValue("class");
        assertTrue(classAttr.contains("dr-menu-node dr-menu-node-icon rich-menu-item-folder"));

    }

    public void testRenderStyle() throws Exception {
        HtmlPage page = renderView();
        assertNotNull(page);
        List links = page.getDocumentElement().getHtmlElementsByTagName("link");
        // Assert.assertEquals(1, links.size());
        assertNotNull(links);
        HtmlElement link = (HtmlElement) links.get(0);
        assertTrue(link.getAttributeValue("href").contains(
                "menucomponents.xcss"));
    }

    /**
     * Scripts link test.
     */
    public void testRenderScript() throws Exception {
        HtmlPage page = renderView();
        assertNotNull(page);
        // System.out.println(page.asXml());
        List scripts = page.getDocumentElement().getHtmlElementsByTagName(
                "script");
        for (Iterator it = scripts.iterator(); it.hasNext();) {
            HtmlScript item = (HtmlScript) it.next();
            String srcAttr = item.getSrcAttribute();

            if (StringUtils.isNotBlank(srcAttr)) {
                boolean found = false;
                for (Iterator srcIt = javaScripts.iterator(); srcIt.hasNext();) {
                    String src = (String) srcIt.next();

                    found = srcAttr.contains(src);
                    if (found) {
                        break;
                    }
                }

                assertTrue(found);
            }
        }
    }

    public void testRenderImages() throws Exception {
        renderView();
        InternetResourceBuilder builder = ResourceBuilderImpl.getInstance();
        InternetResource resource = builder.createResource(null, MenuNodeImage.class.getName());
        assertNotNull(resource);
        String uri = "http:" + resource.getUri(facesContext, null);
        Page page = webClient.getPage(uri);
        assertTrue(page.getWebResponse().getStatusCode() == HttpServletResponse.SC_OK);
        ImageInfo info = new ImageInfo();
        info.setInput(page.getWebResponse().getContentAsStream());
        //image recognizable?
        assertTrue(info.check());
        assertEquals(ImageInfo.FORMAT_GIF, info.getFormat());
    }
    
}
