/**
 * 
 */
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;

/**
 * @author Andrey Markavstov
 * 
 */
public class AjaxPollTest extends SeleniumTestBase implements RichSeleniumTest {

    public AjaxPollTest() {
        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 testAjaxPollComponent() {
        _testAjaxPollComponent(Template.SIMPLE);
        _testAjaxPollComponent(Template.DATA_TABLE);
        _testAjaxPollComponent(Template.MODAL_PANEL);
    }

    private void _testAjaxPollComponent(Template template) {
        renderPage(template);
        String parentId = getParentId() + "_form:";
        String pollId = parentId + "poll";
        String inputId = parentId + "_value";

        enablePoll(parentId + "_enabled");
        AssertValueEquals(inputId, "1");

        writeStatus("Polling in progress...");
        pause(1500, pollId);
        AssertValueNotEquals(inputId, "1",
                "Polling does not fire after component was enabled. Or 'ReRender' attribute does not work");

        waiteForCondition("document.getElementById('" + inputId + "').value == 8", 7000);

        pause(1500, pollId);
        writeStatus("Polling should be stopped...");

        AssertValueEquals(inputId, "8", "Polling should be stopped. It continues to poll after component was disabled.");
        AssertTextEquals(parentId + "_text", "Polling",
                "Polling terminated abnormal or invalid count of ajax requests has been submitted");

    }

    private void enablePoll(String id) {
        writeStatus("Enable polling...");
        try {
            clickById(id);
            waitForAjaxCompletion();
        } catch (Exception e) {
            Assert.fail("Poll component failed after attempt to enable polling. Caused by " + e);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.ajax4jsf.test.base.RichSeleniumTest#getTestUrl()
     */
    public String getTestUrl() {
        return "pages/ajaxPoll/ajaxPollTest.xhtml";
    }

}
