package org.richfaces.testng;

import static org.testng.Assert.assertEquals;

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

public class JSFunctionTest extends SeleniumTestBase implements RichSeleniumTest {

    public JSFunctionTest() {
        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 testJSFunctionComponent() throws Exception {
    	_testJSFunctionComponent(Template.SIMPLE);
        _testJSFunctionComponent(Template.DATA_TABLE);
        _testJSFunctionComponent(Template.MODAL_PANEL);
    }

    private void _testJSFunctionComponent(Template template) {
        renderPage(template);

        String showNameElemId = getParentId() + "showname";
        selenium.mouseOver("alex");
        waitForAjaxCompletion();
        assertEquals(getTextById(showNameElemId), "Alex");

        selenium.mouseOver("jonh");
        waitForAjaxCompletion();
        assertEquals(getTextById(showNameElemId), "Jonh");

        selenium.mouseOver("roger");
        waitForAjaxCompletion();
        assertEquals(getTextById(showNameElemId), "Roger");
    }

    public String getTestUrl() {
        return "pages/jsFunction/jsFunctionTest.xhtml";
    }

}
