/**
 * 
 */
package org.richfaces.renderkit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItem;
import javax.faces.component.UISelectItems;
import javax.faces.model.SelectItem;

import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
import org.richfaces.component.UIComboBox;
import org.richfaces.renderkit.html.ComboBoxRenderer;

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

/**
 * @author Anton  Belevich
 *
 */
public class ComboBoxRendererTest extends AbstractAjax4JsfTestCase {
	
    private UIComponent form;
    private UIComboBox comboBox;
    private ComboBoxRenderer renderer;
    String suggestions = "Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,Florida,Massachusetts,Michigan,Georgia,Hawaii,Idaho,Indiana,Iowa,Kansas,Kentucky,Louisiana,Maine,Minnesota,Mississippi,Missouri,Montana,Nebraska";
    List selectItems = new ArrayList();

    public void setUp() throws Exception {
	super.setUp();
	renderer = new ComboBoxRenderer();
	comboBox = (UIComboBox) application.createComponent("org.richfaces.ComboBox");
	comboBox.setSuggestionValues(Arrays.asList(suggestions.split(",")));
	selectItems.add(new SelectItem("District of Columbia"));
	selectItems.add(new SelectItem("Illinois"));
	selectItems.add(new SelectItem("Maryland"));
	selectItems.add(new SelectItem("Nevada"));
	selectItems.add(new SelectItem("New Hampshire"));
	selectItems.add(new SelectItem("New Jersey"));

	UISelectItem item1 = new UISelectItem();
	item1.setValue(new SelectItem("Oregon"));

	UISelectItem item2 = new UISelectItem();
	item2.setValue(new SelectItem("Pennsylvania"));

	UISelectItem item3 = new UISelectItem();
	item3.setValue(new SelectItem("Rhode Island"));

	UISelectItem item4 = new UISelectItem();
	item4.setValue(new SelectItem("South Carolina"));

	comboBox.getChildren().add(item1);
	comboBox.getChildren().add(item2);
	comboBox.getChildren().add(item3);
	comboBox.getChildren().add(item4);

	UISelectItems items = new UISelectItems();
	items.setValue(selectItems);
	facesContext.getViewRoot().getChildren().add(comboBox);

	Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
	String id = "comboBoxValue" + comboBox.getClientId(facesContext);
	requestMap.put(id, "Pennsylvania");
    }

//    public void itemsTextAsJSArrayTest() {
//	String script = renderer.getItemsTextAsJSArray(facesContext, comboBox);
//	assertNotNull(script);
//    }

    public void testRender() throws Exception {

	HtmlPage page = renderView();
	assertNotNull(page);
	HtmlElement elem = page.getHtmlElementById(comboBox.getClientId(facesContext));
	assertNotNull(elem);
	assertEquals(elem.getTagName(), "div");
    }

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

    public ComboBoxRendererTest(String name) {
	super(name);
    }
}
