package org.richfaces.testng;

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


public class AjaxCommandLinkTest extends SeleniumTestBase {

    @Test
    public void testAjaxCommandLinkComponent(Template template) {
        renderPage(template);

    	String parentId = getParentId() + "_form:";
    	
    	String rerenderId = parentId + "_rerender";    	
    	String rerenderStr = getTextById(rerenderId);

    	String linkId = parentId + "l1";
    	boolean ajaxSingle = false;
    	boolean immediate = false;
    	writeStatus("Click link 1");
    	setValidation(true);
    	clickById(linkId);
    	waitForAjaxCompletion();
    	waitForOnCompleteHndler();
    	checkButton(linkId, true, ajaxSingle, immediate, true, true, true, true);
    	checkRerendering(rerenderStr, rerenderId);
    	
    	linkId = parentId + "l2";
    	ajaxSingle = false;
    	immediate = false;
    	writeStatus("Click link 2");
    	setValidation(false);
    	clickById(linkId);
    	waitForAjaxCompletion();
    	waitForOnCompleteHndler();
    	checkButton(linkId, false, ajaxSingle, immediate, false, false, false, false);
    	
    	linkId = parentId + "l3";
    	ajaxSingle = false;
    	immediate = true;
    	writeStatus("Click link 3");
    	setValidation(true);
    	clickById(linkId);
    	waitForAjaxCompletion();
    	waitForOnCompleteHndler();
    	checkButton(linkId, true, ajaxSingle, immediate, true, true, true, false);
    	
    	linkId = parentId + "l4";
    	ajaxSingle = true;
    	immediate = true;
    	writeStatus("Click link 4");
    	setValidation(false);
    	clickById(linkId);
    	waitForAjaxCompletion();
    	waitForOnCompleteHndler();
    	checkButton(linkId, true, ajaxSingle, immediate, true, 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 checkRerendering(String oldRerender, String rerenderId) {
    	String newRerender = getTextById(rerenderId);
    	if (oldRerender.equals(newRerender)) {
    		Assert.fail("<a4j:commandButton> failed. Rerender does not work.");
    	}
    }
    
    private void checkButton(String id, boolean testData, boolean ajaxSingle, boolean immediate, boolean testAction, boolean testActionListener, 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(testActionListener);
    	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:commandLink> [ajaxSingle="+ajaxSingle+" ; immediate="+immediate+"] test failure caused by " + result);
    	}
    	
    }

    public String getTestUrl() {
        return "pages/ajaxCommandLink/ajaxLinkTest.xhtml";
    }

}
