package org.richfaces.testng;

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

public class AjaxStatusTest extends SeleniumTestBase {

    @Test
    public void testAjaxStatusComponent(Template template) {
        renderPage(template);

        String parentId = getParentId() + "_form:";
        String btn1 = parentId + "button1";
        clickById(btn1);

        String reg1Status = parentId + "region1:status";

        writeStatus("Ajax request is in progress");

        AssertVisible(reg1Status + ".start");
        AssertNotVisible(reg1Status + ".stop");
        String start = getTextById(reg1Status + ".start");
        Assert.assertTrue(start.startsWith("In progress..."));
        pause(3000, reg1Status);

        writeStatus("Ajax request completed");

        AssertNotVisible(reg1Status + ".start");
        AssertVisible(reg1Status + ".stop");
        String stop = getTextById(reg1Status + ".stop");
        Assert.assertTrue(stop.startsWith("Complete"));

        String btn2 = parentId + "button2";
        clickById(btn2);

        String reg2Status = parentId + "region2:status";

        writeStatus("Ajax request is in progress");

        AssertVisible(reg2Status + ".start");
        AssertNotVisible(reg2Status + ".stop");
        start = getTextById(reg2Status + ".start");
        Assert.assertTrue(start.startsWith("In progress..."));
        pause(3000, reg2Status);

        writeStatus("Ajax request completed");

        AssertNotVisible(reg2Status + ".start");
        AssertVisible(reg2Status + ".stop");
        stop = getTextById(reg1Status + ".stop");
        Assert.assertTrue(stop.startsWith("Complete"));

    }

    public String getTestUrl() {
        return "pages/ajaxStatus/ajaxStatusTest.xhtml";
    }
}
