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

    public AjaxHTMLCommandLinkTest() {
        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 testAjaxCommandLinkComponent() throws Exception {
    	_testAjaxCommandLinkComponent(Template.SIMPLE);
    	_testAjaxCommandLinkComponent(Template.DATA_TABLE);
    	_testAjaxCommandLinkComponent(Template.MODAL_PANEL);
    }

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

    	String parentId = getParentId() + "_form:";

    	String linkId = parentId + "l1";
    	boolean immediate = false;
    	writeStatus("Click link 1");
    	setValidation(true);
    	clickById(linkId);
    	waitForAjaxCompletion();
    	waitForOnCompleteHndler();
    	checkButton(linkId, true,  immediate, true, true, true);
    	
    	linkId = parentId + "l2";
    	immediate = false;
    	writeStatus("Click link 2");
    	setValidation(false);
    	clickById(linkId);
    	waitForAjaxCompletion();
    	waitForOnCompleteHndler();
    	checkButton(linkId, false,  immediate, false, false, false);
    	
    	linkId = parentId + "l3";
    	immediate = true;
    	writeStatus("Click link 3");
    	setValidation(true);
    	clickById(linkId);
    	waitForAjaxCompletion();
    	waitForOnCompleteHndler();
    	checkButton(linkId, true, immediate, true, true, false);
    	
    	linkId = parentId + "l4";
    	immediate = true;
    	writeStatus("Click link 4");
    	setValidation(false);
    	clickById(linkId);
    	waitForAjaxCompletion();
    	waitForOnCompleteHndler();
    	checkButton(linkId, true, immediate, true, true, false);
    	
      
    }
    
    private void setValidation(boolean success) {
    	StringBuffer buffer = new StringBuffer("setValidation(");
    	buffer.append(success);
    	buffer.append(");");
    	runScript(buffer.toString());
    }
    
    private void waitForOnCompleteHndler () {
    	waiteForCondition("_onCompleteHandler == true;", 3000);
    	runScript("_onCompleteHandler = false;");
    }
    
    private void checkButton(String id, boolean testData, boolean immediate, boolean testAction,  boolean testFListener, boolean testInput) {
    	writeStatus("Checking link...");
    	StringBuffer buffer = new StringBuffer("checkLink('");
    	buffer.append(id);
    	buffer.append("',");
    	buffer.append(testData);
    	buffer.append(",");
    	buffer.append(testAction);
    	buffer.append(",");
    	buffer.append(testFListener);
    	buffer.append(",");
    	buffer.append(testInput);
    	buffer.append(");");
    	String result = runScript(buffer.toString());
    	if (result != null && result.length() > 0) {
    		Assert.fail("<a4j:htmlCommandLink> [immediate="+immediate+"] test failure caused by " + result);
    	}
    	
    }

    public String getTestUrl() {
        return "pages/ajaxHTMLCommandLink/ajaxHTMLLinkTest.xhtml";
    }

}
