/**
 * 
 */
package org.richfaces.testng;

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

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

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

}
