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

    public InplaceSelectTest() {
        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 testInplaceSelectComponent() throws Exception {
        _testInplaceSelectComponent(Template.SIMPLE);
        _testInplaceSelectComponent(Template.DATA_TABLE);
        _testInplaceSelectComponent(Template.MODAL_PANEL);
    }

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

        String parentId = getParentId() + "_form:";
        String inplaceSelectId = parentId + "inplaceSelect";
        String okButton = parentId + "inplaceSelect" + "ok";
        //String inplaceTmpValueElemId = parentId + "inplaceSelect" + "inplaceTmpValue";
        String cancelButton = parentId + "inplaceSelect" + "cancel";
        String popupId = "list" + inplaceSelectId;

        writeStatus("click the component");

        clickById(inplaceSelectId);
        Assert.assertFalse(isVisibleById(popupId), "Component pop-up should not show up on solitary click");

        writeStatus("double click the component");

        selenium.doubleClick(inplaceSelectId);
        Assert.assertTrue(isVisibleById(popupId), "Component pop-up should show up on double click");

        writeStatus("Check that controls buttons are present");

        Assert.assertTrue(isPresentById(okButton));
        Assert.assertTrue(isPresentById(cancelButton));

        writeStatus("Select second element"); //Birch

        selenium.mouseMove("xpath=//div[@id='list" + inplaceSelectId + "']/span[2]");
        selenium.fireEvent(okButton, "mousedown");

        writeStatus("Check that a new element is selected");

        AssertValueEquals(inplaceSelectId + "inplaceValue", "Birch");

        writeStatus("Select another element"); // Aspen

        selenium.doubleClick(inplaceSelectId);

        selenium.mouseMove("xpath=//div[@id='list" + inplaceSelectId + "']/span[3]");
        selenium.fireEvent(cancelButton, "mousedown");

        writeStatus("Cancel selected value.");
        AssertValueEquals(inplaceSelectId + "inplaceValue", "Birch", "A value has not to be changed");

        writeStatus("Verify javascript event triggering");

        runScript("theLatestEvent='undefined'");

        writeStatus("Double click the component");
        selenium.doubleClick(inplaceSelectId);

        Assert.assertEquals(runScript("theLatestEvent"), "editactivated", "editactivated event has not been triggered");

        writeStatus("Stop editing with ok");
        selenium.fireEvent(okButton, "mousedown");
        Assert.assertEquals(runScript("theLatestEvent"), "viewactivated", "viewactivated event has not been triggered");

//      viewactivated event is not fired ... almost sure bug
//      writeStatus("Stop editing with cancel");
//      selenium.fireEvent(cancelButton, "mousedown");
//      Assert.assertEquals(runScript("theLatestEvent"), "viewactivated", "viewactivated event has not been triggered");

    }

    public String getTestUrl() {
        return "pages/inplaceSelect/inplaceSelectTest.xhtml";
    }

}
