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 LoadScriptTest extends SeleniumTestBase implements RichSeleniumTest {

    public LoadScriptTest() {
        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 testLoadScriptComponent() throws Exception {
    	_testLoadScriptComponent(Template.SIMPLE);
        _testLoadScriptComponent(Template.DATA_TABLE);
        _testLoadScriptComponent(Template.MODAL_PANEL);
    }

    private void _testLoadScriptComponent(Template template) {
        renderPage(template);
        String jsCall = "summarize(%1$s, %2$s);";
        int opr1 = 1;
        int opr2 = 2;
        String result = runScript(String.format(jsCall, opr1, opr2));
        assertEquals(Integer.parseInt(result), opr1 + opr2);
    }

    public String getTestUrl() {
        return "pages/loadScript/loadScriptTest.xhtml";
    }

}
