package org.richfaces.testng;

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

public class AjaxRegionTest extends SeleniumTestBase {

    @Test
    public 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";
    }

}
