package org.richfaces.testng;

import org.ajax4jsf.template.Template;
import org.richfaces.RichSeleniumTest;
import org.richfaces.SeleniumTestBase;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class AjaxRegionTest extends SeleniumTestBase implements RichSeleniumTest {

    public AjaxRegionTest() {
        super("http", "localhost", serverPort);
    }

    /**
     * This method are invoking before selenium tests started
     */
    @BeforeMethod
    @Parameters({"browser", "filterPrefix"})
    public void startSelenium(String browser, String filterPrefix) {
    	super.startSelenium(browser, filterPrefix);
    }
    
    
    @BeforeMethod
    @Parameters({"loadStyleStrategy", "loadScriptStrategy"})
    protected void loadConfiguration(String loadStyleStrategy, String loadScriptStrategy) throws Exception {
	super.loadConfiguration(loadStyleStrategy, loadScriptStrategy);
    }

    /**
     * This method are invoking after selenium tests completed
     */
    @AfterMethod(alwaysRun=true)
    public void stopSelenium() {
        super.stopSelenium();
    }

    @Test
    public void testAjaxRegionComponent() throws Exception {
    	_testAjaxRegionComponent(Template.SIMPLE);
        _testAjaxRegionComponent(Template.DATA_TABLE);
        _testAjaxRegionComponent(Template.MODAL_PANEL);
    }

    private void _testAjaxRegionComponent(Template template) {
        renderPage(template);

        String parentId = getParentId() + "_form:";

        writeStatus("check nested regions");

        String externalLink = parentId + "externalLink";
        String internalLink = parentId + "internalLink";

        String internalElemId = parentId + "internal";
        String externalElemId = parentId + "external";

        type(internalElemId, "5");
        type(externalElemId, "5");

        clickAjaxCommandAndWait(internalLink);
        AssertValueEquals(internalElemId, "5");
        AssertValueEquals(externalElemId, "0");

        type(internalElemId, "10");
        type(externalElemId, "10");

        clickAjaxCommandAndWait(externalLink);
        AssertValueEquals(internalElemId, "10");
        AssertValueEquals(externalElemId, "10");

        writeStatus("verify \"selfRendered\" component's attribute");

        String selfRenderedLink = parentId + "selfRenderedLink";
        clickAjaxCommandAndWait(selfRenderedLink);
        AssertNotPresent("transientText");

        writeStatus("verify \"renderRegionOnly\" component's attribute");

        String renderOnlyLink = parentId + "renderOnlyLink";
        String renderLink = parentId + "renderLink";

        String renderOnlyElemId = parentId + "renderOnly";
        String renderElemId = parentId + "render";

        type(renderOnlyElemId, "7");

        clickAjaxCommandAndWait(renderOnlyLink);
        AssertValueEquals(renderOnlyElemId, "7");
        AssertValueEquals(renderElemId, "0");

        type(renderElemId, "11");
        clickAjaxCommandAndWait(renderLink);

        AssertValueEquals(renderOnlyElemId, "11");
        AssertValueEquals(renderElemId, "11");
    }

    public String getTestUrl() {
        return "pages/ajaxRegion/ajaxRegionTest.xhtml";
    }

}
