package org.richfaces.testng;

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

public class AjaxStatusTest extends SeleniumTestBase implements RichSeleniumTest {

    public AjaxStatusTest() {
        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 testAjaxStatusComponent() throws Exception {
    	_testAjaxStatusComponent(Template.SIMPLE);
        _testAjaxStatusComponent(Template.DATA_TABLE);
        _testAjaxStatusComponent(Template.MODAL_PANEL);
    }

    private 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";
    }
}
