package org.richfaces.testng;

import org.ajax4jsf.template.Template;
import org.richfaces.SeleniumTestBase;
import org.testng.Assert;
import org.testng.annotations.Test;

public class MessageTest extends SeleniumTestBase {
	
	private String mainForm;

    private String inputText;
    
    private String message;
    
    private String submit;
	
    private String rendered;
	
	private String showDetail;

	private String showSummary;

	private String ajaxRendered;

	private void init(Template template) {
        renderPage(template, "#{messageBean.init}");
        mainForm = getParentId() + "mainForm";
        inputText = mainForm + ":inputText";
        message = mainForm + ":message";
        submit = mainForm + ":submit";
        String attrForm = getParentId() + "attrForm";
        rendered = attrForm + ":rendered"; 
        showDetail = attrForm + ":showDetail"; 
        showSummary = attrForm + ":showSummary"; 
        ajaxRendered = attrForm + ":ajaxRendered"; 
    }

    /**
     * showDetail and showSummary attributes work
     */
    @Test
    public void testShowDetailAndShowSummary(Template template) {
    	init(template);
    	String locator = "xpath=id('" + message + "')/span[2]";
		Assert.assertTrue(selenium.getText(locator).length() == 0);
    	selenium.type(inputText, "fatal");
    	clickAjaxCommandAndWait(submit);
    	Assert.assertTrue("messageDetail".equals(selenium.getText(locator)));
    	clickAjaxCommandAndWait(showSummary);
    	selenium.type(inputText, "fatal");
    	clickAjaxCommandAndWait(submit);
    	Assert.assertTrue("messageSummarymessageDetail".equals(selenium.getText(locator)));
    	clickAjaxCommandAndWait(showDetail);
    	selenium.type(inputText, "fatal");
    	clickAjaxCommandAndWait(submit);
    	Assert.assertTrue("messageSummary".equals(selenium.getText(locator)));
    	
    }

    /**
     * level selects message of respective level
     */
    @Test
    public void testLevel(Template template) {
    	//TODO
    	Assert.fail("This test should be completed after resolving bug RF-5107.");
    }
    
    /**
     * ajaxRendered message output first message for attached component by ajax request
     */
    @Test
    public void testAjaxRendered(Template template) {
    	init(template);
    	Assert.assertTrue(selenium.isElementPresent(mainForm + ":passedMarker"));
    	selenium.type(inputText, "fatal");
    	clickAjaxCommandAndWait(mainForm + ":submitWithoutReRender");
    	Assert.assertTrue(selenium.isElementPresent(mainForm + ":passedMarker"));
    	clickAjaxCommandAndWait(ajaxRendered);
    	clickAjaxCommandAndWait(mainForm + ":submitWithoutReRender");
    	Assert.assertTrue(selenium.isElementPresent(mainForm + ":fatalMarker"));
    }
    
    /**
     * check markers rendering
     */
    @Test
    public void testMarkers(Template template) {
    	init(template);
    	Assert.assertTrue(selenium.isElementPresent(mainForm + ":passedMarker"));
    	selenium.type(inputText, "fatal");
    	clickAjaxCommandAndWait(submit);
    	Assert.assertTrue(selenium.isElementPresent(mainForm + ":fatalMarker"));
    	selenium.type(inputText, "error");
    	clickAjaxCommandAndWait(submit);
    	Assert.assertTrue(selenium.isElementPresent(mainForm + ":errorMarker"));
    	selenium.type(inputText, "warn");
    	clickAjaxCommandAndWait(submit);
    	Assert.assertTrue(selenium.isElementPresent(mainForm + ":warnMarker"));
    	selenium.type(inputText, "info");
    	clickAjaxCommandAndWait(submit);
    	Assert.assertTrue(selenium.isElementPresent(mainForm + ":infoMarker"));
    }
    
    /**
     *   	 component with rendered = false is not present on the page
     */
    @Test
    public void testRendered(Template template) {
    	init(template);
        init(template); 
        Assert.assertTrue(selenium.isElementPresent(message), "ListShuttle must be rendered.");

        selenium.click(rendered);
        waitForAjaxCompletion();
        clickAjaxCommandAndWait(submit);       
        
        Assert.assertFalse(selenium.isElementPresent(message), "ListShuttle mustn't be rendered.");
    }
    	
    @Override
	public String getTestUrl() {
		return "pages/message/message.xhtml";
	}

}
